Rev 56 | Rev 61 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
12 | hbuss | 1 | /*####################################################################################### |
2 | MK3Mag 3D-Magnet sensor |
||
32 | holgerb | 3 | !!! THIS IS NOT FREE SOFTWARE !!! |
12 | hbuss | 4 | #######################################################################################*/ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
6 | // + Copyright (c) 05.2008 Holger Buss |
||
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
||
1 | ingob | 8 | // + Nur für den privaten Gebrauch |
12 | hbuss | 9 | // + www.MikroKopter.com |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
12 | hbuss | 12 | // + mit unserer Zustimmung zulässig |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
12 | hbuss | 16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
||
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
12 | hbuss | 20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
24 | // + eindeutig als Ursprung verlinkt werden |
||
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
27 | // + Benutzung auf eigene Gefahr |
||
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | holgerb | 32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
12 | hbuss | 33 | // + this list of conditions and the following disclaimer. |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
||
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
36 | // + from this software without specific prior written permission. |
||
32 | holgerb | 37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
12 | hbuss | 38 | // + for non-commercial use (directly or indirectly) |
32 | holgerb | 39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
12 | hbuss | 40 | // + with our written permission |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
||
42 | // + In case of doubt please contact: info@MikroKopter.de |
||
32 | holgerb | 43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
||
12 | hbuss | 45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
32 | holgerb | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
12 | hbuss | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | holgerb | 57 | #include <avr/io.h> |
58 | #include <avr/interrupt.h> |
||
59 | #include <avr/wdt.h> |
||
58 | killagreg | 60 | #include <avr/pgmspace.h> |
41 | killagreg | 61 | #include <stdarg.h> |
62 | #include <string.h> |
||
1 | ingob | 63 | #include "main.h" |
58 | killagreg | 64 | #include "menu.h" |
1 | ingob | 65 | #include "uart.h" |
32 | holgerb | 66 | #include "timer0.h" |
67 | #include "twislave.h" |
||
1 | ingob | 68 | |
37 | killagreg | 69 | // slave addresses |
70 | #define FC_ADDRESS 1 |
||
71 | #define NC_ADDRESS 2 |
||
72 | #define MK3MAG_ADDRESS 3 |
||
1 | ingob | 73 | |
32 | holgerb | 74 | #define FALSE 0 |
75 | #define TRUE 1 |
||
1 | ingob | 76 | |
32 | holgerb | 77 | // keep buffers as small as possible |
78 | #define TXD_BUFFER_LEN 100 |
||
79 | #define RXD_BUFFER_LEN 30 |
||
1 | ingob | 80 | |
32 | holgerb | 81 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
82 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
83 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
84 | volatile uint8_t txd_complete = TRUE; |
||
85 | volatile uint8_t ReceivedBytes = 0; |
||
41 | killagreg | 86 | volatile uint8_t *pRxData = 0; |
87 | volatile uint8_t RxDataLen = 0; |
||
1 | ingob | 88 | |
37 | killagreg | 89 | // send flags |
90 | #define RQST_VERSION_INFO 0x01 |
||
91 | #define RQST_DEBUG_DATA 0x02 |
||
92 | #define RQST_DEBUG_LABEL 0x04 |
||
93 | #define RQST_COMPASS_HEADING 0x08 |
||
94 | #define RQST_EXTERN_CTRL 0x10 |
||
52 | killagreg | 95 | #define RQST_DISPLAY_DATA 0x20 |
3 | ingob | 96 | |
37 | killagreg | 97 | |
32 | holgerb | 98 | uint8_t RequestFlags = 0x00; |
99 | uint8_t RequestDebugLabel = 0; |
||
41 | killagreg | 100 | uint8_t ConfirmFrame = 0; |
58 | killagreg | 101 | |
52 | killagreg | 102 | uint8_t DisplayLine = 0; |
58 | killagreg | 103 | |
52 | killagreg | 104 | uint16_t PC_Connected = 0; |
105 | uint16_t FC_Connected = 0; |
||
32 | holgerb | 106 | |
107 | |||
108 | DebugOut_t DebugOut; |
||
109 | ExternData_t ExternData; |
||
110 | ExternControl_t ExternControl; |
||
41 | killagreg | 111 | UART_VersionInfo_t UART_VersionInfo; |
32 | holgerb | 112 | |
41 | killagreg | 113 | uint16_t DebugData_Timer; |
114 | uint16_t DebugData_Interval = 500; |
||
32 | holgerb | 115 | |
58 | killagreg | 116 | const prog_uint8_t ANALOG_LABEL[32][16] = |
1 | ingob | 117 | { |
37 | killagreg | 118 | //1234567890123456 |
119 | "Magnet X ", //0 |
||
120 | "Magnet Y ", |
||
121 | "Magnet Z ", |
||
122 | "RawMagnet X ", |
||
123 | "RawMagnet Y ", |
||
124 | "RawMagnet Z ", //5 |
||
125 | "Attitude Nick ", |
||
126 | "Attitude Roll ", |
||
127 | "Magnet X Offset ", |
||
128 | "Magnet X Range ", |
||
129 | "Magnet Y Offset ", //10 |
||
130 | "Magnet Y Range ", |
||
131 | "Magnet Z Offset ", |
||
132 | "Magnet Z Range ", |
||
133 | "Calstate ", |
||
134 | "Heading ", //15 |
||
135 | "User0 ", |
||
136 | "User1 ", |
||
137 | "Acc X ", |
||
138 | "Acc Y ", |
||
139 | "Acc Z ", //20 |
||
140 | "RawAcc X ", |
||
141 | "RawAcc Y ", |
||
142 | "RawAcc Z ", |
||
143 | "Acc X Offset ", |
||
144 | "Acc Y Offset ", //25 |
||
145 | "Acc Z Offset ", |
||
55 | killagreg | 146 | "Heading X ", |
147 | "Heading Y ", |
||
148 | "Attitude Source ", |
||
37 | killagreg | 149 | "I2C Error ", //30 |
150 | "I2C Okay " |
||
1 | ingob | 151 | }; |
152 | |||
153 | |||
32 | holgerb | 154 | /****************************************************************/ |
155 | /* Initialization of the USART0 */ |
||
156 | /****************************************************************/ |
||
157 | void USART0_Init (void) |
||
158 | { |
||
41 | killagreg | 159 | uint8_t sreg = SREG; |
32 | holgerb | 160 | uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1); |
7 | hbuss | 161 | |
32 | holgerb | 162 | // disable all interrupts before configuration |
163 | cli(); |
||
164 | |||
165 | // disable RX-Interrupt |
||
166 | UCSR0B &= ~(1 << RXCIE0); |
||
167 | // disable TX-Interrupt |
||
168 | UCSR0B &= ~(1 << TXCIE0); |
||
169 | |||
170 | // set direction of RXD0 and TXD0 pins |
||
171 | |||
172 | // set RXD0 (PD0) as an input pin tristate |
||
173 | DDRD &= ~(1 << DDD0); |
||
174 | PORTD &= ~(1 << PORTD0); |
||
175 | // set TXD0 (PD1) as an output pin |
||
176 | DDRD |= (1 << DDD1); |
||
177 | PORTD &= ~(1 << PORTD1); |
||
178 | |||
179 | |||
180 | // USART0 Baud Rate Register |
||
181 | // set clock divider |
||
182 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
183 | UBRR0L = (uint8_t)ubrr; |
||
184 | |||
37 | killagreg | 185 | // USART0 Control and Status Register A, B, C |
32 | holgerb | 186 | |
187 | // enable double speed operation |
||
188 | UCSR0A |= (1 << U2X0); |
||
189 | |||
190 | // set asynchronous mode |
||
191 | UCSR0C &= ~(1 << UMSEL01); |
||
192 | UCSR0C &= ~(1 << UMSEL00); |
||
193 | // no parity |
||
194 | UCSR0C &= ~(1 << UPM01); |
||
195 | UCSR0C &= ~(1 << UPM00); |
||
196 | // 1 stop bit |
||
197 | UCSR0C &= ~(1 << USBS0); |
||
198 | // 8-bit |
||
199 | UCSR0B &= ~(1 << UCSZ02); |
||
200 | UCSR0C |= (1 << UCSZ01); |
||
201 | UCSR0C |= (1 << UCSZ00); |
||
202 | |||
203 | // enable receiver and transmitter |
||
204 | UCSR0B |= (1 << RXEN0); |
||
205 | UCSR0B |= (1 << TXEN0); |
||
206 | |||
207 | // flush receive buffer |
||
208 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
||
209 | |||
210 | // enable RX-Interrupt |
||
211 | UCSR0B |= (1 << RXCIE0); |
||
212 | // enable TX-Interrupt |
||
213 | UCSR0B |= (1 << TXCIE0); |
||
214 | |||
41 | killagreg | 215 | // initialize the debug timer |
216 | DebugData_Timer = SetDelay(DebugData_Interval); |
||
217 | |||
218 | // unlock rxd_buffer |
||
32 | holgerb | 219 | rxd_buffer_locked = FALSE; |
41 | killagreg | 220 | pRxData = 0; |
221 | RxDataLen = 0; |
||
222 | |||
223 | // no bytes to send |
||
32 | holgerb | 224 | txd_complete = TRUE; |
225 | |||
226 | |||
47 | killagreg | 227 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
228 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
||
229 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
||
230 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
231 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
32 | holgerb | 232 | |
52 | killagreg | 233 | // send version info at startup |
32 | holgerb | 234 | USART0_putchar ('\n'); |
235 | USART0_putchar ('C'); |
||
236 | USART0_putchar ('P'); |
||
237 | USART0_putchar (':'); |
||
238 | USART0_putchar ('V'); |
||
53 | killagreg | 239 | USART0_putchar ('0' + VERSION_MAJOR); |
32 | holgerb | 240 | USART0_putchar ('.'); |
53 | killagreg | 241 | USART0_putchar ('0' + VERSION_MINOR/10); |
242 | USART0_putchar ('0' + VERSION_MINOR%10); |
||
41 | killagreg | 243 | USART0_putchar ('a' + VERSION_PATCH); |
32 | holgerb | 244 | USART0_putchar ('\n'); |
41 | killagreg | 245 | |
246 | // restore global interrupt flags |
||
247 | SREG = sreg; |
||
1 | ingob | 248 | } |
249 | |||
32 | holgerb | 250 | // --------------------------------------------------------------------------------- |
251 | void USART0_EnableTXD(void) |
||
1 | ingob | 252 | { |
32 | holgerb | 253 | DDRD |= (1<<DDD1); // set TXD pin as output |
254 | PORTD &= ~(1 << PORTD1); |
||
255 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
||
55 | killagreg | 256 | //changing the interrupt flag yields to strange effects |
257 | //UCSR0B |= (1 << TXCIE0); // enable TX-Interrupt |
||
1 | ingob | 258 | } |
259 | |||
32 | holgerb | 260 | // --------------------------------------------------------------------------------- |
261 | void USART0_DisableTXD(void) |
||
1 | ingob | 262 | { |
32 | holgerb | 263 | while(!txd_complete){ }; |
55 | killagreg | 264 | //changing the interrupt flag yields to strange effects |
265 | //UCSR0B &= ~(1 << TXCIE0); // disable TX-Interrupt |
||
266 | UCSR0B &= ~(1 << TXEN0); // disable TX in USART |
||
32 | holgerb | 267 | DDRD &= ~(1<<DDD1); // set TXD pin as input |
268 | PORTD &= ~(1 << PORTD1); |
||
269 | } |
||
1 | ingob | 270 | |
32 | holgerb | 271 | /****************************************************************/ |
272 | /* USART0 transmitter ISR */ |
||
273 | /****************************************************************/ |
||
274 | ISR(USART_TX_vect) |
||
275 | { |
||
276 | static uint16_t ptr_txd_buffer = 0; |
||
277 | uint8_t tmp_tx; |
||
278 | if(!txd_complete) // transmission not completed |
||
279 | { |
||
280 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
||
281 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
||
282 | // if terminating character or end of txd buffer was reached |
||
283 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
||
284 | { |
||
285 | ptr_txd_buffer = 0; // reset txd pointer |
||
286 | txd_complete = TRUE; // stop transmission |
||
287 | } |
||
288 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
||
289 | } |
||
290 | // transmission completed |
||
291 | else ptr_txd_buffer = 0; |
||
1 | ingob | 292 | } |
293 | |||
32 | holgerb | 294 | /****************************************************************/ |
295 | /* USART0 receiver ISR */ |
||
296 | /****************************************************************/ |
||
297 | ISR(USART_RX_vect) |
||
1 | ingob | 298 | { |
32 | holgerb | 299 | static uint16_t crc; |
300 | uint8_t crc1, crc2; |
||
301 | uint8_t c; |
||
302 | static uint8_t ptr_rxd_buffer = 0; |
||
1 | ingob | 303 | |
32 | holgerb | 304 | c = UDR0; // catch the received byte |
1 | ingob | 305 | |
32 | holgerb | 306 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
1 | ingob | 307 | |
32 | holgerb | 308 | // the rxd buffer is unlocked |
309 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
||
310 | { |
||
311 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
312 | crc = c; // init crc |
||
313 | } |
||
314 | #if 0 |
||
315 | else if (ptr_rxd_buffer == 1) // handle address |
||
316 | { |
||
317 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
318 | crc += c; // update crc |
||
319 | } |
||
320 | #endif |
||
321 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
||
322 | { |
||
323 | if(c != '\r') // no termination character |
||
324 | { |
||
325 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
326 | crc += c; // update crc |
||
327 | } |
||
328 | else // termination character was received |
||
329 | { |
||
330 | // the last 2 bytes are no subject for checksum calculation |
||
331 | // they are the checksum itself |
||
332 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
||
333 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
||
334 | // calculate checksum from transmitted data |
||
335 | crc %= 4096; |
||
336 | crc1 = '=' + crc / 64; |
||
337 | crc2 = '=' + crc % 64; |
||
338 | // compare checksum to transmitted checksum bytes |
||
339 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
||
340 | { // checksum valid |
||
41 | killagreg | 341 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
342 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
||
32 | holgerb | 343 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
344 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
||
345 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
||
346 | } |
||
347 | else |
||
348 | { // checksum invalid |
||
349 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
350 | } |
||
351 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
352 | } |
||
353 | } |
||
354 | else // rxd buffer overrun |
||
355 | { |
||
356 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
357 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
358 | } |
||
1 | ingob | 359 | } |
360 | |||
361 | |||
362 | // -------------------------------------------------------------------------- |
||
32 | holgerb | 363 | void AddCRC(uint16_t datalen) |
1 | ingob | 364 | { |
32 | holgerb | 365 | uint16_t tmpCRC = 0, i; |
366 | for(i = 0; i < datalen; i++) |
||
367 | { |
||
368 | tmpCRC += txd_buffer[i]; |
||
369 | } |
||
370 | tmpCRC %= 4096; |
||
371 | txd_buffer[i++] = '=' + tmpCRC / 64; |
||
372 | txd_buffer[i++] = '=' + tmpCRC % 64; |
||
373 | txd_buffer[i++] = '\r'; |
||
374 | txd_complete = FALSE; |
||
375 | UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR) |
||
1 | ingob | 376 | } |
377 | |||
378 | |||
379 | |||
32 | holgerb | 380 | // -------------------------------------------------------------------------- |
46 | killagreg | 381 | void SendOutData(uint8_t cmd, uint8_t address, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
1 | ingob | 382 | { |
41 | killagreg | 383 | va_list ap; |
32 | holgerb | 384 | uint16_t pt = 0; |
385 | uint8_t a,b,c; |
||
386 | uint8_t ptr = 0; |
||
387 | |||
41 | killagreg | 388 | uint8_t *pdata = 0; |
389 | int len = 0; |
||
390 | |||
46 | killagreg | 391 | txd_buffer[pt++] = '#'; // Start character |
392 | txd_buffer[pt++] = 'a' + address; // Address (a=0; b=1,...) |
||
393 | txd_buffer[pt++] = cmd; // Command |
||
32 | holgerb | 394 | |
41 | killagreg | 395 | va_start(ap, numofbuffers); |
42 | killagreg | 396 | if(numofbuffers) |
41 | killagreg | 397 | { |
398 | pdata = va_arg(ap, uint8_t*); |
||
399 | len = va_arg(ap, int); |
||
400 | ptr = 0; |
||
42 | killagreg | 401 | numofbuffers--; |
41 | killagreg | 402 | } |
403 | |||
32 | holgerb | 404 | while(len) |
405 | { |
||
41 | killagreg | 406 | if(len) |
407 | { |
||
408 | a = pdata[ptr++]; |
||
409 | len--; |
||
410 | if((!len) && numofbuffers) |
||
411 | { |
||
412 | pdata = va_arg(ap, uint8_t*); |
||
413 | len = va_arg(ap, int); |
||
414 | ptr = 0; |
||
415 | numofbuffers--; |
||
416 | } |
||
417 | } |
||
418 | else a = 0; |
||
419 | if(len) |
||
420 | { |
||
421 | b = pdata[ptr++]; |
||
422 | len--; |
||
423 | if((!len) && numofbuffers) |
||
424 | { |
||
425 | pdata = va_arg(ap, uint8_t*); |
||
426 | len = va_arg(ap, int); |
||
427 | ptr = 0; |
||
428 | numofbuffers--; |
||
429 | } |
||
430 | } |
||
431 | else b = 0; |
||
432 | if(len) |
||
433 | { |
||
434 | c = pdata[ptr++]; |
||
435 | len--; |
||
436 | if((!len) && numofbuffers) |
||
437 | { |
||
438 | pdata = va_arg(ap, uint8_t*); |
||
439 | len = va_arg(ap, int); |
||
440 | ptr = 0; |
||
441 | numofbuffers--; |
||
442 | } |
||
443 | } |
||
444 | else c = 0; |
||
32 | holgerb | 445 | txd_buffer[pt++] = '=' + (a >> 2); |
446 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
447 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
448 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
||
449 | } |
||
41 | killagreg | 450 | va_end(ap); |
32 | holgerb | 451 | AddCRC(pt); // add checksum after data block and initates the transmission |
1 | ingob | 452 | } |
453 | |||
32 | holgerb | 454 | |
1 | ingob | 455 | // -------------------------------------------------------------------------- |
41 | killagreg | 456 | void Decode64(void) |
1 | ingob | 457 | { |
32 | holgerb | 458 | uint8_t a,b,c,d; |
459 | uint8_t x,y,z; |
||
41 | killagreg | 460 | uint8_t ptrIn = 3; |
461 | uint8_t ptrOut = 3; |
||
462 | uint8_t len = ReceivedBytes - 6; |
||
463 | |||
32 | holgerb | 464 | while(len) |
465 | { |
||
466 | a = rxd_buffer[ptrIn++] - '='; |
||
467 | b = rxd_buffer[ptrIn++] - '='; |
||
468 | c = rxd_buffer[ptrIn++] - '='; |
||
469 | d = rxd_buffer[ptrIn++] - '='; |
||
41 | killagreg | 470 | //if(ptrIn > ReceivedBytes - 3) break; |
1 | ingob | 471 | |
32 | holgerb | 472 | x = (a << 2) | (b >> 4); |
473 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
474 | z = ((c & 0x03) << 6) | d; |
||
475 | |||
41 | killagreg | 476 | if(len--) rxd_buffer[ptrOut++] = x; else break; |
477 | if(len--) rxd_buffer[ptrOut++] = y; else break; |
||
478 | if(len--) rxd_buffer[ptrOut++] = z; else break; |
||
32 | holgerb | 479 | } |
41 | killagreg | 480 | pRxData = &rxd_buffer[3]; |
481 | RxDataLen = ptrOut - 3; |
||
17 | holgerb | 482 | } |
483 | |||
32 | holgerb | 484 | |
41 | killagreg | 485 | |
32 | holgerb | 486 | // -------------------------------------------------------------------------- |
487 | int16_t USART0_putchar (int8_t c) |
||
1 | ingob | 488 | { |
32 | holgerb | 489 | // if tx is not enabled return immediatly |
490 | if(!(UCSR0B & (1 << TXEN0))) return (0); |
||
491 | if (c == '\n') USART0_putchar('\r'); |
||
492 | // wait until previous character was send |
||
493 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
494 | // send character |
||
495 | UDR0 = c; |
||
496 | return (0); |
||
1 | ingob | 497 | } |
498 | |||
499 | |||
32 | holgerb | 500 | // -------------------------------------------------------------------------- |
501 | void USART0_ProcessRxData(void) |
||
1 | ingob | 502 | { |
32 | holgerb | 503 | // if data in the rxd buffer are not locked immediately return |
504 | if(!rxd_buffer_locked) return; |
||
1 | ingob | 505 | |
46 | killagreg | 506 | Decode64(); // decode data block in rxd_buffer |
507 | |||
45 | ingob | 508 | switch(rxd_buffer[1]-'a') // check Slave Address |
32 | holgerb | 509 | { |
44 | killagreg | 510 | case MK3MAG_ADDRESS: |
46 | killagreg | 511 | |
44 | killagreg | 512 | switch(rxd_buffer[2]) // check for CmdID |
513 | { |
||
514 | case 'w': // old style |
||
515 | case 'k': // Attitude info from FC |
||
516 | memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData)); |
||
517 | RequestFlags |= RQST_COMPASS_HEADING; |
||
518 | AttitudeSource = ATTITUDE_SOURCE_UART; |
||
519 | Orientation = ExternData.Orientation; |
||
520 | FC_Connected = 255; |
||
521 | break; |
||
32 | holgerb | 522 | |
44 | killagreg | 523 | default: |
524 | // unsupported command |
||
525 | break; |
||
46 | killagreg | 526 | } // case MK3MAG_ADDRESS: |
32 | holgerb | 527 | |
52 | killagreg | 528 | default: // any Slave Address |
46 | killagreg | 529 | |
44 | killagreg | 530 | switch(rxd_buffer[2]) // check for CmdID |
531 | { |
||
532 | case 'b': // extern control |
||
533 | memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl)); |
||
534 | #define KEY1 0x01 |
||
535 | #define KEY2 0x02 |
||
536 | #define KEY3 0x04 |
||
537 | #define KEY4 0x08 |
||
538 | #define KEY5 0x10 |
||
539 | // use right arrow at display for switching the calstate |
||
540 | if(ExternControl.RemoteButtons & KEY2) |
||
541 | { |
||
542 | ExternData.CalState++; |
||
543 | if(ExternData.CalState == 6) ExternData.CalState = 0; |
||
544 | } |
||
545 | ConfirmFrame = ExternControl.Frame; |
||
52 | killagreg | 546 | PC_Connected = 2000; |
44 | killagreg | 547 | break; |
37 | killagreg | 548 | |
44 | killagreg | 549 | case 'd': // request for the debug data |
550 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
||
45 | ingob | 551 | if(DebugData_Interval>0) RequestFlags |= RQST_DEBUG_DATA; |
52 | killagreg | 552 | PC_Connected = 2000; |
44 | killagreg | 553 | break; |
32 | holgerb | 554 | |
44 | killagreg | 555 | case 'v': // request version and board release |
556 | RequestFlags |= RQST_VERSION_INFO; |
||
52 | killagreg | 557 | PC_Connected = 2000; |
44 | killagreg | 558 | break; |
32 | holgerb | 559 | |
52 | killagreg | 560 | case 'h': // request display data |
58 | killagreg | 561 | RemoteKeys |= pRxData[0]; |
562 | if(RemoteKeys) DisplayLine = 0; |
||
52 | killagreg | 563 | RequestFlags |= RQST_DISPLAY_DATA; |
564 | PC_Connected = 2000; |
||
49 | ingob | 565 | break; |
566 | |||
44 | killagreg | 567 | case 'a':// Labels of the Analog Debug outputs |
568 | RequestDebugLabel = pRxData[0]; |
||
569 | RequestFlags |= RQST_DEBUG_LABEL; |
||
52 | killagreg | 570 | PC_Connected = 2000; |
44 | killagreg | 571 | break; |
572 | |||
573 | case 'g':// get extern control data |
||
574 | RequestFlags |= RQST_EXTERN_CTRL; |
||
52 | killagreg | 575 | PC_Connected = 2000; |
44 | killagreg | 576 | break; |
577 | |||
578 | default: |
||
579 | // unsupported command |
||
580 | break; |
||
581 | } |
||
582 | break; // default: |
||
32 | holgerb | 583 | } |
584 | // unlock the rxd buffer after processing |
||
41 | killagreg | 585 | pRxData = 0; |
586 | RxDataLen = 0; |
||
32 | holgerb | 587 | rxd_buffer_locked = FALSE; |
1 | ingob | 588 | } |
589 | |||
590 | |||
32 | holgerb | 591 | |
1 | ingob | 592 | //--------------------------------------------------------------------------------------------- |
32 | holgerb | 593 | void USART0_TransmitTxData(void) |
1 | ingob | 594 | { |
32 | holgerb | 595 | if(!(UCSR0B & (1 << TXEN0))) return; |
596 | |||
597 | if(!txd_complete) return; |
||
598 | |||
41 | killagreg | 599 | if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete) |
37 | killagreg | 600 | { |
58 | killagreg | 601 | uint8_t label[16]; |
602 | memcpy_P(label, ANALOG_LABEL[RequestDebugLabel], 16); // read lable from flash to sram buffer |
||
603 | SendOutData('A', MK3MAG_ADDRESS, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), label, 16); |
||
41 | killagreg | 604 | RequestDebugLabel = 0xFF; |
605 | RequestFlags &= ~RQST_DEBUG_LABEL; |
||
37 | killagreg | 606 | } |
32 | holgerb | 607 | |
41 | killagreg | 608 | if(ConfirmFrame && txd_complete) |
37 | killagreg | 609 | { |
44 | killagreg | 610 | SendOutData('B', MK3MAG_ADDRESS, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame)); |
41 | killagreg | 611 | ConfirmFrame = 0; |
32 | holgerb | 612 | } |
613 | |||
56 | killagreg | 614 | if(( ((DebugData_Interval > 0) && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete) |
37 | killagreg | 615 | { |
41 | killagreg | 616 | SetDebugValues(); |
44 | killagreg | 617 | SendOutData('D', MK3MAG_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut)); |
41 | killagreg | 618 | DebugData_Timer = SetDelay(DebugData_Interval); |
619 | RequestFlags &= ~RQST_DEBUG_DATA; |
||
37 | killagreg | 620 | } |
32 | holgerb | 621 | |
52 | killagreg | 622 | if((RequestFlags & RQST_DISPLAY_DATA) && txd_complete) |
623 | { |
||
58 | killagreg | 624 | LCD_PrintMenu(); |
625 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), &DisplayBuff[DisplayLine * 20], 20); |
||
52 | killagreg | 626 | DisplayLine++; |
627 | if(DisplayLine >= 4) DisplayLine = 0; |
||
628 | RequestFlags &= ~RQST_DISPLAY_DATA; |
||
629 | } |
||
41 | killagreg | 630 | |
631 | if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete) |
||
32 | holgerb | 632 | { |
44 | killagreg | 633 | SendOutData('G', MK3MAG_ADDRESS, 1, (uint8_t *) &ExternControl,sizeof(ExternControl)); |
37 | killagreg | 634 | RequestFlags &= ~RQST_EXTERN_CTRL; |
32 | holgerb | 635 | } |
636 | |||
41 | killagreg | 637 | if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete) |
32 | holgerb | 638 | { |
54 | killagreg | 639 | SendOutData('K', FC_ADDRESS, 1, (uint8_t *) &Heading, sizeof(Heading)); // send compass heading to FC |
37 | killagreg | 640 | RequestFlags &= ~RQST_COMPASS_HEADING; |
641 | } |
||
41 | killagreg | 642 | |
643 | if((RequestFlags & RQST_VERSION_INFO) && txd_complete) |
||
644 | { |
||
44 | killagreg | 645 | SendOutData('V', MK3MAG_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
41 | killagreg | 646 | RequestFlags &= ~RQST_VERSION_INFO; |
647 | } |
||
1 | ingob | 648 | } |
649 | |||
32 | holgerb | 650 | |
651 | void USART0_Print(int8_t *msg) |
||
17 | holgerb | 652 | { |
32 | holgerb | 653 | uint8_t i = 0; |
37 | killagreg | 654 | while(msg[i] != 0) |
655 | { |
||
656 | USART0_putchar(msg[i++]); |
||
657 | } |
||
17 | holgerb | 658 | } |