Rev 28 | 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 |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
19 | killagreg | 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. |
||
19 | killagreg | 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) |
19 | killagreg | 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 |
||
19 | killagreg | 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 |
||
19 | killagreg | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
12 | hbuss | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | killagreg | 57 | #include <avr/io.h> |
58 | #include <avr/interrupt.h> |
||
59 | #include <avr/wdt.h> |
||
1 | ingob | 60 | #include "main.h" |
61 | #include "uart.h" |
||
19 | killagreg | 62 | #include "timer0.h" |
63 | #include "twislave.h" |
||
1 | ingob | 64 | |
65 | |||
19 | killagreg | 66 | #define FALSE 0 |
67 | #define TRUE 1 |
||
1 | ingob | 68 | |
29 | killagreg | 69 | // keep buffers as small as possible |
23 | killagreg | 70 | #define TXD_BUFFER_LEN 100 |
29 | killagreg | 71 | #define RXD_BUFFER_LEN 30 |
1 | ingob | 72 | |
19 | killagreg | 73 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
74 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
75 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
76 | volatile uint8_t txd_complete = TRUE; |
||
77 | volatile uint8_t ReceivedBytes = 0; |
||
1 | ingob | 78 | |
23 | killagreg | 79 | #define VERSION_INFO 0x01 |
80 | #define DEBUG_DATA 0x02 |
||
81 | #define DEBUG_LABEL 0x04 |
||
82 | #define COMPASS_HEADING 0x08 |
||
21 | killagreg | 83 | |
23 | killagreg | 84 | uint8_t RequestFlags = 0x00; |
85 | uint8_t RequestDebugLabel = 0; |
||
21 | killagreg | 86 | |
29 | killagreg | 87 | uint8_t PC_Connected = 0; |
88 | uint8_t FC_Connected = 0; |
||
3 | ingob | 89 | |
19 | killagreg | 90 | uint8_t MySlaveAddr = 0; |
1 | ingob | 91 | |
19 | killagreg | 92 | |
23 | killagreg | 93 | struct DebugOut_t DebugOut; |
94 | struct ExternData_t ExternData; |
||
95 | struct ExternControl_t ExternControl; |
||
96 | struct VersionInfo_t VersionInfo; |
||
19 | killagreg | 97 | |
98 | uint16_t Debug_Timer; |
||
99 | |||
100 | const uint8_t ANALOG_LABEL[32][16] = |
||
1 | ingob | 101 | { |
7 | hbuss | 102 | //1234567890123456 |
19 | killagreg | 103 | "Magnet X ", //0 |
104 | "Magnet Y ", |
||
7 | hbuss | 105 | "Magnet Z ", |
28 | killagreg | 106 | "RawMagnet X ", |
107 | "RawMagnet Y ", |
||
108 | "RawMagnet Z ", //5 |
||
19 | killagreg | 109 | "Attitude Nick ", |
110 | "Attitude Roll ", |
||
28 | killagreg | 111 | "Magnet X Offset ", |
112 | "Magnet X Range ", |
||
113 | "Magnet Y Offset ", //10 |
||
114 | "Magnet Y Range ", |
||
115 | "Magnet Z Offset ", |
||
116 | "Magnet Z Range ", |
||
7 | hbuss | 117 | "Calstate ", |
21 | killagreg | 118 | "Heading ", //15 |
5 | hbuss | 119 | "User0 ", |
120 | "User1 ", |
||
28 | killagreg | 121 | "Acc X ", |
122 | "Acc Y ", |
||
123 | "Acc Z ", //20 |
||
124 | "RawAcc X ", |
||
125 | "RawAcc Y ", |
||
126 | "RawAcc Z ", |
||
127 | "Acc X Offset ", |
||
128 | "Acc Y Offset ", //25 |
||
129 | "Acc Z Offset ", |
||
7 | hbuss | 130 | "Analog27 ", |
131 | "Analog28 ", |
||
132 | "Analog29 ", |
||
26 | killagreg | 133 | "I2C Error ", //30 |
134 | "I2C Okay " |
||
1 | ingob | 135 | }; |
136 | |||
137 | |||
19 | killagreg | 138 | /****************************************************************/ |
139 | /* Initialization of the USART0 */ |
||
140 | /****************************************************************/ |
||
141 | void USART0_Init (void) |
||
142 | { |
||
29 | killagreg | 143 | uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1); |
19 | killagreg | 144 | |
145 | // disable all interrupts before configuration |
||
146 | cli(); |
||
147 | |||
148 | // disable RX-Interrupt |
||
149 | UCSR0B &= ~(1 << RXCIE0); |
||
150 | // disable TX-Interrupt |
||
151 | UCSR0B &= ~(1 << TXCIE0); |
||
152 | |||
153 | // set direction of RXD0 and TXD0 pins |
||
154 | |||
22 | killagreg | 155 | // set RXD0 (PD0) as an input pin tristate |
19 | killagreg | 156 | DDRD &= ~(1 << DDD0); |
22 | killagreg | 157 | PORTD &= ~(1 << PORTD0); |
19 | killagreg | 158 | // set TXD0 (PD1) as an output pin |
159 | DDRD |= (1 << DDD1); |
||
22 | killagreg | 160 | PORTD &= ~(1 << PORTD1); |
19 | killagreg | 161 | |
22 | killagreg | 162 | |
19 | killagreg | 163 | // USART0 Baud Rate Register |
164 | // set clock divider |
||
165 | UBRR0H = (uint8_t)(ubrr >> 8); |
||
166 | UBRR0L = (uint8_t)ubrr; |
||
167 | |||
168 | // USART0 Control and Status Register A, B, C |
||
169 | |||
21 | killagreg | 170 | // enable double speed operation |
19 | killagreg | 171 | UCSR0A |= (1 << U2X0); |
22 | killagreg | 172 | |
19 | killagreg | 173 | // set asynchronous mode |
174 | UCSR0C &= ~(1 << UMSEL01); |
||
175 | UCSR0C &= ~(1 << UMSEL00); |
||
176 | // no parity |
||
177 | UCSR0C &= ~(1 << UPM01); |
||
178 | UCSR0C &= ~(1 << UPM00); |
||
179 | // 1 stop bit |
||
180 | UCSR0C &= ~(1 << USBS0); |
||
181 | // 8-bit |
||
182 | UCSR0B &= ~(1 << UCSZ02); |
||
183 | UCSR0C |= (1 << UCSZ01); |
||
184 | UCSR0C |= (1 << UCSZ00); |
||
185 | |||
22 | killagreg | 186 | // enable receiver and transmitter |
187 | UCSR0B |= (1 << RXEN0); |
||
188 | UCSR0B |= (1 << TXEN0); |
||
189 | |||
19 | killagreg | 190 | // flush receive buffer |
191 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
||
192 | |||
193 | // enable RX-Interrupt |
||
194 | UCSR0B |= (1 << RXCIE0); |
||
195 | // enable TX-Interrupt |
||
196 | UCSR0B |= (1 << TXCIE0); |
||
197 | |||
198 | rxd_buffer_locked = FALSE; |
||
199 | txd_complete = TRUE; |
||
200 | |||
201 | |||
202 | VersionInfo.Major = VERSION_MAJOR; |
||
203 | VersionInfo.Minor = VERSION_MINOR; |
||
204 | VersionInfo.PCCompatible = VERSION_COMPATIBLE; |
||
205 | |||
22 | killagreg | 206 | |
207 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
||
29 | killagreg | 208 | USART0_putchar ('\n'); |
209 | USART0_putchar ('C'); |
||
210 | USART0_putchar ('P'); |
||
211 | USART0_putchar (':'); |
||
212 | USART0_putchar ('V'); |
||
213 | USART0_putchar (0x30 + VERSION_MAJOR); |
||
214 | USART0_putchar ('.'); |
||
215 | USART0_putchar (0x30 + VERSION_MINOR/10); |
||
216 | USART0_putchar (0x30 + VERSION_MINOR%10); |
||
217 | USART0_putchar ('\n'); |
||
19 | killagreg | 218 | } |
219 | |||
220 | // --------------------------------------------------------------------------------- |
||
221 | void USART0_EnableTXD(void) |
||
1 | ingob | 222 | { |
22 | killagreg | 223 | DDRD |= (1<<DDD1); // set TXD pin as output |
224 | PORTD &= ~(1 << PORTD1); |
||
19 | killagreg | 225 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
22 | killagreg | 226 | UCSR0B |= (1 << TXCIE0); // disable TX-Interrupt |
1 | ingob | 227 | } |
228 | |||
19 | killagreg | 229 | // --------------------------------------------------------------------------------- |
230 | void USART0_DisableTXD(void) |
||
1 | ingob | 231 | { |
21 | killagreg | 232 | while(!txd_complete){ }; |
233 | |||
22 | killagreg | 234 | UCSR0B &= ~(1 << TXCIE0); // disable TX-Interrupt |
19 | killagreg | 235 | UCSR0B &= ~(1 << TXEN0); // disable TXD in USART |
236 | DDRD &= ~(1<<DDD1); // set TXD pin as input |
||
22 | killagreg | 237 | PORTD &= ~(1 << PORTD1); |
1 | ingob | 238 | } |
239 | |||
19 | killagreg | 240 | /****************************************************************/ |
241 | /* USART0 transmitter ISR */ |
||
242 | /****************************************************************/ |
||
243 | ISR(USART_TX_vect) |
||
1 | ingob | 244 | { |
19 | killagreg | 245 | static uint16_t ptr_txd_buffer = 0; |
246 | uint8_t tmp_tx; |
||
247 | if(!txd_complete) // transmission not completed |
||
248 | { |
||
249 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
||
250 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
||
251 | // if terminating character or end of txd buffer was reached |
||
252 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
||
253 | { |
||
254 | ptr_txd_buffer = 0; // reset txd pointer |
||
21 | killagreg | 255 | txd_complete = TRUE; // stop transmission |
19 | killagreg | 256 | } |
257 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
||
258 | } |
||
259 | // transmission completed |
||
260 | else ptr_txd_buffer = 0; |
||
261 | } |
||
1 | ingob | 262 | |
19 | killagreg | 263 | /****************************************************************/ |
264 | /* USART0 receiver ISR */ |
||
265 | /****************************************************************/ |
||
266 | ISR(USART_RX_vect) |
||
267 | { |
||
268 | static uint16_t crc; |
||
269 | uint8_t crc1, crc2; |
||
270 | uint8_t c; |
||
21 | killagreg | 271 | static uint8_t ptr_rxd_buffer = 0; |
1 | ingob | 272 | |
19 | killagreg | 273 | c = UDR0; // catch the received byte |
1 | ingob | 274 | |
19 | killagreg | 275 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
276 | |||
277 | // the rxd buffer is unlocked |
||
278 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
||
279 | { |
||
280 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
281 | crc = c; // init crc |
||
282 | } |
||
283 | #if 0 |
||
284 | else if (ptr_rxd_buffer == 1) // handle address |
||
285 | { |
||
286 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
287 | crc += c; // update crc |
||
288 | } |
||
289 | #endif |
||
290 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
||
291 | { |
||
292 | if(c != '\r') // no termination character |
||
293 | { |
||
294 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
295 | crc += c; // update crc |
||
296 | } |
||
297 | else // termination character was received |
||
298 | { |
||
299 | // the last 2 bytes are no subject for checksum calculation |
||
300 | // they are the checksum itself |
||
301 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
||
302 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
||
303 | // calculate checksum from transmitted data |
||
304 | crc %= 4096; |
||
305 | crc1 = '=' + crc / 64; |
||
306 | crc2 = '=' + crc % 64; |
||
307 | // compare checksum to transmitted checksum bytes |
||
308 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
||
309 | { // checksum valid |
||
310 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
||
311 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
||
312 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
||
313 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
||
314 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
||
315 | } |
||
316 | else |
||
317 | { // checksum invalid |
||
318 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
319 | } |
||
320 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
321 | } |
||
322 | } |
||
323 | else // rxd buffer overrun |
||
324 | { |
||
325 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
326 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
327 | } |
||
1 | ingob | 328 | } |
329 | |||
330 | |||
19 | killagreg | 331 | // -------------------------------------------------------------------------- |
332 | void AddCRC(uint16_t datalen) |
||
1 | ingob | 333 | { |
19 | killagreg | 334 | uint16_t tmpCRC = 0, i; |
335 | for(i = 0; i < datalen; i++) |
||
336 | { |
||
337 | tmpCRC += txd_buffer[i]; |
||
338 | } |
||
339 | tmpCRC %= 4096; |
||
340 | txd_buffer[i++] = '=' + tmpCRC / 64; |
||
341 | txd_buffer[i++] = '=' + tmpCRC % 64; |
||
342 | txd_buffer[i++] = '\r'; |
||
343 | txd_complete = FALSE; |
||
344 | UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR) |
||
345 | } |
||
1 | ingob | 346 | |
347 | |||
348 | |||
349 | // -------------------------------------------------------------------------- |
||
19 | killagreg | 350 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
1 | ingob | 351 | { |
19 | killagreg | 352 | uint16_t pt = 0; |
353 | uint8_t a,b,c; |
||
354 | uint8_t ptr = 0; |
||
355 | |||
356 | txd_buffer[pt++] = '#'; // Start character |
||
357 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
||
358 | txd_buffer[pt++] = cmd; // Command |
||
359 | |||
360 | while(len) |
||
361 | { |
||
362 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
||
363 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
||
364 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
||
365 | txd_buffer[pt++] = '=' + (a >> 2); |
||
366 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
367 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
368 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
||
369 | } |
||
370 | AddCRC(pt); // add checksum after data block and initates the transmission |
||
1 | ingob | 371 | } |
372 | |||
373 | |||
374 | // -------------------------------------------------------------------------- |
||
19 | killagreg | 375 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
1 | ingob | 376 | { |
19 | killagreg | 377 | uint8_t a,b,c,d; |
378 | uint8_t ptr = 0; |
||
379 | uint8_t x,y,z; |
||
380 | while(len) |
||
381 | { |
||
382 | a = rxd_buffer[ptrIn++] - '='; |
||
383 | b = rxd_buffer[ptrIn++] - '='; |
||
384 | c = rxd_buffer[ptrIn++] - '='; |
||
385 | d = rxd_buffer[ptrIn++] - '='; |
||
386 | if(ptrIn > max - 2) break; |
||
1 | ingob | 387 | |
19 | killagreg | 388 | x = (a << 2) | (b >> 4); |
389 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
390 | z = ((c & 0x03) << 6) | d; |
||
391 | |||
392 | if(len--) ptrOut[ptr++] = x; else break; |
||
393 | if(len--) ptrOut[ptr++] = y; else break; |
||
394 | if(len--) ptrOut[ptr++] = z; else break; |
||
395 | } |
||
1 | ingob | 396 | } |
397 | |||
398 | |||
19 | killagreg | 399 | // -------------------------------------------------------------------------- |
29 | killagreg | 400 | int16_t USART0_putchar (int8_t c) |
1 | ingob | 401 | { |
19 | killagreg | 402 | // if tx is not enabled return immediatly |
403 | if(!(UCSR0B & (1 << TXEN0))) return (0); |
||
29 | killagreg | 404 | if (c == '\n') USART0_putchar('\r'); |
19 | killagreg | 405 | // wait until previous character was send |
406 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
407 | // send character |
||
408 | UDR0 = c; |
||
1 | ingob | 409 | return (0); |
410 | } |
||
411 | |||
19 | killagreg | 412 | |
1 | ingob | 413 | // -------------------------------------------------------------------------- |
19 | killagreg | 414 | void USART0_ProcessRxData(void) |
1 | ingob | 415 | { |
19 | killagreg | 416 | // if data in the rxd buffer are not locked immediately return |
417 | if(!rxd_buffer_locked) return; |
||
1 | ingob | 418 | |
19 | killagreg | 419 | uint8_t tmp_char_arr2[2]; // local char buffer |
1 | ingob | 420 | |
19 | killagreg | 421 | switch(rxd_buffer[2]) |
422 | { |
||
423 | case 'w':// Attitude |
||
424 | Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes); |
||
29 | killagreg | 425 | RequestFlags |= COMPASS_HEADING; |
28 | killagreg | 426 | AttitudeSource = ATTITUDE_SOURCE_UART; |
29 | killagreg | 427 | Orientation = ExternData.Orientation; |
428 | FC_Connected = 255; |
||
19 | killagreg | 429 | break; |
1 | ingob | 430 | |
19 | killagreg | 431 | case 'b': // extern control |
432 | case 'c': // extern control with debug request |
||
433 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes); |
||
21 | killagreg | 434 | #define KEY1 0x01 |
435 | #define KEY2 0x02 |
||
436 | #define KEY3 0x04 |
||
437 | #define KEY4 0x08 |
||
438 | #define KEY5 0x10 |
||
439 | // use right arrow at display for switching the calstate |
||
440 | if(ExternControl.RemoteButtons & KEY2) |
||
441 | { |
||
442 | ExternData.CalState++; |
||
443 | if(ExternData.CalState == 6) ExternData.CalState = 0; |
||
444 | } |
||
26 | killagreg | 445 | //ExternData.Attitude[0] = ExternControl.Par1; |
446 | //ExternData.Attitude[1] = ExternControl.Par2; |
||
21 | killagreg | 447 | PC_Connected = 255; |
19 | killagreg | 448 | break; |
1 | ingob | 449 | |
19 | killagreg | 450 | case 'h':// x-1 display columns |
451 | PC_Connected = 255; |
||
452 | break; |
||
1 | ingob | 453 | |
19 | killagreg | 454 | case 'v': // get version and board release |
23 | killagreg | 455 | RequestFlags |= VERSION_INFO; |
19 | killagreg | 456 | PC_Connected = 255; |
457 | break; |
||
458 | |||
459 | case 'a':// Labels of the Analog Debug outputs |
||
460 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
||
461 | RequestDebugLabel = tmp_char_arr2[0]; |
||
23 | killagreg | 462 | RequestFlags |= DEBUG_LABEL; |
19 | killagreg | 463 | PC_Connected = 255; |
464 | break; |
||
465 | |||
466 | case 'g':// get debug data |
||
23 | killagreg | 467 | RequestFlags |= DEBUG_DATA; |
19 | killagreg | 468 | PC_Connected = 255; |
469 | break; |
||
470 | } |
||
471 | // unlock the rxd buffer after processing |
||
472 | rxd_buffer_locked = FALSE; |
||
1 | ingob | 473 | } |
474 | |||
475 | |||
19 | killagreg | 476 | |
477 | //--------------------------------------------------------------------------------------------- |
||
478 | void USART0_TransmitTxData(void) |
||
1 | ingob | 479 | { |
19 | killagreg | 480 | if(!(UCSR0B & (1 << TXEN0))) return; |
1 | ingob | 481 | |
19 | killagreg | 482 | if(!txd_complete) return; |
1 | ingob | 483 | |
23 | killagreg | 484 | if(CheckDelay(Debug_Timer)) |
19 | killagreg | 485 | { |
486 | SetDebugValues(); |
||
487 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
||
488 | Debug_Timer = SetDelay(250); |
||
489 | } |
||
1 | ingob | 490 | |
23 | killagreg | 491 | if(RequestFlags & DEBUG_LABEL) |
19 | killagreg | 492 | { |
493 | SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16); |
||
494 | RequestDebugLabel = 255; |
||
23 | killagreg | 495 | RequestFlags &= ~DEBUG_LABEL; |
19 | killagreg | 496 | } |
1 | ingob | 497 | |
23 | killagreg | 498 | if(RequestFlags & VERSION_INFO) |
19 | killagreg | 499 | { |
500 | SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo)); |
||
23 | killagreg | 501 | RequestFlags &= ~VERSION_INFO; |
19 | killagreg | 502 | } |
503 | |||
23 | killagreg | 504 | if(RequestFlags & DEBUG_DATA) |
19 | killagreg | 505 | { |
506 | SetDebugValues(); |
||
507 | SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
||
23 | killagreg | 508 | RequestFlags &= ~DEBUG_DATA; |
19 | killagreg | 509 | } |
510 | |||
23 | killagreg | 511 | if(RequestFlags & COMPASS_HEADING) |
19 | killagreg | 512 | { |
513 | SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); |
||
23 | killagreg | 514 | RequestFlags &= ~COMPASS_HEADING; |
19 | killagreg | 515 | } |
1 | ingob | 516 | } |
29 | killagreg | 517 | |
518 | |||
519 | void USART0_Print(int8_t *msg) |
||
520 | { |
||
521 | uint8_t i = 0; |
||
522 | while(msg[i] != 0) |
||
523 | { |
||
524 | USART0_putchar(msg[i++]); |
||
525 | } |
||
526 | } |