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