Subversion Repositories FlightCtrl

Rev

Rev 683 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
3
// + only for non-profit use
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
 
8
#include "main.h"
685 killagreg 9
#include "menu.h"
10
#include "timer0.h"
1 ingob 11
#include "uart.h"
685 killagreg 12
#include "fc.h"
13
#include "_Settings.h"
14
#include "rc.h"
1 ingob 15
 
685 killagreg 16
 
1 ingob 17
unsigned char DebugGetAnforderung = 0,DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0;
18
unsigned volatile char SioTmp = 0;
683 killagreg 19
unsigned volatile char txd_buffer[TXD_BUFFER_LEN];
20
unsigned volatile char rxd_buffer[RXD_BUFFER_LEN];
21
 
1 ingob 22
unsigned volatile char NeuerDatensatzEmpfangen = 0;
23
unsigned volatile char NeueKoordinateEmpfangen = 0;
24
unsigned volatile char UebertragungAbgeschlossen = 1;
25
unsigned volatile char CntCrcError = 0;
26
unsigned volatile char AnzahlEmpfangsBytes = 0;
27
unsigned volatile char PC_DebugTimeout = 0;
499 hbuss 28
unsigned char RemotePollDisplayLine = 0;
395 hbuss 29
unsigned char NurKanalAnforderung = 0;
499 hbuss 30
unsigned char DebugTextAnforderung = 255;
1 ingob 31
unsigned char PcZugriff = 100;
32
unsigned char MotorTest[4] = {0,0,0,0};
683 killagreg 33
unsigned char DubWiseKeys[4] = {0,0,0,0};
1 ingob 34
unsigned char MeineSlaveAdresse;
595 hbuss 35
unsigned char ConfirmFrame;
1 ingob 36
struct str_DebugOut    DebugOut;
595 hbuss 37
struct str_ExternControl  ExternControl;
1 ingob 38
struct str_VersionInfo VersionInfo;
39
int Debug_Timer;
40
 
499 hbuss 41
const unsigned char ANALOG_TEXT[32][16] =
42
{
683 killagreg 43
   //1234567890123456
499 hbuss 44
    "IntegralNick    ", //0
45
    "IntegralRoll    ",
683 killagreg 46
    "AccNick         ",
499 hbuss 47
    "AccRoll         ",
48
    "GyroGier        ",
49
    "HoehenWert      ", //5
50
    "AccZ            ",
51
    "Gas             ",
52
    "KompassValue    ",
513 hbuss 53
    "Spannung        ",
54
    "Empfang         ", //10
55
    "Analog11        ",
499 hbuss 56
    "Motor_Vorne     ",
57
    "Motor_Hinten    ",
58
    "Motor_Links     ",
59
    "Motor_Rechts    ", //15
513 hbuss 60
    "Acc_Z           ",
61
    "MittelAccNick   ",
62
    "MittelAccRoll   ",
63
    "IntegralErrNick ",
499 hbuss 64
    "IntegralErrRoll ", //20
513 hbuss 65
    "MittelIntNick   ",
66
    "MittelIntRoll       ",
67
    "NeutralNick     ",
68
    "RollOffset      ",
69
    "IntRoll*Faktor  ", //25
623 hbuss 70
    "Analog26        ",
499 hbuss 71
    "DirektAusglRoll ",
513 hbuss 72
    "MesswertRoll    ",
73
    "AusgleichRoll   ",
74
    "I-LageRoll      ", //30
75
    "StickRoll       "
499 hbuss 76
};
77
 
78
 
79
 
683 killagreg 80
/****************************************************************/
81
/*              Initialization of the USART0                    */
82
/****************************************************************/
83
void USART0_Init (void)
1 ingob 84
{
683 killagreg 85
        uint8_t sreg = SREG;
86
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1);
87
 
88
        // disable all interrupts before configuration
89
        cli();
90
 
91
        // disable RX-Interrupt
92
        UCSR0B &= ~(1 << RXCIE0);
