Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1995 - 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) Holger Buss, Ingo Busker
3
// + only for non-profit use
4
// + www.MikroKopter.com
5
// + see the File "License.txt" for further Informations
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
#include <stdarg.h>
8
#include <string.h>
9
#include <avr/pgmspace.h>
10
#include "main.h"
11
#include "uart.h"
12
#include "libfc.h"
13
#include "eeprom.h"
14
 
15
#define FC_ADDRESS 1
16
#define NC_ADDRESS 2
17
#define MK3MAG_ADDRESS 3
18
#define BL_CTRL_ADDRESS 5
19
 
20
#define ABO_TIMEOUT 4000 // disable abo after 4 seconds
21
#define MAX_SENDE_BUFF     170
22
#define MAX_EMPFANGS_BUFF  170
23
 
24
#define BLPARAM_REVISION 1
25
#define MASK_SET_PWM_SCALING            0x01
26
#define MASK_SET_CURRENT_LIMIT          0x02
27
#define MASK_SET_TEMP_LIMIT                     0x04
28
#define MASK_SET_CURRENT_SCALING        0x08
29
#define MASK_SET_BITCONFIG                      0x10
30
#define MASK_RESET_CAPCOUNTER           0x20
31
#define MASK_SET_DEFAULT_PARAMS         0x40
32
#define MASK_SET_SAVE_EEPROM            0x80
33
 
34
typedef struct
35
{
36
        unsigned char Revision;                 // revision of parameter structure
37
        unsigned char Address;                  // target address
38
        unsigned char PwmScaling;               // maximum value of pwm setpoint
39
        unsigned char CurrentLimit;             // current limit in 1A steps
40
        unsigned char TemperatureLimit; // in °C
41
        unsigned char CurrentScaling;   // scaling factor for current measurement
42
        unsigned char BitConfig;                // see defines above
43
        unsigned char SetMask;                  // filter for active paramters
44
        unsigned char Checksum;                 // checksum for parameter sturcture
45
}  __attribute__((packed)) BLParameter_t;
46
 
47
 
48
unsigned char GetExternalControl = 0,DebugDisplayAnforderung1 = 0, DebugDisplayAnforderung = 0,DebugDataAnforderung = 0,GetVersionAnforderung = 0, GetPPMChannelAnforderung = 0;
49
unsigned char DisplayLine = 0;
50
unsigned volatile char SioTmp = 0;
51
unsigned volatile char NeuerDatensatzEmpfangen = 0;
52
unsigned volatile char NeueKoordinateEmpfangen = 0;
53
unsigned volatile char UebertragungAbgeschlossen = 1;
54
unsigned volatile char CntCrcError = 0;
55
unsigned volatile char AnzahlEmpfangsBytes = 0;
56
unsigned volatile char TxdBuffer[MAX_SENDE_BUFF];
57
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
58
 
59
unsigned char *pRxData = 0;
60
unsigned char RxDataLen = 0;
61
unsigned volatile char PC_DebugTimeout = 0;
62
unsigned volatile char PC_MotortestActive = 0;
63
unsigned char DebugTextAnforderung = 255;
64
 
65
unsigned char PcZugriff = 100;
66
unsigned char MotorTest[16];
67
unsigned char MeineSlaveAdresse = 1; // Flight-Ctrl
68
unsigned char ConfirmFrame;
69
struct str_DebugOut    DebugOut;
70
struct str_ExternControl  ExternControl;
71
struct str_VersionInfo VersionInfo;
72
struct str_WinkelOut WinkelOut;
73
struct str_Data3D Data3D;
74
 
75
int Display_Timer, Debug_Timer,Kompass_Timer,Timer3D;
76
unsigned int DebugDataIntervall = 0, Intervall3D = 0, Display_Interval = 0;
77
unsigned int AboTimeOut = 0;
78
unsigned volatile char ReceiverUpdateModeActive = 0; // 1 = Update      2 = JetiBox-Simulation
79
 
80
 
81
 
82
 
83
 
84
#ifdef WITH_FULL_ANALOG_TEXT            ///  MartinW main.h means no memsave
85
#warning : "### with  normal ANALOG_TEXT[32][16] ###"
86
 
