Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
1623 - 3
// + Nur für den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten und nicht-kommerziellen Gebrauch zulässig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1623 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1623 - 17
// + auf anderen Webseiten oder Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
18
// + eindeutig als Ursprung verlinkt und genannt werden
1612 dongfang 19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1623 - 22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1623 - 25
// + mit unserer Zustimmung zulässig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1623 - 47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1612 dongfang 49
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50
// +  POSSIBILITY OF SUCH DAMAGE.
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1623 - 52
 
1612 dongfang 53
#include <avr/io.h>
54
#include <avr/interrupt.h>
55
#include <avr/wdt.h>
56
#include <avr/pgmspace.h>
57
#include <stdarg.h>
58
#include <string.h>
59
 
60
#include "eeprom.h"
61
#include "menu.h"
62
#include "timer0.h"
63
#include "uart0.h"
64
#include "attitude.h"
65
#include "rc.h"
66
#include "externalControl.h"
1775 - 67
#include "output.h"
1612 dongfang 68
 
69
#ifdef USE_MK3MAG
70
#include "mk3mag.h"
71
#endif
72
 
73
#define FC_ADDRESS 1
74
#define NC_ADDRESS 2
75
#define MK3MAG_ADDRESS 3
76
 
77
#define FALSE   0
78
#define TRUE    1
79
//int8_t test __attribute__ ((section (".noinit")));
1821 - 80
uint8_t request_VerInfo = FALSE;
81
uint8_t request_ExternalControl = FALSE;
82
uint8_t request_Display = FALSE;
83
uint8_t request_Display1 = FALSE;
84
uint8_t request_DebugData = FALSE;
85
uint8_t request_Data3D = FALSE;
86
uint8_t request_DebugLabel = 255;
87
uint8_t request_PPMChannels = FALSE;
88
uint8_t request_MotorTest = FALSE;
89
uint8_t request_variables = FALSE;
1775 - 90
 
1612 dongfang 91
uint8_t DisplayLine = 0;
92
 
93
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
94
volatile uint8_t rxd_buffer_locked = FALSE;
95
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
96
volatile uint8_t txd_complete = TRUE;
97
volatile uint8_t ReceivedBytes = 0;
98
volatile uint8_t *pRxData = 0;
99
volatile uint8_t RxDataLen = 0;
100
 
1821 - 101
uint8_t motorTestActive = 0;
102
uint8_t motorTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1612 dongfang 103
uint8_t ConfirmFrame;
104
 
105
typedef struct {
1821 - 106
        int16_t Heading;
107
}__attribute__((packed)) Heading_t;
1612 dongfang 108
 
1821 - 109
DebugOut_t DebugOut;
110
Data3D_t Data3D;
111
UART_VersionInfo_t UART_VersionInfo;
1612 dongfang 112
 
113
uint16_t DebugData_Timer;
114
uint16_t Data3D_Timer;
115
uint16_t DebugData_Interval = 500; // in 1ms
116
uint16_t Data3D_Interval = 0; // in 1ms
117
 
118
#ifdef USE_MK3MAG
119
int16_t Compass_Timer;
120
#endif
121
 
122
// keep lables in flash to save 512 bytes of sram space
123
const prog_uint8_t ANALOG_LABEL[32][16] = {
1821 - 124
                //1234567890123456
125
                "AnglePitch      ", //0
126
                "AngleRoll       ",
127
                "AngleYaw        ",
128
                "GyroPitch(PID)  ",
129
                "GyroRoll(PID)   ",
130
                "GyroYaw         ", //5
131
                "GyroPitch(AC)   ",
132
                "GyroRoll(AC)    ",
133
                "GyroYaw(AC)     ",
134
                "AccPitch (angle)",
135
                "AccRoll (angle) ", //10
136
                "UBat            ",
137
                "Pitch Term      ",
138
                "Roll Term       ",
139
                "Yaw Term        ",
140
                "Throttle Term   ", //15
141
                "0th O Corr pitch", "0th O Corr roll ",
142
                "DriftCompDelta P",
143
                "DriftCompDelta R",
144
                "ADPitchGyroOffs ", //20
145
                "ADRollGyroOffs  ", "M1              ", "M2              ",
146
                "M3              ",
147
                "M4              ", //25
148
                "ControlYaw      ", "Airpress. Range ", "DriftCompPitch  ",
149
                "DriftCompRoll   ", "AirpressFiltered", //30
150
                "AirpressADC     " };
