Rev 1980 | Rev 2019 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1612 | dongfang | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
1963 | - | 3 | // + Nur f�r den privaten Gebrauch |
1612 | dongfang | 4 | // + www.MikroKopter.com |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1963 | - | 6 | // + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation), |
7 | // + dass eine Nutzung (auch auszugsweise) nur f�r den privaten und nicht-kommerziellen Gebrauch zul�ssig ist. |
||
1612 | dongfang | 8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
1963 | - | 10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen, |
1612 | dongfang | 11 | // + Verkauf von Luftbildaufnahmen, usw. |
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1963 | - | 13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht, |
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen |
||
1612 | dongfang | 15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
1963 | - | 17 | // + auf anderen Webseiten oder Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
1623 | - | 18 | // + eindeutig als Ursprung verlinkt und genannt werden |
1612 | dongfang | 19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1963 | - | 20 | // + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion |
1612 | dongfang | 21 | // + Benutzung auf eigene Gefahr |
1963 | - | 22 | // + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den |
1612 | dongfang | 23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
1963 | - | 25 | // + mit unserer Zustimmung zul�ssig |
1612 | dongfang | 26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
30 | // + this list of conditions and the following disclaimer. |
||
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
32 | // + from this software without specific prior written permission. |
||
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
34 | // + for non-commercial use (directly or indirectly) |
||
1868 | - | 35 | // + Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
1612 | dongfang | 36 | // + with our written permission |
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
38 | // + clearly linked as origin |
||
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
1623 | - | 47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
48 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
1612 | dongfang | 49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
50 | // + POSSIBILITY OF SUCH DAMAGE. |
||
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1623 | - | 52 | |
1612 | dongfang | 53 | #include <avr/io.h> |
54 | #include <avr/interrupt.h> |
||
55 | #include <avr/wdt.h> |
||
56 | #include <avr/pgmspace.h> |
||
57 | #include <stdarg.h> |
||
58 | #include <string.h> |
||
59 | |||
60 | #include "eeprom.h" |
||
61 | #include "menu.h" |
||
62 | #include "timer0.h" |
||
63 | #include "uart0.h" |
||
64 | #include "rc.h" |
||
65 | #include "externalControl.h" |
||
1775 | - | 66 | #include "output.h" |
1864 | - | 67 | #include "attitude.h" |
1612 | dongfang | 68 | |
69 | #ifdef USE_MK3MAG |
||
70 | #include "mk3mag.h" |
||
71 | #endif |
||
72 | |||
73 | #define FC_ADDRESS 1 |
||
74 | #define NC_ADDRESS 2 |
||
75 | #define MK3MAG_ADDRESS 3 |
||
76 | |||
77 | #define FALSE 0 |
||
78 | #define TRUE 1 |
||
79 | //int8_t test __attribute__ ((section (".noinit"))); |
||
1821 | - | 80 | uint8_t request_VerInfo = FALSE; |
81 | uint8_t request_ExternalControl = FALSE; |
||
82 | uint8_t request_Display = FALSE; |
||
83 | uint8_t request_Display1 = FALSE; |
||
84 | uint8_t request_DebugData = FALSE; |
||
85 | uint8_t request_Data3D = FALSE; |
||
86 | uint8_t request_DebugLabel = 255; |
||
87 | uint8_t request_PPMChannels = FALSE; |
||
88 | uint8_t request_MotorTest = FALSE; |
||
89 | uint8_t request_variables = FALSE; |
||
1775 | - | 90 | |
1612 | dongfang | 91 | uint8_t DisplayLine = 0; |
92 | |||
93 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
||
94 | volatile uint8_t rxd_buffer_locked = FALSE; |
||
95 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
||
96 | volatile uint8_t txd_complete = TRUE; |
||
97 | volatile uint8_t ReceivedBytes = 0; |
||
98 | volatile uint8_t *pRxData = 0; |
||
99 | volatile uint8_t RxDataLen = 0; |
||
100 | |||
1821 | - | 101 | uint8_t motorTestActive = 0; |
102 | uint8_t motorTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
||
1612 | dongfang | 103 | uint8_t ConfirmFrame; |
104 | |||
105 | typedef struct { |
||
1821 | - | 106 | int16_t Heading; |
107 | }__attribute__((packed)) Heading_t; |
||
1612 | dongfang | 108 | |
1955 | - | 109 | DebugOut_t debugOut; |
1821 | - | 110 | Data3D_t Data3D; |
111 | UART_VersionInfo_t UART_VersionInfo; |
||
1612 | dongfang | 112 | |
113 | uint16_t DebugData_Timer; |
||
114 | uint16_t Data3D_Timer; |
||
1969 | - | 115 | uint16_t DebugData_Interval = 0; // in 1ms |
1612 | dongfang | 116 | uint16_t Data3D_Interval = 0; // in 1ms |
117 | |||
118 | #ifdef USE_MK3MAG |
||
119 | int16_t Compass_Timer; |
||
120 | #endif |
||
121 | |||
122 | // keep lables in flash to save 512 bytes of sram space |
||
123 | const prog_uint8_t ANALOG_LABEL[32][16] = { |
||
1821 | - | 124 | //1234567890123456 |
125 | "AnglePitch ", //0 |
||
126 | "AngleRoll ", |
||
127 | "AngleYaw ", |
||
1974 | - | 128 | "GyroPitch ", |
129 | "GyroRoll ", |
||
130 | "GyroYaw ", //5 |
||
131 | "AccPitch ", |
||
132 | "AccRoll ", |
||
133 | "AccZ ", |
||
1821 | - | 134 | "AccPitch (angle)", |
135 | "AccRoll (angle) ", //10 |
||
1869 | - | 136 | "UBat ", |
1821 | - | 137 | "Pitch Term ", |
138 | "Roll Term ", |
||
1955 | - | 139 | "Yaw Term ", |
1980 | - | 140 | "Throttle Term ", //15 |
1955 | - | 141 | "gyroP ", |
1986 | - | 142 | "ControlAct/10 ", |
143 | "Acc. Vector ", |
||
144 | "Max. Acc. Vector", |
||
145 | "var0 ", //20 |
||
146 | "raw ubat ", |
||
1864 | - | 147 | "M1 ", |
148 | "M2 ", |
||
1821 | - | 149 | "M3 ", |
150 | "M4 ", //25 |
||
1864 | - | 151 | "ControlYaw ", |
152 | "Airpress. Range ", |
||
153 | "DriftCompPitch ", |
||
154 | "DriftCompRoll ", |
||
1970 | - | 155 | "Altitude ", //30 |
1821 | - | 156 | "AirpressADC " }; |
1612 | dongfang | 157 | |
158 | /****************************************************************/ |
||
159 | /* Initialization of the USART0 */ |
||
160 | /****************************************************************/ |
||
1821 | - | 161 | void usart0_Init(void) { |
162 | uint8_t sreg = SREG; |
||
163 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * USART0_BAUD) - 1); |
||
164 | |||
165 | // disable all interrupts before configuration |
||
166 | cli(); |
||
167 | |||
168 | // disable RX-Interrupt |
||
169 | UCSR0B &= ~(1 << RXCIE0); |
||
170 | // disable TX-Interrupt |
||
171 | UCSR0B &= ~(1 << TXCIE0); |
||
172 | |||
173 | // set direction of RXD0 and TXD0 pins |
||
174 | // set RXD0 (PD0) as an input pin |
||
175 | PORTD |= (1 << PORTD0); |
||
176 | DDRD &= ~(1 << DDD0); |
||
177 | // set TXD0 (PD1) as an output pin |
||
178 | PORTD |= (1 << PORTD1); |
||
179 | DDRD |= (1 << DDD1); |
||
180 | |||
181 | // USART0 Baud Rate Register |
||
182 | // set clock divider |
||
183 | UBRR0H = (uint8_t) (ubrr >> 8); |
||
184 | UBRR0L = (uint8_t) ubrr; |
||
185 | |||
186 | // USART0 Control and Status Register A, B, C |
||
187 | |||
188 | // enable double speed operation in |
||
189 | UCSR0A |= (1 << U2X0); |
||
190 | // enable receiver and transmitter in |
||
191 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
||
192 | // set asynchronous mode |
||
193 | UCSR0C &= ~(1 << UMSEL01); |
||
194 | UCSR0C &= ~(1 << UMSEL00); |
||
195 | // no parity |
||
196 | UCSR0C &= ~(1 << UPM01); |
||
197 | UCSR0C &= ~(1 << UPM00); |
||
198 | // 1 stop bit |
||
199 | UCSR0C &= ~(1 << USBS0); |
||
200 | // 8-bit |
||
201 | UCSR0B &= ~(1 << UCSZ02); |
||
202 | UCSR0C |= (1 << UCSZ01); |
||
203 | UCSR0C |= (1 << UCSZ00); |
||
204 | |||
205 | // flush receive buffer |
||
206 | while (UCSR0A & (1 << RXC0)) |
||
207 | UDR0; |
||
208 | |||
209 | // enable interrupts at the end |
||
210 | // enable RX-Interrupt |
||
211 | UCSR0B |= (1 << RXCIE0); |
||
212 | // enable TX-Interrupt |
||
213 | UCSR0B |= (1 << TXCIE0); |
||
214 | |||
215 | // initialize the debug timer |
||
1887 | - | 216 | DebugData_Timer = setDelay(DebugData_Interval); |
1821 | - | 217 | |
218 | // unlock rxd_buffer |
||
219 | rxd_buffer_locked = FALSE; |
||
220 | pRxData = 0; |
||
221 | RxDataLen = 0; |
||
222 | |||
223 | // no bytes to send |
||
224 | txd_complete = TRUE; |
||
225 | |||
1612 | dongfang | 226 | #ifdef USE_MK3MAG |
1887 | - | 227 | Compass_Timer = setDelay(220); |
1612 | dongfang | 228 | #endif |
1821 | - | 229 | |
230 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
||
231 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
||
232 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
||
233 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
||
234 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
||
235 | |||
236 | // restore global interrupt flags |
||
237 | SREG = sreg; |
||
1612 | dongfang | 238 | } |
239 | |||
240 | /****************************************************************/ |
||
241 | /* USART0 transmitter ISR */ |
||
242 | /****************************************************************/ |
||
1821 | - | 243 | ISR(USART0_TX_vect) |
244 | { |
||
245 | static uint16_t ptr_txd_buffer = 0; |
||
246 | uint8_t tmp_tx; |
||
247 | if (!txd_complete) { // transmission not completed |
||
248 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
||
249 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
||
250 | // if terminating character or end of txd buffer was reached |
||
251 | if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) { |
||
252 | ptr_txd_buffer = 0; // reset txd pointer |
||
253 | txd_complete = 1; // stop transmission |
||
254 | } |
||
255 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
||
256 | } |
||
257 | // transmission completed |
||
258 | else |
||
259 | ptr_txd_buffer = 0; |
||
1612 | dongfang | 260 | } |
261 | |||
262 | /****************************************************************/ |
||
263 | /* USART0 receiver ISR */ |
||
264 | /****************************************************************/ |
||
1821 | - | 265 | ISR(USART0_RX_vect) |
266 | { |
||
1969 | - | 267 | static uint16_t checksum; |
1821 | - | 268 | static uint8_t ptr_rxd_buffer = 0; |
1969 | - | 269 | uint8_t checksum1, checksum2; |
1821 | - | 270 | uint8_t c; |
1612 | dongfang | 271 | |
1821 | - | 272 | c = UDR0; // catch the received byte |
1612 | dongfang | 273 | |
1821 | - | 274 | if (rxd_buffer_locked) |
275 | return; // if rxd buffer is locked immediately return |
||
1612 | dongfang | 276 | |
1821 | - | 277 | // the rxd buffer is unlocked |
278 | if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received |
||
279 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
||
1969 | - | 280 | checksum = c; // init checksum |
1821 | - | 281 | } |
1612 | dongfang | 282 | #if 0 |
1821 | - | 283 | else if (ptr_rxd_buffer == 1) { // handle address |
284 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
1969 | - | 285 | checksum += c; // update checksum |
1821 | - | 286 | } |
1612 | dongfang | 287 | #endif |
1821 | - | 288 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
289 | if (c != '\r') { // no termination character |
||
290 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
||
1969 | - | 291 | checksum += c; // update checksum |
1821 | - | 292 | } else { // termination character was received |
293 | // the last 2 bytes are no subject for checksum calculation |
||
294 | // they are the checksum itself |
||
1969 | - | 295 | checksum -= rxd_buffer[ptr_rxd_buffer - 2]; |
296 | checksum -= rxd_buffer[ptr_rxd_buffer - 1]; |
||
1821 | - | 297 | // calculate checksum from transmitted data |
1969 | - | 298 | checksum %= 4096; |
299 | checksum1 = '=' + checksum / 64; |
||
300 | checksum2 = '=' + checksum % 64; |
||
1821 | - | 301 | // compare checksum to transmitted checksum bytes |
1969 | - | 302 | if ((checksum1 == rxd_buffer[ptr_rxd_buffer - 2]) && (checksum2 |
1821 | - | 303 | == rxd_buffer[ptr_rxd_buffer - 1])) { |
304 | // checksum valid |
||
305 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
||
306 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
||
307 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
||
308 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
||
309 | if (rxd_buffer[2] == 'R') { |
||
310 | wdt_enable(WDTO_250MS); |
||
311 | } // Reset-Commando |
||
312 | } else { // checksum invalid |
||
313 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
314 | } |
||
315 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
||
316 | } |
||
317 | } else { // rxd buffer overrun |
||
318 | ptr_rxd_buffer = 0; // reset rxd buffer |
||
319 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
||
320 | } |
||
1612 | dongfang | 321 | } |
322 | |||
323 | // -------------------------------------------------------------------------- |
||
1969 | - | 324 | void Addchecksum(uint16_t datalen) { |
325 | uint16_t tmpchecksum = 0, i; |
||
1821 | - | 326 | for (i = 0; i < datalen; i++) { |
1969 | - | 327 | tmpchecksum += txd_buffer[i]; |
1821 | - | 328 | } |
1969 | - | 329 | tmpchecksum %= 4096; |
330 | txd_buffer[i++] = '=' + tmpchecksum / 64; |
||
331 | txd_buffer[i++] = '=' + tmpchecksum % 64; |
||
1821 | - | 332 | txd_buffer[i++] = '\r'; |
333 | txd_complete = FALSE; |
||
334 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
||
1612 | dongfang | 335 | } |
336 | |||
337 | // -------------------------------------------------------------------------- |
||
1775 | - | 338 | // application example: |
339 | // SendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16); |
||
340 | /* |
||
1821 | - | 341 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
342 | va_list ap; |
||
343 | uint16_t txd_bufferIndex = 0; |
||
344 | uint8_t *currentBuffer; |
||
345 | uint8_t currentBufferIndex; |
||
346 | uint16_t lengthOfCurrentBuffer; |
||
347 | uint8_t shift = 0; |
||
1775 | - | 348 | |
1821 | - | 349 | txd_buffer[txd_bufferIndex++] = '#'; // Start character |
350 | txd_buffer[txd_bufferIndex++] = 'a' + addr; // Address (a=0; b=1,...) |
||
351 | txd_buffer[txd_bufferIndex++] = cmd; // Command |
||
1775 | - | 352 | |
1821 | - | 353 | va_start(ap, numofbuffers); |
354 | |||
355 | while(numofbuffers) { |
||
356 | currentBuffer = va_arg(ap, uint8_t*); |
||
357 | lengthOfCurrentBuffer = va_arg(ap, int); |
||
358 | currentBufferIndex = 0; |
||
359 | // Encode data: 3 bytes of data are encoded into 4 bytes, |
||
360 | // where the 2 most significant bits are both 0. |
||
361 | while(currentBufferIndex != lengthOfCurrentBuffer) { |
||
362 | if (!shift) txd_buffer[txd_bufferIndex] = 0; |
||
363 | txd_buffer[txd_bufferIndex] |= currentBuffer[currentBufferIndex] >> (shift + 2); |
||
364 | txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111; |
||
365 | shift += 2; |
||
366 | if (shift == 6) { shift=0; txd_bufferIndex++; } |
||
367 | currentBufferIndex++; |
||
368 | } |
||
369 | } |
||
370 | // If the number of data bytes was not divisible by 3, stuff |
||
371 | // with 0 pseudodata until length is again divisible by 3. |
||
372 | if (shift == 2) { |
||
373 | // We need to stuff with zero bytes at the end. |
||
374 | txd_buffer[txd_bufferIndex] &= 0b00110000; |
||
375 | txd_buffer[++txd_bufferIndex] = 0; |
||
376 | shift = 4; |
||
377 | } |
||
378 | if (shift == 4) { |
||
379 | // We need to stuff with zero bytes at the end. |
||
380 | txd_buffer[txd_bufferIndex++] &= 0b00111100; |
||
381 | txd_buffer[txd_bufferIndex] = 0; |
||
382 | } |
||
383 | va_end(ap); |
||
1969 | - | 384 | Addchecksum(pt); // add checksum after data block and initates the transmission |
1821 | - | 385 | } |
386 | */ |
||
387 | |||
1775 | - | 388 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
1821 | - | 389 | va_list ap; |
390 | uint16_t pt = 0; |
||
391 | uint8_t a, b, c; |
||
392 | uint8_t ptr = 0; |
||
1612 | dongfang | 393 | |
1821 | - | 394 | uint8_t *pdata = 0; |
395 | int len = 0; |
||
1612 | dongfang | 396 | |
1821 | - | 397 | txd_buffer[pt++] = '#'; // Start character |
398 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
||
399 | txd_buffer[pt++] = cmd; // Command |
||
400 | |||
401 | va_start(ap, numofbuffers); |
||
402 | |||
403 | if (numofbuffers) { |
||
404 | pdata = va_arg(ap, uint8_t*); |
||
405 | len = va_arg(ap, int); |
||
406 | ptr = 0; |
||
407 | numofbuffers--; |
||
408 | } |
||
409 | |||
410 | while (len) { |
||
411 | if (len) { |
||
412 | a = pdata[ptr++]; |
||
413 | len--; |
||
414 | if ((!len) && numofbuffers) { |
||
415 | pdata = va_arg(ap, uint8_t*); |
||
416 | len = va_arg(ap, int); |
||
417 | ptr = 0; |
||
418 | numofbuffers--; |
||
419 | } |
||
420 | } else |
||
421 | a = 0; |
||
422 | if (len) { |
||
423 | b = pdata[ptr++]; |
||
424 | len--; |
||
425 | if ((!len) && numofbuffers) { |
||
426 | pdata = va_arg(ap, uint8_t*); |
||
427 | len = va_arg(ap, int); |
||
428 | ptr = 0; |
||
429 | numofbuffers--; |
||
430 | } |
||
431 | } else |
||
432 | b = 0; |
||
433 | if (len) { |
||
434 | c = pdata[ptr++]; |
||
435 | len--; |
||
436 | if ((!len) && numofbuffers) { |
||
437 | pdata = va_arg(ap, uint8_t*); |
||
438 | len = va_arg(ap, int); |
||
439 | ptr = 0; |
||
440 | numofbuffers--; |
||
441 | } |
||
442 | } else |
||
443 | c = 0; |
||
444 | txd_buffer[pt++] = '=' + (a >> 2); |
||
445 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
446 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
447 | txd_buffer[pt++] = '=' + (c & 0x3f); |
||
448 | } |
||
449 | va_end(ap); |
||
1969 | - | 450 | Addchecksum(pt); // add checksum after data block and initates the transmission |
1612 | dongfang | 451 | } |
452 | |||
453 | // -------------------------------------------------------------------------- |
||
454 | void Decode64(void) { |
||
1821 | - | 455 | uint8_t a, b, c, d; |
456 | uint8_t x, y, z; |
||
457 | uint8_t ptrIn = 3; |
||
458 | uint8_t ptrOut = 3; |
||
459 | uint8_t len = ReceivedBytes - 6; |
||
460 | |||
461 | while (len) { |
||
462 | a = rxd_buffer[ptrIn++] - '='; |
||
463 | b = rxd_buffer[ptrIn++] - '='; |
||
464 | c = rxd_buffer[ptrIn++] - '='; |
||
465 | d = rxd_buffer[ptrIn++] - '='; |
||
466 | //if(ptrIn > ReceivedBytes - 3) break; |
||
467 | |||
468 | x = (a << 2) | (b >> 4); |
||
469 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
470 | z = ((c & 0x03) << 6) | d; |
||
471 | |||
472 | if (len--) |
||
473 | rxd_buffer[ptrOut++] = x; |
||
474 | else |
||
475 | break; |
||
476 | if (len--) |
||
477 | rxd_buffer[ptrOut++] = y; |
||
478 | else |
||
479 | break; |
||
480 | if (len--) |
||
481 | rxd_buffer[ptrOut++] = z; |
||
482 | else |
||
483 | break; |
||
484 | } |
||
485 | pRxData = &rxd_buffer[3]; |
||
486 | RxDataLen = ptrOut - 3; |
||
1612 | dongfang | 487 | } |
488 | |||
489 | // -------------------------------------------------------------------------- |
||
1645 | - | 490 | void usart0_ProcessRxData(void) { |
1821 | - | 491 | // We control the motorTestActive var from here: Count it down. |
492 | if (motorTestActive) |
||
493 | motorTestActive--; |
||
494 | // if data in the rxd buffer are not locked immediately return |
||
495 | if (!rxd_buffer_locked) |
||
496 | return; |
||
1980 | - | 497 | uint8_t tempchar[3]; |
1821 | - | 498 | Decode64(); // decode data block in rxd_buffer |
1775 | - | 499 | |
1821 | - | 500 | switch (rxd_buffer[1] - 'a') { |
1775 | - | 501 | |
1821 | - | 502 | case FC_ADDRESS: |
503 | switch (rxd_buffer[2]) { |
||
1612 | dongfang | 504 | #ifdef USE_MK3MAG |
1821 | - | 505 | case 'K':// compass value |
506 | compassHeading = ((Heading_t *)pRxData)->Heading; |
||
507 | // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180; |
||
508 | break; |
||
1612 | dongfang | 509 | #endif |
1821 | - | 510 | case 't': // motor test |
511 | if (RxDataLen > 20) { |
||
512 | memcpy(&motorTest[0], (uint8_t*) pRxData, sizeof(motorTest)); |
||
513 | } else { |
||
514 | memcpy(&motorTest[0], (uint8_t*) pRxData, 4); |
||
515 | } |
||
516 | motorTestActive = 255; |
||
517 | externalControlActive = 255; |
||
518 | break; |
||
1612 | dongfang | 519 | |
1821 | - | 520 | case 'n':// "Get Mixer Table |
521 | while (!txd_complete) |
||
522 | ; // wait for previous frame to be sent |
||
1960 | - | 523 | SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &mixerMatrix, sizeof(mixerMatrix)); |
1821 | - | 524 | break; |
1612 | dongfang | 525 | |
1821 | - | 526 | case 'm':// "Set Mixer Table |
527 | if (pRxData[0] == EEMIXER_REVISION) { |
||
1960 | - | 528 | memcpy(&mixerMatrix, (uint8_t*) pRxData, sizeof(mixerMatrix)); |
529 | mixerMatrix_writeToEEProm(); |
||
1821 | - | 530 | while (!txd_complete) |
531 | ; // wait for previous frame to be sent |
||
1980 | - | 532 | tempchar[0] = 1; |
1821 | - | 533 | } else { |
1980 | - | 534 | tempchar[0] = 0; |
1821 | - | 535 | } |
1980 | - | 536 | SendOutData('M', FC_ADDRESS, 1, &tempchar, 1); |
1821 | - | 537 | break; |
1612 | dongfang | 538 | |
1821 | - | 539 | case 'p': // get PPM channels |
540 | request_PPMChannels = TRUE; |
||
541 | break; |
||
1612 | dongfang | 542 | |
1821 | - | 543 | case 'q':// request settings |
544 | if (pRxData[0] == 0xFF) { |
||
1960 | - | 545 | pRxData[0] = getParamByte(PID_ACTIVE_SET); |
1821 | - | 546 | } |
547 | // limit settings range |
||
548 | if (pRxData[0] < 1) |
||
549 | pRxData[0] = 1; // limit to 1 |
||
550 | else if (pRxData[0] > 5) |
||
551 | pRxData[0] = 5; // limit to 5 |
||
552 | // load requested parameter set |
||
1960 | - | 553 | paramSet_readFromEEProm(pRxData[0]); |
1980 | - | 554 | tempchar[0] = pRxData[0]; |
555 | tempchar[1] = EEPARAM_REVISION; |
||
556 | tempchar[2] = sizeof(staticParams); |
||
1821 | - | 557 | while (!txd_complete) |
558 | ; // wait for previous frame to be sent |
||
1980 | - | 559 | SendOutData('Q', FC_ADDRESS, 2, &tempchar, 3, (uint8_t *) &staticParams, sizeof(staticParams)); |
1821 | - | 560 | break; |
1612 | dongfang | 561 | |
1821 | - | 562 | case 's': // save settings |
563 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
||
564 | { |
||
565 | if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] |
||
566 | == EEPARAM_REVISION)) // check for setting to be in range and version of settings |
||
567 | { |
||
568 | memcpy(&staticParams, (uint8_t*) &pRxData[2], sizeof(staticParams)); |
||
1960 | - | 569 | paramSet_writeToEEProm(pRxData[0]); |
1980 | - | 570 | tempchar[0] = getActiveParamSet(); |
571 | beepNumber(tempchar[0]); |
||
1821 | - | 572 | } else { |
1980 | - | 573 | tempchar[0] = 0; //indicate bad data |
1821 | - | 574 | } |
575 | while (!txd_complete) |
||
576 | ; // wait for previous frame to be sent |
||
1980 | - | 577 | SendOutData('S', FC_ADDRESS, 1, &tempchar, 1); |
1821 | - | 578 | } |
579 | break; |
||
1612 | dongfang | 580 | |
1821 | - | 581 | default: |
582 | //unsupported command received |
||
583 | break; |
||
584 | } // case FC_ADDRESS: |
||
1612 | dongfang | 585 | |
1821 | - | 586 | default: // any Slave Address |
587 | switch (rxd_buffer[2]) { |
||
588 | case 'a':// request for labels of the analog debug outputs |
||
589 | request_DebugLabel = pRxData[0]; |
||
590 | if (request_DebugLabel > 31) |
||
591 | request_DebugLabel = 31; |
||
592 | break; |
||
1612 | dongfang | 593 | |
1821 | - | 594 | case 'b': // submit extern control |
595 | memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl)); |
||
596 | ConfirmFrame = externalControl.frame; |
||
597 | externalControlActive = 255; |
||
598 | break; |
||
1612 | dongfang | 599 | |
1821 | - | 600 | case 'h':// request for display columns |
601 | RemoteKeys |= pRxData[0]; |
||
602 | if (RemoteKeys) |
||
603 | DisplayLine = 0; |
||
604 | request_Display = TRUE; |
||
605 | break; |
||
1612 | dongfang | 606 | |
1821 | - | 607 | case 'l':// request for display columns |
608 | MenuItem = pRxData[0]; |
||
609 | request_Display1 = TRUE; |
||
610 | break; |
||
1612 | dongfang | 611 | |
1821 | - | 612 | case 'v': // request for version and board release |
613 | request_VerInfo = TRUE; |
||
614 | break; |
||
1775 | - | 615 | |
1821 | - | 616 | case 'x': |
617 | request_variables = TRUE; |
||
618 | break; |
||
1612 | dongfang | 619 | |
1821 | - | 620 | case 'g':// get external control data |
621 | request_ExternalControl = TRUE; |
||
622 | break; |
||
1612 | dongfang | 623 | |
1821 | - | 624 | case 'd': // request for the debug data |
625 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
||
626 | if (DebugData_Interval > 0) |
||
627 | request_DebugData = TRUE; |
||
628 | break; |
||
1612 | dongfang | 629 | |
1821 | - | 630 | case 'c': // request for the 3D data |
631 | Data3D_Interval = (uint16_t) pRxData[0] * 10; |
||
632 | if (Data3D_Interval > 0) |
||
633 | request_Data3D = TRUE; |
||
634 | break; |
||
635 | |||
636 | default: |
||
637 | //unsupported command received |
||
638 | break; |
||
639 | } |
||
640 | break; // default: |
||
641 | } |
||
642 | // unlock the rxd buffer after processing |
||
643 | pRxData = 0; |
||
644 | RxDataLen = 0; |
||
645 | rxd_buffer_locked = FALSE; |
||
1612 | dongfang | 646 | } |
647 | |||
1645 | - | 648 | /************************************************************************/ |
1963 | - | 649 | /* Routine f�r die Serielle Ausgabe */ |
1645 | - | 650 | /************************************************************************/ |
1821 | - | 651 | int16_t uart_putchar(int8_t c) { |
652 | if (c == '\n') |
||
653 | uart_putchar('\r'); |
||
654 | // wait until previous character was send |
||
655 | loop_until_bit_is_set(UCSR0A, UDRE0); |
||
656 | // send character |
||
657 | UDR0 = c; |
||
658 | return (0); |
||
1612 | dongfang | 659 | } |
660 | |||
661 | //--------------------------------------------------------------------------------------------- |
||
1645 | - | 662 | void usart0_TransmitTxData(void) { |
1821 | - | 663 | if (!txd_complete) |
664 | return; |
||
1612 | dongfang | 665 | |
1821 | - | 666 | if (request_VerInfo && txd_complete) { |
667 | SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, |
||
668 | sizeof(UART_VersionInfo)); |
||
669 | request_VerInfo = FALSE; |
||
670 | } |
||
1612 | dongfang | 671 | |
1821 | - | 672 | if (request_Display && txd_complete) { |
673 | LCD_PrintMenu(); |
||
674 | SendOutData('H', FC_ADDRESS, 2, &DisplayLine, sizeof(DisplayLine), |
||
675 | &DisplayBuff[DisplayLine * 20], 20); |
||
676 | DisplayLine++; |
||
677 | if (DisplayLine >= 4) |
||
678 | DisplayLine = 0; |
||
679 | request_Display = FALSE; |
||
680 | } |
||
1612 | dongfang | 681 | |
1821 | - | 682 | if (request_Display1 && txd_complete) { |
683 | LCD_PrintMenu(); |
||
684 | SendOutData('L', FC_ADDRESS, 3, &MenuItem, sizeof(MenuItem), &MaxMenuItem, |
||
685 | sizeof(MaxMenuItem), DisplayBuff, sizeof(DisplayBuff)); |
||
686 | request_Display1 = FALSE; |
||
687 | } |
||
688 | |||
1963 | - | 689 | if (request_DebugLabel != 0xFF) { // Texte f�r die Analogdaten |
1821 | - | 690 | uint8_t label[16]; // local sram buffer |
691 | memcpy_P(label, ANALOG_LABEL[request_DebugLabel], 16); // read lable from flash to sram buffer |
||
692 | SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_DebugLabel, |
||
693 | sizeof(request_DebugLabel), label, 16); |
||
694 | request_DebugLabel = 0xFF; |
||
695 | } |
||
696 | |||
1969 | - | 697 | if (ConfirmFrame && txd_complete) { // Datensatz ohne checksum bestätigen |
698 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*) &ConfirmFrame, sizeof(ConfirmFrame)); |
||
1821 | - | 699 | ConfirmFrame = 0; |
700 | } |
||
701 | |||
1887 | - | 702 | if (((DebugData_Interval && checkDelay(DebugData_Timer)) || request_DebugData) |
1821 | - | 703 | && txd_complete) { |
1955 | - | 704 | SendOutData('D', FC_ADDRESS, 1, (uint8_t *) &debugOut, sizeof(debugOut)); |
1887 | - | 705 | DebugData_Timer = setDelay(DebugData_Interval); |
1821 | - | 706 | request_DebugData = FALSE; |
707 | } |
||
708 | |||
1887 | - | 709 | if (((Data3D_Interval && checkDelay(Data3D_Timer)) || request_Data3D) |
1821 | - | 710 | && txd_complete) { |
711 | SendOutData('C', FC_ADDRESS, 1, (uint8_t *) &Data3D, sizeof(Data3D)); |
||
712 | Data3D.AngleNick = (int16_t) ((10 * angle[PITCH]) |
||
713 | / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1° |
||
714 | Data3D.AngleRoll = (int16_t) ((10 * angle[ROLL]) |
||
715 | / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1° |
||
716 | Data3D.Heading = (int16_t) ((10 * yawGyroHeading) / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1° |
||
1887 | - | 717 | Data3D_Timer = setDelay(Data3D_Interval); |
1821 | - | 718 | request_Data3D = FALSE; |
719 | } |
||
720 | |||
721 | if (request_ExternalControl && txd_complete) { |
||
722 | SendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl, |
||
723 | sizeof(externalControl)); |
||
724 | request_ExternalControl = FALSE; |
||
725 | } |
||
726 | |||
1612 | dongfang | 727 | #ifdef USE_MK3MAG |
1887 | - | 728 | if((checkDelay(Compass_Timer)) && txd_complete) { |
1969 | - | 729 | toMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg |
730 | toMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg |
||
731 | toMk3Mag.UserParam[0] = dynamicParams.userParams[0]; |
||
732 | toMk3Mag.UserParam[1] = dynamicParams.userParams[1]; |
||
733 | toMk3Mag.CalState = compassCalState; |
||
734 | SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &toMk3Mag,sizeof(toMk3Mag)); |
||
1821 | - | 735 | // the last state is 5 and should be send only once to avoid multiple flash writing |
736 | if(compassCalState > 4) compassCalState = 0; |
||
1887 | - | 737 | Compass_Timer = setDelay(99); |
1821 | - | 738 | } |
1612 | dongfang | 739 | #endif |
740 | |||
1821 | - | 741 | if (request_MotorTest && txd_complete) { |
742 | SendOutData('T', FC_ADDRESS, 0); |
||
743 | request_MotorTest = FALSE; |
||
744 | } |
||
1775 | - | 745 | |
1821 | - | 746 | if (request_PPMChannels && txd_complete) { |
747 | SendOutData('P', FC_ADDRESS, 1, (uint8_t *) &PPM_in, sizeof(PPM_in)); |
||
748 | request_PPMChannels = FALSE; |
||
749 | } |
||
750 | |||
751 | if (request_variables && txd_complete) { |
||
752 | SendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables)); |
||
753 | request_variables = FALSE; |
||
754 | } |
||
1612 | dongfang | 755 | } |