Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2136 | - | 1 | /***************************************************************************** |
2 | * Copyright (C) 2008 Thomas Kaiser, thomas@ft-fanpage.de * |
||
3 | * Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net * |
||
4 | * Copyright (C) 2011 Christian "Cebra" Brandtner, brandtner@brandtner.net * |
||
5 | * Copyright (C) 2011 Harald Bongartz * |
||
6 | * * |
||
7 | * This program is free software; you can redistribute it and/or modify * |
||
8 | * it under the terms of the GNU General Public License as published by * |
||
9 | * the Free Software Foundation; either version 2 of the License. * |
||
10 | * * |
||
11 | * This program is distributed in the hope that it will be useful, * |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
14 | * GNU General Public License for more details. * |
||
15 | * * |
||
16 | * You should have received a copy of the GNU General Public License * |
||
17 | * along with this program; if not, write to the * |
||
18 | * Free Software Foundation, Inc., * |
||
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
20 | * * |
||
21 | * * |
||
22 | * Credits to: * |
||
23 | * Holger Buss & Ingo Busker from mikrokopter.de for the MK project + SVN * |
||
24 | * http://www.mikrokopter.de * |
||
25 | * Gregor "killagreg" Stobrawa for his version of the MK code * |
||
26 | * Thomas Kaiser "thkais" for the original project. See * |
||
27 | * http://www.ft-fanpage.de/mikrokopter/ * |
||
28 | * http://forum.mikrokopter.de/topic-4061-1.html * |
||
29 | * Claas Anders "CaScAdE" Rathje for providing the font and his C-OSD code * |
||
30 | * http://www.mylifesucks.de/oss/c-osd/ * |
||
31 | * Harald Bongartz "HaraldB" for providing his Ideas and Code for usibility* |
||
32 | *****************************************************************************/ |
||
33 | |||
34 | //############################################################################ |
||
35 | //# HISTORY usart.c |
||
36 | //# |
||
37 | //# 3.04.2015 Cebra |
||
38 | //# - chg: 2 weitere /r am Ende des gesendeten Strings angefügt, damit Funktion gleich wie im MK-Tool |
||
39 | //# |
||
40 | //# 24.01.2014 OG |
||
41 | //# - add: Debug-Code fuer die ISR-RX Funktion |
||
42 | //# schaltbar via define DEBUG_FC_COMMUNICATION in main.h - der Code |
||
43 | //# kann somit drin bleiben |
||
44 | //# - add: Source-History |
||
45 | //############################################################################ |
||
46 | |||
47 | #include <avr/io.h> |
||
48 | #include <avr/interrupt.h> |
||
49 | #include <avr/pgmspace.h> |
||
50 | #include <avr/wdt.h> |
||
51 | #include "../cpu.h" |
||
52 | #include <util/delay.h> |
||
53 | #include <stdarg.h> |
||
54 | |||
55 | #include "../main.h" |
||
56 | #include "usart.h" |
||
57 | #include "../lcd/lcd.h" |
||
58 | #include "../timer/timer.h" |
||
59 | #include "uart1.h" |
||
60 | #include "../eeprom/eeprom.h" |
||
61 | #include "../osd/osd.h" |
||
62 | |||
63 | |||
64 | uint8_t buffer[30]; |
||
65 | |||
66 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
||
67 | volatile uint8_t txd_complete = TRUE; |
||
68 | |||
69 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
70 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
71 | volatile uint8_t ReceivedBytes = 0; |
||
72 | volatile uint8_t *pRxData = 0; |
||
73 | volatile uint8_t RxDataLen = 0; |
||
74 | |||
75 | volatile uint16_t stat_crc_error = 0; |
||
76 | volatile uint16_t stat_overflow_error = 0; |
||
77 | |||
78 | volatile uint8_t rx_byte; |
||
79 | volatile uint8_t rxFlag = 0; |
||
80 | |||
81 | |||
82 | #define UART_RXBUFSIZE 64 |
||
83 | #define UART_NO_DATA 0x0100 /* no receive data available */ |
||
84 | |||
85 | volatile static uint8_t rxbuf[UART_RXBUFSIZE]; |
||
86 | volatile static uint8_t *volatile rxhead, *volatile rxtail; |
||
87 | |||
88 | |||
89 | #ifdef DEBUG_FC_COMMUNICATION |
||
90 | char *tmp = (char *)" "; |
||
91 | #endif |
||
92 | |||
93 | |||
94 | |||
95 | /* |
||
96 | |||
97 | //----------------------------------------------------------------------------- |
||
98 | // USART1 transmitter ISR |
||
99 | ISR (USART1_TX_vect) |
||
100 | { |
||
101 | static uint16_t ptr_txd1_buffer = 0; |
||
102 | uint8_t tmp_tx1; |
||
103 | |||
104 | if(!txd1_complete) // transmission not completed |
||
105 | { |
||
106 | ptr_txd1_buffer++; // [0] was already sent |
||
107 | tmp_tx1 = txd1_buffer[ptr_txd1_buffer]; |
||
108 | // if terminating character or end of txd buffer was reached |
||
109 | if((tmp_tx1 == '\r') || (ptr_txd1_buffer == TXD_BUFFER_LEN)) |
||
110 | { |
||
111 | ptr_txd1_buffer = 0; // reset txd pointer |
||
112 | txd1_complete = TRUE; // stop transmission |
||
113 | } |
||
114 | UDR1 = tmp_tx1; // send current byte will trigger this ISR again |
||
115 | } |
||
116 | // transmission completed |
||
117 | else ptr_txd1_buffer = 0; |
||
118 | } |
||
119 | |||
120 | */ |
||
121 | |||
122 | |||
123 | #ifdef USART_INT |
||
124 | //----------------------------------------------------------------------------- |
||
125 | // USART0 transmitter ISR |
||
126 | ISR (USART_TX_vect) |
||
127 | { |
||
128 | static uint16_t ptr_txd_buffer = 0; |
||
129 | uint8_t tmp_tx; |
||
130 | |||
131 | if(!txd_complete) // transmission not completed |
||
132 | { |
||
133 | ptr_txd_buffer++; // [0] was already sent |
||
134 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
||
135 | // if terminating character or end of txd buffer was reached |
||
136 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
||
137 | { |
||
138 | ptr_txd_buffer = 0; // reset txd pointer |
||
139 | txd_complete = TRUE; // stop transmission |
||
140 | } |
||
141 | UDR = tmp_tx; // send current byte will trigger this ISR again |
||
142 | } |
||
143 | // transmission completed |
||
144 | else ptr_txd_buffer = 0; |
||
145 | } |
||
146 | #endif |
||
147 | |||
148 | |||
149 | void SetBaudUart0(uint8_t Baudrate) |
||
150 | { |
||
151 | switch (Baudrate) |
||
152 | { |
||
153 | case Baud_2400: USART_Init( UART_BAUD_SELECT(2400,F_CPU) ); break; |
||
154 | case Baud_4800: USART_Init( UART_BAUD_SELECT(4800,F_CPU) ); break; |
||
155 | case Baud_9600: USART_Init( UART_BAUD_SELECT(9600,F_CPU) ); break; |
||
156 | case Baud_19200: USART_Init( UART_BAUD_SELECT(19200,F_CPU) ); break; |
||
157 | case Baud_38400: USART_Init( UART_BAUD_SELECT(38400,F_CPU) ); break; |
||
158 | case Baud_57600: USART_Init( UART_BAUD_SELECT(57600,F_CPU) ); break; |
||
159 | // case Baud_115200: USART_Init( UART_BAUD_SELECT(115200,F_CPU) ); break; |
||
160 | // Macro erechnet falschen Wert (9,85 = 9) für 115200 Baud mit 20Mhz Quarz, zu grosse Abweichung |
||
161 | |||
162 | //#warning "Baurate prüfen wenn kein 20 Mhz Quarz verwendet wird" |
||
163 | |||
164 | case Baud_115200: USART_Init( 10 ); break; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | |||
169 | //----------------------------------------------------------------------------- |
||
170 | // |
||
171 | |||
172 | // |
||
173 | //uint8_t uart_getc_nb(uint8_t *c) |
||
174 | //{ |
||
175 | // if (rxhead==rxtail) return 0; |
||
176 | // *c = *rxtail; |
||
177 | // if (++rxtail == (rxbuf + UART_RXBUFSIZE)) rxtail = rxbuf; |
||
178 | // return 1; |
||
179 | //} |
||
180 | |||
181 | |||
182 | |||
183 | |||
184 | ISR (USART0_RX_vect) |
||
185 | { |
||
186 | static uint16_t crc; |
||
187 | static uint8_t ptr_rxd_buffer = 0; |
||
188 | uint8_t crc1, crc2; |
||
189 | uint8_t c; |
||
190 | // IdleTimer = 0; |
||
191 | |||
192 | |||
193 | |||
194 | if (current_hardware == Wi232) |
||
195 | { |
||
196 | // rx_byte = c; |
||
197 | // rxFlag = 1; |
||
198 | int diff; |
||
199 | uint8_t c; |
||
200 | c=UDR; |
||
201 | diff = rxhead - rxtail; |
||
202 | if (diff < 0) diff += UART_RXBUFSIZE; |
||
203 | if (diff < UART_RXBUFSIZE -1) |
||
204 | { |
||
205 | *rxhead = c; |
||
206 | ++rxhead; |
||
207 | if (rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf; |
||
208 | }; |
||
209 | // USART_putc (c); |
||
210 | return; |
||
211 | } |
||
212 | |||
213 | |||
214 | if (current_hardware == MKGPS) |
||
215 | { |
||
216 | // rx_byte = c; |
||
217 | // rxFlag = 1; |
||
218 | int diff; |
||
219 | uint8_t c; |
||
220 | c=UDR; |
||
221 | diff = rxhead - rxtail; |
||
222 | if (diff < 0) diff += UART_RXBUFSIZE; |
||
223 | if (diff < UART_RXBUFSIZE -1) |
||
224 | { |
||
225 | *rxhead = c; |
||
226 | ++rxhead; |
||
227 | if (rxhead == (rxbuf + UART_RXBUFSIZE)) rxhead = rxbuf; |
||
228 | }; |
||
229 | |||
230 | return; |
||
231 | } |
||
232 | |||
233 | |||
234 | |||
235 | c = UDR; // catch the received byte |
||
236 | if (OSD_active && Config.OSD_SendOSD) // Daten an SV2 senden |
||
237 | uart1_putc(c); |
||
238 | |||
239 | |||
240 | if (rxd_buffer_locked) |
||
241 | return; // if rxd buffer is locked immediately return |
||
242 | |||
243 | // the rxd buffer is unlocked |
||
244 | if ((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
||
245 | { |
||
246 | // OG DEBUG: DEBUG_FC_COMMUNICATION (einstellbar in main.h) |
||
247 | #ifdef DEBUG_FC_COMMUNICATION |
||
248 | lcd_print( "#", MNORMAL ); // OG DEBUG |
||
249 | #endif |
||
250 | |||
251 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
252 | crc = c; // init crc |
||
253 | } |
||
254 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
||
255 | { |
||
256 | if(c != '\r') // no termination character |
||
257 | { |
||
258 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
259 | crc += c; // update crc |
||
260 | } |
||
261 | else // termination character was received |
||
262 | { |
||
263 | // the last 2 bytes are no subject for checksum calculation |
||
264 | // they are the checksum itself |
||
265 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
||
266 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
||
267 | // calculate checksum from transmitted data |
||
268 | crc %= 4096; |
||
269 | crc1 = '=' + crc / 64; |
||
270 | crc2 = '=' + crc % 64; |
||
271 | |||
272 | // compare checksum to transmitted checksum bytes |
||
273 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
||
274 | { // checksum valid |
||
275 | |||
276 | // OG DEBUG: DEBUG_FC_COMMUNICATION (einstellbar in main.h) |
||
277 | #ifdef DEBUG_FC_COMMUNICATION |
||
278 | *tmp = rxd_buffer[2]; |
||
279 | lcd_print( tmp, MNORMAL ); |
||
280 | lcd_putc( 0,0, '[', MNORMAL ); |
||
281 | lcd_putc( 1,0, rxd_buffer[2], MNORMAL ); |
||
282 | lcd_putc( 2,0, ']', MNORMAL ); x |
||
283 | if( rxd_buffer[2] != 'k' ) set_beep( 13, 0x0001, BeepNormal); |
||
284 | if( rxd_buffer[2] == 'Q' ) set_beep( 150, 0x0080, BeepNormal); |
||
285 | #endif |
||
286 | |||
287 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
||
288 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
||
289 | |||
290 | if (mode == rxd_buffer[2]) |
||
291 | { |
||
292 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
||
293 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
||
294 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
||
295 | } |
||
296 | } |
||
297 | else |
||
298 | { // checksum invalid |
||
299 | stat_crc_error++; |
||
300 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
301 | } |
||
302 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
303 | } |
||
304 | } |
||
305 | else // rxd buffer overrun |
||
306 | { |
||
307 | stat_overflow_error++; |
||
308 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
309 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
310 | } |
||
311 | } |
||
312 | |||
313 | |||
314 | //----------------------------------------------------------------------------- |
||
315 | // Function: uart0_getc() |
||
316 | // Purpose: return byte from ringbuffer |
||
317 | // Returns: lower byte: received byte from ringbuffer |
||
318 | // higher byte: last receive error |
||
319 | //----------------------------------------------------------------------------- |
||
320 | char USART_getc(void) |
||
321 | { |
||
322 | char val; |
||
323 | |||
324 | // while(rxhead==rxtail) ; |
||
325 | if (rxhead==rxtail) |
||
326 | return val=0; |
||
327 | // IdleTimer = 0; |
||
328 | val = *rxtail; |
||
329 | if (++rxtail == (rxbuf + UART_RXBUFSIZE)) |
||
330 | rxtail = rxbuf; |
||
331 | |||
332 | return val; |
||
333 | } |
||
334 | |||
335 | |||
336 | uint8_t uart_getc_nb(uint8_t *c) |
||
337 | { |
||
338 | if (rxhead==rxtail) |
||
339 | return 0; |
||
340 | // IdleTimer = 0; |
||
341 | *c = *rxtail; |
||
342 | if (++rxtail == (rxbuf + UART_RXBUFSIZE)) |
||
343 | rxtail = rxbuf; |
||
344 | return 1; |
||
345 | } |
||
346 | //----------------------------------------------------------------------------- |
||
347 | // |
||
348 | |||
349 | |||
350 | |||
351 | |||
352 | |||
353 | //----------------------------------------------------------------------------- |
||
354 | // |
||
355 | void USART_Init (unsigned int baudrate) |
||
356 | { |
||
357 | // set clock divider |
||
358 | // #undef BAUD |
||
359 | // #define BAUD baudrate |
||
360 | // #include <util/setbaud.h> |
||
361 | // UBRRH = UBRRH_VALUE; |
||
362 | // UBRRL = UBRRL_VALUE; |
||
363 | |||
364 | //#ifndef F_CPU |
||
365 | ///* In neueren Version der WinAVR/Mfile Makefile-Vorlage kann |
||
366 | // F_CPU im Makefile definiert werden, eine nochmalige Definition |
||
367 | // hier wuerde zu einer Compilerwarnung fuehren. Daher "Schutz" durch |
||
368 | // #ifndef/#endif |
||
369 | // |
||
370 | // Dieser "Schutz" kann zu Debugsessions führen, wenn AVRStudio |
||
371 | // verwendet wird und dort eine andere, nicht zur Hardware passende |
||
372 | // Taktrate eingestellt ist: Dann wird die folgende Definition |
||
373 | // nicht verwendet, sondern stattdessen der Defaultwert (8 MHz?) |
||
374 | // von AVRStudio - daher Ausgabe einer Warnung falls F_CPU |
||
375 | // noch nicht definiert: */ |
||
376 | //#warning "F_CPU war noch nicht definiert, wird nun nachgeholt mit 4000000" |
||
377 | //#define F_CPU 18432000UL // Systemtakt in Hz - Definition als unsigned long beachten |
||
378 | // Ohne ergeben sich unten Fehler in der Berechnung |
||
379 | //#endif |
||
380 | //#define BAUD 115200UL // Baudrate |
||
381 | // |
||
382 | //// Berechnungen |
||
383 | //#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // clever runden |
||
384 | //#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Reale Baudrate |
||
385 | //#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler. |
||
386 | // |
||
387 | // |
||
388 | //#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010)) |
||
389 | // #error "Systematischer Fehler der Baudrate grösser 1% und damit zu hoch!" |
||
390 | //#endif |
||
391 | |||
392 | |||
393 | UBRRH = (unsigned char)(baudrate>>8); |
||
394 | UBRRL = (unsigned char) baudrate; |
||
395 | // UBRRH = (unsigned char)(BAUD_REAL>>8); |
||
396 | // UBRRL = (unsigned char) BAUD_REAL; |
||
397 | |||
398 | #if USE_2X |
||
399 | UCSRA |= (1 << U2X); // enable double speed operation |
||
400 | #else |
||
401 | UCSRA &= ~(1 << U2X); // disable double speed operation |
||
402 | #endif |
||
403 | |||
404 | // set 8N1 |
||
405 | #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega32__) |
||
406 | UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0); |
||
407 | #else |
||
408 | UCSRC = (1 << UCSZ1) | (1 << UCSZ0); |
||
409 | #endif |
||
410 | UCSRB &= ~(1 << UCSZ2); |
||
411 | |||
412 | // flush receive buffer |
||
413 | while ( UCSRA & (1 << RXC) ) UDR; |
||
414 | |||
415 | UCSRB |= (1 << RXEN) | (1 << TXEN); |
||
416 | #ifdef USART_INT |
||
417 | UCSRB |= (1 << RXCIE) | (1 << TXCIE); |
||
418 | #else |
||
419 | UCSRB |= (1 << RXCIE); |
||
420 | #endif |
||
421 | |||
422 | rxhead = rxtail = rxbuf; |
||
423 | |||
424 | } |
||
425 | |||
426 | |||
427 | |||
428 | |||
429 | |||
430 | //----------------------------------------------------------------------------- |
||
431 | // disable the txd pin of usart |
||
432 | void USART_DisableTXD (void) |
||
433 | { |
||
434 | #ifdef USART_INT |
||
435 | UCSRB &= ~(1 << TXCIE); // disable TX-Interrupt |
||
436 | #endif |
||
437 | UCSRB &= ~(1 << TXEN); // disable TX in USART |
||
438 | DDRB &= ~(1 << DDB3); // set TXD pin as input |
||
439 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
||
440 | } |
||
441 | |||
442 | //----------------------------------------------------------------------------- |
||
443 | // enable the txd pin of usart |
||
444 | void USART_EnableTXD (void) |
||
445 | { |
||
446 | DDRB |= (1 << DDB3); // set TXD pin as output |
||
447 | PORTB &= ~(1 << PORTB3); // disable pullup on TXD pin |
||
448 | UCSRB |= (1 << TXEN); // enable TX in USART |
||
449 | #ifdef USART_INT |
||
450 | UCSRB |= (1 << TXCIE); // enable TX-Interrupt |
||
451 | #endif |
||
452 | } |
||
453 | |||
454 | //----------------------------------------------------------------------------- |
||
455 | // short script to directly send a request thorugh usart including en- and disabling it |
||
456 | // where <address> is the address of the receipient, <label> is which data set to request |
||
457 | // and <ms> represents the milliseconds delay between data |
||
458 | void USART_request_mk_data (uint8_t cmd, uint8_t addr, uint8_t ms) |
||
459 | { |
||
460 | USART_EnableTXD (); // re-enable TXD pin |
||
461 | |||
462 | unsigned char mstenth = ms/10; |
||
463 | SendOutData(cmd, addr, 1, &mstenth, 1); |
||
464 | // wait until command transmitted |
||
465 | while (txd_complete == FALSE); |
||
466 | |||
467 | USART_DisableTXD (); // disable TXD pin again |
||
468 | } |
||
469 | |||
470 | //----------------------------------------------------------------------------- |
||
471 | // |
||
472 | void USART_putc (char c) |
||
473 | { |
||
474 | #ifdef USART_INT |
||
475 | #else |
||
476 | loop_until_bit_is_set(UCSRA, UDRE); |
||
477 | UDR = c; |
||
478 | #endif |
||
479 | } |
||
480 | |||
481 | |||
482 | |||
483 | //----------------------------------------------------------------------------- |
||
484 | // |
||
485 | void USART_puts (char *s) |
||
486 | { |
||
487 | #ifdef USART_INT |
||
488 | #else |
||
489 | while (*s) |
||
490 | { |
||
491 | USART_putc (*s); |
||
492 | s++; |
||
493 | } |
||
494 | #endif |
||
495 | } |
||
496 | |||
497 | //----------------------------------------------------------------------------- |
||
498 | // |
||
499 | void USART_puts_p (const char *s) |
||
500 | { |
||
501 | #ifdef USART_INT |
||
502 | #else |
||
503 | while (pgm_read_byte(s)) |
||
504 | { |
||
505 | USART_putc (pgm_read_byte(s)); |
||
506 | s++; |
||
507 | } |
||
508 | #endif |
||
509 | } |
||
510 | |||
511 | //----------------------------------------------------------------------------- |
||
512 | // |
||
513 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
||
514 | { |
||
515 | va_list ap; |
||
516 | uint16_t pt = 0; |
||
517 | uint8_t a,b,c; |
||
518 | uint8_t ptr = 0; |
||
519 | uint16_t tmpCRC = 0; |
||
520 | |||
521 | uint8_t *pdata = 0; |
||
522 | int len = 0; |
||
523 | |||
524 | txd_buffer[pt++] = '#'; // Start character |
||
525 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
||
526 | txd_buffer[pt++] = cmd; // Command |
||
527 | |||
528 | va_start(ap, numofbuffers); |
||
529 | if(numofbuffers) |
||
530 | { |
||
531 | pdata = va_arg (ap, uint8_t*); |
||
532 | len = va_arg (ap, int); |
||
533 | ptr = 0; |
||
534 | numofbuffers--; |
||
535 | } |
||
536 | |||
537 | while(len) |
||
538 | { |
||
539 | if(len) |
||
540 | { |
||
541 | a = pdata[ptr++]; |
||
542 | len--; |
||
543 | if((!len) && numofbuffers) |
||
544 | { |
||
545 | pdata = va_arg(ap, uint8_t*); |
||
546 | len = va_arg(ap, int); |
||
547 | ptr = 0; |
||
548 | numofbuffers--; |
||
549 | } |
||
550 | } |
||
551 | else |
||
552 | a = 0; |
||
553 | |||
554 | if(len) |
||
555 | { |
||
556 | b = pdata[ptr++]; |
||
557 | len--; |
||
558 | if((!len) && numofbuffers) |
||
559 | { |
||
560 | pdata = va_arg(ap, uint8_t*); |
||
561 | len = va_arg(ap, int); |
||
562 | ptr = 0; |
||
563 | numofbuffers--; |
||
564 | } |
||
565 | } |
||
566 | else |
||
567 | b = 0; |
||
568 | |||
569 | if(len) |
||
570 | { |
||
571 | c = pdata[ptr++]; |
||
572 | len--; |
||
573 | if((!len) && numofbuffers) |
||
574 | { |
||
575 | pdata = va_arg(ap, uint8_t*); |
||
576 | len = va_arg(ap, int); |
||
577 | ptr = 0; |
||
578 | numofbuffers--; |
||
579 | } |
||
580 | } |
||
581 | else |
||
582 | c = 0; |
||
583 | |||
584 | txd_buffer[pt++] = '=' + (a >> 2); |
||
585 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
586 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
587 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
||
588 | } |
||
589 | va_end(ap); |
||
590 | |||
591 | for(a = 0; a < pt; a++) |
||
592 | { |
||
593 | tmpCRC += txd_buffer[a]; |
||
594 | } |
||
595 | tmpCRC %= 4096; |
||
596 | txd_buffer[pt++] = '=' + tmpCRC / 64; |
||
597 | txd_buffer[pt++] = '=' + tmpCRC % 64; |
||
598 | txd_buffer[pt++] = '\r'; |
||
599 | txd_buffer[pt++] = '\r'; // Add 3.4.2015 Cebra |
||
600 | txd_buffer[pt++] = '\r'; // Add 3.4.2015 Cebra |
||
601 | |||
602 | txd_complete = FALSE; |
||
603 | #ifdef USART_INT |
||
604 | UDR = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
||
605 | #else |
||
606 | for(a = 0; a < pt; a++) |
||
607 | { |
||
608 | loop_until_bit_is_set(UCSRA, UDRE); |
||
609 | UDR = txd_buffer[a]; |
||
610 | } |
||
611 | txd_complete = TRUE; |
||
612 | #endif |
||
613 | } |
||
614 | |||
615 | //----------------------------------------------------------------------------- |
||
616 | // |
||
617 | void Decode64 (void) |
||
618 | { |
||
619 | uint8_t a,b,c,d; |
||
620 | uint8_t ptrIn = 3; |
||
621 | uint8_t ptrOut = 3; |
||
622 | uint8_t len = ReceivedBytes - 6; |
||
623 | |||
624 | while (len) |
||
625 | { |
||
626 | a = rxd_buffer[ptrIn++] - '='; |
||
627 | b = rxd_buffer[ptrIn++] - '='; |
||
628 | c = rxd_buffer[ptrIn++] - '='; |
||
629 | d = rxd_buffer[ptrIn++] - '='; |
||
630 | //if(ptrIn > ReceivedBytes - 3) break; |
||
631 | |||
632 | if (len--) |
||
633 | rxd_buffer[ptrOut++] = (a << 2) | (b >> 4); |
||
634 | else |
||
635 | break; |
||
636 | |||
637 | if (len--) |
||
638 | rxd_buffer[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2); |
||
639 | else |
||
640 | break; |
||
641 | |||
642 | if (len--) |
||
643 | rxd_buffer[ptrOut++] = ((c & 0x03) << 6) | d; |
||
644 | else |
||
645 | break; |
||
646 | } |
||
647 | pRxData = &rxd_buffer[3]; |
||
648 | RxDataLen = ptrOut - 3; |
||
649 | } |
||
650 | |||
651 | |||
652 | //----------------------------------------------------------------------------- |
||
653 | // |
||
654 | void SwitchToNC (void) |
||
655 | { |
||
656 | |||
657 | if(hardware == NC) |
||
658 | { |
||
659 | // switch to NC |
||
660 | USART_putc (0x1b); |
||
661 | USART_putc (0x1b); |
||
662 | USART_putc (0x55); |
||
663 | USART_putc (0xaa); |
||
664 | USART_putc (0x00); |
||
665 | current_hardware = NC; |
||
666 | _delay_ms (50); |
||
667 | } |
||
668 | } |
||
669 | |||
670 | //----------------------------------------------------------------------------- |
||
671 | // |
||
672 | |||
673 | |||
674 | //----------------------------------------------------------------------------- |
||
675 | // |
||
676 | void SwitchToWi232 (void) |
||
677 | { |
||
678 | |||
679 | // if(hardware == NC) |
||
680 | { |
||
681 | // switch to Wi232 |
||
682 | current_hardware = Wi232; |
||
683 | _delay_ms (50); |
||
684 | } |
||
685 | } |
||
686 | |||
687 | //----------------------------------------------------------------------------- |
||
688 | // |
||
689 | void SwitchToFC (void) |
||
690 | { |
||
691 | uint8_t cmd; |
||
692 | |||
693 | if (current_hardware == NC) |
||
694 | { |
||
695 | // switch to FC |
||
696 | cmd = 0x00; // 0 = FC, 1 = MK3MAG, 2 = MKGPS |
||
697 | SendOutData('u', ADDRESS_NC, 1, &cmd, 1); |
||
698 | current_hardware = FC; |
||
699 | _delay_ms (50); |
||
700 | } |
||
701 | } |
||
702 | |||
703 | //----------------------------------------------------------------------------- |
||
704 | // |
||
705 | void SwitchToMAG (void) |
||
706 | { |
||
707 | uint8_t cmd; |
||
708 | |||
709 | if (current_hardware == NC) |
||
710 | { |
||
711 | // switch to MK3MAG |
||
712 | cmd = 0x01; // 0 = FC, 1 = MK3MAG, 2 = MKGPS |
||
713 | SendOutData('u', ADDRESS_NC, 1, &cmd, 1); |
||
714 | current_hardware = MK3MAG; |
||
715 | _delay_ms (50); |
||
716 | } |
||
717 | } |
||
718 | |||
719 | //----------------------------------------------------------------------------- |
||
720 | // |
||
721 | void SwitchToGPS (void) |
||
722 | { |
||
723 | uint8_t cmd; |
||
724 | if (current_hardware == NC) |
||
725 | { |
||
726 | // switch to MKGPS |
||
727 | cmd = 0x02; // 0 = FC, 1 = MK3MAG, 2 = MKGPS |
||
728 | SendOutData('u', ADDRESS_NC, 1, &cmd, 1); |
||
729 | current_hardware = MKGPS; |
||
730 | _delay_ms (50); |
||
731 | } |
||
732 | } |