1612 dongfang 151
 
152
/****************************************************************/
153
/*              Initialization of the USART0                    */
154
/****************************************************************/
1821 - 155
void usart0_Init(void) {
156
        uint8_t sreg = SREG;
157
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * USART0_BAUD) - 1);
158
 
159
        // disable all interrupts before configuration
160
        cli();
161
 
162
        // disable RX-Interrupt
163
        UCSR0B &= ~(1 << RXCIE0);
164
        // disable TX-Interrupt
165
        UCSR0B &= ~(1 << TXCIE0);
166
 
167
        // set direction of RXD0 and TXD0 pins
168
        // set RXD0 (PD0) as an input pin
169
        PORTD |= (1 << PORTD0);
170
        DDRD &= ~(1 << DDD0);
171
        // set TXD0 (PD1) as an output pin
172
        PORTD |= (1 << PORTD1);
173
        DDRD |= (1 << DDD1);
174
 
175
        // USART0 Baud Rate Register
176
        // set clock divider
177
        UBRR0H = (uint8_t) (ubrr >> 8);
178
        UBRR0L = (uint8_t) ubrr;
179
 
180
        // USART0 Control and Status Register A, B, C
181
 
182
        // enable double speed operation in
183
        UCSR0A |= (1 << U2X0);
184
        // enable receiver and transmitter in
185
        UCSR0B = (1 << TXEN0) | (1 << RXEN0);
186
        // set asynchronous mode
187
        UCSR0C &= ~(1 << UMSEL01);
188
        UCSR0C &= ~(1 << UMSEL00);
189
        // no parity
190
        UCSR0C &= ~(1 << UPM01);
191
        UCSR0C &= ~(1 << UPM00);
192
        // 1 stop bit
193
        UCSR0C &= ~(1 << USBS0);
194
        // 8-bit
195
        UCSR0B &= ~(1 << UCSZ02);
196
        UCSR0C |= (1 << UCSZ01);
197
        UCSR0C |= (1 << UCSZ00);
198
 
199
        // flush receive buffer
200
        while (UCSR0A & (1 << RXC0))
201
                UDR0;
202
 
203
        // enable interrupts at the end
204
        // enable RX-Interrupt
205
        UCSR0B |= (1 << RXCIE0);
206
        // enable TX-Interrupt
207
        UCSR0B |= (1 << TXCIE0);
208
 
209
        // initialize the debug timer
210
        DebugData_Timer = SetDelay(DebugData_Interval);
211
 
212
        // unlock rxd_buffer
213
        rxd_buffer_locked = FALSE;
214
        pRxData = 0;
215
        RxDataLen = 0;
216
 
217
        // no bytes to send
218
        txd_complete = TRUE;
219
 
1612 dongfang 220
#ifdef USE_MK3MAG
1821 - 221
        Compass_Timer = SetDelay(220);
1612 dongfang 222
#endif
1821 - 223
 
224
        UART_VersionInfo.SWMajor = VERSION_MAJOR;
225
        UART_VersionInfo.SWMinor = VERSION_MINOR;
226
        UART_VersionInfo.SWPatch = VERSION_PATCH;
227
        UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
228
        UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
229
 
230
        // restore global interrupt flags
231
        SREG = sreg;
1612 dongfang 232
}
233
 
234
/****************************************************************/
235
/* USART0 transmitter ISR                                       */
236
/****************************************************************/
1821 - 237
ISR(USART0_TX_vect)
238
{
239
        static uint16_t ptr_txd_buffer = 0;
240
        uint8_t tmp_tx;
241
        if (!txd_complete) { // transmission not completed
242
                ptr_txd_buffer++; // die [0] wurde schon gesendet
243
                tmp_tx = txd_buffer[ptr_txd_buffer];
244
                // if terminating character or end of txd buffer was reached
245
                if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) {
246
                        ptr_txd_buffer = 0; // reset txd pointer
247
                        txd_complete = 1; // stop transmission
248
                }
249
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
250
        }