87
const unsigned char ANALOG_TEXT[32][16] PROGMEM =
88
{
89
   //1234567890123456
90
    "AngleNick       ", //0
91
    "AngleRoll       ",
92
    "AccNick         ",
93
    "AccRoll         ",
94
    "YawGyro         ",
95
    "Height Value    ", //5
96
    "AccZ            ",
97
    "Gas             ",
98
    "Compass Value   ",
99
    "Voltage [0.1V]  ",
100
    "Receiver Level  ", //10
101
    "Gyro Compass    ",
102
    "Motor 1         ",
103
    "Motor 2         ",
104
    "Motor 3         ",
105
    "Motor 4         ", //15
106
    "Motor 5         ",///
107
    "Motor 6         ",///
108
    "nc alt speed    ",
109
    "19              ",
110
    "Servo           ", //20
111
    "Hovergas        ",
112
    "Current [0.1A]  ",
113
    "Capacity [mAh]  ",
114
    "Hight Setpoint  ",
115
    "Motor 7         ", //25 ///
116
    "Motor 8         ",///
117
    "Compass Setpoint",
118
    "I2C-Error       ",
119
    "BL Limit        ",
120
    "GPS_Nick        ", //30
121
    "GPS_Roll        "
122
};     
123
 
124
#else
125
#warning : "### with  reduced ANALOG_TEXT[32][13] ###"  
126
const unsigned char ANALOG_TEXT[32][13] PROGMEM =
127
{
128
   //1234567890123456
129
    "AngleNick    ", //0
130
    "AngleRoll    ",
131
    "AccNick      ",
132
    "AccRoll      ",
133
    "YawGyro      ",
134
    "Height Value ", //5
135
    "AccZ         ",
136
    "Gas          ",
137
    "Compass Value",
138
    "Voltage[0.1V]",
139
    "ReceiverLevel", //10
140
    "Gyro Compass ",
141
    "Motor 1      ",
142
    "Motor 2      ",
143
    "Motor 3      ",
144
    "Motor 4      ", //15
145
    "Motor 5      ",///
146
    "Motor 6      ",///
147
    "nc alt speed ",
148
    "19           ",
149
    "Servo        ", //20
150
    "Hovergas     ",
151
    "Current[0.1A]",
152
    "Capacity[mAh]",
153
    "Hight Setp   ",
154
    "Motor 8      ", //25 ///
155
    "Motor 9      ",///
156
    "Compass Setp ",
157
    "I2C-Error    ",
158
    "BL Limit     ",
159
    "GPS_Nick     ", //30
160
    "GPS_Roll     "
161
};      
162
 
163
 
164
#endif
165
 
166
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
167
//++ Sende-Part der Datenübertragung
168
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
169
ISR(USART0_TX_vect)
170
{
171
 static unsigned int ptr = 0;
172
 unsigned char tmp_tx;
173
 
174
 if(!UebertragungAbgeschlossen)
175
  {
176
   ptr++;                    // die [0] wurde schon gesendet
177
   tmp_tx = TxdBuffer[ptr];
178
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
179
    {
180
     ptr = 0;
181
     UebertragungAbgeschlossen = 1;
182
    }
183
   UDR0 = tmp_tx;
184
  }
185
  else ptr = 0;
186
}
187
 
188
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
189
//++ Empfangs-Part der Datenübertragung, incl. CRC-Auswertung
190
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
191
ISR(USART0_RX_vect)
192
{
193
 static unsigned int crc;
194
 static unsigned char crc1,crc2,buf_ptr;
195
 static unsigned char UartState = 0;
196
 unsigned char CrcOkay = 0;
197
 
198
 if (ReceiverUpdateModeActive == 1) {   UDR1 = UDR0; return; }          // 1 = Update      
199
 if (ReceiverUpdateModeActive == 2) {   RxdBuffer[0] = UDR0; return; }  // 2 = JetiBox-Simulation 
200
 
201
 SioTmp = UDR0;
202
 
203
 if(buf_ptr >= MAX_SENDE_BUFF)    UartState = 0;
204
 if(SioTmp == '\r' && UartState == 2)
205
  {
206
   UartState = 0;
207
   crc -= RxdBuffer[buf_ptr-2];
208
   crc -= RxdBuffer[buf_ptr-1];
209
   crc %= 4096;
210
   crc1 = '=' + crc / 64;
211
   crc2 = '=' + crc % 64;
212
   CrcOkay = 0;
213
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; CntCrcError++;};
214
   if(!NeuerDatensatzEmpfangen && CrcOkay) // Datensatz schon verarbeitet
215
    {
216
     NeuerDatensatzEmpfangen = 1;
217
         AnzahlEmpfangsBytes = buf_ptr + 1;
218
     RxdBuffer[buf_ptr] = '\r';
219
         if(RxdBuffer[2] == 'R')
220
          {
221
 
222
#ifdef WITH_MKTOOL_Display                      ///  MartinW main.h
223
#warning : "### with  MKTool Display ###"
224
           LcdClear();
225
#endif
226
 
227
           wdt_enable(WDTO_250MS); // Reset-Commando
228
           ServoActive = 0;
229
          }
230
 
231
        }
232
  }