93
        // disable TX-Interrupt
94
        UCSR0B &= ~(1 << TXCIE0);
95
 
96
        // set direction of RXD0 and TXD0 pins
97
        // set RXD0 (PD0) as an input pin
98
        PORTD |= (1 << PORTD0);
99
        DDRD &= ~(1 << DDD0);
100
        // set TXD0 (PD1) as an output pin
101
        PORTD |= (1 << PORTD1);
102
        DDRD |=  (1 << DDD1);
103
 
104
        // USART0 Baud Rate Register
105
        // set clock divider
106
        UBRR0H = (uint8_t)(ubrr >> 8);
107
        UBRR0L = (uint8_t)ubrr;
108
 
109
        // USART0 Control and Status Register A, B, C
110
 
111
        // enable double speed operation in
112
        UCSR0A |= (1 << U2X0);
113
        // enable receiver and transmitter in
114
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
115
        // set asynchronous mode
116
        UCSR0C &= ~(1 << UMSEL01);
117
        UCSR0C &= ~(1 << UMSEL00);
118
        // no parity
119
        UCSR0C &= ~(1 << UPM01);
120
        UCSR0C &= ~(1 << UPM00);
121
        // 1 stop bit
122
        UCSR0C &= ~(1 << USBS0);
123
        // 8-bit
124
        UCSR0B &= ~(1 << UCSZ02);
125
        UCSR0C |=  (1 << UCSZ01);
126
        UCSR0C |=  (1 << UCSZ00);
127
 
128
                // flush receive buffer
129
        while ( UCSR0A & (1<<RXC0) ) UDR0;
130
 
131
        // enable interrupts at the end
132
        // enable RX-Interrupt
133
        UCSR0B |= (1 << RXCIE0);
134
        // enable TX-Interrupt
135
        UCSR0B |= (1 << TXCIE0);
136
 
137
        // restore global interrupt flags
138
    SREG = sreg;
139
}
140
 
141
/****************************************************************/
142
/*               USART0 transmitter ISR                         */
143
/****************************************************************/
144
ISR(USART0_TX_vect)
145
{
146
 static uint16_t ptr = 0;
147
 uint8_t tmp_tx;
148
 if(!UebertragungAbgeschlossen)
1 ingob 149
  {
150
   ptr++;                    // die [0] wurde schon gesendet
683 killagreg 151
   tmp_tx = txd_buffer[ptr];
152
   if((tmp_tx == '\r') || (ptr == TXD_BUFFER_LEN))
1 ingob 153
    {
154
     ptr = 0;
155
     UebertragungAbgeschlossen = 1;
156
    }
683 killagreg 157
   UDR0 = tmp_tx; // send byte will trigger this ISR again
158
  }
1 ingob 159
  else ptr = 0;
160
}
161
 