251
        // transmission completed
252
        else
253
                ptr_txd_buffer = 0;
1612 dongfang 254
}
255
 
256
/****************************************************************/
257
/* USART0 receiver               ISR                            */
258
/****************************************************************/
1821 - 259
ISR(USART0_RX_vect)
260
{
261
        static uint16_t crc;
262
        static uint8_t ptr_rxd_buffer = 0;
263
        uint8_t crc1, crc2;
264
        uint8_t c;
1612 dongfang 265
 
1821 - 266
        c = UDR0; // catch the received byte
1612 dongfang 267
 
1821 - 268
        if (rxd_buffer_locked)
269
                return; // if rxd buffer is locked immediately return
1612 dongfang 270
 
1821 - 271
        // the rxd buffer is unlocked
272
        if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received
273
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
274
                crc = c; // init crc
275
        }
1612 dongfang 276
#if 0
1821 - 277
        else if (ptr_rxd_buffer == 1) { // handle address
278
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
279
                crc += c; // update crc
280
        }
1612 dongfang 281
#endif
1821 - 282
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
283
                if (c != '\r') { // no termination character
284
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
285
                        crc += c; // update crc
286
                } else { // termination character was received
287
                        // the last 2 bytes are no subject for checksum calculation
288
                        // they are the checksum itself
289
                        crc -= rxd_buffer[ptr_rxd_buffer - 2];
290
                        crc -= rxd_buffer[ptr_rxd_buffer - 1];
291
                        // calculate checksum from transmitted data
292
                        crc %= 4096;
293
                        crc1 = '=' + crc / 64;
294
                        crc2 = '=' + crc % 64;
295
                        // compare checksum to transmitted checksum bytes
296
                        if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2
297
                                        == rxd_buffer[ptr_rxd_buffer - 1])) {
298
                                // checksum valid
299
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
300
                                ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
301
                                rxd_buffer_locked = TRUE; // lock the rxd buffer
302
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
303
                                if (rxd_buffer[2] == 'R') {
304
                                        wdt_enable(WDTO_250MS);
305
                                } // Reset-Commando
306
                        } else { // checksum invalid
307
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
308
                        }
309
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
310
                }
311
        } else { // rxd buffer overrun
312
                ptr_rxd_buffer = 0; // reset rxd buffer
313
                rxd_buffer_locked = FALSE; // unlock rxd buffer
314
        }
1612 dongfang 315
}
316
 
317
// --------------------------------------------------------------------------
318
void AddCRC(uint16_t datalen) {
1821 - 319
        uint16_t tmpCRC = 0, i;
320
        for (i = 0; i < datalen; i++) {
321
                tmpCRC += txd_buffer[i];
322
        }
323
        tmpCRC %= 4096;
324
        txd_buffer[i++] = '=' + tmpCRC / 64;
325
        txd_buffer[i++] = '=' + tmpCRC % 64;
326
        txd_buffer[i++] = '\r';
327
        txd_complete = FALSE;
328
        UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR)
1612 dongfang 329
}
330
 
