Subversion Repositories FlightCtrl

Rev

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

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