Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1910 - 1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <avr/wdt.h>
4
#include <avr/pgmspace.h>
5
#include <stdarg.h>
6
#include <string.h>
7
 
8
#include "eeprom.h"
9
#include "timer0.h"
10
#include "uart0.h"
11
#include "rc.h"
12
#include "externalControl.h"
13
#include "output.h"
14
#include "attitude.h"
15
 
16
 
17
#ifdef USE_MK3MAG
18
#include "mk3mag.h"
19
#endif
20
 
21
#define FC_ADDRESS 1
22
#define NC_ADDRESS 2
23
#define MK3MAG_ADDRESS 3
24
 
25
#define FALSE   0
26
#define TRUE    1
27
//int8_t test __attribute__ ((section (".noinit")));
28
uint8_t request_VerInfo = FALSE;
29
uint8_t request_ExternalControl = FALSE;
30
uint8_t request_Display = FALSE;
31
uint8_t request_Display1 = FALSE;
32
uint8_t request_DebugData = FALSE;
33
uint8_t request_Data3D = FALSE;
34
uint8_t request_DebugLabel = 255;
35
uint8_t request_PPMChannels = FALSE;
36
uint8_t request_OutputTest = FALSE;
37
uint8_t request_variables = FALSE;
38
 
39
uint8_t DisplayLine = 0;
40
 
41
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
42
volatile uint8_t rxd_buffer_locked = FALSE;
43
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
44
volatile uint8_t txd_complete = TRUE;
45
volatile uint8_t ReceivedBytes = 0;
46
volatile uint8_t *pRxData = 0;
47
volatile uint8_t RxDataLen = 0;
48
 
49
uint8_t outputTestActive = 0;
50
uint8_t outputTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
51
uint8_t ConfirmFrame;
52
 
53
typedef struct {
54
        int16_t Heading;
55
}__attribute__((packed)) Heading_t;
56
 
57
DebugOut_t DebugOut;
58
Data3D_t Data3D;
59
UART_VersionInfo_t UART_VersionInfo;
60
 
61
uint16_t DebugData_Timer;
62
uint16_t Data3D_Timer;
63
uint16_t DebugData_Interval = 500; // in 1ms
64
uint16_t Data3D_Interval = 0; // in 1ms
65
 
66
#ifdef USE_MK3MAG
67
int16_t Compass_Timer;
68
#endif
69
 
70
// keep lables in flash to save 512 bytes of sram space
71
const prog_uint8_t ANALOG_LABEL[32][16] = {
72
                //1234567890123456
73
                "AnglePitch      ", //0
74
                "AngleRoll       ",
75
                "AngleYaw        ",
76
                "GyroPitch(PID)  ",
77
                "GyroRoll(PID)   ",
78
                "GyroYaw         ", //5
79
                "GyroFactorPitch ",
80
                "GyroFactorRoll  ",
81
                "GyroFactorYaw   ",
82
                "GyroDPitch      ",
83
                "GyroDRoll       ",//10
84
                "GyroDYaw        ",
85
                "Pitch Term      ",
86
                "Roll Term       ",
87
                "Throttle Term   ",
88
                "Yaw Term        ", //15
89
                "0th O Corr pitch",
90
                "0th O Corr roll ",
91
                "ControlIntePitch",
92
                "ControlInteRoll ",
93
                "UBat            ", //20
94
                "hoverThrottle   ",
95
                "IPartPitch      ", // OK
96
                "IPartRoll       ",
97
                "S3 (THROTTLE)   ",
98
                "S4 (RUDDER)     ", // OK //25
99
                "ControlYaw      ",
100
                "Airpress. Range ", // OK
101
                "DriftCompPitch  ",
102
                "DriftCompRoll   ",
103
                "AirpressFiltered", // MISSING //30
104
                "AirpressADC     " };
105
 
106
/****************************************************************/
107
/*              Initialization of the USART0                    */
108
/****************************************************************/
109
void usart0_Init(void) {
110
        uint8_t sreg = SREG;
111
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * USART0_BAUD) - 1);
112
 
113
        // disable all interrupts before configuration
114
        cli();
115
 
116
        // disable RX-Interrupt
117
        UCSR0B &= ~(1 << RXCIE0);
118
        // disable TX-Interrupt
119
        UCSR0B &= ~(1 << TXCIE0);