331
// --------------------------------------------------------------------------
1775 - 332
// application example:
333
// SendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16);
334
/*
1821 - 335
 void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
336
 va_list ap;
337
 uint16_t txd_bufferIndex = 0;
338
 uint8_t *currentBuffer;
339
 uint8_t currentBufferIndex;
340
 uint16_t lengthOfCurrentBuffer;
341
 uint8_t shift = 0;
1775 - 342
 
1821 - 343
 txd_buffer[txd_bufferIndex++] = '#';                   // Start character
344
 txd_buffer[txd_bufferIndex++] = 'a' + addr;            // Address (a=0; b=1,...)
345
 txd_buffer[txd_bufferIndex++] = cmd;                   // Command
1775 - 346
 
1821 - 347
 va_start(ap, numofbuffers);
348
 
349
 while(numofbuffers) {
350
 currentBuffer = va_arg(ap, uint8_t*);
351
 lengthOfCurrentBuffer = va_arg(ap, int);
352
 currentBufferIndex = 0;
353
 // Encode data: 3 bytes of data are encoded into 4 bytes,
354
 // where the 2 most significant bits are both 0.
355
 while(currentBufferIndex != lengthOfCurrentBuffer) {
356
 if (!shift) txd_buffer[txd_bufferIndex] = 0;
357
 txd_buffer[txd_bufferIndex]  |= currentBuffer[currentBufferIndex] >> (shift + 2);
358
 txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111;
359
 shift += 2;
360
 if (shift == 6) { shift=0; txd_bufferIndex++; }
361
 currentBufferIndex++;
362
 }
363
 }
364
 // If the number of data bytes was not divisible by 3, stuff
365
 //  with 0 pseudodata  until length is again divisible by 3.
366
 if (shift == 2) {
367
 // We need to stuff with zero bytes at the end.
368
 txd_buffer[txd_bufferIndex]  &= 0b00110000;
369
 txd_buffer[++txd_bufferIndex] = 0;
370
 shift = 4;
371
 }
372
 if (shift == 4) {
373
 // We need to stuff with zero bytes at the end.
374
 txd_buffer[txd_bufferIndex++] &= 0b00111100;
375
 txd_buffer[txd_bufferIndex]    = 0;
376
 }
377
 va_end(ap);
378
 AddCRC(pt); // add checksum after data block and initates the transmission
379
 }
380
 */
381
 
1775 - 382
void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ...
1821 - 383
        va_list ap;
384
        uint16_t pt = 0;
385
        uint8_t a, b, c;
386
        uint8_t ptr = 0;
1612 dongfang 387
 
1821 - 388
        uint8_t *pdata = 0;
389
        int len = 0;
1612 dongfang 390
 
1821 - 391
        txd_buffer[pt++] = '#'; // Start character
392
        txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...)
393
        txd_buffer[pt++] = cmd; // Command
394
 
395
        va_start(ap, numofbuffers);
396
 
397
        if (numofbuffers) {
398
                pdata = va_arg(ap, uint8_t*);
399
                len = va_arg(ap, int);
400
                ptr = 0;
401
                numofbuffers--;
402
        }
403
 
404
        while (len) {
405
                if (len) {
406
                        a = pdata[ptr++];
407
                        len--;
408
                        if ((!len) && numofbuffers) {
409
                                pdata = va_arg(ap, uint8_t*);
410
                                len = va_arg(ap, int);
411
                                ptr = 0;
412
                                numofbuffers--;
413
                        }
414
                } else
415
                        a = 0;
416
                if (len) {
417
                        b = pdata[ptr++];
418
                        len--;
419
                        if ((!len) && numofbuffers) {
420
                                pdata = va_arg(ap, uint8_t*);
421
                                len = va_arg(ap, int);
422
                                ptr = 0;
423
                                numofbuffers--;
424
                        }
425
                } else
426
                        b = 0;
427
                if (len) {
428
                        c = pdata[ptr++];
429
                        len--;
430
                        if ((!len) && numofbuffers) {
431
                                pdata = va_arg(ap, uint8_t*);
432
                                len = va_arg(ap, int);
433
                                ptr = 0;
434
                                numofbuffers--;
435
                        }
436
                } else
437
                        c = 0;
438
                txd_buffer[pt++] = '=' + (a >> 2);
439
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
440
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
441
                txd_buffer[pt++] = '=' + (c & 0x3f);
442
        }
443
        va_end(ap);
444
        AddCRC(pt); // add checksum after data block and initates the transmission
1612 dongfang 445
}
446
 
