Rev 434 | Rev 744 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
426 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
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 | |||
8 | #include <avr/io.h> |
||
9 | #include <avr/interrupt.h> |
||
434 | killagreg | 10 | #include <avr/pgmspace.h> |
426 | killagreg | 11 | #include <avr/wdt.h> |
12 | #include <stdarg.h> |
||
13 | #include <string.h> |
||
14 | |||
15 | #include "main.h" |
||
16 | #include "menu.h" |
||
17 | #include "timer0.h" |
||
18 | #include "uart0.h" |
||
19 | #include "ubx.h" |
||
436 | killagreg | 20 | #include "printf_P.h" |
426 | killagreg | 21 | |
22 | |||
23 | #define FC_ADDRESS 1 |
||
24 | #define NC_ADDRESS 2 |
||
25 | #define MK3MAG_ADDRESS 3 |
||
26 | |||
27 | #define FM_ADDRESS 10 // FOLLOW ME |
||
28 | |||
29 | #define FALSE 0 |
||
30 | #define TRUE 1 |
||
31 | |||
32 | |||
33 | //int8_t test __attribute__ ((section (".noinit"))); |
||
34 | uint8_t Request_VerInfo = FALSE; |
||
35 | uint8_t Request_Display = FALSE; |
||
36 | uint8_t Request_Display1 = FALSE; |
||
37 | uint8_t Request_ExternalControl = FALSE; |
||
38 | uint8_t Request_DebugData = FALSE; |
||
39 | uint8_t Request_DebugLabel = 255; |
||
40 | uint8_t Request_SendFollowMe = FALSE; |
||
41 | uint8_t DisplayLine = 0; |
||
42 | |||
43 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
||
44 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
45 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
46 | volatile uint8_t txd_complete = TRUE; |
||
47 | volatile uint8_t ReceivedBytes = 0; |
||
48 | volatile uint8_t *pRxData = 0; |
||
49 | volatile uint8_t RxDataLen = 0; |
||
50 | |||
51 | uint8_t PcAccess = 100; |
||
52 | |||
53 | ExternControl_t ExternControl; |
||
54 | DebugOut_t DebugOut; |
||
55 | UART_VersionInfo_t UART_VersionInfo; |
||
56 | |||
57 | uint16_t DebugData_Timer; |
||
434 | killagreg | 58 | uint16_t DebugData_Interval = 0; // in 1ms |
426 | killagreg | 59 | |
60 | Waypoint_t FollowMe; |
||
61 | |||
434 | killagreg | 62 | // keep lables in flash to save 512 bytes of sram space |
63 | const prog_uint8_t ANALOG_LABEL[32][16] = |
||
426 | killagreg | 64 | { |
65 | //1234567890123456 |
||
66 | "Analog_Ch0 ", //0 |
||
67 | "Analog_Ch1 ", |
||
68 | "Analog_Ch2 ", |
||
69 | "Analog_Ch3 ", |
||
70 | "Analog_Ch4 ", |
||
71 | "Analog_Ch5 ", //5 |
||
72 | "Analog_Ch6 ", |
||
73 | "Analog_Ch7 ", |
||
74 | "UBat ", |
||
436 | killagreg | 75 | "SysState ", |
76 | "Debug10 ", //10 |
||
77 | "Debug11 ", |
||
78 | "Debug12 ", |
||
79 | "Debug13 ", |
||
80 | "Debug14 ", |
||
81 | "Debug15 ", //15 |
||
426 | killagreg | 82 | "Zellenzahl ", |
83 | "PowerOn ", |
||
84 | "Debug18 ", |
||
85 | "Debug19 ", |
||
86 | "Debug20 ", //20 |
||
87 | "Debug21 ", |
||
88 | "Debug22 ", |
||
89 | "Debug23 ", |
||
90 | "Debug24 ", |
||
91 | "Debug25 ", //25 |
||
92 | "Debug26 ", |
||
93 | "Debug27 ", |
||
94 | "Debug28 ", |
||
95 | "Debug29 ", |
||
96 | "Debug30 ", //30 |
||
97 | "Debug31 " |
||
98 | }; |
||
99 | |||
100 | |||
101 | |||
102 | /****************************************************************/ |
||
103 | /* Initialization of the USART0 */ |
||
104 | /****************************************************************/ |
||
105 | void USART0_Init (void) |
||
106 | { |
||
107 | uint8_t sreg = SREG; |
||
108 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * USART0_BAUD) - 1); |
||
109 | |||
110 | // disable all interrupts before configuration |
||
111 | cli(); |
||
112 | |||
113 | // disable RX-Interrupt |
||
114 | UCSR0B &= ~(1 << RXCIE0); |
||
115 | // disable TX-Interrupt |
||
116 | UCSR0B &= ~(1 << TXCIE0); |
||
117 | |||
118 | // set direction of RXD0 and TXD0 pins |
||
119 | // set RXD0 (PD0) as an input pin |
||
120 | PORTD |= (1 << PORTD0); |
||
121 | DDRD &= ~(1 << DDD0); |
||
122 | // set TXD0 (PD1) as an output pin |
||
123 | PORTD |= (1 << PORTD1); |
||
124 | DDRD |= (1 << DDD1); |
||
125 | |||
126 | // USART0 Baud Rate Register |
||
127 | // set clock divider |
||
128 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
129 | UBRR0L = (uint8_t)ubrr; |
||
130 | |||
131 | // USART0 Control and Status Register A, B, C |
||
132 | |||
133 | // enable double speed operation in |
||
134 | UCSR0A |= (1 << U2X0); |
||
135 | // enable receiver and transmitter in |
||
136 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
||
137 | // set asynchronous mode |
||
138 | UCSR0C &= ~(1 << UMSEL01); |
||
139 | UCSR0C &= ~(1 << UMSEL00); |
||
140 | // no parity |
||
141 | UCSR0C &= ~(1 << UPM01); |
||
142 | UCSR0C &= ~(1 << UPM00); |
||
143 | // 1 stop bit |
||
144 | UCSR0C &= ~(1 << USBS0); |
||
145 | // 8-bit |
||
146 | UCSR0B &= ~(1 << UCSZ02); |
||
147 | UCSR0C |= (1 << UCSZ01); |
||
148 | UCSR0C |= (1 << UCSZ00); |
||
149 | |||
150 | // flush receive buffer |
||
151 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
||
152 | |||
153 | // enable interrupts at the end |
||
154 | // enable RX-Interrupt |
||
155 | UCSR0B |= (1 << RXCIE0); |
||
156 | // enable TX-Interrupt |
||
157 | UCSR0B |= (1 << TXCIE0); |
||
158 | |||
159 | // initialize the debug timer |
||
160 | DebugData_Timer = SetDelay(DebugData_Interval); |
||
161 | |||
162 | // unlock rxd_buffer |
||
163 | rxd_buffer_locked = FALSE; |
||
164 | pRxData = 0; |
||
165 | RxDataLen = 0; |
||
166 | |||
167 | // no bytes to send |
||
168 | txd_complete = TRUE; |
||
169 | |||
170 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
||
171 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
||
172 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
||
173 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
174 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
175 | |||
176 | // restore global interrupt flags |
||
177 | SREG = sreg; |
||
434 | killagreg | 178 | sei(); |
179 | printf("\r\n UART0 init...ok"); |
||
426 | killagreg | 180 | } |
181 | |||
182 | /****************************************************************/ |
||
183 | /* USART0 transmitter ISR */ |
||
184 | /****************************************************************/ |
||
185 | ISR(USART0_TX_vect) |
||
186 | { |
||
187 | static uint16_t ptr_txd_buffer = 0; |
||
188 | uint8_t tmp_tx; |
||
189 | if(!txd_complete) // transmission not completed |
||
190 | { |
||
191 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
||
192 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
||
193 | // if terminating character or end of txd buffer was reached |
||
194 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
||
195 | { |
||
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 ptr_txd_buffer = 0; |
||
203 | } |
||
204 | |||
205 | /****************************************************************/ |
||
206 | /* USART0 receiver ISR */ |
||
207 | /****************************************************************/ |
||
208 | ISR(USART0_RX_vect) |
||
209 | { |
||
210 | static uint16_t crc; |
||
211 | static uint8_t ptr_rxd_buffer = 0; |
||
212 | uint8_t crc1, crc2; |
||
213 | uint8_t c; |
||
214 | |||
215 | c = UDR0; // catch the received byte |
||
216 | |||
217 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
||
218 | |||
219 | // the rxd buffer is unlocked |
||
220 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
||
221 | { |
||
222 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
223 | crc = c; // init crc |
||
224 | } |
||
225 | #if 0 |
||
226 | else if (ptr_rxd_buffer == 1) // handle address |
||
227 | { |
||
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 | { |
||
234 | if(c != '\r') // no termination character |
||
235 | { |
||
236 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
237 | crc += c; // update crc |
||
238 | } |
||
239 | else // termination character was received |
||
240 | { |
||
241 | // the last 2 bytes are no subject for checksum calculation |
||
242 | // they are the checksum itself |
||
243 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
||
244 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
||
245 | // calculate checksum from transmitted data |
||
246 | crc %= 4096; |
||
247 | crc1 = '=' + crc / 64; |
||
248 | crc2 = '=' + crc % 64; |
||
249 | // compare checksum to transmitted checksum bytes |
||
250 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
||
251 | { // checksum valid |
||
252 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
||
253 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
||
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') {wdt_enable(WDTO_250MS);} // Reset-Commando |
||
257 | } |
||
258 | else |
||
259 | { // checksum invalid |
||
260 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
261 | } |
||
262 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
263 | } |
||
264 | } |
||
265 | else // rxd buffer overrun |
||
266 | { |
||
267 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
268 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
269 | } |
||
270 | |||
271 | } |
||
272 | |||
273 | |||
274 | // -------------------------------------------------------------------------- |
||
275 | void AddCRC(uint16_t datalen) |
||
276 | { |
||
277 | uint16_t tmpCRC = 0, i; |
||
278 | for(i = 0; i < datalen; i++) |
||
279 | { |
||
280 | tmpCRC += txd_buffer[i]; |
||
281 | } |
||
282 | tmpCRC %= 4096; |
||
283 | txd_buffer[i++] = '=' + tmpCRC / 64; |
||
284 | txd_buffer[i++] = '=' + tmpCRC % 64; |
||
285 | txd_buffer[i++] = '\r'; |
||
286 | txd_complete = FALSE; |
||
287 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
||
288 | } |
||
289 | |||
290 | |||
291 | |||
292 | // -------------------------------------------------------------------------- |
||
293 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
||
294 | { |
||
295 | va_list ap; |
||
296 | uint16_t pt = 0; |
||
297 | uint8_t a,b,c; |
||
298 | uint8_t ptr = 0; |
||
299 | |||
300 | uint8_t *pdata = 0; |
||
301 | int len = 0; |
||
302 | |||
303 | txd_buffer[pt++] = '#'; // Start character |
||
304 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
||
305 | txd_buffer[pt++] = cmd; // Command |
||
306 | |||
307 | va_start(ap, numofbuffers); |
||
308 | if(numofbuffers) |
||
309 | { |
||
310 | pdata = va_arg(ap, uint8_t*); |
||
311 | len = va_arg(ap, int); |
||
312 | ptr = 0; |
||
313 | numofbuffers--; |
||
314 | } |
||
315 | |||
316 | while(len) |
||
317 | { |
||
318 | if(len) |
||
319 | { |
||
320 | a = pdata[ptr++]; |
||
321 | len--; |
||
322 | if((!len) && numofbuffers) |
||
323 | { |
||
324 | pdata = va_arg(ap, uint8_t*); |
||
325 | len = va_arg(ap, int); |
||
326 | ptr = 0; |
||
327 | numofbuffers--; |
||
328 | } |
||
329 | } |
||
330 | else a = 0; |
||
331 | if(len) |
||
332 | { |
||
333 | b = pdata[ptr++]; |
||
334 | len--; |
||
335 | if((!len) && numofbuffers) |
||
336 | { |
||
337 | pdata = va_arg(ap, uint8_t*); |
||
338 | len = va_arg(ap, int); |
||
339 | ptr = 0; |
||
340 | numofbuffers--; |
||
341 | } |
||
342 | } |
||
343 | else b = 0; |
||
344 | if(len) |
||
345 | { |
||
346 | c = 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 c = 0; |
||
357 | txd_buffer[pt++] = '=' + (a >> 2); |
||
358 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
359 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
360 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
||
361 | } |
||
362 | va_end(ap); |
||
363 | AddCRC(pt); // add checksum after data block and initates the transmission |
||
364 | } |
||
365 | |||
366 | |||
367 | // -------------------------------------------------------------------------- |
||
368 | void Decode64(void) |
||
369 | { |
||
370 | uint8_t a,b,c,d; |
||
371 | uint8_t x,y,z; |
||
372 | uint8_t ptrIn = 3; |
||
373 | uint8_t ptrOut = 3; |
||
374 | uint8_t len = ReceivedBytes - 6; |
||
375 | |||
376 | while(len) |
||
377 | { |
||
378 | a = rxd_buffer[ptrIn++] - '='; |
||
379 | b = rxd_buffer[ptrIn++] - '='; |
||
380 | c = rxd_buffer[ptrIn++] - '='; |
||
381 | d = rxd_buffer[ptrIn++] - '='; |
||
382 | //if(ptrIn > ReceivedBytes - 3) break; |
||
383 | |||
384 | x = (a << 2) | (b >> 4); |
||
385 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
386 | z = ((c & 0x03) << 6) | d; |
||
387 | |||
388 | if(len--) rxd_buffer[ptrOut++] = x; else break; |
||
389 | if(len--) rxd_buffer[ptrOut++] = y; else break; |
||
390 | if(len--) rxd_buffer[ptrOut++] = z; else break; |
||
391 | } |
||
392 | pRxData = &rxd_buffer[3]; |
||
393 | RxDataLen = ptrOut - 3; |
||
394 | } |
||
395 | |||
396 | |||
397 | // -------------------------------------------------------------------------- |
||
398 | void USART0_ProcessRxData(void) |
||
399 | { |
||
400 | // if data in the rxd buffer are not locked immediately return |
||
401 | if(!rxd_buffer_locked) return; |
||
402 | |||
403 | Decode64(); // decode data block in rxd_buffer |
||
404 | |||
405 | |||
406 | switch(rxd_buffer[1] - 'a') |
||
407 | { |
||
408 | case FM_ADDRESS: |
||
409 | |||
410 | switch(rxd_buffer[2]) |
||
411 | { |
||
412 | default: |
||
413 | //unsupported command received |
||
414 | break; |
||
415 | } // case FC_ADDRESS: |
||
416 | |||
417 | default: // any Slave Address |
||
418 | |||
419 | switch(rxd_buffer[2]) |
||
420 | { |
||
421 | case 'a':// request for labels of the analog debug outputs |
||
422 | Request_DebugLabel = pRxData[0]; |
||
423 | if(Request_DebugLabel > 31) Request_DebugLabel = 31; |
||
424 | PcAccess = 255; |
||
425 | break; |
||
426 | |||
427 | case 'h':// request for display columns |
||
428 | PcAccess = 255; |
||
429 | RemoteKeys |= pRxData[0]; |
||
430 | if(RemoteKeys) DisplayLine = 0; |
||
431 | Request_Display = TRUE; |
||
432 | break; |
||
433 | |||
434 | case 'l':// request for display columns |
||
435 | PcAccess = 255; |
||
436 | MenuItem = pRxData[0]; |
||
437 | Request_Display1 = TRUE; |
||
438 | break; |
||
439 | |||
440 | case 'v': // request for version and board release |
||
441 | Request_VerInfo = TRUE; |
||
442 | break; |
||
443 | |||
444 | case 'd': // request for the debug data |
||
445 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
||
446 | if(DebugData_Interval > 0) Request_DebugData = TRUE; |
||
447 | break; |
||
448 | |||
449 | case 'g':// get external control data |
||
450 | Request_ExternalControl = TRUE; |
||
451 | break; |
||
452 | |||
453 | default: |
||
454 | //unsupported command received |
||
455 | break; |
||
456 | } |
||
457 | break; // default: |
||
458 | } |
||
459 | // unlock the rxd buffer after processing |
||
460 | pRxData = 0; |
||
461 | RxDataLen = 0; |
||
462 | rxd_buffer_locked = FALSE; |
||
463 | } |
||
464 | |||
465 | //############################################################################ |
||
466 | //Routine für die Serielle Ausgabe |
||
467 | int16_t uart_putchar (int8_t c) |
||
468 | //############################################################################ |
||
469 | { |
||
470 | if (c == '\n') |
||
471 | uart_putchar('\r'); |
||
472 | // wait until previous character was send |
||
473 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
474 | // send character |
||
475 | UDR0 = c; |
||
476 | return (0); |
||
477 | } |
||
478 | |||
479 | |||
480 | //--------------------------------------------------------------------------------------------- |
||
481 | void USART0_TransmitTxData(void) |
||
482 | { |
||
483 | if(!txd_complete) return; |
||
484 | |||
485 | if(Request_VerInfo && txd_complete) |
||
486 | { |
||
487 | SendOutData('V', FM_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
||
488 | Request_VerInfo = FALSE; |
||
489 | } |
||
490 | if(Request_Display && txd_complete) |
||
491 | { |
||
492 | LCD_PrintMenu(); |
||
493 | SendOutData('H', FM_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20); |
||
494 | DisplayLine++; |
||
495 | if(DisplayLine >= 4) DisplayLine = 0; |
||
496 | Request_Display = FALSE; |
||
497 | } |
||
498 | if(Request_Display1 && txd_complete) |
||
499 | { |
||
500 | LCD_PrintMenu(); |
||
501 | SendOutData('L', FM_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff)); |
||
502 | Request_Display1 = FALSE; |
||
503 | } |
||
504 | if(Request_DebugLabel != 0xFF) // Texte für die Analogdaten |
||
505 | { |
||
434 | killagreg | 506 | uint8_t label[16]; // local sram buffer |
507 | memcpy_P(label, ANALOG_LABEL[Request_DebugLabel], 16); // read lable from flash to sram buffer |
||
508 | SendOutData('A', FM_ADDRESS, 2, (uint8_t *) &Request_DebugLabel, sizeof(Request_DebugLabel), label, 16); |
||
426 | killagreg | 509 | Request_DebugLabel = 0xFF; |
510 | } |
||
511 | if(Request_ExternalControl && txd_complete) |
||
512 | { |
||
513 | SendOutData('G', FM_ADDRESS, 1,(uint8_t *) &ExternControl, sizeof(ExternControl)); |
||
514 | Request_ExternalControl = FALSE; |
||
515 | } |
||
516 | if( ((DebugData_Interval && CheckDelay(DebugData_Timer)) || Request_DebugData) && txd_complete) |
||
517 | { |
||
518 | SendOutData('D', FM_ADDRESS, 1,(uint8_t *) &DebugOut, sizeof(DebugOut)); |
||
519 | DebugData_Timer = SetDelay(DebugData_Interval); |
||
520 | Request_DebugData = FALSE; |
||
521 | } |
||
522 | if(Request_SendFollowMe && txd_complete) |
||
523 | { |
||
524 | SendOutData('s', NC_ADDRESS, 1, (uint8_t *)&FollowMe, sizeof(FollowMe)); |
||
525 | FollowMe.Position.Status = PROCESSED; |
||
526 | Request_SendFollowMe = FALSE; |
||
527 | } |
||
528 | } |
||
529 |