Rev 1927 | Rev 2102 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1927 | Rev 2099 | ||
---|---|---|---|
Line 10... | Line 10... | ||
10 | #include "uart0.h" |
10 | #include "uart0.h" |
11 | #include "rc.h" |
11 | #include "rc.h" |
12 | #include "externalControl.h" |
12 | #include "externalControl.h" |
13 | #include "output.h" |
13 | #include "output.h" |
14 | #include "attitude.h" |
14 | #include "attitude.h" |
15 | - | ||
16 | - | ||
17 | #ifdef USE_MK3MAG |
- | |
18 | #include "mk3mag.h" |
15 | #include "commands.h" |
19 | #endif |
- | |
Line 20... | Line 16... | ||
20 | 16 | ||
21 | #define FC_ADDRESS 1 |
17 | #define FC_ADDRESS 1 |
22 | #define NC_ADDRESS 2 |
18 | #define NC_ADDRESS 2 |
Line 23... | Line 19... | ||
23 | #define MK3MAG_ADDRESS 3 |
19 | #define MK3MAG_ADDRESS 3 |
24 | 20 | ||
- | 21 | #define FALSE 0 |
|
25 | #define FALSE 0 |
22 | #define TRUE 1 |
26 | #define TRUE 1 |
23 | |
- | 24 | int8_t displayBuff[DISPLAYBUFFSIZE]; |
|
27 | //int8_t test __attribute__ ((section (".noinit"))); |
25 | uint8_t dispPtr = 0; |
28 | uint8_t request_VerInfo = FALSE; |
26 | |
29 | uint8_t request_ExternalControl = FALSE; |
27 | uint8_t requestedDebugLabel = 255; |
30 | uint8_t request_Display = FALSE; |
28 | uint8_t request_verInfo = FALSE; |
31 | uint8_t request_Display1 = FALSE; |
29 | uint8_t request_externalControl = FALSE; |
32 | uint8_t request_DebugData = FALSE; |
- | |
33 | uint8_t request_Data3D = FALSE; |
30 | uint8_t request_debugData = FALSE; |
34 | uint8_t request_DebugLabel = 255; |
31 | uint8_t request_data3D = FALSE; |
35 | uint8_t request_PPMChannels = FALSE; |
32 | uint8_t request_PPMChannels = FALSE; |
- | 33 | uint8_t request_servoTest = FALSE; |
|
Line -... | Line 34... | ||
- | 34 | uint8_t request_variables = FALSE; |
|
- | 35 | uint8_t request_OSD = FALSE; |
|
- | 36 | ||
- | 37 | /* |
|
- | 38 | #define request_verInfo (1<<0) |
|
- | 39 | #define request_externalControl (1<<1) |
|
- | 40 | #define request_display (1<<3) |
|
- | 41 | #define request_display1 (1<<4) |
|
- | 42 | #define request_debugData (1<<5) |
|
- | 43 | #define request_data3D (1<<6) |
|
- | 44 | #define request_PPMChannels (1<<7) |
|
- | 45 | #define request_motorTest (1<<8) |
|
- | 46 | #define request_variables (1<<9) |
|
36 | uint8_t request_OutputTest = FALSE; |
47 | #define request_OSD (1<<10) |
Line 37... | Line 48... | ||
37 | uint8_t request_variables = FALSE; |
48 | */ |
38 | 49 | ||
39 | uint8_t DisplayLine = 0; |
50 | //uint16_t request = 0; |
40 | 51 | ||
41 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
52 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
42 | volatile uint8_t rxd_buffer_locked = FALSE; |
53 | volatile uint8_t rxd_buffer_locked = FALSE; |
43 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
54 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
Line 44... | Line 55... | ||
44 | volatile uint8_t txd_complete = TRUE; |
55 | volatile uint8_t txd_complete = TRUE; |
45 | volatile uint8_t ReceivedBytes = 0; |
56 | volatile uint8_t receivedBytes = 0; |
46 | volatile uint8_t *pRxData = 0; |
57 | volatile uint8_t *pRxData = 0; |
Line 47... | Line 58... | ||
47 | volatile uint8_t RxDataLen = 0; |
58 | volatile uint8_t rxDataLen = 0; |
48 | 59 | ||
49 | uint8_t outputTestActive = 0; |
60 | uint8_t servoTestActive = 0; |
Line 50... | Line -... | ||
50 | uint8_t outputTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
- | |
51 | uint8_t ConfirmFrame; |
61 | uint8_t servoTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
52 | - | ||
53 | typedef struct { |
62 | uint8_t confirmFrame; |
54 | int16_t Heading; |
63 | |
55 | }__attribute__((packed)) Heading_t; |
64 | typedef struct { |
- | 65 | int16_t heading; |
|
56 | 66 | }__attribute__((packed)) Heading_t; |
|
57 | DebugOut_t DebugOut; |
67 | |
- | 68 | Data3D_t data3D; |
|
Line 58... | Line 69... | ||
58 | Data3D_t Data3D; |
69 | |
59 | UART_VersionInfo_t UART_VersionInfo; |
70 | uint16_t debugData_timer; |
60 | 71 | uint16_t data3D_timer; |
|
Line 61... | Line 72... | ||
61 | uint16_t DebugData_Timer; |
72 | uint16_t OSD_timer; |
62 | uint16_t Data3D_Timer; |
73 | uint16_t debugData_interval = 0; // in 1ms |
63 | uint16_t DebugData_Interval = 500; // in 1ms |
74 | uint16_t data3D_interval = 0; // in 1ms |
64 | uint16_t Data3D_Interval = 0; // in 1ms |
75 | uint16_t OSD_interval = 0; |
65 | 76 | ||
66 | #ifdef USE_MK3MAG |
77 | #ifdef USE_DIRECT_GPS |
67 | int16_t Compass_Timer; |
78 | int16_t toMk3MagTimer; |
68 | #endif |
79 | #endif |
69 | 80 | ||
70 | // keep lables in flash to save 512 bytes of sram space |
81 | // keep lables in flash to save 512 bytes of sram space |
71 | const prog_uint8_t ANALOG_LABEL[32][16] = { |
82 | const prog_uint8_t ANALOG_LABEL[32][16] = { |
72 | //1234567890123456 |
83 | //1234567890123456 |
73 | "AnglePitch ", //0 |
84 | "AnglePitch ", //0 |
74 | "AngleRoll ", |
85 | "AngleRoll ", |
75 | "AngleYaw ", |
86 | "AngleYaw ", |
76 | "GyroPitch(PID) ", |
87 | "GyroPitch ", |
77 | "GyroRoll(PID) ", |
88 | "GyroRoll ", |
78 | "GyroYaw ", //5 |
89 | "GyroYaw ", //5 |
79 | "GyroFactorPitch ", |
90 | "PitchTerm ", |
80 | "GyroFactorRoll ", |
91 | "RollTerm ", |
81 | "GyroFactorYaw ", |
92 | "ThrottleTerm ", |
82 | "GyroDPitch ", |
93 | "YawTerm ", |
83 | "GyroDRoll ",//10 |
94 | "heightP ", //10 |
84 | "GyroDYaw ", |
95 | "heightI ", |
85 | "Pitch Term ", |
96 | "heightD ", |
86 | "Roll Term ", |
97 | "gyroActivity ", |
87 | "Throttle Term ", |
98 | "ca ", |
88 | "Yaw Term ", //15 |
99 | "GActivityDivider", //15 |
89 | "0th O Corr pitch", |
100 | "NaviMode ", |
90 | "0th O Corr roll ", |
101 | "NaviStatus ", |
91 | "ControlIntePitch", |
102 | "NaviStickP ", |
92 | "ControlInteRoll ", |
103 | "NaviStickR ", |
93 | "UBat ", //20 |
104 | "control act wghd", //20 |
94 | "hoverThrottle ", |
105 | "acc vector wghd ", |
95 | "IPartPitch ", // OK |
106 | "Height[dm] ", |
- | 107 | "dHeight ", |
|
Line 96... | Line 108... | ||
96 | "IPartRoll ", |
108 | "acc vector ", |
97 | "S3 (THROTTLE) ", |
109 | "EFT ", //25 |
98 | "S4 (RUDDER) ", // OK //25 |
110 | "naviPitch ", |
99 | "ControlYaw ", |
111 | "naviRoll ", |
100 | "Airpress. Range ", // OK |
112 | "tolerance ", |
101 | "DriftCompPitch ", |
113 | "Gyro Act Cont. ", |
Line 102... | Line 114... | ||
102 | "DriftCompRoll ", |
114 | "GPS altitude ", //30 |
103 | "AirpressFiltered", // MISSING //30 |
115 | "GPS vert accura " |
Line 159... | Line 171... | ||
159 | UCSR0B |= (1 << RXCIE0); |
171 | UCSR0B |= (1 << RXCIE0); |
160 | // enable TX-Interrupt |
172 | // enable TX-Interrupt |
161 | UCSR0B |= (1 << TXCIE0); |
173 | UCSR0B |= (1 << TXCIE0); |
Line 162... | Line 174... | ||
162 | 174 | ||
163 | // initialize the debug timer |
175 | // initialize the debug timer |
Line 164... | Line 176... | ||
164 | DebugData_Timer = setDelay(DebugData_Interval); |
176 | debugData_timer = setDelay(debugData_interval); |
165 | 177 | ||
166 | // unlock rxd_buffer |
178 | // unlock rxd_buffer |
167 | rxd_buffer_locked = FALSE; |
179 | rxd_buffer_locked = FALSE; |
Line 168... | Line 180... | ||
168 | pRxData = 0; |
180 | pRxData = 0; |
169 | RxDataLen = 0; |
181 | rxDataLen = 0; |
Line -... | Line 182... | ||
- | 182 | ||
- | 183 | // no bytes to send |
|
- | 184 | txd_complete = TRUE; |
|
- | 185 | ||
170 | 186 | #ifdef USE_DIRECT_GPS |
|
171 | // no bytes to send |
187 | toMk3MagTimer = setDelay(220); |
172 | txd_complete = TRUE; |
188 | #endif |
173 | 189 | ||
174 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
190 | versionInfo.SWMajor = VERSION_MAJOR; |
Line 175... | Line 191... | ||
175 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
191 | versionInfo.SWMinor = VERSION_MINOR; |
176 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
192 | versionInfo.SWPatch = VERSION_PATCH; |
177 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
193 | versionInfo.protoMajor = VERSION_SERIAL_MAJOR; |
Line 178... | Line 194... | ||
178 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
194 | versionInfo.protoMinor = VERSION_SERIAL_MINOR; |
179 | 195 | ||
180 | // restore global interrupt flags |
196 | // restore global interrupt flags |
181 | SREG = sreg; |
197 | SREG = sreg; |
182 | } |
- | |
183 | 198 | } |
|
184 | /****************************************************************/ |
199 | |
185 | /* USART0 transmitter ISR */ |
200 | /****************************************************************/ |
186 | /****************************************************************/ |
201 | /* USART0 transmitter ISR */ |
187 | ISR(USART0_TX_vect) |
202 | /****************************************************************/ |
Line 204... | Line 219... | ||
204 | } |
219 | } |
Line 205... | Line 220... | ||
205 | 220 | ||
206 | /****************************************************************/ |
221 | /****************************************************************/ |
207 | /* USART0 receiver ISR */ |
222 | /* USART0 receiver ISR */ |
208 | /****************************************************************/ |
223 | /****************************************************************/ |
209 | ISR(USART0_RX_vect) |
- | |
210 | { |
224 | ISR(USART0_RX_vect) { |
211 | static uint16_t crc; |
225 | static uint16_t checksum; |
212 | static uint8_t ptr_rxd_buffer = 0; |
226 | static uint8_t ptr_rxd_buffer = 0; |
213 | uint8_t crc1, crc2; |
227 | uint8_t checksum1, checksum2; |
Line 214... | Line 228... | ||
214 | uint8_t c; |
228 | uint8_t c; |
Line 215... | Line 229... | ||
215 | 229 | ||
216 | c = UDR0; // catch the received byte |
230 | c = UDR0; // catch the received byte |
Line 217... | Line 231... | ||
217 | 231 | ||
218 | if (rxd_buffer_locked) |
232 | if (rxd_buffer_locked) |
219 | return; // if rxd buffer is locked immediately return |
233 | return; // if rxd buffer is locked immediately return |
220 | 234 | ||
221 | // the rxd buffer is unlocked |
- | |
222 | if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received |
- | |
223 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
- | |
224 | crc = c; // init crc |
- | |
225 | } |
- | |
226 | #if 0 |
235 | // the rxd buffer is unlocked |
227 | else if (ptr_rxd_buffer == 1) { // handle address |
- | |
228 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
236 | if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received |
229 | crc += c; // update crc |
237 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
230 | } |
238 | checksum = c; // init checksum |
231 | #endif |
239 | } |
232 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
240 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
233 | if (c != '\r') { // no termination character |
241 | if (c != '\r') { // no termination character |
234 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
242 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
235 | crc += c; // update crc |
243 | checksum += c; // update checksum |
236 | } else { // termination character was received |
244 | } else { // termination character was received |
237 | // the last 2 bytes are no subject for checksum calculation |
245 | // the last 2 bytes are no subject for checksum calculation |
238 | // they are the checksum itself |
246 | // they are the checksum itself |
239 | crc -= rxd_buffer[ptr_rxd_buffer - 2]; |
247 | checksum -= rxd_buffer[ptr_rxd_buffer - 2]; |
240 | crc -= rxd_buffer[ptr_rxd_buffer - 1]; |
248 | checksum -= rxd_buffer[ptr_rxd_buffer - 1]; |
241 | // calculate checksum from transmitted data |
249 | // calculate checksum from transmitted data |
242 | crc %= 4096; |
250 | checksum %= 4096; |
243 | crc1 = '=' + crc / 64; |
251 | checksum1 = '=' + checksum / 64; |
244 | crc2 = '=' + crc % 64; |
252 | checksum2 = '=' + checksum % 64; |
245 | // compare checksum to transmitted checksum bytes |
253 | // compare checksum to transmitted checksum bytes |
246 | if ((crc1 == rxd_buffer[ptr_rxd_buffer - 2]) && (crc2 |
254 | if ((checksum1 == rxd_buffer[ptr_rxd_buffer - 2]) && (checksum2 |
247 | == rxd_buffer[ptr_rxd_buffer - 1])) { |
255 | == rxd_buffer[ptr_rxd_buffer - 1])) { |
248 | // checksum valid |
256 | // checksum valid |
249 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
257 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
250 | ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
258 | receivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
251 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
259 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
Line 263... | Line 271... | ||
263 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
271 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
264 | } |
272 | } |
265 | } |
273 | } |
Line 266... | Line 274... | ||
266 | 274 | ||
267 | // -------------------------------------------------------------------------- |
275 | // -------------------------------------------------------------------------- |
268 | void AddCRC(uint16_t datalen) { |
276 | void addChecksum(uint16_t datalen) { |
269 | uint16_t tmpCRC = 0, i; |
277 | uint16_t tmpchecksum = 0, i; |
270 | for (i = 0; i < datalen; i++) { |
278 | for (i = 0; i < datalen; i++) { |
271 | tmpCRC += txd_buffer[i]; |
279 | tmpchecksum += txd_buffer[i]; |
272 | } |
280 | } |
273 | tmpCRC %= 4096; |
281 | tmpchecksum %= 4096; |
274 | txd_buffer[i++] = '=' + tmpCRC / 64; |
282 | txd_buffer[i++] = '=' + (tmpchecksum >> 6); |
275 | txd_buffer[i++] = '=' + tmpCRC % 64; |
283 | txd_buffer[i++] = '=' + (tmpchecksum & 0x3F); |
276 | txd_buffer[i++] = '\r'; |
284 | txd_buffer[i++] = '\r'; |
277 | txd_complete = FALSE; |
285 | txd_complete = FALSE; |
278 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
286 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
Line 279... | Line 287... | ||
279 | } |
287 | } |
280 | 288 | ||
281 | // -------------------------------------------------------------------------- |
289 | // -------------------------------------------------------------------------- |
282 | // application example: |
290 | // application example: |
283 | // SendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16); |
291 | // sendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16); |
284 | /* |
292 | /* |
285 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
293 | void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
286 | va_list ap; |
294 | va_list ap; |
287 | uint16_t txd_bufferIndex = 0; |
295 | uint16_t txd_bufferIndex = 0; |
288 | uint8_t *currentBuffer; |
296 | uint8_t *currentBuffer; |
Line 323... | Line 331... | ||
323 | // We need to stuff with zero bytes at the end. |
331 | // We need to stuff with zero bytes at the end. |
324 | txd_buffer[txd_bufferIndex++] &= 0b00111100; |
332 | txd_buffer[txd_bufferIndex++] &= 0b00111100; |
325 | txd_buffer[txd_bufferIndex] = 0; |
333 | txd_buffer[txd_bufferIndex] = 0; |
326 | } |
334 | } |
327 | va_end(ap); |
335 | va_end(ap); |
328 | AddCRC(pt); // add checksum after data block and initates the transmission |
336 | Addchecksum(pt); // add checksum after data block and initates the transmission |
329 | } |
337 | } |
330 | */ |
338 | */ |
Line 331... | Line 339... | ||
331 | 339 | ||
332 | void SendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
340 | void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
333 | va_list ap; |
341 | va_list ap; |
334 | uint16_t pt = 0; |
342 | uint16_t pt = 0; |
335 | uint8_t a, b, c; |
343 | uint8_t a, b, c; |
Line 389... | Line 397... | ||
389 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
397 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
390 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
398 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
391 | txd_buffer[pt++] = '=' + (c & 0x3f); |
399 | txd_buffer[pt++] = '=' + (c & 0x3f); |
392 | } |
400 | } |
393 | va_end(ap); |
401 | va_end(ap); |
394 | AddCRC(pt); // add checksum after data block and initates the transmission |
402 | addChecksum(pt); // add checksum after data block and initates the transmission |
395 | } |
403 | } |
Line 396... | Line 404... | ||
396 | 404 | ||
397 | // -------------------------------------------------------------------------- |
405 | // -------------------------------------------------------------------------- |
398 | void Decode64(void) { |
406 | void decode64(void) { |
399 | uint8_t a, b, c, d; |
407 | uint8_t a, b, c, d; |
400 | uint8_t x, y, z; |
408 | uint8_t x, y, z; |
401 | uint8_t ptrIn = 3; |
409 | uint8_t ptrIn = 3; |
402 | uint8_t ptrOut = 3; |
410 | uint8_t ptrOut = 3; |
Line 403... | Line 411... | ||
403 | uint8_t len = ReceivedBytes - 6; |
411 | uint8_t len = receivedBytes - 6; |
404 | 412 | ||
405 | while (len) { |
413 | while (len) { |
406 | a = rxd_buffer[ptrIn++] - '='; |
414 | a = rxd_buffer[ptrIn++] - '='; |
Line 425... | Line 433... | ||
425 | rxd_buffer[ptrOut++] = z; |
433 | rxd_buffer[ptrOut++] = z; |
426 | else |
434 | else |
427 | break; |
435 | break; |
428 | } |
436 | } |
429 | pRxData = &rxd_buffer[3]; |
437 | pRxData = &rxd_buffer[3]; |
430 | RxDataLen = ptrOut - 3; |
438 | rxDataLen = ptrOut - 3; |
431 | } |
439 | } |
Line 432... | Line 440... | ||
432 | 440 | ||
433 | // -------------------------------------------------------------------------- |
441 | // -------------------------------------------------------------------------- |
434 | void usart0_ProcessRxData(void) { |
442 | void usart0_processRxData(void) { |
435 | // We control the outputTestActive var from here: Count it down. |
443 | // We control the servoTestActive var from here: Count it down. |
436 | if (outputTestActive) |
444 | if (servoTestActive) |
437 | outputTestActive--; |
445 | servoTestActive--; |
438 | // if data in the rxd buffer are not locked immediately return |
446 | // if data in the rxd buffer are not locked immediately return |
439 | if (!rxd_buffer_locked) |
447 | if (!rxd_buffer_locked) |
440 | return; |
448 | return; |
441 | uint8_t tempchar1, tempchar2; |
449 | uint8_t tempchar[3]; |
Line 442... | Line 450... | ||
442 | Decode64(); // decode data block in rxd_buffer |
450 | decode64(); // decode data block in rxd_buffer |
Line 443... | Line 451... | ||
443 | 451 | ||
444 | switch (rxd_buffer[1] - 'a') { |
452 | switch (rxd_buffer[1] - 'a') { |
445 | 453 | ||
446 | case FC_ADDRESS: |
454 | case FC_ADDRESS: |
- | 455 | switch (rxd_buffer[2]) { |
|
447 | switch (rxd_buffer[2]) { |
456 | #ifdef USE_DIRECT_GPS |
448 | #ifdef USE_MK3MAG |
457 | case 'K':// compass value |
449 | case 'K':// compass value |
458 | // What is the point of this - the compass will overwrite this soon? |
450 | compassHeading = ((Heading_t *)pRxData)->Heading; |
459 | magneticHeading = ((Heading_t *)pRxData)->heading; |
451 | // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180; |
460 | // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180; |
452 | break; |
461 | break; |
453 | #endif |
462 | #endif |
454 | case 't': // motor test |
463 | case 't': // motor test |
455 | if (RxDataLen > 20) { |
464 | if (rxDataLen > 20) { |
456 | memcpy(&outputTest[0], (uint8_t*) pRxData, sizeof(outputTest)); |
465 | memcpy(&servoTest[0], (uint8_t*) pRxData, sizeof(servoTest)); |
457 | } else { |
466 | } else { |
458 | memcpy(&outputTest[0], (uint8_t*) pRxData, 4); |
467 | memcpy(&servoTest[0], (uint8_t*) pRxData, 4); |
459 | } |
468 | } |
Line 460... | Line -... | ||
460 | outputTestActive = 255; |
- | |
461 | externalControlActive = 255; |
- | |
462 | break; |
- | |
463 | - | ||
464 | case 'n':// "Get Mixer Table |
- | |
465 | while (!txd_complete) |
- | |
466 | ; // wait for previous frame to be sent |
- | |
467 | SendOutData('N', FC_ADDRESS, 1, (uint8_t *) &Mixer, sizeof(Mixer)); |
- | |
468 | break; |
- | |
469 | - | ||
470 | case 'm':// "Set Mixer Table |
- | |
471 | if (pRxData[0] == EEMIXER_REVISION) { |
- | |
472 | memcpy(&Mixer, (uint8_t*) pRxData, sizeof(Mixer)); |
- | |
473 | MixerTable_WriteToEEProm(); |
- | |
474 | while (!txd_complete) |
- | |
475 | ; // wait for previous frame to be sent |
- | |
476 | tempchar1 = 1; |
- | |
477 | } else { |
- | |
478 | tempchar1 = 0; |
- | |
479 | } |
469 | servoTestActive = 255; |
480 | SendOutData('M', FC_ADDRESS, 1, &tempchar1, 1); |
470 | externalControlActive = 255; |
481 | break; |
471 | break; |
Line -... | Line 472... | ||
- | 472 | ||
- | 473 | case 'p': // get PPM channels |
|
- | 474 | request_PPMChannels = TRUE; |
|
- | 475 | break; |
|
- | 476 | ||
- | 477 | case 'i':// Read IMU configuration |
|
- | 478 | tempchar[0] = IMUCONFIG_REVISION; |
|
- | 479 | tempchar[1] = sizeof(IMUConfig); |
|
- | 480 | while (!txd_complete) |
|
- | 481 | ; // wait for previous frame to be sent |
|
- | 482 | sendOutData('I', FC_ADDRESS, 2, &tempchar, 2, (uint8_t *) &IMUConfig, sizeof(IMUConfig)); |
|
- | 483 | break; |
|
- | 484 | ||
- | 485 | case 'j':// Save IMU configuration |
|
- | 486 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
|
- | 487 | { |
|
- | 488 | if ((pRxData[0] == IMUCONFIG_REVISION) && (pRxData[1] == sizeof(IMUConfig))) { |
|
- | 489 | memcpy(&IMUConfig, (uint8_t*) &pRxData[2], sizeof(IMUConfig)); |
|
- | 490 | IMUConfig_writeToEEprom(); |
|
- | 491 | tempchar[0] = 1; //indicate ok data |
|
- | 492 | } else { |
|
- | 493 | tempchar[0] = 0; //indicate bad data |
|
- | 494 | } |
|
- | 495 | while (!txd_complete) |
|
482 | 496 | ; // wait for previous frame to be sent |
|
483 | case 'p': // get PPM channels |
497 | sendOutData('J', FC_ADDRESS, 1, &tempchar, 1); |
484 | request_PPMChannels = TRUE; |
498 | } |
485 | break; |
499 | break; |
486 | 500 | ||
487 | case 'q':// request settings |
501 | case 'q':// request settings |
488 | if (pRxData[0] == 0xFF) { |
502 | if (pRxData[0] == 0xFF) { |
489 | pRxData[0] = GetParamByte(PID_ACTIVE_SET); |
503 | pRxData[0] = getParamByte(PID_ACTIVE_SET); |
490 | } |
504 | } |
491 | // limit settings range |
505 | // limit settings range |
- | 506 | if (pRxData[0] < 1) |
|
492 | if (pRxData[0] < 1) |
507 | pRxData[0] = 1; // limit to 1 |
- | 508 | else if (pRxData[0] > 5) |
|
493 | pRxData[0] = 1; // limit to 1 |
509 | pRxData[0] = 5; // limit to 5 |
494 | else if (pRxData[0] > 5) |
510 | // load requested parameter set |
- | 511 | ||
495 | pRxData[0] = 5; // limit to 5 |
512 | paramSet_readFromEEProm(pRxData[0]); |
496 | // load requested parameter set |
513 | |
497 | ParamSet_ReadFromEEProm(); |
514 | tempchar[0] = pRxData[0]; |
498 | tempchar1 = pRxData[0]; |
- | |
499 | tempchar2 = EEPARAM_REVISION; |
- | |
500 | while (!txd_complete) |
515 | tempchar[1] = EEPARAM_REVISION; |
Line 501... | Line 516... | ||
501 | ; // wait for previous frame to be sent |
516 | tempchar[2] = sizeof(staticParams); |
502 | SendOutData('Q', FC_ADDRESS, 3, &tempchar1, sizeof(tempchar1), |
517 | while (!txd_complete) |
503 | &tempchar2, sizeof(tempchar2), (uint8_t *) &staticParams, |
518 | ; // wait for previous frame to be sent |
504 | sizeof(staticParams)); |
- | |
505 | break; |
519 | sendOutData('Q', FC_ADDRESS, 2, &tempchar, 3, (uint8_t *) &staticParams, sizeof(staticParams)); |
506 | 520 | break; |
|
507 | case 's': // save settings |
521 | |
508 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
522 | case 's': // save settings |
- | 523 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
|
- | 524 | { |
|
509 | { |
525 | if ((pRxData[1] == EEPARAM_REVISION) && (pRxData[2] == sizeof(staticParams))) // check for setting to be in range and version of settings |
510 | if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] |
526 | { |
511 | == EEPARAM_REVISION)) // check for setting to be in range and version of settings |
527 | memcpy(&staticParams, (uint8_t*) &pRxData[3], sizeof(staticParams)); |
512 | { |
528 | paramSet_writeToEEProm(1); |
513 | memcpy(&staticParams, (uint8_t*) &pRxData[2], sizeof(staticParams)); |
529 | configuration_paramSetDidChange(); |
514 | ParamSet_WriteToEEProm(); |
530 | tempchar[0] = 1; |
515 | beepNumber(1); |
531 | beepNumber(tempchar[0]); |
516 | } else { |
532 | } else { |
517 | tempchar1 = 0; //indicate bad data |
533 | tempchar[0] = 0; //indicate bad data |
Line 518... | Line 534... | ||
518 | } |
534 | } |
519 | while (!txd_complete) |
535 | while (!txd_complete) |
Line 528... | Line 544... | ||
528 | } // case FC_ADDRESS: |
544 | } // case FC_ADDRESS: |
Line 529... | Line 545... | ||
529 | 545 | ||
530 | default: // any Slave Address |
546 | default: // any Slave Address |
531 | switch (rxd_buffer[2]) { |
547 | switch (rxd_buffer[2]) { |
532 | case 'a':// request for labels of the analog debug outputs |
548 | case 'a':// request for labels of the analog debug outputs |
533 | request_DebugLabel = pRxData[0]; |
549 | requestedDebugLabel = pRxData[0]; |
534 | if (request_DebugLabel > 31) |
550 | if (requestedDebugLabel > 31) |
535 | request_DebugLabel = 31; |
- | |
536 | externalControlActive = 255; |
551 | requestedDebugLabel = 31; |
Line 537... | Line 552... | ||
537 | break; |
552 | break; |
538 | 553 | ||
539 | case 'b': // submit extern control |
554 | case 'b': // submit extern control |
540 | memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl)); |
555 | memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl)); |
541 | ConfirmFrame = externalControl.frame; |
556 | confirmFrame = externalControl.frame; |
Line 542... | Line 557... | ||
542 | externalControlActive = 255; |
557 | externalControlActive = 255; |
543 | break; |
558 | break; |
544 | - | ||
545 | case 'h':// request for display columns |
- | |
546 | externalControlActive = 255; |
- | |
547 | request_Display = TRUE; |
- | |
548 | break; |
559 | |
549 | 560 | case 'o':// request for OSD data (FC style) |
|
550 | case 'l':// request for display columns |
561 | OSD_interval = (uint16_t) pRxData[0] * 10; |
551 | externalControlActive = 255; |
562 | if (OSD_interval > 0) |
552 | request_Display1 = TRUE; |
563 | request_OSD = TRUE; |
553 | break; |
564 | break; |
554 | 565 | ||
Line 555... | Line 566... | ||
555 | case 'v': // request for version and board release |
566 | case 'v': // request for version and board release |
556 | request_VerInfo = TRUE; |
567 | request_verInfo = TRUE; |
557 | break; |
568 | break; |
Line 558... | Line 569... | ||
558 | 569 | ||
559 | case 'x': |
570 | case 'x': |
560 | request_variables = TRUE; |
571 | request_variables = TRUE; |
561 | break; |
572 | break; |
562 | 573 | ||
563 | case 'g':// get external control data |
574 | case 'g':// get external control data |
564 | request_ExternalControl = TRUE; |
575 | request_externalControl = TRUE; |
565 | break; |
576 | break; |
566 | 577 | ||
Line 567... | Line 578... | ||
567 | case 'd': // request for the debug data |
578 | case 'd': // request for the debug data |
568 | DebugData_Interval = (uint16_t) pRxData[0] * 10; |
579 | debugData_interval = (uint16_t) pRxData[0] * 10; |
569 | if (DebugData_Interval > 0) |
580 | if (debugData_interval > 0) |
570 | request_DebugData = TRUE; |
581 | request_debugData = TRUE; |
571 | break; |
582 | break; |
Line 572... | Line 583... | ||
572 | 583 | ||
573 | case 'c': // request for the 3D data |
584 | case 'c': // request for the 3D data |
574 | Data3D_Interval = (uint16_t) pRxData[0] * 10; |
585 | data3D_interval = (uint16_t) pRxData[0] * 10; |
575 | if (Data3D_Interval > 0) |
586 | if (data3D_interval > 0) |
576 | request_Data3D = TRUE; |
587 | request_data3D = TRUE; |
577 | break; |
588 | break; |
578 | 589 | ||
579 | default: |
590 | default: |
580 | //unsupported command received |
591 | //unsupported command received |
581 | break; |
592 | break; |
582 | } |
593 | } |
Line 583... | Line 594... | ||
583 | break; // default: |
594 | break; // default: |
584 | } |
595 | } |
585 | // unlock the rxd buffer after processing |
596 | // unlock the rxd buffer after processing |
586 | pRxData = 0; |
597 | pRxData = 0; |
587 | RxDataLen = 0; |
598 | rxDataLen = 0; |
588 | rxd_buffer_locked = FALSE; |
599 | rxd_buffer_locked = FALSE; |
589 | } |
600 | } |
Line 600... | Line 611... | ||
600 | UDR0 = c; |
611 | UDR0 = c; |
601 | return (0); |
612 | return (0); |
602 | } |
613 | } |
Line 603... | Line 614... | ||
603 | 614 | ||
604 | //--------------------------------------------------------------------------------------------- |
615 | //--------------------------------------------------------------------------------------------- |
605 | void usart0_TransmitTxData(void) { |
616 | void usart0_transmitTxData(void) { |
606 | if (!txd_complete) |
617 | if (!txd_complete) |
Line 607... | Line 618... | ||
607 | return; |
618 | return; |
608 | 619 | ||
609 | if (request_VerInfo && txd_complete) { |
- | |
610 | SendOutData('V', FC_ADDRESS, 1, (uint8_t *) &UART_VersionInfo, |
620 | if (request_verInfo && txd_complete) { |
611 | sizeof(UART_VersionInfo)); |
- | |
612 | request_VerInfo = FALSE; |
- | |
613 | } |
- | |
614 | - | ||
615 | if (request_Display && txd_complete) { |
- | |
616 | //LCD_PrintMenu(); |
- | |
617 | SendOutData('H', FC_ADDRESS, 0); |
- | |
618 | DisplayLine++; |
- | |
619 | if (DisplayLine >= 4) |
- | |
620 | DisplayLine = 0; |
621 | sendOutData('V', FC_ADDRESS, 1, (uint8_t *) &versionInfo, sizeof(versionInfo)); |
Line 621... | Line -... | ||
621 | request_Display = FALSE; |
- | |
622 | } |
- | |
623 | - | ||
624 | if (request_Display1 && txd_complete) { |
- | |
625 | // LCD_PrintMenu(); |
- | |
626 | SendOutData('L', FC_ADDRESS, 0); |
- | |
627 | request_Display1 = FALSE; |
622 | request_verInfo = FALSE; |
628 | } |
623 | } |
629 | 624 | ||
630 | if (request_DebugLabel != 0xFF) { // Texte für die Analogdaten |
625 | if (requestedDebugLabel != 0xFF && txd_complete) { // Texte f�r die Analogdaten |
631 | uint8_t label[16]; // local sram buffer |
626 | uint8_t label[16]; // local sram buffer |
632 | memcpy_P(label, ANALOG_LABEL[request_DebugLabel], 16); // read lable from flash to sram buffer |
627 | memcpy_P(label, ANALOG_LABEL[requestedDebugLabel], 16); // read lable from flash to sram buffer |
633 | SendOutData('A', FC_ADDRESS, 2, (uint8_t *) &request_DebugLabel, |
628 | sendOutData('A', FC_ADDRESS, 2, (uint8_t *) &requestedDebugLabel, |
Line 634... | Line 629... | ||
634 | sizeof(request_DebugLabel), label, 16); |
629 | sizeof(requestedDebugLabel), label, 16); |
635 | request_DebugLabel = 0xFF; |
630 | requestedDebugLabel = 0xFF; |
636 | } |
- | |
637 | 631 | } |
|
638 | if (ConfirmFrame && txd_complete) { // Datensatz ohne CRC bestätigen |
- | |
639 | SendOutData('B', FC_ADDRESS, 1, (uint8_t*) &ConfirmFrame, |
- | |
640 | sizeof(ConfirmFrame)); |
- | |
641 | ConfirmFrame = 0; |
- | |
642 | } |
- | |
643 | - | ||
644 | if (((DebugData_Interval && checkDelay(DebugData_Timer)) || request_DebugData) |
- | |
645 | && txd_complete) { |
632 | |
Line 646... | Line 633... | ||
646 | SendOutData('D', FC_ADDRESS, 1, (uint8_t *) &DebugOut, sizeof(DebugOut)); |
633 | if (confirmFrame && txd_complete) { // Datensatz ohne checksum best�tigen |
647 | DebugData_Timer = setDelay(DebugData_Interval); |
634 | sendOutData('B', FC_ADDRESS, 1, (uint8_t*) &confirmFrame, sizeof(confirmFrame)); |
648 | request_DebugData = FALSE; |
635 | confirmFrame = 0; |
649 | } |
636 | } |
- | 637 | ||
- | 638 | if (((debugData_interval && checkDelay(debugData_timer)) || request_debugData) |
|
- | 639 | && txd_complete) { |
|
- | 640 | sendOutData('D', FC_ADDRESS, 1, (uint8_t *) &debugOut, sizeof(debugOut)); |
|
650 | 641 | debugData_timer = setDelay(debugData_interval); |
|
651 | if (((Data3D_Interval && checkDelay(Data3D_Timer)) || request_Data3D) |
642 | request_debugData = FALSE; |
652 | && txd_complete) { |
643 | } |
653 | SendOutData('C', FC_ADDRESS, 1, (uint8_t *) &Data3D, sizeof(Data3D)); |
644 | |
654 | Data3D.AngleNick = (int16_t) ((10 * angle[PITCH]) |
645 | if (((data3D_interval && checkDelay(data3D_timer)) || request_data3D) && txd_complete) { |
655 | / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1° |
646 | sendOutData('C', FC_ADDRESS, 1, (uint8_t *) &data3D, sizeof(data3D)); |
656 | Data3D.AngleRoll = (int16_t) ((10 * angle[ROLL]) |
647 | data3D.anglePitch = (int16_t) (attitude[PITCH] / (GYRO_DEG_FACTOR/10)); // convert to multiple of 0.1 deg |
Line 657... | Line 648... | ||
657 | / GYRO_DEG_FACTOR_PITCHROLL); // convert to multiple of 0.1° |
648 | data3D.angleRoll = (int16_t) (attitude[ROLL] / (GYRO_DEG_FACTOR/10)); // convert to multiple of 0.1 deg |
658 | Data3D.Heading = (int16_t) ((10 * angle[YAW]) / GYRO_DEG_FACTOR_YAW); // convert to multiple of 0.1° |
649 | data3D.heading = (int16_t) (attitude[YAW] / (GYRO_DEG_FACTOR/10)); // convert to multiple of 0.1 deg |
659 | Data3D_Timer = setDelay(Data3D_Interval); |
650 | data3D_timer = setDelay(data3D_interval); |
660 | request_Data3D = FALSE; |
651 | request_data3D = FALSE; |
661 | } |
652 | } |
Line 662... | Line -... | ||
662 | - | ||
663 | if (request_ExternalControl && txd_complete) { |
- | |
664 | SendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl, |
- | |
665 | sizeof(externalControl)); |
- | |
666 | request_ExternalControl = FALSE; |
- | |
667 | } |
- | |
668 | - | ||
669 | - | ||
670 | #ifdef USE_MK3MAG |
- | |
671 | if((checkDelay(Compass_Timer)) && txd_complete) { |
- | |
672 | ToMk3Mag.Attitude[0] = (int16_t)((10 * angle[PITCH]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg |
- | |
673 | ToMk3Mag.Attitude[1] = (int16_t)((10 * angle[ROLL]) / GYRO_DEG_FACTOR_PITCHROLL); // approx. 0.1 deg |
- | |
674 | ToMk3Mag.UserParam[0] = dynamicParams.UserParams[0]; |
- | |
675 | ToMk3Mag.UserParam[1] = dynamicParams.UserParams[1]; |
- | |
676 | ToMk3Mag.CalState = compassCalState; |
653 | |
677 | SendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &ToMk3Mag,sizeof(ToMk3Mag)); |
654 | if (request_externalControl && txd_complete) { |
678 | // the last state is 5 and should be send only once to avoid multiple flash writing |
655 | sendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl, |
679 | if(compassCalState > 4) compassCalState = 0; |
656 | sizeof(externalControl)); |
Line 680... | Line 657... | ||
680 | Compass_Timer = setDelay(99); |
657 | request_externalControl = FALSE; |
681 | } |
658 | } |
682 | #endif |
659 | |
683 | 660 | ||
Line 684... | Line 661... | ||
684 | if (request_OutputTest && txd_complete) { |
661 | if (request_servoTest && txd_complete) { |
685 | SendOutData('T', FC_ADDRESS, 0); |
662 | sendOutData('T', FC_ADDRESS, 0); |
686 | request_OutputTest = FALSE; |
663 | request_servoTest = FALSE; |
687 | } |
664 | } |
- | 665 | ||
- | 666 | if (request_PPMChannels && txd_complete) { |
|
- | 667 | sendOutData('P', FC_ADDRESS, 1, (uint8_t *) &PPM_in, sizeof(PPM_in)); |
|
- | 668 | request_PPMChannels = FALSE; |
|
- | 669 | } |
|
- | 670 | ||
- | 671 | if (request_variables && txd_complete) { |
|
- | 672 | sendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables)); |
|
- | 673 | request_variables = FALSE; |
|
- | 674 | } |
|
- | 675 | ||
688 | 676 | if (((OSD_interval && checkDelay(OSD_timer)) || request_OSD) && txd_complete) { |