Subversion Repositories FlightCtrl

Rev

Rev 2109 | Rev 2133 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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