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