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