120
 
121
        // set direction of RXD0 and TXD0 pins
122
        // set RXD0 (PD0) as an input pin
123
        PORTD |= (1 << PORTD0);
124
        DDRD &= ~(1 << DDD0);
125
        // set TXD0 (PD1) as an output pin
126
        PORTD |= (1 << PORTD1);
127
        DDRD |= (1 << DDD1);
128
 
129
        // USART0 Baud Rate Register
130
        // set clock divider
131
        UBRR0H = (uint8_t) (ubrr >> 8);
132
        UBRR0L = (uint8_t) ubrr;
133
 
134
        // USART0 Control and Status Register A, B, C
135
 
136
        // enable double speed operation in
137
        UCSR0A |= (1 << U2X0);
138
        // enable receiver and transmitter in
139
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
140
        // set asynchronous mode
141
        UCSR0C &= ~(1 << UMSEL01);
142
        UCSR0C &= ~(1 << UMSEL00);
143
        // no parity
144
        UCSR0C &= ~(1 << UPM01);
145
        UCSR0C &= ~(1 << UPM00);
146
        // 1 stop bit
147
        UCSR0C &= ~(1 << USBS0);
148
        // 8-bit
149
        UCSR0B &= ~(1 << UCSZ02);
150
        UCSR0C |= (1 << UCSZ01);
151
        UCSR0C |= (1 << UCSZ00);
152
 
153
        // flush receive buffer
154
        while (UCSR0A & (1 << RXC0))
155
                UDR0;
156
 
157
        // enable interrupts at the end
158
        // enable RX-Interrupt
159
        UCSR0B |= (1 << RXCIE0);
160
        // enable TX-Interrupt
161
        UCSR0B |= (1 << TXCIE0);
162
 
163
        // initialize the debug timer
164
        DebugData_Timer = setDelay(DebugData_Interval);
165
 
166
        // unlock rxd_buffer
167
        rxd_buffer_locked = FALSE;
168
        pRxData = 0;
169
        RxDataLen = 0;
170
 
171
        // no bytes to send
172
        txd_complete = TRUE;
173
 
174
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
175
        UART_VersionInfo.SWMinor = VERSION_MINOR;
176
        UART_VersionInfo.SWPatch = VERSION_PATCH;
177
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
178
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
179
 
180
        // restore global interrupt flags
181
        SREG = sreg;
182
}
183
 
184
/****************************************************************/
185
/* USART0 transmitter ISR                                       */
186
/****************************************************************/
187
ISR(USART0_TX_vect)
188
{
189
        static uint16_t ptr_txd_buffer = 0;
190
        uint8_t tmp_tx;
191
        if (!txd_complete) { // transmission not completed
192
                ptr_txd_buffer++; // die [0] wurde schon gesendet
193
                tmp_tx = txd_buffer[ptr_txd_buffer];
194
                // if terminating character or end of txd buffer was reached
195
                if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) {
196
                        ptr_txd_buffer = 0; // reset txd pointer
197
                        txd_complete = 1; // stop transmission
198
                }
199
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
200
        }
201
        // transmission completed
202
        else
203
                ptr_txd_buffer = 0;
204
}
205
 
206
/****************************************************************/
207
/* USART0 receiver               ISR                            */
208
/****************************************************************/
209
ISR(USART0_RX_vect)
210
{
211
        static uint16_t crc;
212
        static uint8_t ptr_rxd_buffer = 0;
213
        uint8_t crc1, crc2;
214
        uint8_t c;
215
 
216
        c = UDR0; // catch the received byte
217
 
218
        if (rxd_buffer_locked)
219
                return; // if rxd buffer is locked immediately return
220
 
221
        // the rxd buffer is unlocked
222
        if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received
223
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
224
                crc = c; // init crc
225
        }
226
#if 0
227
        else if (ptr_rxd_buffer == 1) { // handle address
228
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
229
                crc += c; // update crc
230
        }
