Subversion Repositories FlightCtrl

Rev

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

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