447
// --------------------------------------------------------------------------
448
void Decode64(void) {
1821 - 449
        uint8_t a, b, c, d;
450
        uint8_t x, y, z;
451
        uint8_t ptrIn = 3;
452
        uint8_t ptrOut = 3;
453
        uint8_t len = ReceivedBytes - 6;
454
 
455
        while (len) {
456
                a = rxd_buffer[ptrIn++] - '=';
457
                b = rxd_buffer[ptrIn++] - '=';
458
                c = rxd_buffer[ptrIn++] - '=';
459
                d = rxd_buffer[ptrIn++] - '=';
460
                //if(ptrIn > ReceivedBytes - 3) break;
461
 
462
                x = (a << 2) | (b >> 4);
463
                y = ((b & 0x0f) << 4) | (c >> 2);
464
                z = ((c & 0x03) << 6) | d;
465
 
466
                if (len--)
467
                        rxd_buffer[ptrOut++] = x;
468
                else
469
                        break;
470
                if (len--)
471
                        rxd_buffer[ptrOut++] = y;
472
                else
473
                        break;
474
                if (len--)
475
                        rxd_buffer[ptrOut++] = z;
476
                else
477
                        break;
478
        }
479
        pRxData = &rxd_buffer[3];
480
        RxDataLen = ptrOut - 3;
1612 dongfang 481
}
482
 
483
// --------------------------------------------------------------------------
1645 - 484
void usart0_ProcessRxData(void) {
1821 - 485
        // We control the motorTestActive var from here: Count it down.
486
        if (motorTestActive)
487
                motorTestActive--;
488
        // if data in the rxd buffer are not locked immediately return
489
        if (!rxd_buffer_locked)
490
                return;
491
        uint8_t tempchar1, tempchar2;
492
        Decode64(); // decode data block in rxd_buffer
1775 - 493
 
1821 - 494
        switch (rxd_buffer[1] - 'a') {
1775 - 495
 
1821 - 496
        case FC_ADDRESS:
497
                switch (rxd_buffer[2]) {
1612 dongfang 498
#ifdef USE_MK3MAG
1821 - 499
                case 'K':// compass value
500
                compassHeading = ((Heading_t *)pRxData)->Heading;
501
                // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180;
502
                break;
1612 dongfang 503
#endif
1821 - 504
                case 't': // motor test
505
                        if (RxDataLen > 20) {
506
                                memcpy(&motorTest[0], (uint8_t*) pRxData, sizeof(motorTest));
507
                        } else {
508
                                memcpy(&motorTest[0], (uint8_t*) pRxData, 4);
509
                        }
510
                        motorTestActive = 255;
511
                        externalControlActive = 255;
512
                        break;
1612 dongfang 513
 
1821 - 514
                case 'n':// "Get Mixer Table
515
                        while (!txd_complete)
516
                                ; // wait for previous frame to be sent
517
                        SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &Mixer, sizeof(Mixer));
518
                        break;
1612 dongfang 519
 
1821 - 520
                case 'm':// "Set Mixer Table
521
                        if (pRxData[0] == EEMIXER_REVISION) {
522
                                memcpy(&Mixer, (uint8_t*) pRxData, sizeof(Mixer));
523
                                MixerTable_WriteToEEProm();
524
                                while (!txd_complete)
525
                                        ; // wait for previous frame to be sent
526
                                tempchar1 = 1;
527
                        } else {
528
                                tempchar1 = 0;
529
                        }
530
                        SendOutData('M', FC_ADDRESS, 1, &tempchar1, 1);
531
                        break;
1612 dongfang 532
 
1821 - 533
                case 'p': // get PPM channels
534
                        request_PPMChannels = TRUE;
535
                        break;
1612 dongfang 536
 
1821 - 537
                case 'q':// request settings
538
                        if (pRxData[0] == 0xFF) {
539
                                pRxData[0] = GetParamByte(PID_ACTIVE_SET);
540
                        }
541
                        // limit settings range
542
                        if (pRxData[0] < 1)
543
                                pRxData[0] = 1; // limit to 1
544
                        else if (pRxData[0] > 5)
545
                                pRxData[0] = 5; // limit to 5
546
                        // load requested parameter set
547
                        ParamSet_ReadFromEEProm(pRxData[0]);
548
                        tempchar1 = pRxData[0];
549
                        tempchar2 = EEPARAM_REVISION;
550
                        while (!txd_complete)
551
                                ; // wait for previous frame to be sent
552
                        SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1),
553
                                        &tempchar2, sizeof(tempchar2), (uint8_t *) &staticParams,
554
                                        sizeof(staticParams));
555
                        break;
1612 dongfang 556
 
1821 - 557
                case 's': // save settings
558
                        if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off
559
                        {
560
                                if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1]