231
#endif
232
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
233
                if (c != '\r') { // no termination character
234
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
235
                        crc += c; // update crc
236
                } else { // termination character was received
237
                        // the last 2 bytes are no subject for checksum calculation
238
                        // they are the checksum itself
239
                        crc -= rxd_buffer[ptr_rxd_buffer - 2];
240
                        crc -= rxd_buffer[ptr_rxd_buffer - 1];
241
                        // calculate checksum from transmitted data
242
                        crc %= 4096;
243
                        crc1 = '=' + crc / 64;
244
                        crc2 = '=' + crc % 64;
245
                        // compare checksum to transmitted checksum bytes
246
                        if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2
247
                                        == rxd_buffer[ptr_rxd_buffer - 1])) {
248
                                // checksum valid
249
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
250
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
251
                                rxd_buffer_locked = TRUE; // lock the rxd buffer
252
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
253
                                if (rxd_buffer[2] == 'R') {
254
                                        wdt_enable(WDTO_250MS);
255
                                } // Reset-Commando
256
                        } else { // checksum invalid
257
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
258
                        }
259
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
260
                }
261
        } else { // rxd buffer overrun
262
                ptr_rxd_buffer = 0; // reset rxd buffer
263
                rxd_buffer_locked = FALSE; // unlock rxd buffer
264
        }
265
}
266
 
267
// --------------------------------------------------------------------------
268
void AddCRC(uint16_t datalen) {
269
        uint16_t tmpCRC = 0, i;
270
        for (i = 0; i < datalen; i++) {
271
                tmpCRC += txd_buffer[i];
272
        }
273
        tmpCRC %= 4096;
274
        txd_buffer[i++] = '=' + tmpCRC / 64;
275
        txd_buffer[i++] = '=' + tmpCRC % 64;
276
        txd_buffer[i++] = '\r';
277
        txd_complete = FALSE;
278
        UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
279
}
280
 
281
// --------------------------------------------------------------------------
282
// application example:
283
// SendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16);
284
/*
285
 void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
286
 va_list ap;
287
 uint16_t txd_bufferIndex = 0;
288
 uint8_t *currentBuffer;
289
 uint8_t currentBufferIndex;
290
 uint16_t lengthOfCurrentBuffer;
291
 uint8_t shift = 0;
292
 
293
 txd_buffer[txd_bufferIndex++] = '#';                   // Start character
294
 txd_buffer[txd_bufferIndex++] = 'a' + addr;            // Address (a=0; b=1,...)
295
 txd_buffer[txd_bufferIndex++] = cmd;                   // Command
296
 
297
 va_start(ap, numofbuffers);
298
 
299
 while(numofbuffers) {
300
 currentBuffer = va_arg(ap, uint8_t*);
301
 lengthOfCurrentBuffer = va_arg(ap, int);
302
 currentBufferIndex = 0;
303
 // Encode data: 3 bytes of data are encoded into 4 bytes,
304
 // where the 2 most significant bits are both 0.
305
 while(currentBufferIndex != lengthOfCurrentBuffer) {
306
 if (!shift) txd_buffer[txd_bufferIndex] = 0;
307
 txd_buffer[txd_bufferIndex]  |= currentBuffer[currentBufferIndex] >> (shift + 2);
308
 txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111;
309
 shift += 2;
310
 if (shift == 6) { shift=0; txd_bufferIndex++; }
311
 currentBufferIndex++;
312
 }
313
 }
314
 // If the number of data bytes was not divisible by 3, stuff
315
 //  with 0 pseudodata  until length is again divisible by 3.
316
 if (shift == 2) {
317
 // We need to stuff with zero bytes at the end.
318
 txd_buffer[txd_bufferIndex]  &= 0b00110000;
319
 txd_buffer[++txd_bufferIndex] = 0;
320
 shift = 4;
321
 }
322
 if (shift == 4) {
323
 // We need to stuff with zero bytes at the end.
324
 txd_buffer[txd_bufferIndex++] &= 0b00111100;
325
 txd_buffer[txd_bufferIndex]    = 0;
326
 }
327
 va_end(ap);
328
 AddCRC(pt); // add checksum after data block and initates the transmission
329
 }
330
 */
331
 
332
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
333
        va_list ap;
334
        uint16_t pt = 0;
335
        uint8_t a, b, c;
336
        uint8_t ptr = 0;
337
 
338
        uint8_t *pdata = 0;
339
        int len = 0;
340
 
341
        txd_buffer[pt++] = '#'; // Start character
342
        txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...)
343
        txd_buffer[pt++] = cmd; // Command
344
 
345
        va_start(ap, numofbuffers);
