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