233
  else
234
  switch(UartState)
235
  {
236
   case 0:
237
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
238
                  buf_ptr = 0;
239
                  RxdBuffer[buf_ptr++] = SioTmp;
240
                  crc = SioTmp;
241
          break;
242
   case 1: // Adresse auswerten
243
                  UartState++;
244
                  RxdBuffer[buf_ptr++] = SioTmp;
245
                  crc += SioTmp;
246
                  break;
247
   case 2: //  Eingangsdaten sammeln
248
                  RxdBuffer[buf_ptr] = SioTmp;
249
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
250
                  else UartState = 0;
251
                  crc += SioTmp;
252
                  break;
253
   default:
254
          UartState = 0;
255
          break;
256
  }
257
}
258
 
259
 
260
// --------------------------------------------------------------------------
261
void AddCRC(unsigned int wieviele)
262
{
263
 unsigned int tmpCRC = 0,i;
264
 for(i = 0; i < wieviele;i++)
265
  {
266
   tmpCRC += TxdBuffer[i];
267
  }
268
   tmpCRC %= 4096;
269
   TxdBuffer[i++] = '=' + tmpCRC / 64;
270
   TxdBuffer[i++] = '=' + tmpCRC % 64;
271
   TxdBuffer[i++] = '\r';
272
  UebertragungAbgeschlossen = 0;
273
  UDR0 = TxdBuffer[0];
274
}
275
 
276
 
277
 
278
// --------------------------------------------------------------------------
279
void SendOutData(unsigned char cmd,unsigned char address, unsigned char BufferAnzahl, ...) //unsigned char *snd, unsigned char len)
280
{
281
 va_list ap;
282
 unsigned int pt = 0;
283
 unsigned char a,b,c;
284
 unsigned char ptr = 0;
285
 
286
 unsigned char *snd = 0;
287
 int len = 0;
288
 
289
 TxdBuffer[pt++] = '#';                         // Startzeichen
290
 TxdBuffer[pt++] = 'a' + address;               // Adresse (a=0; b=1,...)
291
 TxdBuffer[pt++] = cmd;                         // Commando
292
 
293
 va_start(ap, BufferAnzahl);
294
 if(BufferAnzahl)
295
 {
296
                snd = va_arg(ap, unsigned char*);
297
                len = va_arg(ap, int);
298
                ptr = 0;
299
                BufferAnzahl--;
300
 }
301
 while(len)
302
  {
303
        if(len)
304
        {
305
           a = snd[ptr++];
306
           len--;
307
           if((!len) && BufferAnzahl)
308
                {
309
                        snd = va_arg(ap, unsigned char*);
310
                        len = va_arg(ap, int);
311
                        ptr = 0;
312
                        BufferAnzahl--;
313
                }
314
        }
315
        else a = 0;
316
        if(len)
317
        {
318
                b = snd[ptr++];
319
                len--;
320
                if((!len) && BufferAnzahl)
321
                {
322
                        snd = va_arg(ap, unsigned char*);
323
                        len = va_arg(ap, int);
324
                        ptr = 0;
325
                        BufferAnzahl--;
326
                }
327
        }
328
        else b = 0;
329
        if(len)
330
        {
331
                c = snd[ptr++];
332
                len--;
333
                if((!len) && BufferAnzahl)
334
                {
335
                        snd = va_arg(ap, unsigned char*);
336
                        len = va_arg(ap, int);
337
                        ptr = 0;
338
                        BufferAnzahl--;
339
                }
340
        }
341
        else c = 0;
342
   TxdBuffer[pt++] = '=' + (a >> 2);
343
   TxdBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
344
   TxdBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
345
   TxdBuffer[pt++] = '=' + ( c & 0x3f);
346
  }
347
 va_end(ap);
348
 AddCRC(pt);
349
}
350
 
351
// --------------------------------------------------------------------------
352
void Decode64(void)  // die daten werden im rx buffer dekodiert, das geht nur, weil aus 4 byte immer 3 gemacht werden.
353
{
354
 unsigned char a,b,c,d;
355
 unsigned char x,y,z;
356
 unsigned char ptrIn = 3; // start at begin of data block
357
 unsigned char ptrOut = 3;
358
 unsigned char len = AnzahlEmpfangsBytes - 6; // von der Gesamtbytezahl eines Frames gehen 3 Bytes des Headers  ('#',Addr, Cmd) und 3 Bytes des Footers (CRC1, CRC2, '\r') ab.
359
 
360
 while(len)
361
  {
362
   a = RxdBuffer[ptrIn++] - '=';
363
   b = RxdBuffer[ptrIn++] - '=';
364
   c = RxdBuffer[ptrIn++] - '=';
365
   d = RxdBuffer[ptrIn++] - '=';
366
 
367
   x = (a << 2) | (b >> 4);
368
   y = ((b & 0x0f) << 4) | (c >> 2);
369
   z = ((c & 0x03) << 6) | d;
370
 
371
   if(len--) RxdBuffer[ptrOut++] = x; else break;
372
   if(len--) RxdBuffer[ptrOut++] = y; else break;
373
   if(len--) RxdBuffer[ptrOut++] = z;   else break;
374
  }
375
        pRxData = (unsigned char*)&RxdBuffer[3]; // decodierte Daten beginnen beim 4. Byte
376
        RxDataLen = ptrOut - 3;  // wie viele Bytes wurden dekodiert?
377
 
378
}
379
 
