Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
886 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | ingob | 2 | // + Copyright (c) 04.2007 Holger Buss |
3 | // + only for non-profit use |
||
4 | // + www.MikroKopter.com |
||
5 | // + see the File "License.txt" for further Informations |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | |||
886 | killagreg | 8 | #include <avr/io.h> |
9 | #include <avr/interrupt.h> |
||
10 | #include <avr/wdt.h> |
||
1078 | killagreg | 11 | #include <stdarg.h> |
12 | #include <string.h> |
||
886 | killagreg | 13 | |
14 | #include "eeprom.h" |
||
1 | ingob | 15 | #include "main.h" |
886 | killagreg | 16 | #include "menu.h" |
17 | #include "timer0.h" |
||
1 | ingob | 18 | #include "uart.h" |
886 | killagreg | 19 | #include "fc.h" |
20 | #include "_Settings.h" |
||
21 | #include "rc.h" |
||
908 | killagreg | 22 | #if defined (USE_KILLAGREG) || defined (USE_MK3MAG) |
886 | killagreg | 23 | #include "ubx.h" |
24 | #endif |
||
908 | killagreg | 25 | #ifdef USE_MK3MAG |
886 | killagreg | 26 | #include "mk3mag.h" |
27 | #endif |
||
1 | ingob | 28 | |
29 | |||
1078 | killagreg | 30 | #define FC_ADDRESS 1 |
31 | #define NC_ADDRESS 2 |
||
32 | #define MK3MAG_ADDRESS 3 |
||
693 | hbuss | 33 | |
886 | killagreg | 34 | #define FALSE 0 |
35 | #define TRUE 1 |
||
36 | |||
37 | //int8_t test __attribute__ ((section (".noinit"))); |
||
1078 | killagreg | 38 | uint8_t Request_VerInfo = FALSE; |
39 | uint8_t Request_ExternalControl = FALSE; |
||
40 | uint8_t Request_Display = FALSE; |
||
41 | uint8_t Request_Display1 = FALSE; |
||
42 | uint8_t Request_DebugData = FALSE; |
||
43 | uint8_t Request_DebugLabel = 255; |
||
44 | uint8_t Request_PPMChannels = FALSE; |
||
45 | uint8_t Request_MotorTest = FALSE; |
||
46 | uint8_t DisplayLine = 0; |
||
886 | killagreg | 47 | |
48 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
||
49 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
50 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
51 | volatile uint8_t txd_complete = TRUE; |
||
52 | volatile uint8_t ReceivedBytes = 0; |
||
1078 | killagreg | 53 | volatile uint8_t *pRxData = 0; |
54 | volatile uint8_t RxDataLen = 0; |
||
886 | killagreg | 55 | |
56 | uint8_t PcAccess = 100; |
||
57 | uint8_t MotorTest[4] = {0,0,0,0}; |
||
58 | uint8_t ConfirmFrame; |
||
59 | |||
1078 | killagreg | 60 | typedef struct |
61 | { |
||
62 | int16_t Heading; |
||
1081 | killagreg | 63 | } __attribute__((packed)) Heading_t; |
1078 | killagreg | 64 | |
886 | killagreg | 65 | DebugOut_t DebugOut; |
66 | ExternControl_t ExternControl; |
||
1078 | killagreg | 67 | UART_VersionInfo_t UART_VersionInfo; |
886 | killagreg | 68 | |
1078 | killagreg | 69 | uint16_t DebugData_Timer; |
70 | uint16_t DebugData_Interval = 500; // in 1ms |
||
886 | killagreg | 71 | |
908 | killagreg | 72 | #ifdef USE_MK3MAG |
886 | killagreg | 73 | int16_t Compass_Timer; |
74 | #endif |
||
75 | |||
76 | |||
77 | const uint8_t ANALOG_LABEL[32][16] = |
||
499 | hbuss | 78 | { |
886 | killagreg | 79 | //1234567890123456 |
911 | killagreg | 80 | "IntegralNick ", //0 |
499 | hbuss | 81 | "IntegralRoll ", |
911 | killagreg | 82 | "AccNick ", |
499 | hbuss | 83 | "AccRoll ", |
886 | killagreg | 84 | "GyroYaw ", |
85 | "ReadingHeight ", //5 |
||
499 | hbuss | 86 | "AccZ ", |
911 | killagreg | 87 | "Gas ", |
886 | killagreg | 88 | "CompassHeading ", |
89 | "Voltage ", |
||
90 | "Receiver Level ", //10 |
||
91 | "YawGyroHeading ", |
||
92 | "Motor_Front ", |
||
93 | "Motor_Rear ", |
||
1078 | killagreg | 94 | "Motor_Left ", |
95 | "Motor_Right ", //15 |
||
936 | killagreg | 96 | " ", |
886 | killagreg | 97 | " ", |
1104 | killagreg | 98 | " ", |
99 | " ", |
||
854 | hbuss | 100 | "Servo ", //20 |
911 | killagreg | 101 | "Nick ", |
854 | hbuss | 102 | "Roll ", |
720 | ingob | 103 | " ", |
104 | " ", |
||
105 | " ", //25 |
||
106 | " ", |
||
1078 | killagreg | 107 | "Kalman_MaxDrift ", |
720 | ingob | 108 | " ", |
1078 | killagreg | 109 | "Kalman K ", |
911 | killagreg | 110 | "GPS_Nick ", //30 |
720 | ingob | 111 | "GPS_Roll " |
499 | hbuss | 112 | }; |
113 | |||
114 | |||
115 | |||
886 | killagreg | 116 | /****************************************************************/ |
117 | /* Initialization of the USART0 */ |
||
118 | /****************************************************************/ |
||
119 | void USART0_Init (void) |
||
1 | ingob | 120 | { |
886 | killagreg | 121 | uint8_t sreg = SREG; |
122 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
||
123 | |||
124 | // disable all interrupts before configuration |
||
125 | cli(); |
||
126 | |||
127 | // disable RX-Interrupt |
||
128 | UCSR0B &= ~(1 << RXCIE0); |
||
129 | // disable TX-Interrupt |
||
130 | UCSR0B &= ~(1 << TXCIE0); |
||
131 | |||
132 | // set direction of RXD0 and TXD0 pins |
||
133 | // set RXD0 (PD0) as an input pin |
||
134 | PORTD |= (1 << PORTD0); |
||
135 | DDRD &= ~(1 << DDD0); |
||
136 | // set TXD0 (PD1) as an output pin |
||
137 | PORTD |= (1 << PORTD1); |
||
138 | DDRD |= (1 << DDD1); |
||
139 | |||
140 | // USART0 Baud Rate Register |
||
141 | // set clock divider |
||
142 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
143 | UBRR0L = (uint8_t)ubrr; |
||
144 | |||
145 | // USART0 Control and Status Register A, B, C |
||
146 | |||
147 | // enable double speed operation in |
||
148 | UCSR0A |= (1 << U2X0); |
||
149 | // enable receiver and transmitter in |
||
150 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
||
151 | // set asynchronous mode |
||
152 | UCSR0C &= ~(1 << UMSEL01); |
||
153 | UCSR0C &= ~(1 << UMSEL00); |
||
154 | // no parity |
||
155 | UCSR0C &= ~(1 << UPM01); |
||
156 | UCSR0C &= ~(1 << UPM00); |
||
157 | // 1 stop bit |
||
158 | UCSR0C &= ~(1 << USBS0); |
||
159 | // 8-bit |
||
160 | UCSR0B &= ~(1 << UCSZ02); |
||
161 | UCSR0C |= (1 << UCSZ01); |
||
162 | UCSR0C |= (1 << UCSZ00); |
||
163 | |||
164 | // flush receive buffer |
||
165 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
||
166 | |||
167 | // enable interrupts at the end |
||
168 | // enable RX-Interrupt |
||
169 | UCSR0B |= (1 << RXCIE0); |
||
170 | // enable TX-Interrupt |
||
171 | UCSR0B |= (1 << TXCIE0); |
||
172 | |||
1078 | killagreg | 173 | // initialize the debug timer |
174 | DebugData_Timer = SetDelay(DebugData_Interval); |
||
175 | |||
176 | // unlock rxd_buffer |
||
886 | killagreg | 177 | rxd_buffer_locked = FALSE; |
1078 | killagreg | 178 | pRxData = 0; |
179 | RxDataLen = 0; |
||
180 | |||
181 | // no bytes to send |
||
886 | killagreg | 182 | txd_complete = TRUE; |
183 | |||
908 | killagreg | 184 | #ifdef USE_MK3MAG |
886 | killagreg | 185 | Compass_Timer = SetDelay(220); |
186 | #endif |
||
187 | |||
1078 | killagreg | 188 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
189 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
||
190 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
||
191 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
192 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
193 | |||
886 | killagreg | 194 | // restore global interrupt flags |
195 | SREG = sreg; |
||
1 | ingob | 196 | } |
197 | |||
886 | killagreg | 198 | /****************************************************************/ |
199 | /* USART0 transmitter ISR */ |
||
200 | /****************************************************************/ |
||
201 | ISR(USART0_TX_vect) |
||
1 | ingob | 202 | { |
886 | killagreg | 203 | static uint16_t ptr_txd_buffer = 0; |
204 | uint8_t tmp_tx; |
||
205 | if(!txd_complete) // transmission not completed |
||
206 | { |
||
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 | { |
||
212 | ptr_txd_buffer = 0; // reset txd pointer |
||
213 | txd_complete = 1; // stop transmission |
||
214 | } |
||
215 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
||
216 | } |
||
217 | // transmission completed |
||
218 | else ptr_txd_buffer = 0; |
||
219 | } |
||
1 | ingob | 220 | |
886 | killagreg | 221 | /****************************************************************/ |
222 | /* USART0 receiver ISR */ |
||
223 | /****************************************************************/ |
||
224 | ISR(USART0_RX_vect) |
||
225 | { |
||
226 | static uint16_t crc; |
||
227 | static uint8_t ptr_rxd_buffer = 0; |
||
228 | uint8_t crc1, crc2; |
||
229 | uint8_t c; |
||
230 | |||
231 | c = UDR0; // catch the received byte |
||
232 | |||
953 | killagreg | 233 | #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG)) |
886 | killagreg | 234 | // If the FC 1.0 cpu is used the ublox module should be conneced to rxd of the 1st uart. |
235 | // The FC 1.1 /1.2 has the ATMEGA644p cpu with a 2nd uart to which the ublox should be connected. |
||
236 | #if defined (__AVR_ATmega644P__) |
||
237 | if(BoardRelease == 10) ubx_parser(c); |
||
238 | #else |
||
239 | ubx_parser(c); |
||
240 | #endif |
||
908 | killagreg | 241 | #endif |
886 | killagreg | 242 | |
243 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
||
244 | |||
245 | // the rxd buffer is unlocked |
||
246 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
||
247 | { |
||
248 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
249 | crc = c; // init crc |
||
250 | } |
||
251 | #if 0 |
||
252 | else if (ptr_rxd_buffer == 1) // handle address |
||
253 | { |
||
254 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
255 | crc += c; // update crc |
||
256 | } |
||
257 | #endif |
||
258 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
||
259 | { |
||
260 | if(c != '\r') // no termination character |
||
261 | { |
||
262 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
263 | crc += c; // update crc |
||
264 | } |
||
265 | else // termination character was received |
||
266 | { |
||
267 | // the last 2 bytes are no subject for checksum calculation |
||
268 | // they are the checksum itself |
||
269 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
||
270 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
||
271 | // calculate checksum from transmitted data |
||
272 | crc %= 4096; |
||
273 | crc1 = '=' + crc / 64; |
||
274 | crc2 = '=' + crc % 64; |
||
275 | // compare checksum to transmitted checksum bytes |
||
276 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
||
277 | { // checksum valid |
||
1078 | killagreg | 278 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
279 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
||
886 | killagreg | 280 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
281 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
||
282 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
||
283 | } |
||
284 | else |
||
285 | { // checksum invalid |
||
286 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
287 | } |
||
288 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
289 | } |
||
290 | } |
||
291 | else // rxd buffer overrun |
||
292 | { |
||
293 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
294 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
295 | } |
||
296 | |||
1 | ingob | 297 | } |
298 | |||
299 | |||
300 | // -------------------------------------------------------------------------- |
||
886 | killagreg | 301 | void AddCRC(uint16_t datalen) |
1 | ingob | 302 | { |
886 | killagreg | 303 | uint16_t tmpCRC = 0, i; |
304 | for(i = 0; i < datalen; i++) |
||
305 | { |
||
306 | tmpCRC += txd_buffer[i]; |
||
307 | } |
||
308 | tmpCRC %= 4096; |
||
309 | txd_buffer[i++] = '=' + tmpCRC / 64; |
||
310 | txd_buffer[i++] = '=' + tmpCRC % 64; |
||
311 | txd_buffer[i++] = '\r'; |
||
312 | txd_complete = FALSE; |
||
313 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
||
1 | ingob | 314 | } |
315 | |||
316 | |||
317 | |||
318 | // -------------------------------------------------------------------------- |
||
1078 | killagreg | 319 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
1 | ingob | 320 | { |
1078 | killagreg | 321 | va_list ap; |
886 | killagreg | 322 | uint16_t pt = 0; |
323 | uint8_t a,b,c; |
||
324 | uint8_t ptr = 0; |
||
1 | ingob | 325 | |
1078 | killagreg | 326 | uint8_t *pdata = 0; |
327 | int len = 0; |
||
1 | ingob | 328 | |
1078 | killagreg | 329 | txd_buffer[pt++] = '#'; // Start character |
330 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
||
331 | txd_buffer[pt++] = cmd; // Command |
||
332 | |||
333 | va_start(ap, numofbuffers); |
||
334 | if(numofbuffers) |
||
335 | { |
||
336 | pdata = va_arg(ap, uint8_t*); |
||
337 | len = va_arg(ap, int); |
||
338 | ptr = 0; |
||
339 | numofbuffers--; |
||
340 | } |
||
341 | |||
886 | killagreg | 342 | while(len) |
343 | { |
||
1078 | killagreg | 344 | if(len) |
345 | { |
||
346 | a = pdata[ptr++]; |
||
347 | len--; |
||
348 | if((!len) && numofbuffers) |
||
349 | { |
||
350 | pdata = va_arg(ap, uint8_t*); |
||
351 | len = va_arg(ap, int); |
||
352 | ptr = 0; |
||
353 | numofbuffers--; |
||
354 | } |
||
355 | } |
||
356 | else a = 0; |
||
357 | if(len) |
||
358 | { |
||
359 | b = pdata[ptr++]; |
||
360 | len--; |
||
361 | if((!len) && numofbuffers) |
||
362 | { |
||
363 | pdata = va_arg(ap, uint8_t*); |
||
364 | len = va_arg(ap, int); |
||
365 | ptr = 0; |
||
366 | numofbuffers--; |
||
367 | } |
||
368 | } |
||
369 | else b = 0; |
||
370 | if(len) |
||
371 | { |
||
372 | c = pdata[ptr++]; |
||
373 | len--; |
||
374 | if((!len) && numofbuffers) |
||
375 | { |
||
376 | pdata = va_arg(ap, uint8_t*); |
||
377 | len = va_arg(ap, int); |
||
378 | ptr = 0; |
||
379 | numofbuffers--; |
||
380 | } |
||
381 | } |
||
382 | else c = 0; |
||
886 | killagreg | 383 | txd_buffer[pt++] = '=' + (a >> 2); |
384 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
385 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
386 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
||
387 | } |
||
1078 | killagreg | 388 | va_end(ap); |
886 | killagreg | 389 | AddCRC(pt); // add checksum after data block and initates the transmission |
1 | ingob | 390 | } |
391 | |||
392 | |||
393 | // -------------------------------------------------------------------------- |
||
1078 | killagreg | 394 | void Decode64(void) |
1 | ingob | 395 | { |
886 | killagreg | 396 | uint8_t a,b,c,d; |
397 | uint8_t x,y,z; |
||
1078 | killagreg | 398 | uint8_t ptrIn = 3; |
399 | uint8_t ptrOut = 3; |
||
400 | uint8_t len = ReceivedBytes - 6; |
||
401 | |||
886 | killagreg | 402 | while(len) |
403 | { |
||
404 | a = rxd_buffer[ptrIn++] - '='; |
||
405 | b = rxd_buffer[ptrIn++] - '='; |
||
406 | c = rxd_buffer[ptrIn++] - '='; |
||
407 | d = rxd_buffer[ptrIn++] - '='; |
||
1078 | killagreg | 408 | //if(ptrIn > ReceivedBytes - 3) break; |
1 | ingob | 409 | |
886 | killagreg | 410 | x = (a << 2) | (b >> 4); |
411 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
412 | z = ((c & 0x03) << 6) | d; |
||
1 | ingob | 413 | |
1078 | killagreg | 414 | if(len--) rxd_buffer[ptrOut++] = x; else break; |
415 | if(len--) rxd_buffer[ptrOut++] = y; else break; |
||
416 | if(len--) rxd_buffer[ptrOut++] = z; else break; |
||
886 | killagreg | 417 | } |
1078 | killagreg | 418 | pRxData = &rxd_buffer[3]; |
419 | RxDataLen = ptrOut - 3; |
||
1 | ingob | 420 | } |
421 | |||
886 | killagreg | 422 | |
1 | ingob | 423 | // -------------------------------------------------------------------------- |
886 | killagreg | 424 | void USART0_ProcessRxData(void) |
1 | ingob | 425 | { |
886 | killagreg | 426 | // if data in the rxd buffer are not locked immediately return |
427 | if(!rxd_buffer_locked) return; |
||
1 | ingob | 428 | |
1078 | killagreg | 429 | uint8_t tempchar1, tempchar2; |
886 | killagreg | 430 | |
1078 | killagreg | 431 | Decode64(); // decode data block in rxd_buffer |
886 | killagreg | 432 | |
1078 | killagreg | 433 | switch(rxd_buffer[1] - 'a') |
886 | killagreg | 434 | { |
1078 | killagreg | 435 | case FC_ADDRESS: |
436 | |||
437 | switch(rxd_buffer[2]) |
||
438 | { |
||
439 | #ifdef USE_MK3MAG |
||
440 | case 'K':// compass value |
||
1081 | killagreg | 441 | CompassHeading = ((Heading_t *)pRxData)->Heading; |
1078 | killagreg | 442 | CompassOffCourse = ((540 + CompassHeading - CompassCourse) % 360) - 180; |
443 | break; |
||
444 | #endif |
||
445 | |||
446 | case 't':// motor test |
||
447 | memcpy(&MotorTest[0], (uint8_t*)pRxData, sizeof(MotorTest)); |
||
448 | //Request_MotorTest = TRUE; |
||
449 | PcAccess = 255; |
||
450 | break; |
||
451 | |||
452 | case 'p': // get PPM channels |
||
453 | Request_PPMChannels = TRUE; |
||
454 | break; |
||
455 | |||
456 | case 'q':// request settings |
||
457 | if(pRxData[0] == 0xFF) |
||
458 | { |
||
459 | pRxData[0] = GetParamByte(PID_ACTIVE_SET); |
||
460 | } |
||
461 | // limit settings range |
||
462 | if(pRxData[0] < 1) pRxData[0] = 1; // limit to 1 |
||
463 | else if(pRxData[0] > 5) pRxData[0] = 5; // limit to 5 |
||
886 | killagreg | 464 | // load requested parameter set |
1078 | killagreg | 465 | ParamSet_ReadFromEEProm(pRxData[0]); |
886 | killagreg | 466 | |
1078 | killagreg | 467 | tempchar1 = pRxData[0]; |
468 | tempchar2 = EEPARAM_VERSION; |
||
469 | while(!txd_complete); // wait for previous frame to be sent |
||
470 | SendOutData('Q', FC_ADDRESS,3, &tempchar1, sizeof(tempchar1), &tempchar2, sizeof(tempchar2), (uint8_t *) &ParamSet, sizeof(ParamSet)); |
||
471 | break; |
||
886 | killagreg | 472 | |
1078 | killagreg | 473 | case 's': // save settings |
474 | if((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_VERSION)) // check for setting to be in range and version of settings |
||
475 | { |
||
476 | memcpy(&ParamSet, (uint8_t*)&pRxData[2], sizeof(ParamSet)); |
||
477 | ParamSet_WriteToEEProm(pRxData[0]); |
||
478 | TurnOver180Nick = (int32_t) ParamSet.AngleTurnOverNick * 2500L; |
||
479 | TurnOver180Roll = (int32_t) ParamSet.AngleTurnOverRoll * 2500L; |
||
480 | tempchar1 = GetActiveParamSet(); |
||
481 | Beep(tempchar1); |
||
482 | } |
||
483 | else |
||
484 | { |
||
485 | tempchar1 = 0; //indicate bad data |
||
486 | } |
||
487 | while(!txd_complete); // wait for previous frame to be sent |
||
488 | SendOutData('S', FC_ADDRESS,1, &tempchar1, sizeof(tempchar1)); |
||
489 | break; |
||
886 | killagreg | 490 | |
1078 | killagreg | 491 | default: |
492 | //unsupported command received |
||
493 | break; |
||
494 | } // case FC_ADDRESS: |
||
886 | killagreg | 495 | |
1078 | killagreg | 496 | default: // any Slave Address |
497 | |||
498 | switch(rxd_buffer[2]) |
||
499 | { |
||
500 | case 'a':// request for labels of the analog debug outputs |
||
501 | Request_DebugLabel = pRxData[0]; |
||
502 | if(Request_DebugLabel > 31) Request_DebugLabel = 31; |
||
503 | PcAccess = 255; |
||
504 | break; |
||
505 | |||
506 | case 'b': // submit extern control |
||
507 | memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl)); |
||
508 | ConfirmFrame = ExternControl.Frame; |
||
509 | PcAccess = 255; |
||
510 | break; |
||
511 | |||
512 | case 'h':// request for display columns |
||
513 | PcAccess = 255; |
||
514 | RemoteKeys |= pRxData[0]; |
||
515 | if(RemoteKeys) DisplayLine = 0; |
||
516 | Request_Display = TRUE; |
||
517 | break; |
||
518 | |||
519 | case 'l':// request for display columns |
||
520 | PcAccess = 255; |
||
521 | MenuItem = pRxData[0]; |
||
522 | Request_Display1 = TRUE; |
||
523 | break; |
||
524 | |||
525 | case 'v': // request for version and board release |
||
526 | Request_VerInfo = TRUE; |
||
527 | break; |
||
528 | |||
529 | case 'g':// get external control data |
||
530 | Request_ExternalControl = TRUE; |
||
531 | break; |
||
532 | |||
533 | case 'd': // request for the debug data |
||
534 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
||
535 | if(DebugData_Interval > 0) Request_DebugData = TRUE; |
||
536 | break; |
||
537 | |||
538 | default: |
||
539 | //unsupported command received |
||
540 | break; |
||
541 | } |
||
542 | break; // default: |
||
886 | killagreg | 543 | } |
544 | // unlock the rxd buffer after processing |
||
1078 | killagreg | 545 | pRxData = 0; |
546 | RxDataLen = 0; |
||
886 | killagreg | 547 | rxd_buffer_locked = FALSE; |
1 | ingob | 548 | } |
549 | |||
550 | //############################################################################ |
||
551 | //Routine für die Serielle Ausgabe |
||
886 | killagreg | 552 | int16_t uart_putchar (int8_t c) |
1 | ingob | 553 | //############################################################################ |
554 | { |
||
555 | if (c == '\n') |
||
556 | uart_putchar('\r'); |
||
886 | killagreg | 557 | // wait until previous character was send |
558 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
1078 | killagreg | 559 | // send character |
886 | killagreg | 560 | UDR0 = c; |
1 | ingob | 561 | return (0); |
562 | } |
||
563 | |||
564 | |||
886 | killagreg | 565 | //--------------------------------------------------------------------------------------------- |
566 | void USART0_TransmitTxData(void) |
||
1 | ingob | 567 | { |
886 | killagreg | 568 | if(!txd_complete) return; |
1 | ingob | 569 | |
1078 | killagreg | 570 | if(Request_VerInfo && txd_complete) |
886 | killagreg | 571 | { |
1078 | killagreg | 572 | SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
573 | Request_VerInfo = FALSE; |
||
886 | killagreg | 574 | } |
1078 | killagreg | 575 | if(Request_Display && txd_complete) |
576 | { |
||
577 | LCD_PrintMenu(); |
||
578 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20); |
||
579 | DisplayLine++; |
||
580 | if(DisplayLine >= 4) DisplayLine = 0; |
||
581 | Request_Display = FALSE; |
||
582 | } |
||
583 | if(Request_Display1 && txd_complete) |
||
584 | { |
||
585 | LCD_PrintMenu(); |
||
586 | SendOutData('L', FC_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff)); |
||
587 | Request_Display1 = FALSE; |
||
588 | } |
||
589 | if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten |
||
590 | { |
||
591 | SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), ANALOG_LABEL[Request_DebugLabel], 16); |
||
592 | Request_DebugLabel = 0xFF; |
||
593 | } |
||
594 | if(ConfirmFrame && txd_complete) // Datensatz ohne CRC bestätigen |
||
595 | { |
||
596 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*)&ConfirmFrame, sizeof(ConfirmFrame)); |
||
597 | ConfirmFrame = 0; |
||
598 | } |
||
599 | if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete) |
||
600 | { |
||
601 | SendOutData('D', FC_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut)); |
||
602 | DebugData_Timer = SetDelay(DebugData_Interval); |
||
603 | Request_DebugData = FALSE; |
||
604 | } |
||
605 | if(Request_ExternalControl && txd_complete) |
||
606 | { |
||
607 | SendOutData('G', FC_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl)); |
||
608 | Request_ExternalControl = FALSE; |
||
609 | } |
||
1 | ingob | 610 | |
908 | killagreg | 611 | #ifdef USE_MK3MAG |
886 | killagreg | 612 | if((CheckDelay(Compass_Timer)) && txd_complete) |
613 | { |
||
1097 | killagreg | 614 | ToMk3Mag.Attitude[0] = (int16_t) (IntegralNick / 130L); // approx. 0.1 deg |
615 | ToMk3Mag.Attitude[1] = (int16_t) (IntegralRoll / 130L); // approx. 0.1 deg |
||
886 | killagreg | 616 | ToMk3Mag.UserParam[0] = FCParam.UserParam1; |
617 | ToMk3Mag.UserParam[1] = FCParam.UserParam2; |
||
618 | ToMk3Mag.CalState = CompassCalState; |
||
1078 | killagreg | 619 | SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag)); |
886 | killagreg | 620 | // the last state is 5 and should be send only once to avoid multiple flash writing |
621 | if(CompassCalState > 4) CompassCalState = 0; |
||
622 | Compass_Timer = SetDelay(99); |
||
623 | } |
||
624 | #endif |
||
1078 | killagreg | 625 | if(Request_MotorTest && txd_complete) |
626 | { |
||
627 | SendOutData('T', FC_ADDRESS, 0); |
||
628 | Request_MotorTest = FALSE; |
||
629 | } |
||
630 | if(Request_PPMChannels && txd_complete) |
||
631 | { |
||
632 | SendOutData('P', FC_ADDRESS, 1, (uint8_t *)&PPM_in, sizeof(PPM_in)); |
||
633 | Request_PPMChannels = FALSE; |
||
634 | } |
||
1 | ingob | 635 | } |
636 |