346
 
347
        if (numofbuffers) {
348
                pdata = va_arg(ap, uint8_t*);
349
                len = va_arg(ap, int);
350
                ptr = 0;
351
                numofbuffers--;
352
        }
353
 
354
        while (len) {
355
                if (len) {
356
                        a = pdata[ptr++];
357
                        len--;
358
                        if ((!len) && numofbuffers) {
359
                                pdata = va_arg(ap, uint8_t*);
360
                                len = va_arg(ap, int);
361
                                ptr = 0;
362
                                numofbuffers--;
363
                        }
364
                } else
365
                        a = 0;
366
                if (len) {
367
                        b = pdata[ptr++];
368
                        len--;
369
                        if ((!len) && numofbuffers) {
370
                                pdata = va_arg(ap, uint8_t*);
371
                                len = va_arg(ap, int);
372
                                ptr = 0;
373
                                numofbuffers--;
374
                        }
375
                } else
376
                        b = 0;
377
                if (len) {
378
                        c = pdata[ptr++];
379
                        len--;
380
                        if ((!len) && numofbuffers) {
381
                                pdata = va_arg(ap, uint8_t*);
382
                                len = va_arg(ap, int);
383
                                ptr = 0;
384
                                numofbuffers--;
385
                        }
386
                } else
387
                        c = 0;
388
                txd_buffer[pt++] = '=' + (a >> 2);
389
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
390
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
391
                txd_buffer[pt++] = '=' + (c & 0x3f);
392
        }
393
        va_end(ap);
394
        AddCRC(pt); // add checksum after data block and initates the transmission
395
}
396
 
397
// --------------------------------------------------------------------------
398
void Decode64(void) {
399
        uint8_t a, b, c, d;
400
        uint8_t x, y, z;
401
        uint8_t ptrIn = 3;
402
        uint8_t ptrOut = 3;
403
        uint8_t len = ReceivedBytes - 6;
404
 
405
        while (len) {
406
                a = rxd_buffer[ptrIn++] - '=';
407
                b = rxd_buffer[ptrIn++] - '=';
408
                c = rxd_buffer[ptrIn++] - '=';
409
                d = rxd_buffer[ptrIn++] - '=';
410
                //if(ptrIn > ReceivedBytes - 3) break;
411
 
412
                x = (a << 2) | (b >> 4);
413
                y = ((b & 0x0f) << 4) | (c >> 2);
414
                z = ((c & 0x03) << 6) | d;
415
 
416
                if (len--)
417
                        rxd_buffer[ptrOut++] = x;
418
                else
419
                        break;
420
                if (len--)
421
                        rxd_buffer[ptrOut++] = y;
422
                else
423
                        break;
424
                if (len--)
425
                        rxd_buffer[ptrOut++] = z;
426
                else
427
                        break;
428
        }
429
        pRxData = &rxd_buffer[3];
430
        RxDataLen = ptrOut - 3;
431
}
432
 