380
// --------------------------------------------------------------------------
381
void BearbeiteRxDaten(void)
382
{
383
 if(!NeuerDatensatzEmpfangen) return;
384
 
385
        unsigned char tempchar1, tempchar2;
386
        Decode64(); // dekodiere datenblock im Empfangsbuffer
387
        switch(RxdBuffer[1]-'a') // check for Slave Address
388
        {
389
                case FC_ADDRESS: // FC special commands
390
                switch(RxdBuffer[2])
391
                {
392
                        case 'K':// Kompasswert
393
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
394
//                                      KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180;
395
                                        break;
396
                        case 't':// Motortest
397
                                if(AnzahlEmpfangsBytes > 20) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
398
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
399
                                        PC_MotortestActive = 240;
400
                                        //while(!UebertragungAbgeschlossen);
401
                                        //SendOutData('T', MeineSlaveAdresse, 0);
402
                                        PcZugriff = 255;
403
                                        break;
404
 
405
                        case 'n':// "Get Mixer
406
                                        while(!UebertragungAbgeschlossen);
407
                    SendOutData('N', FC_ADDRESS, 1, (unsigned char *) &Mixer, sizeof(Mixer) - 1);
408
                                        Debug("Mixer lesen");
409
                                        break;
410
 
411
                        case 'm':// "Write Mixer
412
                    if(pRxData[0] == EEMIXER_REVISION)
413
                                        {
414
                       memcpy(&Mixer, (unsigned char *)pRxData, sizeof(Mixer) - 1);
415
                       MixerTable_WriteToEEProm();
416
                                           tempchar1 = 1;
417
                                           VersionInfo.HardwareError[1] &= ~FC_ERROR1_MIXER;
418
                                        }
419
                    else
420
                    {
421
                                                tempchar1 = 0;
422
                                        }
423
                                        while(!UebertragungAbgeschlossen);
424
                                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
425
                                        break;
426
 
427
                        case 'p': // get PPM Channels
428
                                        GetPPMChannelAnforderung = 1;
429
                                        PcZugriff = 255;
430
                                        break;
431
 
432
                        case 'q':// "Get"-Anforderung für Settings
433
                                        // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
434
                                        if((10 <= pRxData[0]) && (pRxData[0] < 20))
435
                                        {
436
                                                tempchar1 = pRxData[0] - 10;
437
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
438
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
439
                                                SetDefaultParameter(tempchar1, 1);
440
                                        }
441
                                        else if((20 <= pRxData[0]) && (pRxData[0] < 30))
442
                                        {
443
                                                tempchar1 = pRxData[0] - 20;
444
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
445
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
446
                                                SetDefaultParameter(tempchar1, 0);
447
                                        }
448
                                        else
449
                                        {
450
                                                tempchar1 = pRxData[0];
451
                                                if(tempchar1 == 0xFF)
452
                                                {
453
                                                        tempchar1 = GetActiveParamSet();
454
                                                }
455
                                                if(tempchar1< 1) tempchar1 = 1; // limit to 1
456
                                                else if(tempchar1 > 5) tempchar1 = 5; // limit to 5
457
                                                // load requested parameter set
458
                                                ParamSet_ReadFromEEProm(tempchar1);
459
                                        }
460
                                        while(!UebertragungAbgeschlossen);
461
                                        SendOutData('Q', FC_ADDRESS, 2, &tempchar1, sizeof(tempchar1), (unsigned char *) &EE_Parameter, sizeof(EE_Parameter) - 1);
462
                                        Debug("Lese Setting %d", tempchar1);
463
 
464
                                        break;
465
 
466
                        case 's': // Parametersatz speichern
467
                                        if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION) && MotorenEin == 0) // check for setting to be in range
468
                                        {
469
                                                memcpy(&EE_Parameter, (uint8_t*)&pRxData[1], sizeof(EE_Parameter) - 1);
470
                                                ParamSet_WriteToEEProm(pRxData[0]);
471
                                                Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
472
                                                Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
473
                                                tempchar1 = GetActiveParamSet();
474
                                        }
475
                                        else
476
                                        {
477
                                                tempchar1 = 0; // mark in response an invlid setting
478
                                        }
479
                                        while(!UebertragungAbgeschlossen);
480
                                        SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
481
                                        if(!MotorenEin) Piep(tempchar1,110);
482
                                        LipoDetection(0);
483
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
484
                                        break;
485
                        case 'f': // auf anderen Parametersatz umschalten
486
                                if((1 <= pRxData[0]) && (pRxData[0] <= 5)) ParamSet_ReadFromEEProm(pRxData[0]);
487
                                        tempchar1 = GetActiveParamSet();
488
                                        while(!UebertragungAbgeschlossen);
489
                                        SendOutData('F', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
490
                                        if(!MotorenEin) Piep(tempchar1,110);
491
                                        LipoDetection(0);
492
                                        LIBFC_ReceiverInit(EE_Parameter.Receiver);
493
                                        break;
494
                        case 'y':// serial Potis
495
                                        PPM_in[13] = (signed char) pRxData[0]; PPM_in[14] = (signed char) pRxData[1]; PPM_in[15] = (signed char) pRxData[2]; PPM_in[16] = (signed char) pRxData[3];
496
                                        PPM_in[17] = (signed char) pRxData[4]; PPM_in[18] = (signed char) pRxData[5]; PPM_in[19] = (signed char) pRxData[6]; PPM_in[20] = (signed char) pRxData[7];
497
                                        PPM_in[21] = (signed char) pRxData[8]; PPM_in[22] = (signed char) pRxData[9]; PPM_in[23] = (signed char) pRxData[10]; PPM_in[24] = (signed char) pRxData[11];
498
                                        break;
499
 
500
                        case 'u': // request BL parameter
501
                                Debug("Reading BL %d", pRxData[0]);
502
                                // try to read BL configuration
503
                                tempchar2 = I2C_ReadBLConfig(pRxData[0]);
504
                                if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
505
                                else tempchar1 = 0;
506
                                while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
507
                                SendOutData('U', FC_ADDRESS, 4, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), &pRxData[0], 1, &BLConfig, sizeof(BLConfig_t));
508
                                break;
509
 
510
                        case 'w': // write BL parameter
511
                                Debug("Writing BL %d", pRxData[0]);
512
                                if(RxDataLen >= 1+sizeof(BLConfig_t))
513
                                {
514
                                        memcpy(&BLConfig, (uint8_t*)(&pRxData[1]), sizeof(BLConfig_t));
515
                                        tempchar2 = I2C_WriteBLConfig(pRxData[0]);
516
                                        if(tempchar2 == BLCONFIG_SUCCESS) tempchar1 = 1;
517
                                        else tempchar1 = 0; // indicate error
518
                                        while(!UebertragungAbgeschlossen); // wait for previous frame to be sent
519
                                        SendOutData('W', FC_ADDRESS,2, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2));
520
                                }