561
                                                == EEPARAM_REVISION)) // check for setting to be in range and version of settings
562
                                {
563
                                        memcpy(&staticParams, (uint8_t*) &pRxData[2], sizeof(staticParams));
564
                                        ParamSet_WriteToEEProm(pRxData[0]);
565
                                        /*
566
                                         TODO: Remove this encapsulation breach
567
                                         turnOver180Pitch = (int32_t) staticParams.AngleTurnOverPitch * 2500L;
568
                                         turnOver180Roll = (int32_t) staticParams.AngleTurnOverRoll * 2500L;
569
                                         */
570
                                        tempchar1 = getActiveParamSet();
571
                                        beepNumber(tempchar1);
572
                                } else {
573
                                        tempchar1 = 0; //indicate bad data
574
                                }
575
                                while (!txd_complete)
576
                                        ; // wait for previous frame to be sent
577
                                SendOutData('S', FC_ADDRESS, 1, &tempchar1, sizeof(tempchar1));
578
                        }
579
                        break;
1612 dongfang 580
 
1821 - 581
                default:
582
                        //unsupported command received
583
                        break;
584
                } // case FC_ADDRESS:
1612 dongfang 585
 
1821 - 586
        default: // any Slave Address
587
                switch (rxd_buffer[2]) {
588
                case 'a':// request for labels of the analog debug outputs
589
                        request_DebugLabel = pRxData[0];
590
                        if (request_DebugLabel > 31)
591
                                request_DebugLabel = 31;
592
                        externalControlActive = 255;
593
                        break;
1612 dongfang 594
 
1821 - 595
                case 'b': // submit extern control
596
                        memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl));
597
                        ConfirmFrame = externalControl.frame;
598
                        externalControlActive = 255;
599
                        break;
1612 dongfang 600
 
1821 - 601
                case 'h':// request for display columns
602
                        externalControlActive = 255;
603
                        RemoteKeys |= pRxData[0];
604
                        if (RemoteKeys)
605
                                DisplayLine = 0;
606
                        request_Display = TRUE;
607
                        break;
1612 dongfang 608
 
1821 - 609
                case 'l':// request for display columns
610
                        externalControlActive = 255;
611
                        MenuItem = pRxData[0];
612
                        request_Display1 = TRUE;
613
                        break;
1612 dongfang 614
 
1821 - 615
                case 'v': // request for version and board release
616
                        request_VerInfo = TRUE;
617
                        break;
1775 - 618
 
1821 - 619
                case 'x':
620
                        request_variables = TRUE;
621
                        break;
1612 dongfang 622
 
1821 - 623
                case 'g':// get external control data
624
                        request_ExternalControl = TRUE;
625
                        break;
1612 dongfang 626
 
1821 - 627
                case 'd': // request for the debug data
628
                        DebugData_Interval = (uint16_t) pRxData[0] * 10;
629
                        if (DebugData_Interval > 0)
630
                                request_DebugData = TRUE;
631
                        break;
1612 dongfang 632
 
1821 - 633
                case 'c': // request for the 3D data
634
                        Data3D_Interval = (uint16_t) pRxData[0] * 10;
635
                        if (Data3D_Interval > 0)
636
                                request_Data3D = TRUE;
637
                        break;
638
 
639
                default:
640
                        //unsupported command received
641
                        break;
642
                }
643
                break; // default:
644
        }
645
        // unlock the rxd buffer after processing
646
        pRxData = 0;
647
        RxDataLen = 0;
648
        rxd_buffer_locked = FALSE;
1612 dongfang 649
}
650
 
1645 - 651
/************************************************************************/
652
/* Routine für die Serielle Ausgabe                                     */
653
/************************************************************************/
1821 - 654
int16_t uart_putchar(int8_t c) {
655
        if (c == '\n')
656
                uart_putchar('\r');
657
        // wait until previous character was send
658
        loop_until_bit_is_set(UCSR0A, UDRE0);
659
        // send character
660
        UDR0 = c;
661
        return (0);
1612 dongfang 662
}
663
 