433
// --------------------------------------------------------------------------
434
void usart0_ProcessRxData(void) {
435
        // We control the outputTestActive var from here: Count it down.
436
        if (outputTestActive)
437
                outputTestActive--;
438
        // if data in the rxd buffer are not locked immediately return
439
        if (!rxd_buffer_locked)
440
                return;
441
        uint8_t tempchar1, tempchar2;
442
        Decode64(); // decode data block in rxd_buffer
443
 
444
        switch (rxd_buffer[1] - 'a') {
445
 
446
        case FC_ADDRESS:
447
                switch (rxd_buffer[2]) {
448
#ifdef USE_MK3MAG
449
                case 'K':// compass value
450
                compassHeading = ((Heading_t *)pRxData)->Heading;
451
                // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
452
                break;
453
#endif
454
                case 't': // motor test
455
                        if (RxDataLen > 20) {
456
                                memcpy(&outputTest[0], (uint8_t*) pRxData, sizeof(outputTest));
457
                        } else {
458
                                memcpy(&outputTest[0], (uint8_t*) pRxData, 4);
459
                        }
460
                        outputTestActive = 255;
461
                        externalControlActive = 255;
462
                        break;
463
 
464
                case 'n':// "Get Mixer Table
465
                        while (!txd_complete)
466
                                ; // wait for previous frame to be sent
467
                        SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &Mixer, sizeof(Mixer));
468
                        break;
469
 
470
                case 'm':// "Set Mixer Table
471
                        if (pRxData[0] == EEMIXER_REVISION) {
472
                                memcpy(&Mixer, (uint8_t*) pRxData, sizeof(Mixer));
473
                                MixerTable_WriteToEEProm();
474
                                while (!txd_complete)
475
                                        ; // wait for previous frame to be sent
476
                                tempchar1 = 1;
477
                        } else {
478
                                tempchar1 = 0;
479
                        }
480
                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, 1);
481
                        break;
482
 
483
                case 'p': // get PPM channels
484
                        request_PPMChannels = TRUE;
485
                        break;
486
 
487
                case 'q':// request settings
488
                        if (pRxData[0] == 0xFF) {
489
                                pRxData[0] = GetParamByte(PID_ACTIVE_SET);
490
                        }
491
                        // limit settings range
492
                        if (pRxData[0] < 1)
493
                                pRxData[0] = 1; // limit to 1
494
                        else if (pRxData[0] > 5)
495
                                pRxData[0] = 5; // limit to 5
496
                        // load requested parameter set
1927 - 497
                        ParamSet_ReadFromEEProm();
1910 - 498
                        tempchar1 = pRxData[0];
499
                        tempchar2 = EEPARAM_REVISION;
500
                        while (!txd_complete)
501
                                ; // wait for previous frame to be sent
502
                        SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1),
503
                                        &tempchar2, sizeof(tempchar2), (uint8_t *) &staticParams,
504
                                        sizeof(staticParams));
505
                        break;
506
 
507
                case 's': // save settings
508
                        if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off
509
                        {
510
                                if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1]
511
                                                == EEPARAM_REVISION)) // check for setting to be in range and version of settings
512
                                {
513
                                        memcpy(&staticParams, (uint8_t*) &pRxData[2], sizeof(staticParams));
1927 - 514
                                        ParamSet_WriteToEEProm();
515
                                        beepNumber(1);
1910 - 516
                                } else {
517
                                        tempchar1 = 0; //indicate bad data
518
                                }
519
                                while (!txd_complete)
520
                                        ; // wait for previous frame to be sent
521
                                SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
522
                        }
523
                        break;
524
 
525
                default:
526
                        //unsupported command received
527
                        break;
528
                } // case FC_ADDRESS:
529
 
530
        default: // any Slave Address
531
                switch (rxd_buffer[2]) {
532
                case 'a':// request for labels of the analog debug outputs
533
                        request_DebugLabel = pRxData[0];
534
                        if (request_DebugLabel > 31)
535
                                request_DebugLabel = 31;
536
                        externalControlActive = 255;
537
                        break;
538
 
539
                case 'b': // submit extern control
540
                        memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl));
541
                        ConfirmFrame = externalControl.frame;
542
                        externalControlActive = 255;
543
                        break;
544
 
545
                case 'h':// request for display columns
546
                  externalControlActive = 255;
547
                        request_Display = TRUE;
548
                        break;
549
 
550
                case 'l':// request for display columns
551
                  externalControlActive = 255;
552
                        request_Display1 = TRUE;
553
                        break;
554
 
555
                case 'v': // request for version and board release
556
                        request_VerInfo = TRUE;
557
                        break;
558
 
559
                case 'x':
560
                        request_variables = TRUE;
561
                        break;
562
 
563
                case 'g':// get external control data
564
                        request_ExternalControl = TRUE;
565
                        break;
566
 
567
                case 'd': // request for the debug data
568
                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
569
                        if (DebugData_Interval > 0)
570
                                request_DebugData = TRUE;
571
                        break;
572
 
573
                case 'c': // request for the 3D data
574
                        Data3D_Interval = (uint16_t) pRxData[0] * 10;
575
                        if (Data3D_Interval > 0)
576
                                request_Data3D = TRUE;
577
                        break;
578
 
579
                default:
580
                        //unsupported command received
581
                        break;
582
                }
583
                break; // default:
584
        }
585
        // unlock the rxd buffer after processing
586
        pRxData = 0;
587
        RxDataLen = 0;
588
        rxd_buffer_locked = FALSE;
589
}
590
 