521
                                break;
522
                        case 'j':
523
                                if(MotorenEin) break;
524
 
525
#ifdef WITH_JETI_SIMULATION             ///  MartinW main.h means no memsave
526
#warning : "### with  jeti update command ###"
527
 
528
 
529
 
530
                                tempchar1 = LIBFC_GetCPUType();
531
                                if((tempchar1 == CPU_ATMEGA644P) || (tempchar1 == CPU_ATMEGA1284P))
532
                                {
533
                                        uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/ (8 * 38400L) - 1);
534
 
535
                                        cli();
536
 
537
                                        // UART0 & UART1 disable RX and TX-Interrupt
538
                                        UCSR0B &= ~((1 << RXCIE0)|(1 << TXCIE0));
539
                                        UCSR1B &= ~((1 << RXCIE1)|(1 << TXCIE1));
540
 
541
                                        // UART0 & UART1 disable receiver and transmitter
542
                                        UCSR0B &= ~((1 << TXEN0) | (1 << RXEN0));
543
                                        UCSR1B &= ~((1 << TXEN1) | (1 << RXEN1));
544
 
545
                                        // UART0 & UART1 flush receive buffer explicit
546
                                        while ( UCSR1A & (1<<RXC1) ) UDR1;
547
                                        while ( UCSR0A & (1<<RXC0) ) UDR0;
548
 
549
 
550
                                        if(pRxData[0] == 1) ReceiverUpdateModeActive = 2;
551
                                        else
