Subversion Repositories FlightCtrl

Rev

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