664
//---------------------------------------------------------------------------------------------
1645 - 665
void usart0_TransmitTxData(void) {
1821 - 666
        if (!txd_complete)
667
                return;
1612 dongfang 668
 
1821 - 669
        if (request_VerInfo && txd_complete) {
670
                SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo,
671
                                sizeof(UART_VersionInfo));
672
                request_VerInfo = FALSE;
673
        }
1612 dongfang 674
 
1821 - 675
        if (request_Display && txd_complete) {
676
                LCD_PrintMenu();
677
                SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine),
678
                                &DisplayBuff[DisplayLine * 20], 20);
679
                DisplayLine++;
680
                if (DisplayLine >= 4)
681
                        DisplayLine = 0;
682
                request_Display = FALSE;
683
        }
1612 dongfang 684
 
1821 - 685
        if (request_Display1 && txd_complete) {
686
                LCD_PrintMenu();
687
                SendOutData('L', FC_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem,
688
                                sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff));
689
                request_Display1 = FALSE;
690
        }
691
 
692
        if (request_DebugLabel != 0xFF) { // Texte für die Analogdaten
693
                uint8_t label[16]; // local sram buffer
694
                memcpy_P(label, ANALOG_LABEL[request_DebugLabel], 16); // read lable from flash to sram buffer
695
                SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_DebugLabel,
696
                                sizeof(request_DebugLabel), label, 16);
697
                request_DebugLabel = 0xFF;
698
        }
699
 
700
        if (ConfirmFrame && txd_complete) { // Datensatz ohne CRC bestätigen
701
                SendOutData('B', FC_ADDRESS, 1, (uint8_t*) &ConfirmFrame,
702
                                sizeof(ConfirmFrame));
703
                ConfirmFrame = 0;
704
        }
705
 
706
        if (((DebugData_Interval && CheckDelay(DebugData_Timer)) || request_DebugData)
707
                        && txd_complete) {
708
                SendOutData('D', FC_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
709
                DebugData_Timer = SetDelay(DebugData_Interval);
710
                request_DebugData = FALSE;
711
        }
712
 
713
        if (((Data3D_Interval && CheckDelay(Data3D_Timer)) || request_Data3D)
714
                        && txd_complete) {
715
                SendOutData('C', FC_ADDRESS, 1, (uint8_t *) &Data3D, sizeof(Data3D));
716
                Data3D.AngleNick = (int16_t) ((10 * angle[PITCH])
717
                                / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
718
                Data3D.AngleRoll = (int16_t) ((10 * angle[ROLL])
719
                                / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1°
720
                Data3D.Heading = (int16_t) ((10 * yawGyroHeading) / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1°
721
                Data3D_Timer = SetDelay(Data3D_Interval);
722
                request_Data3D = FALSE;
723
        }
724
 
725
        if (request_ExternalControl && txd_complete) {
726
                SendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl,
727
                                sizeof(externalControl));
728
                request_ExternalControl = FALSE;
729
        }
730
 
1612 dongfang 731
#ifdef USE_MK3MAG
1821 - 732
        if((CheckDelay(Compass_Timer)) && txd_complete) {
733
                ToMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
734
                ToMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg
735
                ToMk3Mag.UserParam[0] = dynamicParams.UserParams[0];
736
                ToMk3Mag.UserParam[1] = dynamicParams.UserParams[1];
737
                ToMk3Mag.CalState = compassCalState;
738
                SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag));
739
                // the last state is 5 and should be send only once to avoid multiple flash writing
740
                if(compassCalState > 4) compassCalState = 0;
741
                Compass_Timer = SetDelay(99);
742
        }
1612 dongfang 743
#endif
744
 
1821 - 745
        if (request_MotorTest && txd_complete) {
746
                SendOutData('T', FC_ADDRESS, 0);
747
                request_MotorTest = FALSE;
748
        }
1775 - 749
 
1821 - 750
        if (request_PPMChannels && txd_complete) {
751
                SendOutData('P', FC_ADDRESS, 1, (uint8_t *) &PPM_in, sizeof(PPM_in));
752
                request_PPMChannels = FALSE;
753
        }
754
 
755
        if (request_variables && txd_complete) {
756
                SendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables));
757
                request_variables = FALSE;
758
        }
1612 dongfang 759
}