552
                                        {           // Jeti or HoTT update
553
//#if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__))
554
                                                if(pRxData[0] == 100)   ubrr = (uint16_t) ((uint32_t) F_CPU/ (8 * 19200L) - 1);  // HoTT
555
//#endif
556
                                                ReceiverUpdateModeActive = 1;
557
                                                // UART0 & UART1 set baudrate
558
                                                UBRR1H = (uint8_t)(ubrr>>8);
559
                                                UBRR1L = (uint8_t)ubrr;
560
                                                UBRR0H = UBRR1H;
561
                                                UBRR0L = UBRR1L;
562
                                                // UART1 no parity
563
                                                UCSR1C &= ~(1 << UPM11);
564
                                                UCSR1C &= ~(1 << UPM10);
565
                                                // UART1 8-bit
566
                                                UCSR1B &= ~(1 << UCSZ12);
567
                                                UCSR1C |= (1 << UCSZ11);
568
                                                UCSR1C |= (1 << UCSZ10);
569
                                        }
570
                                        // UART0 & UART1 1 stop bit
571
                                        UCSR1C &= ~(1 << USBS1);
572
                                        UCSR0C &= ~(1 << USBS0);
573
                                        // UART1 clear 9th bit
574
                                        UCSR1B &= ~(1<<TXB81);
575
                                        // enable receiver and transmitter for UART0 and UART1
576
                                        UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
577
                                        UCSR1B |= (1 << TXEN1) | (1 << RXEN1);
578
                                        // enable RX-Interrupt for UART0 and UART1
579
                                        UCSR0B |= (1 << RXCIE0);
580
                                        UCSR1B |= (1 << RXCIE1);
581
                                        // disable other Interrupts
582
                                        TIMSK0 = 0;
583
                                        TIMSK1 = 0;
584
                                        TIMSK2 = 0;
585
 
586
                                        sei();
587
                                }
588
 
589
#else
590
#warning : "### without  jeti update command ###"       
591
 
592
#endif
593
                                break;
594
 
595
                } // case FC_ADDRESS:
596
 
597
                default: // any Slave Address
598
 
599
                switch(RxdBuffer[2])
600
                {
601
                        // 't' comand placed here only for compatibility to BL
602
                        case 't':// Motortest
603
                                if(AnzahlEmpfangsBytes >= sizeof(MotorTest)) memcpy(&MotorTest[0], (unsigned char *)pRxData, sizeof(MotorTest));
604
                                else memcpy(&MotorTest[0], (unsigned char *)pRxData, 4);
605
                                        while(!UebertragungAbgeschlossen);
606
                                        SendOutData('T', MeineSlaveAdresse, 0);
607
                                        PC_MotortestActive = 250;
608
                                        PcZugriff = 255;
609
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
610
                                        break;
611
                        // 'K' comand placed here only for compatibility to old MK3MAG software, that does not send the right Slave Address
612
                        case 'K':// Kompasswert
613
                                        memcpy((unsigned char *)&KompassValue , (unsigned char *)pRxData, sizeof(KompassValue));
614
//                                      KompassRichtung = ((540 + KompassValue - KompassSollWert) % 360) - 180;
615
                                        break;
616
                        case 'a':// Texte der Analogwerte
617
                                        DebugTextAnforderung = pRxData[0];
618
                                        if (DebugTextAnforderung > 31) DebugTextAnforderung = 31;
619
                                        PcZugriff = 255;
620
                                        break;
621
                        case 'b':
622
                                        memcpy((unsigned char *)&ExternControl, (unsigned char *)pRxData, sizeof(ExternControl));
623
                                        ConfirmFrame = ExternControl.Frame;
624
                                        PcZugriff = 255;
625
                                        break;
626
                        case 'c': // Poll the 3D-Data
627
                    if(!Intervall3D) { if(pRxData[0]) Timer3D = SetDelay(pRxData[0] * 10);}
628
                                        Intervall3D = pRxData[0] * 10;
629
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
630
                                        break;
631
                        case 'd': // Poll the debug data
632
                                        PcZugriff = 255;
633
                                        DebugDataIntervall = (unsigned int)pRxData[0] * 10;
634
                                        if(DebugDataIntervall > 0) DebugDataAnforderung = 1;
635
                                        AboTimeOut = SetDelay(ABO_TIMEOUT);
636
                                        break;
637
 
638
                        case 'h':// x-1 Displayzeilen
639
 
640
#ifdef WITH_MKTOOL_Display                                                              /// MartinW memorysave
641
#warning : "### with  MKTool Display ###"
642
 
643
                                        PcZugriff = 255;
644
                                if((pRxData[0] & 0x80) == 0x00) // old format
645
                                        {
646
                                                DisplayLine = 2;
647
                                                Display_Interval = 0;
648
                                        }
649
                                        else // new format
650
                                        {
651
                                                RemoteKeys |= ~pRxData[0];
652
                                                Display_Interval = (unsigned int)pRxData[1] * 10;
653
                                                DisplayLine = 4;
654
                                                AboTimeOut = SetDelay(ABO_TIMEOUT);
655
                                        }
656
                                        DebugDisplayAnforderung = 1;
657
                                        break;
658
 
659
                        case 'l':// x-1 Displayzeilen
660
                                PcZugriff = 255;
661
                                        MenuePunkt = pRxData[0];
662
                                        DebugDisplayAnforderung1 = 1;
663
 
664
#endif
665
                                        break;
666
                        case 'v': // Version-Anforderung und Ausbaustufe
667
                                        GetVersionAnforderung = 1;
668
                                        break;
669
 
670
                        case 'g'://
671
                                        GetExternalControl = 1;
672
                                        break;
673
 
674
                        default:
675
                                //unsupported command received
676
                                break;
677
                }