683 killagreg 162
/****************************************************************/
163
/*               USART0 receiver ISR                            */
164
/****************************************************************/
165
ISR(USART0_RX_vect)
1 ingob 166
{
683 killagreg 167
 static uint16_t crc;
168
 static uint8_t crc1,crc2,buf_ptr;
169
 static uint8_t UartState = 0;
170
 uint8_t CrcOkay = 0;
1 ingob 171
 
683 killagreg 172
 SioTmp = UDR0;
173
 if(buf_ptr >= RXD_BUFFER_LEN)    UartState = 0;
174
 if(SioTmp == '\r' && UartState == 2)
1 ingob 175
  {
176
   UartState = 0;
683 killagreg 177
   crc -= rxd_buffer[buf_ptr-2];
178
   crc -= rxd_buffer[buf_ptr-1];
1 ingob 179
   crc %= 4096;
180
   crc1 = '=' + crc / 64;
181
   crc2 = '=' + crc % 64;
182
   CrcOkay = 0;
683 killagreg 183
   if((crc1 == rxd_buffer[buf_ptr-2]) && (crc2 == rxd_buffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
1 ingob 184
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
185
    {
683 killagreg 186
     NeuerDatensatzEmpfangen = 1;
1 ingob 187
         AnzahlEmpfangsBytes = buf_ptr;
683 killagreg 188
     rxd_buffer[buf_ptr] = '\r';
189
         if(rxd_buffer[2] == 'R') wdt_enable(WDTO_250MS); // Reset-Commando
190
        }
1 ingob 191
  }
192
  else
193
  switch(UartState)
194
  {
195
   case 0:
196
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
197
                  buf_ptr = 0;
683 killagreg 198
                  rxd_buffer[buf_ptr++] = SioTmp;
1 ingob 199
                  crc = SioTmp;
200
          break;
201
   case 1: // Adresse auswerten
202
                  UartState++;
683 killagreg 203
                  rxd_buffer[buf_ptr++] = SioTmp;
1 ingob 204
                  crc += SioTmp;
205
                  break;
206
   case 2: //  Eingangsdaten sammeln
683 killagreg 207
                  rxd_buffer[buf_ptr] = SioTmp;
208
                  if(buf_ptr < RXD_BUFFER_LEN) buf_ptr++;
1 ingob 209
                  else UartState = 0;
210
                  crc += SioTmp;
211
                  break;
683 killagreg 212
   default:
213
          UartState = 0;
1 ingob 214
          break;
215
  }
216
}
217
 
218
 
683 killagreg 219
 
1 ingob 220
// --------------------------------------------------------------------------
221
void AddCRC(unsigned int wieviele)
222
{
683 killagreg 223
 uint16_t tmpCRC = 0,i;
1 ingob 224
 for(i = 0; i < wieviele;i++)
225
  {
683 killagreg 226
   tmpCRC += txd_buffer[i];
1 ingob 227
  }
228
   tmpCRC %= 4096;
683 killagreg 229
   txd_buffer[i++] = '=' + tmpCRC / 64;
230
   txd_buffer[i++] = '=' + tmpCRC % 64;
231
   txd_buffer[i++] = '\r';
1 ingob 232
  UebertragungAbgeschlossen = 0;
683 killagreg 233
  UDR0 = txd_buffer[0];
1 ingob 234
}
235
 
236
 
237
 
238
// --------------------------------------------------------------------------
683 killagreg 239
void SendOutData(uint8_t cmd,uint8_t modul, uint8_t *snd, uint8_t len)
1 ingob 240
{
683 killagreg 241
 uint16_t pt = 0;
242
 uint8_t a,b,c;
243
 uint8_t ptr = 0;
1 ingob 244
 
683 killagreg 245
 txd_buffer[pt++] = '#';               // Startzeichen
246
 txd_buffer[pt++] = modul;             // Adresse (a=0; b=1,...)
247
 txd_buffer[pt++] = cmd;                        // Commando
1 ingob 248
 
249
 while(len)
250
  {
251
   if(len) { a = snd[ptr++]; len--;} else a = 0;
252
   if(len) { b = snd[ptr++]; len--;} else b = 0;
253
   if(len) { c = snd[ptr++]; len--;} else c = 0;
683 killagreg 254
   txd_buffer[pt++] = '=' + (a >> 2);
255
   txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
256
   txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
257
   txd_buffer[pt++] = '=' + ( c & 0x3f);
1 ingob 258
  }
259
 AddCRC(pt);
260
}
261
 
262
 
263
// --------------------------------------------------------------------------
683 killagreg 264
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)  // Wohin mit den Daten; Wie lang; Wo im rxd_buffer
1 ingob 265
{
683 killagreg 266
 uint8_t a,b,c,d;
267
 uint8_t ptr = 0;
268
 uint8_t x,y,z;
1 ingob 269
 while(len)
270
  {
683 killagreg 271
   a = rxd_buffer[ptrIn++] - '=';
272
   b = rxd_buffer[ptrIn++] - '=';
273
   c = rxd_buffer[ptrIn++] - '=';
274
   d = rxd_buffer[ptrIn++] - '=';
1 ingob 275
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
276
 
277
   x = (a << 2) | (b >> 4);
278
   y = ((b & 0x0f) << 4) | (c >> 2);
279
   z = ((c & 0x03) << 6) | d;
280
 
281
   if(len--) ptrOut[ptr++] = x; else break;
282
   if(len--) ptrOut[ptr++] = y; else break;
283
   if(len--) ptrOut[ptr++] = z; else break;
284
  }
285
 
286
}
287
 
288
// --------------------------------------------------------------------------
289
void BearbeiteRxDaten(void)
290
{
291
 if(!NeuerDatensatzEmpfangen) return;
292
 
683 killagreg 293
 uint8_t tmp_char_arr2[2];
294
 
1 ingob 295
 PcZugriff = 255;
683 killagreg 296
  switch(rxd_buffer[2])
1 ingob 297
  {
499 hbuss 298
   case 'a':// Texte der Analogwerte
683 killagreg 299
            Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
499 hbuss 300
            DebugTextAnforderung = tmp_char_arr2[0];
301
                        break;
595 hbuss 302
   case 'b':
683 killagreg 303
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
685 killagreg 304
                        RemoteButtons |= ExternControl.RemoteButtons;
595 hbuss 305
            ConfirmFrame = ExternControl.Frame;
306
            break;
307
   case 'c':
683 killagreg 308
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
685 killagreg 309
                        RemoteButtons |= ExternControl.RemoteButtons;
595 hbuss 310
            ConfirmFrame = ExternControl.Frame;
1 ingob 311
            DebugDataAnforderung = 1;
312
            break;
313
   case 'h':// x-1 Displayzeilen
683 killagreg 314
            Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
685 killagreg 315
            RemoteButtons |= tmp_char_arr2[0];
395 hbuss 316
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
1 ingob 317
                        DebugDisplayAnforderung = 1;
318
                        break;
319
   case 't':// Motortest
683 killagreg 320
            Decode64((uint8_t *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
1 ingob 321
                        break;
492 hbuss 322
   case 'k':// Keys von DubWise
683 killagreg 323
            Decode64((uint8_t *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes);
595 hbuss 324
                        ConfirmFrame = DubWiseKeys[3];
492 hbuss 325
                        break;
1 ingob 326
   case 'v': // Version-Anforderung     und Ausbaustufe
327
            GetVersionAnforderung = 1;
683 killagreg 328
            break;
329
   case 'g':// "Get"-Anforderung für Debug-Daten
1 ingob 330
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
331
            DebugGetAnforderung = 1;
332
            break;
333
   case 'q':// "Get"-Anforderung für Settings
334
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
683 killagreg 335
            Decode64((uint8_t *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
1 ingob 336
            if(tmp_char_arr2[0] != 0xff)
337
             {
338
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
683 killagreg 339
                  ReadParameterSet(tmp_char_arr2[0], (uint8_t *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
340
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(uint8_t *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
341
             }
342
             else
343
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(uint8_t *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
344
 
1 ingob 345
            break;
683 killagreg 346
 
1 ingob 347
   case 'l':
348
   case 'm':
349
   case 'n':
350
   case 'o':
351
   case 'p': // Parametersatz speichern
683 killagreg 352
            Decode64((uint8_t *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
353
                        WriteParameterSet(rxd_buffer[2] - 'l' + 1, (uint8_t *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
354
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], rxd_buffer[2] - 'l' + 1);  // aktiven Datensatz merken
355
            Umschlag180Nick = (int32_t) EE_Parameter.WinkelUmschlagNick * 2500L;
356
            Umschlag180Roll = (int32_t) EE_Parameter.WinkelUmschlagRoll * 2500L;
1 ingob 357
            Piep(GetActiveParamSetNumber());
358
         break;
683 killagreg 359
 
360
 
1 ingob 361
  }
362
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
363
 NeuerDatensatzEmpfangen = 0;
364
}
365
 
366
//############################################################################
367
//Routine für die Serielle Ausgabe
683 killagreg 368
int16_t uart_putchar (int8_t c)
1 ingob 369
//############################################################################
370
{
371
        if (c == '\n')
372
                uart_putchar('\r');
373
        //Warten solange bis Zeichen gesendet wurde
683 killagreg 374
        loop_until_bit_is_set(UCSR0A, UDRE0);
1 ingob 375
        //Ausgabe des Zeichens
683 killagreg 376
        UDR0 = c;
377
 
1 ingob 378
        return (0);
379
}
380
 
381
// --------------------------------------------------------------------------
382
void WriteProgramData(unsigned int pos, unsigned char wert)
383
{
384
  //if (ProgramLocation == IN_RAM) Buffer[pos] = wert;
385
  // else eeprom_write_byte(&EE_Buffer[pos], wert);
386
  // Buffer[pos] = wert;
387
}
388
 
389
 
390
 
391
//---------------------------------------------------------------------------------------------
683 killagreg 392
void DatenUebertragung(void)
1 ingob 393
{
394
 if(!UebertragungAbgeschlossen) return;
395
 
396
   if(DebugGetAnforderung && UebertragungAbgeschlossen)               // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
683 killagreg 397
   {
398
      SendOutData('G',MeineSlaveAdresse,(uint8_t *) &ExternControl,sizeof(ExternControl));
1 ingob 399
          DebugGetAnforderung = 0;
400
   }
401
 
683 killagreg 402
    if((CheckDelay(Debug_Timer) || DebugDataAnforderung) && UebertragungAbgeschlossen)
1 ingob 403
         {
683 killagreg 404
          SendOutData('D',MeineSlaveAdresse,(uint8_t *) &DebugOut,sizeof(DebugOut));
1 ingob 405
          DebugDataAnforderung = 0;
683 killagreg 406
          Debug_Timer = SetDelay(MIN_DEBUG_INTERVALL);
407
         }
499 hbuss 408
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
409
     {
683 killagreg 410
      SendOutData('A',DebugTextAnforderung + '0',(uint8_t *) ANALOG_TEXT[DebugTextAnforderung],16);
499 hbuss 411
      DebugTextAnforderung = 255;
412
         }
683 killagreg 413
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz ohne CRC bestätigen
595 hbuss 414
         {
683 killagreg 415
      txd_buffer[0] = '#';
416
      txd_buffer[1] = ConfirmFrame;
417
      txd_buffer[2] = '\r';
595 hbuss 418
      UebertragungAbgeschlossen = 0;
419
      ConfirmFrame = 0;
683 killagreg 420
      UDR0 = txd_buffer[0];
595 hbuss 421
     }
1 ingob 422
     if(DebugDisplayAnforderung && UebertragungAbgeschlossen)
423
         {
685 killagreg 424
      LCD_PrintMenu();
1 ingob 425
          DebugDisplayAnforderung = 0;
683 killagreg 426
      if(++RemotePollDisplayLine == 4 || NurKanalAnforderung)
173 holgerb 427
      {
683 killagreg 428
       SendOutData('4',0,(uint8_t *)&PPM_in,sizeof(PPM_in));   // DisplayZeile übertragen
499 hbuss 429
       RemotePollDisplayLine = -1;
683 killagreg 430
      }
431
      else  SendOutData('0' + RemotePollDisplayLine,0,(uint8_t *)&DisplayBuff[20 * RemotePollDisplayLine],20);   // DisplayZeile übertragen
432
         }
433
    if(GetVersionAnforderung && UebertragungAbgeschlossen)
434
     {
435
      SendOutData('V',MeineSlaveAdresse,(uint8_t *) &VersionInfo,sizeof(VersionInfo));
1 ingob 436
          GetVersionAnforderung = 0;
437
     }
438
 
439
}
440