591
/************************************************************************/
592
/* Routine für die Serielle Ausgabe                                     */
593
/************************************************************************/
594
int16_t uart_putchar(int8_t c) {
595
        if (c == '\n')
596
                uart_putchar('\r');
597
        // wait until previous character was send
598
        loop_until_bit_is_set(UCSR0A, UDRE0);
599
        // send character
600
        UDR0 = c;
601
        return (0);
602
}
603
 
604
//---------------------------------------------------------------------------------------------
605
void usart0_TransmitTxData(void) {
606
        if (!txd_complete)
607
                return;
608
 
609
        if (request_VerInfo && txd_complete) {
610
                SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo,
611
                                sizeof(UART_VersionInfo));
612
                request_VerInfo = FALSE;
613
        }
614
 
615
        if (request_Display && txd_complete) {
616
          //LCD_PrintMenu();
617
                SendOutData('H', FC_ADDRESS, 0);
618
                DisplayLine++;
619
                if (DisplayLine >= 4)
620
                        DisplayLine = 0;
621
                request_Display = FALSE;
622
        }
623
 
624
        if (request_Display1 && txd_complete) {
625
          //    LCD_PrintMenu();
626
                SendOutData('L', FC_ADDRESS, 0);
627
                request_Display1 = FALSE;
628
        }
629
 
630
        if (request_DebugLabel != 0xFF) { // Texte für die Analogdaten
631
                uint8_t label[16]; // local sram buffer
632
                memcpy_P(label, ANALOG_LABEL[request_DebugLabel], 16); // read lable from flash to sram buffer
633
                SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_DebugLabel,
634
                                sizeof(request_DebugLabel), label, 16);
635
                request_DebugLabel = 0xFF;
636
        }
637
 
638
        if (ConfirmFrame && txd_complete) { // Datensatz ohne CRC bestätigen
639
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*) &ConfirmFrame,
640
                                sizeof(ConfirmFrame));
641
                ConfirmFrame = 0;
642
        }
643
 
644
        if (((DebugData_Interval && checkDelay(DebugData_Timer)) || request_DebugData)
645
                        && txd_complete) {
646
                SendOutData('D', FC_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
647
                DebugData_Timer = setDelay(DebugData_Interval);
648
                request_DebugData = FALSE;
649
        }
650
 
651
        if (((Data3D_Interval && checkDelay(Data3D_Timer)) || request_Data3D)
652
                        && txd_complete) {
653
                SendOutData('C', FC_ADDRESS, 1, (uint8_t *) &Data3D, sizeof(Data3D));
654
                Data3D.AngleNick = (int16_t) ((10 * angle[PITCH])
655
                                / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
656
                Data3D.AngleRoll = (int16_t) ((10 * angle[ROLL])
657
                                / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
1927 - 658
                Data3D.Heading = (int16_t) ((10 * angle[YAW]) / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1°
1910 - 659
                Data3D_Timer = setDelay(Data3D_Interval);
660
                request_Data3D = FALSE;
661
        }
662
 
663
        if (request_ExternalControl && txd_complete) {
664
                SendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl,
665
                                sizeof(externalControl));
666
                request_ExternalControl = FALSE;
667
        }
668
 
669
 
670
#ifdef USE_MK3MAG
671
        if((checkDelay(Compass_Timer)) && txd_complete) {
672
                ToMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
673
                ToMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
674
                ToMk3Mag.UserParam[0] = dynamicParams.UserParams[0];
675
                ToMk3Mag.UserParam[1] = dynamicParams.UserParams[1];
676
                ToMk3Mag.CalState = compassCalState;
677
                SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag));
678
                // the last state is 5 and should be send only once to avoid multiple flash writing
679
                if(compassCalState > 4) compassCalState = 0;
680
                Compass_Timer = setDelay(99);
681
        }
682
#endif
683
 
684
        if (request_OutputTest && txd_complete) {
685
                SendOutData('T', FC_ADDRESS, 0);
686
                request_OutputTest = FALSE;
687
        }
688
 
689
        if (request_PPMChannels && txd_complete) {
690
                SendOutData('P', FC_ADDRESS, 1, (uint8_t *) &PPM_in, sizeof(PPM_in));
691
                request_PPMChannels = FALSE;
692
        }
693
 
694
        if (request_variables && txd_complete) {
695
                SendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables));
696
                request_variables = FALSE;
697
        }
698
}