678
                break; // default:
679
        }
680
        NeuerDatensatzEmpfangen = 0;
681
        pRxData = 0;
682
        RxDataLen = 0;
683
}
684
 
685
//############################################################################
686
//Routine für die Serielle Ausgabe
687
void uart_putchar (char c)
688
//############################################################################
689
{
690
        //Warten solange bis Zeichen gesendet wurde
691
        loop_until_bit_is_set(UCSR0A, UDRE0);
692
        //Ausgabe des Zeichens
693
        UDR0 = c;
694
}
695
 
696
 
697
//############################################################################
698
//INstallation der Seriellen Schnittstelle
699
void UART_Init (void)
700
//############################################################################
701
{
702
        unsigned int ubrr = (unsigned int) ((unsigned long) F_CPU/(8 * USART0_BAUD) - 1);
703
 
704
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
705
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
706
    // UART Double Speed (U2X)
707
        UCSR0A |= (1 << U2X0);
708
        // RX-Interrupt Freigabe
709
        UCSR0B |= (1 << RXCIE0);
710
        // TX-Interrupt Freigabe
711
        UCSR0B |= (1 << TXCIE0);
712
        // USART0 Baud Rate Register
713
        // set clock divider
714
        UBRR0H = (uint8_t)(ubrr >> 8);
715
        UBRR0L = (uint8_t)ubrr;
716
 
717
        Debug_Timer = SetDelay(DebugDataIntervall);
718
        Kompass_Timer = SetDelay(220);
719
 
720
        VersionInfo.SWMajor = VERSION_MAJOR;
721
        VersionInfo.SWMinor = VERSION_MINOR;
722
        VersionInfo.SWPatch = VERSION_PATCH;
723
        VersionInfo.ProtoMajor  = VERSION_SERIAL_MAJOR;
724
        VersionInfo.ProtoMinor  = VERSION_SERIAL_MINOR;
725
 
726
        pRxData = 0;
727
        RxDataLen = 0;
728
}
729
 
730
//---------------------------------------------------------------------------------------------
731
void DatenUebertragung(void)
732
{
733
        if(!UebertragungAbgeschlossen) return;
734
 
735
        if(CheckDelay(AboTimeOut))
736
        {
737
                Display_Interval = 0;
738
                DebugDataIntervall = 0;
739
                Intervall3D = 0;
740
        }
741
 
742
#ifdef WITH_MKTOOL_Display      //main.h
743
#warning : "### with  MKTool Display ###"
744
 
745
        if(((Display_Interval>0 && CheckDelay(Display_Timer)) || DebugDisplayAnforderung) && UebertragungAbgeschlossen)
746
        {
747
                if(DisplayLine > 3)// new format
748
                {
749
                        Menu();
750
                        SendOutData('H', FC_ADDRESS, 1, (uint8_t *)DisplayBuff, 80);
751
                }
752
                else // old format
753
                {
754
                        LCD_printfxy(0,0,"!!! INCOMPATIBLE !!!");
755
                        SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), (uint8_t *)DisplayBuff, 20);
756
                        if(DisplayLine++ > 3) DisplayLine = 0;
757
                }
758
                Display_Timer = SetDelay(Display_Interval);
759
                DebugDisplayAnforderung = 0;
760
        }
761
        if(DebugDisplayAnforderung1 && UebertragungAbgeschlossen)
762
        {
763
                Menu();
764
                SendOutData('L', FC_ADDRESS, 3, &MenuePunkt, sizeof(MenuePunkt), &MaxMenue, sizeof(MaxMenue), DisplayBuff, sizeof(DisplayBuff));
765
                DebugDisplayAnforderung1 = 0;
766
        }
767
 
768
#endif
769
 
770
        if(GetVersionAnforderung && UebertragungAbgeschlossen)
771
        {
772
                SendOutData('V', FC_ADDRESS, 1, (unsigned char *) &VersionInfo, sizeof(VersionInfo));
773
                GetVersionAnforderung = 0;
774
                Debug_OK("Version gesendet");
775
        }
776
 
777
        if(GetExternalControl && UebertragungAbgeschlossen)           // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
778
        {
779
                SendOutData('G',MeineSlaveAdresse, 1, (unsigned char *) &ExternControl, sizeof(ExternControl));
780
                GetExternalControl = 0;
781
        }
782
    if((CheckDelay(Kompass_Timer)) && UebertragungAbgeschlossen)
783
         {
784
                  WinkelOut.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
785
                  WinkelOut.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
786
                  WinkelOut.UserParameter[0] = Parameter_UserParam1;
787
                  WinkelOut.UserParameter[1] = Parameter_UserParam2;
788
          SendOutData('k', MK3MAG_ADDRESS, 1, (unsigned char *) &WinkelOut,sizeof(WinkelOut));
789
          if(WinkelOut.CalcState > 4)  WinkelOut.CalcState = 6; // wird dann in SPI auf Null gesetzt
790
          Kompass_Timer = SetDelay(99);
791
         }
792
    if(((DebugDataIntervall>0 && CheckDelay(Debug_Timer)) || DebugDataAnforderung) && UebertragungAbgeschlossen)
793
         {
794
                  CopyDebugValues();
795
          SendOutData('D', FC_ADDRESS, 1, (unsigned char *) &DebugOut,sizeof(DebugOut));
796
          DebugDataAnforderung = 0;
797
          if(DebugDataIntervall>0) Debug_Timer = SetDelay(DebugDataIntervall);
798
         }
799
    if(Intervall3D > 0 && CheckDelay(Timer3D) && UebertragungAbgeschlossen)
800
         {
801
                  Data3D.Winkel[0] = (int) (IntegralNick / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
802
                  Data3D.Winkel[1] = (int) (IntegralRoll / (EE_Parameter.GyroAccFaktor * 4));  // etwa in 0.1 Grad
803
          Data3D.Winkel[2] = (int) ((10 * ErsatzKompass) / GIER_GRAD_FAKTOR);
804
                  Data3D.Centroid[0] = SummeNick >> 9;
805
                  Data3D.Centroid[1] = SummeRoll >> 9;
806
                  Data3D.Centroid[2] = Mess_Integral_Gier >> 9;
807
          SendOutData('C', FC_ADDRESS, 1, (unsigned char *) &Data3D,sizeof(Data3D));
808
          Timer3D = SetDelay(Intervall3D);
809
         }
810
    if(DebugTextAnforderung != 255) // Texte für die Analogdaten
811
     {
812
 
813
 
814
 
815
         #ifdef WITH_FULL_ANALOG_TEXT                   ///  MartinW main.h means no memsave
816
#warning : "### with  normal ANALOG_TEXT[32][16] ###"
817
                unsigned char label[16]; // local sram buffer
818
                memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 16); // read lable from flash to sra
819
      SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16); 
820
 
821
#else
822
#warning : "### with  reduced ANALOG_TEXT[32][13] ###"  
823
 
824
                unsigned char label[16]="                "; // local sram buffer ///MartinW; for MKTool, needs 32 bytes
825
          memcpy_P(label, ANALOG_TEXT[DebugTextAnforderung], 13); // read lable from flash to sra
826
      SendOutData('A', FC_ADDRESS, 2, (unsigned char *)&DebugTextAnforderung, sizeof(DebugTextAnforderung),label, 16);
827
#endif
828
 
829
 
830
 
831
      DebugTextAnforderung = 255;
832
         }
833
     if(ConfirmFrame && UebertragungAbgeschlossen)   // Datensatz bestätigen
834
         {
835
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame));
836
        ConfirmFrame = 0;
837
     }
838
 
839
     if(GetPPMChannelAnforderung && UebertragungAbgeschlossen)
840
     {
841
                 SendOutData('P', FC_ADDRESS, 1, (unsigned char *) &PPM_in, sizeof(PPM_in));
842
                 GetPPMChannelAnforderung = 0;
843
         }
844
 
845
#ifdef DEBUG                                                                                                                    // only include functions if DEBUG is defined
846
     if(SendDebugOutput && UebertragungAbgeschlossen)
847
     {
848
                 SendOutData('0', FC_ADDRESS, 1, (unsigned char *) &tDebug, sizeof(tDebug));
849
                 SendDebugOutput = 0;
850
         }
851
#endif
852
 
853
}
854
 
855