Rev 2164 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2164 | Rev 2189 | ||
---|---|---|---|
1 | #include <avr/io.h> |
1 | #include <avr/io.h> |
2 | #include <avr/interrupt.h> |
2 | #include <avr/interrupt.h> |
3 | #include <avr/wdt.h> |
3 | #include <avr/wdt.h> |
4 | #include <avr/pgmspace.h> |
4 | #include <avr/pgmspace.h> |
5 | #include <stdarg.h> |
5 | #include <stdarg.h> |
6 | #include <string.h> |
6 | #include <string.h> |
7 | 7 | ||
8 | #include "eeprom.h" |
8 | #include "eeprom.h" |
9 | #include "menu.h" |
- | |
10 | #include "timer0.h" |
9 | #include "timer0.h" |
11 | #include "uart0.h" |
10 | #include "uart0.h" |
12 | #include "rc.h" |
11 | #include "rc.h" |
13 | #include "externalControl.h" |
12 | #include "externalControl.h" |
14 | #include "output.h" |
13 | #include "debug.h" |
15 | #include "attitude.h" |
14 | #include "profiler.h" |
16 | #include "commands.h" |
15 | #include "beeper.h" |
17 | 16 | ||
18 | #ifdef USE_DIRECT_GPS |
17 | #ifdef USE_DIRECT_GPS |
19 | #include "mk3mag.h" |
18 | #include "mk3mag.h" |
20 | #endif |
19 | #endif |
21 | 20 | ||
22 | #define FC_ADDRESS 1 |
21 | #define FC_ADDRESS 1 |
23 | #define NC_ADDRESS 2 |
22 | #define NC_ADDRESS 2 |
24 | #define MK3MAG_ADDRESS 3 |
23 | #define MK3MAG_ADDRESS 3 |
25 | 24 | ||
26 | #define FALSE 0 |
25 | #define FALSE 0 |
27 | #define TRUE 1 |
26 | #define TRUE 1 |
28 | 27 | ||
- | 28 | DebugOut_t debugOut; |
|
- | 29 | ||
- | 30 | uint8_t requestedAnalogLabel = 255; |
|
29 | uint8_t requestedDebugLabel = 255; |
31 | uint8_t requestedProfilerLabel = 255; |
30 | 32 | ||
31 | uint8_t request_verInfo = FALSE; |
33 | uint8_t request_verInfo; |
32 | uint8_t request_externalControl = FALSE; |
34 | uint8_t request_externalControl; |
33 | uint8_t request_display = FALSE; |
35 | uint8_t request_display; |
34 | uint8_t request_display1 = FALSE; |
36 | uint8_t request_display1; |
35 | uint8_t request_debugData = FALSE; |
37 | uint8_t request_debugData; |
36 | uint8_t request_data3D = FALSE; |
38 | uint8_t request_profilerData; |
37 | uint8_t request_PPMChannels = FALSE; |
39 | uint8_t request_PPMChannels; |
38 | uint8_t request_motorTest = FALSE; |
40 | uint8_t request_outputTest; |
- | 41 | uint8_t request_variables; |
|
39 | uint8_t request_variables = FALSE; |
42 | uint8_t request_OSD; |
40 | uint8_t request_OSD = FALSE; |
43 | uint8_t request_DCM_matrix; |
41 | 44 | ||
42 | /* |
45 | /* |
43 | #define request_verInfo (1<<0) |
46 | #define request_verInfo (1<<0) |
44 | #define request_externalControl (1<<1) |
47 | #define request_externalControl (1<<1) |
45 | #define request_display (1<<3) |
48 | #define request_display (1<<3) |
46 | #define request_display1 (1<<4) |
49 | #define request_display1 (1<<4) |
47 | #define request_debugData (1<<5) |
50 | #define request_debugData (1<<5) |
48 | #define request_data3D (1<<6) |
51 | #define request_data3D (1<<6) |
49 | #define request_PPMChannels (1<<7) |
52 | #define request_PPMChannels (1<<7) |
50 | #define request_motorTest (1<<8) |
53 | #define request_motorTest (1<<8) |
51 | #define request_variables (1<<9) |
54 | #define request_variables (1<<9) |
52 | #define request_OSD (1<<10) |
55 | #define request_OSD (1<<10) |
53 | */ |
56 | */ |
54 | 57 | ||
55 | //uint16_t request = 0; |
58 | //uint16_t request = 0; |
56 | - | ||
57 | uint8_t displayLine = 0; |
- | |
58 | 59 | ||
59 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
60 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
60 | volatile uint8_t rxd_buffer_locked = FALSE; |
61 | volatile uint8_t rxd_buffer_locked = FALSE; |
61 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
62 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
62 | volatile uint8_t txd_complete = TRUE; |
63 | volatile uint8_t txd_complete; |
63 | volatile uint8_t receivedBytes = 0; |
64 | volatile uint8_t receivedBytes; |
64 | volatile uint8_t *pRxData = 0; |
65 | volatile uint8_t *pRxData; |
65 | volatile uint8_t rxDataLen = 0; |
66 | volatile uint8_t rxDataLen; |
66 | 67 | ||
67 | uint8_t motorTestActive = 0; |
68 | uint8_t outputTestActive; |
68 | uint8_t motorTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
69 | uint8_t outputTest[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
69 | uint8_t confirmFrame; |
70 | uint8_t confirmFrame; |
70 | 71 | ||
71 | typedef struct { |
72 | typedef struct { |
72 | int16_t heading; |
73 | int16_t heading; |
73 | }__attribute__((packed)) Heading_t; |
74 | }__attribute__((packed)) Heading_t; |
74 | 75 | ||
75 | Data3D_t data3D; |
76 | IMUData imuData; |
76 | 77 | ||
77 | uint16_t debugData_timer; |
78 | uint16_t debugDataTimer; |
78 | uint16_t data3D_timer; |
79 | uint16_t profilerDataTimer; |
- | 80 | uint16_t OSDDataTimer; |
|
79 | uint16_t OSD_timer; |
81 | uint16_t debugDataInterval; // in 1ms |
80 | uint16_t debugData_interval = 0; // in 1ms |
82 | uint16_t profilerDataInterval; // in 1ms |
81 | uint16_t data3D_interval = 0; // in 1ms |
83 | uint16_t imuDataInterval; // in 1ms |
82 | uint16_t OSD_interval = 0; |
84 | uint16_t OSDDataInterval; |
83 | 85 | ||
84 | #ifdef USE_DIRECT_GPS |
86 | #ifdef USE_DIRECT_GPS |
85 | int16_t toMk3MagTimer; |
87 | int16_t toMk3MagTimer; |
86 | #endif |
88 | #endif |
87 | 89 | ||
88 | // keep lables in flash to save 512 bytes of sram space |
90 | // keep lables in flash to save 512 bytes of sram space |
89 | const prog_uint8_t ANALOG_LABEL[32][16] = { |
- | |
90 | //1234567890123456 |
91 | //1234567890123456 |
- | 92 | const char analogLabel0[] PROGMEM = "AngleRoll"; |
|
- | 93 | const char analogLabel1[] PROGMEM = "AnglePitch"; |
|
- | 94 | const char analogLabel2[] PROGMEM = "AngleYaw"; |
|
- | 95 | const char analogLabel3[] PROGMEM = "GyroX(Roll)"; |
|
- | 96 | const char analogLabel4[] PROGMEM = "GyroY(Pitch)"; |
|
- | 97 | const char analogLabel5[] PROGMEM = "GyroZ(Yaw)"; |
|
- | 98 | const char analogLabel6[] PROGMEM = "AccX(0.01m/s^2)"; |
|
- | 99 | const char analogLabel7[] PROGMEM = "AccY(0.01m/s^2)"; |
|
- | 100 | const char analogLabel8[] PROGMEM = "AccZ(0.01m/s^2)"; |
|
- | 101 | const char analogLabel9[] PROGMEM = "RC pitch"; |
|
- | 102 | const char analogLabel10[] PROGMEM = "RC yaw"; |
|
- | 103 | const char analogLabel11[] PROGMEM = "RC throttle"; |
|
- | 104 | const char analogLabel12[] PROGMEM = "Roll"; |
|
- | 105 | const char analogLabel13[] PROGMEM = "Pitch"; |
|
- | 106 | const char analogLabel14[] PROGMEM = "rollControl"; |
|
- | 107 | const char analogLabel15[] PROGMEM = "pitchControl"; |
|
- | 108 | const char analogLabel16[] PROGMEM = "M1"; |
|
- | 109 | const char analogLabel17[] PROGMEM = "M2"; |
|
- | 110 | const char analogLabel18[] PROGMEM = "M3"; |
|
- | 111 | const char analogLabel19[] PROGMEM = "M4"; |
|
- | 112 | const char analogLabel20[] PROGMEM = "flightmode"; |
|
- | 113 | const char analogLabel21[] PROGMEM = "Att freq"; |
|
- | 114 | const char analogLabel22[] PROGMEM = "Height[dm]"; |
|
- | 115 | const char analogLabel23[] PROGMEM = "dHeight"; |
|
- | 116 | const char analogLabel24[] PROGMEM = "attitudeSumCount"; |
|
- | 117 | const char analogLabel25[] PROGMEM = "simpleAirPressure"; |
|
- | 118 | const char analogLabel26[] PROGMEM = "OCR0A"; |
|
- | 119 | const char analogLabel27[] PROGMEM = "filteredAirPressure"; |
|
- | 120 | const char analogLabel28[] PROGMEM = "height"; |
|
- | 121 | const char analogLabel29[] PROGMEM = "Gyro Act Cont."; |
|
- | 122 | const char analogLabel30[] PROGMEM = "GPS altitude"; |
|
- | 123 | const char analogLabel31[] PROGMEM = "GPS vert accura"; |
|
- | 124 | ||
91 | "AnglePitch ", //0 |
125 | PGM_P ANALOG_LABELS[] PROGMEM = { |
- | 126 | analogLabel0, |
|
92 | "AngleRoll ", |
127 | analogLabel1, |
93 | "AngleYaw ", |
128 | analogLabel2, |
94 | "GyroPitch ", |
129 | analogLabel3, |
95 | "GyroRoll ", |
130 | analogLabel4, |
96 | "GyroYaw ", //5 |
131 | analogLabel5, |
97 | "AccPitch(0.1deg)", |
132 | analogLabel6, |
98 | "AccRoll(0.1deg) ", |
133 | analogLabel7, |
99 | "AccZ ", |
134 | analogLabel8, |
100 | "RC pitch ", |
135 | analogLabel9, |
101 | "RC roll ", //10 |
136 | analogLabel10, |
102 | " ", |
137 | analogLabel11, |
103 | "zerothOrderCorr ", |
138 | analogLabel12, |
104 | "DriftCompPitch ", |
139 | analogLabel13, |
105 | "DriftCompRoll ", |
140 | analogLabel14, |
106 | "GActivityDivider", //15 |
141 | analogLabel15, |
107 | "AccPitch ", |
142 | analogLabel16, |
108 | "AccRoll ", |
143 | analogLabel17, |
109 | "CorrectionSum pi", |
144 | analogLabel18, |
110 | "CorrectionSum ro", |
145 | analogLabel19, |
111 | "control act wghd", //20 |
146 | analogLabel20, |
112 | "acc vector wghd ", |
147 | analogLabel21, |
113 | "Height[dm] ", |
148 | analogLabel22, |
114 | "dHeight ", |
149 | analogLabel23, |
115 | "acc vector ", |
150 | analogLabel24, |
116 | "EFT ", //25 |
151 | analogLabel25, |
117 | "naviPitch ", |
152 | analogLabel26, |
118 | "naviRoll ", |
153 | analogLabel27, |
119 | "Rate Tolerance ", |
154 | analogLabel28, |
120 | "Gyro Act Cont. ", |
155 | analogLabel29, |
121 | "GPS altitude ", //30 |
156 | analogLabel30, |
122 | "GPS vert accura " |
157 | analogLabel31 |
123 | }; |
158 | }; |
124 | 159 | ||
125 | /****************************************************************/ |
160 | /****************************************************************/ |
126 | /* Initialization of the USART0 */ |
161 | /* Initialization of the USART0 */ |
127 | /****************************************************************/ |
162 | /****************************************************************/ |
128 | void usart0_init(void) { |
163 | void usart0_init(void) { |
129 | uint8_t sreg = SREG; |
164 | uint8_t sreg = SREG; |
130 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK / (8 * USART0_BAUD) - 1); |
165 | uint16_t ubrr = (F_CPU / (8 * USART0_BAUD) - 1); |
131 | 166 | ||
132 | // disable all interrupts before configuration |
167 | // disable all interrupts before configuration |
133 | cli(); |
168 | cli(); |
134 | 169 | ||
135 | // disable RX-Interrupt |
170 | // disable RX-Interrupt |
136 | UCSR0B &= ~(1 << RXCIE0); |
171 | UCSR0B &= ~(1 << RXCIE0); |
137 | // disable TX-Interrupt |
172 | // disable TX-Interrupt |
138 | UCSR0B &= ~(1 << TXCIE0); |
173 | UCSR0B &= ~(1 << TXCIE0); |
139 | 174 | ||
140 | // set direction of RXD0 and TXD0 pins |
175 | // set direction of RXD0 and TXD0 pins |
141 | // set RXD0 (PD0) as an input pin |
176 | // set RXD0 (PD0) as an input pin |
142 | PORTD |= (1 << PORTD0); |
177 | PORTD |= (1 << PORTD0); |
143 | DDRD &= ~(1 << DDD0); |
178 | DDRD &= ~(1 << DDD0); |
144 | // set TXD0 (PD1) as an output pin |
179 | // set TXD0 (PD1) as an output pin |
145 | PORTD |= (1 << PORTD1); |
180 | PORTD |= (1 << PORTD1); |
146 | DDRD |= (1 << DDD1); |
181 | DDRD |= (1 << DDD1); |
147 | 182 | ||
148 | // USART0 Baud Rate Register |
183 | // USART0 Baud Rate Register |
149 | // set clock divider |
184 | // set clock divider |
150 | UBRR0H = (uint8_t) (ubrr >> 8); |
185 | UBRR0H = (uint8_t) (ubrr >> 8); |
151 | UBRR0L = (uint8_t) ubrr; |
186 | UBRR0L = (uint8_t) ubrr; |
152 | 187 | ||
153 | // USART0 Control and Status Register A, B, C |
188 | // USART0 Control and Status Register A, B, C |
154 | 189 | ||
155 | // enable double speed operation in |
190 | // enable double speed operation in |
156 | UCSR0A |= (1 << U2X0); |
191 | UCSR0A |= (1 << U2X0); |
157 | // enable receiver and transmitter in |
192 | // enable receiver and transmitter in |
158 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
193 | UCSR0B = (1 << TXEN0) | (1 << RXEN0); |
159 | // set asynchronous mode |
194 | // set asynchronous mode |
160 | UCSR0C &= ~(1 << UMSEL01); |
195 | UCSR0C &= ~(1 << UMSEL01); |
161 | UCSR0C &= ~(1 << UMSEL00); |
196 | UCSR0C &= ~(1 << UMSEL00); |
162 | // no parity |
197 | // no parity |
163 | UCSR0C &= ~(1 << UPM01); |
198 | UCSR0C &= ~(1 << UPM01); |
164 | UCSR0C &= ~(1 << UPM00); |
199 | UCSR0C &= ~(1 << UPM00); |
165 | // 1 stop bit |
200 | // 1 stop bit |
166 | UCSR0C &= ~(1 << USBS0); |
201 | UCSR0C &= ~(1 << USBS0); |
167 | // 8-bit |
202 | // 8-bit |
168 | UCSR0B &= ~(1 << UCSZ02); |
203 | UCSR0B &= ~(1 << UCSZ02); |
169 | UCSR0C |= (1 << UCSZ01); |
204 | UCSR0C |= (1 << UCSZ01); |
170 | UCSR0C |= (1 << UCSZ00); |
205 | UCSR0C |= (1 << UCSZ00); |
171 | 206 | ||
172 | // flush receive buffer |
207 | // flush receive buffer |
173 | while (UCSR0A & (1 << RXC0)) |
208 | while (UCSR0A & (1 << RXC0)) |
174 | UDR0; |
209 | UDR0; |
175 | 210 | ||
176 | // enable interrupts at the end |
211 | // enable interrupts at the end |
177 | // enable RX-Interrupt |
212 | // enable RX-Interrupt |
178 | UCSR0B |= (1 << RXCIE0); |
213 | UCSR0B |= (1 << RXCIE0); |
179 | // enable TX-Interrupt |
214 | // enable TX-Interrupt |
180 | UCSR0B |= (1 << TXCIE0); |
215 | UCSR0B |= (1 << TXCIE0); |
181 | 216 | ||
182 | // initialize the debug timer |
217 | // initialize the debug timer |
183 | debugData_timer = setDelay(debugData_interval); |
218 | debugDataTimer = setDelay(debugDataInterval); |
- | 219 | profilerDataTimer = setDelay(profilerDataInterval); |
|
184 | 220 | ||
185 | // unlock rxd_buffer |
221 | // unlock rxd_buffer |
186 | rxd_buffer_locked = FALSE; |
222 | rxd_buffer_locked = FALSE; |
187 | pRxData = 0; |
223 | pRxData = 0; |
188 | rxDataLen = 0; |
224 | rxDataLen = 0; |
189 | 225 | ||
190 | // no bytes to send |
226 | // no bytes to send |
191 | txd_complete = TRUE; |
227 | txd_complete = TRUE; |
192 | 228 | ||
193 | #ifdef USE_DIRECT_GPS |
229 | #ifdef USE_DIRECT_GPS |
194 | toMk3MagTimer = setDelay(220); |
230 | toMk3MagTimer = setDelay(220); |
195 | #endif |
231 | #endif |
196 | 232 | ||
197 | versionInfo.SWMajor = VERSION_MAJOR; |
233 | versionInfo.SWMajor = VERSION_MAJOR; |
198 | versionInfo.SWMinor = VERSION_MINOR; |
234 | versionInfo.SWMinor = VERSION_MINOR; |
199 | versionInfo.SWPatch = VERSION_PATCH; |
235 | versionInfo.SWPatch = VERSION_PATCH; |
200 | versionInfo.protoMajor = VERSION_SERIAL_MAJOR; |
236 | versionInfo.protoMajor = VERSION_SERIAL_MAJOR; |
201 | versionInfo.protoMinor = VERSION_SERIAL_MINOR; |
237 | versionInfo.protoMinor = VERSION_SERIAL_MINOR; |
202 | 238 | ||
203 | // restore global interrupt flags |
239 | // restore global interrupt flags |
204 | SREG = sreg; |
240 | SREG = sreg; |
205 | } |
241 | } |
206 | 242 | ||
207 | /****************************************************************/ |
243 | /****************************************************************/ |
208 | /* USART0 transmitter ISR */ |
244 | /* USART0 transmitter ISR */ |
209 | /****************************************************************/ |
245 | /****************************************************************/ |
210 | ISR(USART0_TX_vect) { |
246 | ISR(USART0_TX_vect) { |
211 | static uint16_t ptr_txd_buffer = 0; |
247 | static uint16_t ptr_txd_buffer = 0; |
212 | uint8_t tmp_tx; |
248 | uint8_t tmp_tx; |
213 | if (!txd_complete) { // transmission not completed |
249 | if (!txd_complete) { // transmission not completed |
214 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
250 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
215 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
251 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
216 | // if terminating character or end of txd buffer was reached |
252 | // if terminating character or end of txd buffer was reached |
217 | if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) { |
253 | if ((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) { |
218 | ptr_txd_buffer = 0; // reset txd pointer |
254 | ptr_txd_buffer = 0; // reset txd pointer |
219 | txd_complete = 1; // stop transmission |
255 | txd_complete = 1; // stop transmission |
220 | } |
256 | } |
221 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
257 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
222 | } |
258 | } |
223 | // transmission completed |
259 | // transmission completed |
224 | else |
260 | else |
225 | ptr_txd_buffer = 0; |
261 | ptr_txd_buffer = 0; |
226 | } |
262 | } |
227 | 263 | ||
228 | /****************************************************************/ |
264 | /****************************************************************/ |
229 | /* USART0 receiver ISR */ |
265 | /* USART0 receiver ISR */ |
230 | /****************************************************************/ |
266 | /****************************************************************/ |
231 | ISR(USART0_RX_vect) { |
267 | ISR(USART0_RX_vect) { |
232 | static uint16_t checksum; |
268 | static uint16_t checksum; |
233 | static uint8_t ptr_rxd_buffer = 0; |
269 | static uint8_t ptr_rxd_buffer = 0; |
234 | uint8_t checksum1, checksum2; |
270 | uint8_t checksum1, checksum2; |
235 | uint8_t c; |
271 | uint8_t c; |
236 | 272 | ||
237 | c = UDR0; // catch the received byte |
273 | c = UDR0; // catch the received byte |
238 | 274 | ||
239 | if (rxd_buffer_locked) |
275 | if (rxd_buffer_locked) |
240 | return; // if rxd buffer is locked immediately return |
276 | return; // if rxd buffer is locked immediately return |
241 | 277 | ||
242 | // the rxd buffer is unlocked |
278 | // the rxd buffer is unlocked |
243 | if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and syncronisation character is received |
279 | if ((ptr_rxd_buffer == 0) && (c == '#')) { // if rxd buffer is empty and synchronization character is received |
244 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
280 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
245 | checksum = c; // init checksum |
281 | checksum = c; // init checksum |
246 | } |
282 | } |
247 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes |
283 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incoming bytes |
248 | if (c != '\r') { // no termination character |
284 | if (c != '\r') { // no termination character |
249 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
285 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
250 | checksum += c; // update checksum |
286 | checksum += c; // update checksum |
251 | } else { // termination character was received |
287 | } else { // termination character was received |
252 | // the last 2 bytes are no subject for checksum calculation |
288 | // the last 2 bytes are no subject for checksum calculation |
253 | // they are the checksum itself |
289 | // they are the checksum itself |
254 | checksum -= rxd_buffer[ptr_rxd_buffer - 2]; |
290 | checksum -= rxd_buffer[ptr_rxd_buffer - 2]; |
255 | checksum -= rxd_buffer[ptr_rxd_buffer - 1]; |
291 | checksum -= rxd_buffer[ptr_rxd_buffer - 1]; |
256 | // calculate checksum from transmitted data |
292 | // calculate checksum from transmitted data |
257 | checksum %= 4096; |
293 | checksum %= 4096; |
258 | checksum1 = '=' + checksum / 64; |
294 | checksum1 = '=' + checksum / 64; |
259 | checksum2 = '=' + checksum % 64; |
295 | checksum2 = '=' + checksum % 64; |
260 | // compare checksum to transmitted checksum bytes |
296 | // compare checksum to transmitted checksum bytes |
261 | if ((checksum1 == rxd_buffer[ptr_rxd_buffer - 2]) && (checksum2 |
297 | if ((checksum1 == rxd_buffer[ptr_rxd_buffer - 2]) && (checksum2 |
262 | == rxd_buffer[ptr_rxd_buffer - 1])) { |
298 | == rxd_buffer[ptr_rxd_buffer - 1])) { |
263 | // checksum valid |
299 | // checksum valid |
264 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
300 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
265 | receivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
301 | receivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
266 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
302 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
267 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
303 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
268 | if (rxd_buffer[2] == 'R') { |
304 | if (rxd_buffer[2] == 'R') { |
269 | wdt_enable(WDTO_250MS); |
305 | wdt_enable(WDTO_250MS); |
270 | } // Reset-Commando |
306 | } // Reset-Commando |
271 | } else { // checksum invalid |
307 | } else { // checksum invalid |
272 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
308 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
273 | } |
309 | } |
274 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
310 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
275 | } |
311 | } |
276 | } else { // rxd buffer overrun |
312 | } else { // rxd buffer overrun |
277 | ptr_rxd_buffer = 0; // reset rxd buffer |
313 | ptr_rxd_buffer = 0; // reset rxd buffer |
278 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
314 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
279 | } |
315 | } |
280 | } |
316 | } |
281 | 317 | ||
282 | // -------------------------------------------------------------------------- |
318 | // -------------------------------------------------------------------------- |
283 | void addChecksum(uint16_t datalen) { |
319 | void addChecksum(uint16_t datalen) { |
284 | uint16_t tmpchecksum = 0, i; |
320 | uint16_t tmpchecksum = 0, i; |
285 | for (i = 0; i < datalen; i++) { |
321 | for (i = 0; i < datalen; i++) { |
286 | tmpchecksum += txd_buffer[i]; |
322 | tmpchecksum += txd_buffer[i]; |
287 | } |
323 | } |
288 | tmpchecksum %= 4096; |
324 | tmpchecksum %= 4096; |
289 | txd_buffer[i++] = '=' + (tmpchecksum >> 6); |
325 | txd_buffer[i++] = '=' + (tmpchecksum >> 6); |
290 | txd_buffer[i++] = '=' + (tmpchecksum & 0x3F); |
326 | txd_buffer[i++] = '=' + (tmpchecksum & 0x3F); |
291 | txd_buffer[i++] = '\r'; |
327 | txd_buffer[i++] = '\r'; |
292 | txd_complete = FALSE; |
328 | txd_complete = FALSE; |
293 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
329 | UDR0 = txd_buffer[0]; // initiates the transmittion (continued in the TXD ISR) |
294 | } |
330 | } |
295 | 331 | ||
296 | // -------------------------------------------------------------------------- |
332 | // -------------------------------------------------------------------------- |
297 | // application example: |
333 | // application example: |
298 | // sendOutData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16); |
334 | // sendData('A', FC_ADDRESS, 2, (uint8_t *)&request_DebugLabel, sizeof(request_DebugLabel), label, 16); |
299 | /* |
335 | /* |
300 | void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
336 | void sendData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
301 | va_list ap; |
337 | va_list ap; |
302 | uint16_t txd_bufferIndex = 0; |
338 | uint16_t txd_bufferIndex = 0; |
303 | uint8_t *currentBuffer; |
339 | uint8_t *currentBuffer; |
304 | uint8_t currentBufferIndex; |
340 | uint8_t currentBufferIndex; |
305 | uint16_t lengthOfCurrentBuffer; |
341 | uint16_t lengthOfCurrentBuffer; |
306 | uint8_t shift = 0; |
342 | uint8_t shift = 0; |
307 | 343 | ||
308 | txd_buffer[txd_bufferIndex++] = '#'; // Start character |
344 | txd_buffer[txd_bufferIndex++] = '#'; // Start character |
309 | txd_buffer[txd_bufferIndex++] = 'a' + addr; // Address (a=0; b=1,...) |
345 | txd_buffer[txd_bufferIndex++] = 'a' + addr; // Address (a=0; b=1,...) |
310 | txd_buffer[txd_bufferIndex++] = cmd; // Command |
346 | txd_buffer[txd_bufferIndex++] = cmd; // Command |
311 | 347 | ||
312 | va_start(ap, numofbuffers); |
348 | va_start(ap, numofbuffers); |
313 | 349 | ||
314 | while(numofbuffers) { |
350 | while(numofbuffers) { |
315 | currentBuffer = va_arg(ap, uint8_t*); |
351 | currentBuffer = va_arg(ap, uint8_t*); |
316 | lengthOfCurrentBuffer = va_arg(ap, int); |
352 | lengthOfCurrentBuffer = va_arg(ap, int); |
317 | currentBufferIndex = 0; |
353 | currentBufferIndex = 0; |
318 | // Encode data: 3 bytes of data are encoded into 4 bytes, |
354 | // Encode data: 3 bytes of data are encoded into 4 bytes, |
319 | // where the 2 most significant bits are both 0. |
355 | // where the 2 most significant bits are both 0. |
320 | while(currentBufferIndex != lengthOfCurrentBuffer) { |
356 | while(currentBufferIndex != lengthOfCurrentBuffer) { |
321 | if (!shift) txd_buffer[txd_bufferIndex] = 0; |
357 | if (!shift) txd_buffer[txd_bufferIndex] = 0; |
322 | txd_buffer[txd_bufferIndex] |= currentBuffer[currentBufferIndex] >> (shift + 2); |
358 | txd_buffer[txd_bufferIndex] |= currentBuffer[currentBufferIndex] >> (shift + 2); |
323 | txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111; |
359 | txd_buffer[++txd_bufferIndex] = (currentBuffer[currentBufferIndex] << (4 - shift)) & 0b00111111; |
324 | shift += 2; |
360 | shift += 2; |
325 | if (shift == 6) { shift=0; txd_bufferIndex++; } |
361 | if (shift == 6) { shift=0; txd_bufferIndex++; } |
326 | currentBufferIndex++; |
362 | currentBufferIndex++; |
327 | } |
363 | } |
328 | } |
364 | } |
329 | // If the number of data bytes was not divisible by 3, stuff |
365 | // If the number of data bytes was not divisible by 3, stuff |
330 | // with 0 pseudodata until length is again divisible by 3. |
366 | // with 0 pseudodata until length is again divisible by 3. |
331 | if (shift == 2) { |
367 | if (shift == 2) { |
332 | // We need to stuff with zero bytes at the end. |
368 | // We need to stuff with zero bytes at the end. |
333 | txd_buffer[txd_bufferIndex] &= 0b00110000; |
369 | txd_buffer[txd_bufferIndex] &= 0b00110000; |
334 | txd_buffer[++txd_bufferIndex] = 0; |
370 | txd_buffer[++txd_bufferIndex] = 0; |
335 | shift = 4; |
371 | shift = 4; |
336 | } |
372 | } |
337 | if (shift == 4) { |
373 | if (shift == 4) { |
338 | // We need to stuff with zero bytes at the end. |
374 | // We need to stuff with zero bytes at the end. |
339 | txd_buffer[txd_bufferIndex++] &= 0b00111100; |
375 | txd_buffer[txd_bufferIndex++] &= 0b00111100; |
340 | txd_buffer[txd_bufferIndex] = 0; |
376 | txd_buffer[txd_bufferIndex] = 0; |
341 | } |
377 | } |
342 | va_end(ap); |
378 | va_end(ap); |
343 | Addchecksum(pt); // add checksum after data block and initates the transmission |
379 | Addchecksum(pt); // add checksum after data block and initates the transmission |
344 | } |
380 | } |
345 | */ |
381 | */ |
346 | 382 | ||
347 | void sendOutData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
383 | void sendData(uint8_t cmd, uint8_t addr, uint8_t numofbuffers, ...) { // uint8_t *pdata, uint8_t len, ... |
348 | va_list ap; |
384 | va_list ap; |
349 | uint16_t pt = 0; |
385 | uint16_t pt = 0; |
350 | uint8_t a, b, c; |
386 | uint8_t a, b, c; |
351 | uint8_t ptr = 0; |
387 | uint8_t ptr = 0; |
352 | 388 | ||
353 | uint8_t *pdata = 0; |
389 | uint8_t *pdata = 0; |
354 | int len = 0; |
390 | int len = 0; |
355 | 391 | ||
356 | txd_buffer[pt++] = '#'; // Start character |
392 | txd_buffer[pt++] = '#'; // Start character |
357 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
393 | txd_buffer[pt++] = 'a' + addr; // Address (a=0; b=1,...) |
358 | txd_buffer[pt++] = cmd; // Command |
394 | txd_buffer[pt++] = cmd; // Command |
359 | 395 | ||
360 | va_start(ap, numofbuffers); |
396 | va_start(ap, numofbuffers); |
361 | 397 | ||
362 | if (numofbuffers) { |
398 | if (numofbuffers) { |
363 | pdata = va_arg(ap, uint8_t*); |
399 | pdata = va_arg(ap, uint8_t*); |
364 | len = va_arg(ap, int); |
400 | len = va_arg(ap, int); |
365 | ptr = 0; |
401 | ptr = 0; |
366 | numofbuffers--; |
402 | numofbuffers--; |
367 | } |
403 | } |
368 | 404 | ||
369 | while (len) { |
405 | while (len) { |
370 | if (len) { |
406 | if (len) { |
371 | a = pdata[ptr++]; |
407 | a = pdata[ptr++]; |
372 | len--; |
408 | len--; |
373 | if ((!len) && numofbuffers) { |
409 | if ((!len) && numofbuffers) { |
374 | pdata = va_arg(ap, uint8_t*); |
410 | pdata = va_arg(ap, uint8_t*); |
375 | len = va_arg(ap, int); |
411 | len = va_arg(ap, int); |
376 | ptr = 0; |
412 | ptr = 0; |
377 | numofbuffers--; |
413 | numofbuffers--; |
378 | } |
414 | } |
379 | } else |
415 | } else |
380 | a = 0; |
416 | a = 0; |
381 | if (len) { |
417 | if (len) { |
382 | b = pdata[ptr++]; |
418 | b = pdata[ptr++]; |
383 | len--; |
419 | len--; |
384 | if ((!len) && numofbuffers) { |
420 | if ((!len) && numofbuffers) { |
385 | pdata = va_arg(ap, uint8_t*); |
421 | pdata = va_arg(ap, uint8_t*); |
386 | len = va_arg(ap, int); |
422 | len = va_arg(ap, int); |
387 | ptr = 0; |
423 | ptr = 0; |
388 | numofbuffers--; |
424 | numofbuffers--; |
389 | } |
425 | } |
390 | } else |
426 | } else |
391 | b = 0; |
427 | b = 0; |
392 | if (len) { |
428 | if (len) { |
393 | c = pdata[ptr++]; |
429 | c = pdata[ptr++]; |
394 | len--; |
430 | len--; |
395 | if ((!len) && numofbuffers) { |
431 | if ((!len) && numofbuffers) { |
396 | pdata = va_arg(ap, uint8_t*); |
432 | pdata = va_arg(ap, uint8_t*); |
397 | len = va_arg(ap, int); |
433 | len = va_arg(ap, int); |
398 | ptr = 0; |
434 | ptr = 0; |
399 | numofbuffers--; |
435 | numofbuffers--; |
400 | } |
436 | } |
401 | } else |
437 | } else |
402 | c = 0; |
438 | c = 0; |
403 | txd_buffer[pt++] = '=' + (a >> 2); |
439 | txd_buffer[pt++] = '=' + (a >> 2); |
404 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
440 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
405 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
441 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
406 | txd_buffer[pt++] = '=' + (c & 0x3f); |
442 | txd_buffer[pt++] = '=' + (c & 0x3f); |
407 | } |
443 | } |
408 | va_end(ap); |
444 | va_end(ap); |
409 | addChecksum(pt); // add checksum after data block and initates the transmission |
445 | addChecksum(pt); // add checksum after data block and initates the transmission |
410 | } |
446 | } |
411 | 447 | ||
412 | // -------------------------------------------------------------------------- |
448 | // -------------------------------------------------------------------------- |
413 | void decode64(void) { |
449 | void decode64(void) { |
414 | uint8_t a, b, c, d; |
450 | uint8_t a, b, c, d; |
415 | uint8_t x, y, z; |
451 | uint8_t x, y, z; |
416 | uint8_t ptrIn = 3; |
452 | uint8_t ptrIn = 3; |
417 | uint8_t ptrOut = 3; |
453 | uint8_t ptrOut = 3; |
418 | uint8_t len = receivedBytes - 6; |
454 | uint8_t len = receivedBytes - 6; |
419 | 455 | ||
420 | while (len) { |
456 | while (len) { |
421 | a = rxd_buffer[ptrIn++] - '='; |
457 | a = rxd_buffer[ptrIn++] - '='; |
422 | b = rxd_buffer[ptrIn++] - '='; |
458 | b = rxd_buffer[ptrIn++] - '='; |
423 | c = rxd_buffer[ptrIn++] - '='; |
459 | c = rxd_buffer[ptrIn++] - '='; |
424 | d = rxd_buffer[ptrIn++] - '='; |
460 | d = rxd_buffer[ptrIn++] - '='; |
425 | //if(ptrIn > ReceivedBytes - 3) break; |
461 | //if(ptrIn > ReceivedBytes - 3) break; |
426 | 462 | ||
427 | x = (a << 2) | (b >> 4); |
463 | x = (a << 2) | (b >> 4); |
428 | y = ((b & 0x0f) << 4) | (c >> 2); |
464 | y = ((b & 0x0f) << 4) | (c >> 2); |
429 | z = ((c & 0x03) << 6) | d; |
465 | z = ((c & 0x03) << 6) | d; |
430 | 466 | ||
431 | if (len--) |
467 | if (len--) |
432 | rxd_buffer[ptrOut++] = x; |
468 | rxd_buffer[ptrOut++] = x; |
433 | else |
469 | else |
434 | break; |
470 | break; |
435 | if (len--) |
471 | if (len--) |
436 | rxd_buffer[ptrOut++] = y; |
472 | rxd_buffer[ptrOut++] = y; |
437 | else |
473 | else |
438 | break; |
474 | break; |
439 | if (len--) |
475 | if (len--) |
440 | rxd_buffer[ptrOut++] = z; |
476 | rxd_buffer[ptrOut++] = z; |
441 | else |
477 | else |
442 | break; |
478 | break; |
443 | } |
479 | } |
444 | pRxData = &rxd_buffer[3]; |
480 | pRxData = &rxd_buffer[3]; |
445 | rxDataLen = ptrOut - 3; |
481 | rxDataLen = ptrOut - 3; |
446 | } |
482 | } |
447 | 483 | ||
448 | // -------------------------------------------------------------------------- |
484 | // -------------------------------------------------------------------------- |
449 | void usart0_processRxData(void) { |
485 | void usart0_processRxData(void) { |
450 | // We control the motorTestActive var from here: Count it down. |
486 | // We control the outputTestActive var from here: Count it down. |
451 | if (motorTestActive) |
487 | if (outputTestActive) |
452 | motorTestActive--; |
488 | outputTestActive--; |
453 | // if data in the rxd buffer are not locked immediately return |
489 | // if data in the rxd buffer are not locked immediately return |
454 | if (!rxd_buffer_locked) |
490 | if (!rxd_buffer_locked) |
455 | return; |
491 | return; |
- | 492 | ||
456 | uint8_t tempchar[3]; |
493 | uint8_t tempchar[3]; |
457 | decode64(); // decode data block in rxd_buffer |
494 | decode64(); // decode data block in rxd_buffer |
458 | 495 | ||
459 | switch (rxd_buffer[1] - 'a') { |
496 | switch (rxd_buffer[1] - 'a') { |
460 | 497 | ||
461 | case FC_ADDRESS: |
498 | case FC_ADDRESS: |
462 | switch (rxd_buffer[2]) { |
499 | switch (rxd_buffer[2]) { |
463 | #ifdef USE_DIRECT_GPS |
500 | #ifdef USE_DIRECT_GPS |
464 | case 'K':// compass value |
501 | case 'K':// compass value |
465 | // What is the point of this - the compass will overwrite this soon? |
502 | // What is the point of this - the compass will overwrite this soon? |
466 | magneticHeading = ((Heading_t *)pRxData)->heading; |
503 | magneticHeading = ((Heading_t *)pRxData)->heading; |
467 | // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180; |
504 | // compassOffCourse = ((540 + compassHeading - compassCourse) % 360) - 180; |
468 | break; |
505 | break; |
469 | #endif |
506 | #endif |
470 | case 't': // motor test |
507 | case 't': // motor test |
471 | if (rxDataLen > 20) { |
- | |
472 | memcpy(&motorTest[0], (uint8_t*) pRxData, sizeof(motorTest)); |
508 | memcpy(&outputTest[0], (uint8_t*) pRxData, /*sizeof(outputTest)*/ 12); // 12 is an mktool limitation. |
473 | } else { |
- | |
474 | memcpy(&motorTest[0], (uint8_t*) pRxData, 4); |
509 | outputTestActive = 255; |
475 | } |
510 | // Huh?? |
476 | motorTestActive = 255; |
- | |
477 | externalControlActive = 255; |
511 | externalControlActive = 255; |
478 | break; |
512 | break; |
479 | 513 | ||
480 | case 'n':// Read motor mixer |
514 | case 'n':// Read motor mixer |
481 | tempchar[0] = EEMIXER_REVISION; |
515 | tempchar[0] = EEMIXER_REVISION; |
482 | tempchar[1] = sizeof(MotorMixer_t); |
516 | tempchar[1] = sizeof(OutputMixer_t); |
483 | while (!txd_complete) |
517 | while (!txd_complete) |
484 | ; // wait for previous frame to be sent |
518 | ; // wait for previous frame to be sent |
485 | sendOutData('N', FC_ADDRESS, 2, &tempchar, 2, (uint8_t*)&motorMixer, sizeof(motorMixer)); |
519 | sendData('N', FC_ADDRESS, 2, &tempchar, 2, (uint8_t*)&outputMixer, sizeof(OutputMixer_t)); |
486 | break; |
520 | break; |
487 | 521 | ||
488 | case 'm':// "Set Mixer Table |
522 | case 'm':// "Set Mixer Table |
489 | if (pRxData[0] == EEMIXER_REVISION && (pRxData[1] == sizeof(MotorMixer_t))) { |
523 | if (pRxData[0] == EEMIXER_REVISION && (pRxData[1] == sizeof(OutputMixer_t))) { |
490 | memcpy(&motorMixer, (uint8_t*)&pRxData[2], sizeof(motorMixer)); |
524 | memcpy(&outputMixer, (uint8_t*)&pRxData[2], sizeof(OutputMixer_t)); |
491 | motorMixer_writeToEEProm(); |
525 | outputMixer_writeToEEProm(); |
492 | while (!txd_complete) |
526 | while (!txd_complete) |
493 | ; // wait for previous frame to be sent |
527 | ; // wait for previous frame to be sent |
494 | tempchar[0] = 1; |
528 | tempchar[0] = 1; |
495 | } else { |
529 | } else { |
496 | tempchar[0] = 0; |
530 | tempchar[0] = 0; |
497 | } |
531 | } |
498 | sendOutData('M', FC_ADDRESS, 1, &tempchar, 1); |
532 | sendData('M', FC_ADDRESS, 1, &tempchar, 1); |
499 | break; |
533 | break; |
500 | 534 | ||
501 | case 'p': // get PPM channels |
535 | case 'p': // get PPM channels |
502 | request_PPMChannels = TRUE; |
536 | request_PPMChannels = TRUE; |
503 | break; |
537 | break; |
504 | 538 | ||
505 | case 'i':// Read IMU configuration |
539 | case 'i':// Read IMU configuration |
506 | tempchar[0] = IMUCONFIG_REVISION; |
540 | tempchar[0] = IMUCONFIG_REVISION; |
507 | tempchar[1] = sizeof(IMUConfig); |
541 | tempchar[1] = sizeof(IMUConfig); |
508 | while (!txd_complete) |
542 | while (!txd_complete) |
509 | ; // wait for previous frame to be sent |
543 | ; // wait for previous frame to be sent |
510 | sendOutData('I', FC_ADDRESS, 2, &tempchar, 2, (uint8_t *) &IMUConfig, sizeof(IMUConfig)); |
544 | sendData('I', FC_ADDRESS, 2, &tempchar, 2, (uint8_t *) &IMUConfig, sizeof(IMUConfig)); |
511 | break; |
545 | break; |
512 | 546 | ||
513 | case 'j':// Save IMU configuration |
547 | case 'j':// Save IMU configuration |
514 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
548 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
515 | { |
549 | { |
516 | if ((pRxData[0] == IMUCONFIG_REVISION) && (pRxData[1] == sizeof(IMUConfig))) { |
550 | if ((pRxData[0] == IMUCONFIG_REVISION) && (pRxData[1] == sizeof(IMUConfig))) { |
517 | memcpy(&IMUConfig, (uint8_t*) &pRxData[2], sizeof(IMUConfig)); |
551 | memcpy(&IMUConfig, (uint8_t*) &pRxData[2], sizeof(IMUConfig)); |
518 | IMUConfig_writeToEEprom(); |
552 | IMUConfig_writeToEEprom(); |
519 | tempchar[0] = 1; //indicate ok data |
553 | tempchar[0] = 1; //indicate ok data |
520 | } else { |
554 | } else { |
521 | tempchar[0] = 0; //indicate bad data |
555 | tempchar[0] = 0; //indicate bad data |
522 | } |
556 | } |
523 | while (!txd_complete) |
557 | while (!txd_complete) |
524 | ; // wait for previous frame to be sent |
558 | ; // wait for previous frame to be sent |
525 | sendOutData('J', FC_ADDRESS, 1, &tempchar, 1); |
559 | sendData('J', FC_ADDRESS, 1, &tempchar, 1); |
526 | } |
560 | } |
527 | break; |
561 | break; |
528 | 562 | ||
529 | case 'q':// request settings |
563 | case 'q':// request settings |
530 | if (pRxData[0] == 0xFF) { |
564 | if (pRxData[0] == 0xFF) { |
531 | pRxData[0] = getParamByte(PID_ACTIVE_SET); |
565 | pRxData[0] = getParamByte(PID_ACTIVE_SET); |
532 | } |
566 | } |
533 | // limit settings range |
567 | // limit settings range |
534 | if (pRxData[0] < 1) |
568 | if (pRxData[0] < 1) |
535 | pRxData[0] = 1; // limit to 1 |
569 | pRxData[0] = 1; // limit to 1 |
536 | else if (pRxData[0] > 5) |
570 | else if (pRxData[0] > 5) |
537 | pRxData[0] = 5; // limit to 5 |
571 | pRxData[0] = 5; // limit to 5 |
538 | // load requested parameter set |
572 | // load requested parameter set |
539 | 573 | ||
540 | paramSet_readFromEEProm(pRxData[0]); |
574 | paramSet_readFromEEProm(pRxData[0]); |
541 | 575 | ||
542 | tempchar[0] = pRxData[0]; |
576 | tempchar[0] = pRxData[0]; |
543 | tempchar[1] = EEPARAM_REVISION; |
577 | tempchar[1] = EEPARAM_REVISION; |
544 | tempchar[2] = sizeof(staticParams); |
578 | tempchar[2] = sizeof(staticParams); |
545 | while (!txd_complete) |
579 | while (!txd_complete) |
546 | ; // wait for previous frame to be sent |
580 | ; // wait for previous frame to be sent |
547 | sendOutData('Q', FC_ADDRESS, 2, &tempchar, 3, (uint8_t *) &staticParams, sizeof(staticParams)); |
581 | sendData('Q', FC_ADDRESS, 2, &tempchar, 3, (uint8_t *) &staticParams, sizeof(staticParams)); |
548 | break; |
582 | break; |
549 | 583 | ||
550 | case 's': // save settings |
584 | case 's': // save settings |
551 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
585 | if (!(MKFlags & MKFLAG_MOTOR_RUN)) // save settings only if motors are off |
552 | { |
586 | { |
553 | if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION) && (pRxData[2] == sizeof(staticParams))) // check for setting to be in range and version of settings |
587 | if ((1 <= pRxData[0]) && (pRxData[0] <= 5) && (pRxData[1] == EEPARAM_REVISION) && (pRxData[2] == sizeof(staticParams))) // check for setting to be in range and version of settings |
554 | { |
588 | { |
555 | memcpy(&staticParams, (uint8_t*) &pRxData[3], sizeof(staticParams)); |
589 | memcpy(&staticParams, (uint8_t*) &pRxData[3], sizeof(staticParams)); |
556 | paramSet_writeToEEProm(pRxData[0]); |
590 | paramSet_writeToEEProm(pRxData[0]); |
557 | setActiveParamSet(pRxData[0]); |
591 | setActiveParamSet(pRxData[0]); |
558 | configuration_paramSetDidChange(); |
592 | configuration_paramSetDidChange(); |
559 | tempchar[0] = getActiveParamSet(); |
593 | tempchar[0] = getActiveParamSet(); |
560 | beepNumber(tempchar[0]); |
594 | beepNumber(tempchar[0]); |
561 | } else { |
595 | } else { |
562 | tempchar[0] = 0; //indicate bad data |
596 | tempchar[0] = sizeof(staticParams); //indicate bad data |
563 | } |
597 | } |
564 | while (!txd_complete) |
598 | while (!txd_complete) |
565 | ; // wait for previous frame to be sent |
599 | ; // wait for previous frame to be sent |
566 | sendOutData('S', FC_ADDRESS, 1, &tempchar, 1); |
600 | sendData('S', FC_ADDRESS, 1, &tempchar, 1); |
567 | } |
601 | } |
568 | break; |
602 | break; |
569 | 603 | ||
570 | default: |
604 | default: |
571 | //unsupported command received |
605 | //unsupported command received |
572 | break; |
606 | break; |
573 | } // case FC_ADDRESS: |
607 | } // case FC_ADDRESS: |
574 | 608 | ||
575 | default: // any Slave Address |
609 | default: // any Slave Address |
576 | switch (rxd_buffer[2]) { |
610 | switch (rxd_buffer[2]) { |
577 | case 'a':// request for labels of the analog debug outputs |
611 | case 'a':// request for labels of the analog debug outputs |
578 | requestedDebugLabel = pRxData[0]; |
612 | requestedAnalogLabel = pRxData[0]; |
579 | if (requestedDebugLabel > 31) |
613 | if (requestedAnalogLabel > 31) |
580 | requestedDebugLabel = 31; |
614 | requestedAnalogLabel = 31; |
581 | break; |
615 | break; |
582 | 616 | ||
583 | case 'b': // submit extern control |
617 | case 'b': // submit extern control |
584 | memcpy(&externalControl, (uint8_t*) pRxData, sizeof(externalControl)); |
618 | memcpy(&externalControl, (uint8_t*) pRxData, sizeof(ExternalControl_t)); |
585 | confirmFrame = externalControl.frame; |
619 | confirmFrame = externalControl.frame; |
586 | externalControlActive = 255; |
620 | externalControlActive = 255; |
587 | break; |
621 | break; |
588 | 622 | ||
589 | case 'h':// request for display columns |
623 | case 'd': // request for the debug data |
590 | remoteKeys |= pRxData[0]; |
- | |
591 | if (remoteKeys) |
624 | debugDataInterval = (uint16_t) pRxData[0] * 10; |
592 | displayLine = 0; |
625 | if (debugDataInterval > 0) |
593 | request_display = TRUE; |
626 | request_debugData = TRUE; |
594 | break; |
627 | break; |
- | 628 | ||
- | 629 | case 'e': // Requeset for the DCM matrix |
|
- | 630 | request_DCM_matrix = TRUE; |
|
- | 631 | break; |
|
595 | 632 | ||
- | 633 | case 'f': |
|
- | 634 | requestedProfilerLabel = pRxData[0]; |
|
- | 635 | if (requestedProfilerLabel > 15) |
|
- | 636 | requestedProfilerLabel = 15; |
|
- | 637 | break; |
|
- | 638 | ||
- | 639 | case 'u': |
|
596 | case 'l':// request for display columns |
640 | profilerDataInterval = (uint16_t) pRxData[0] * 10; |
597 | menuItem = pRxData[0]; |
641 | if (profilerDataInterval > 0) |
598 | request_display1 = TRUE; |
642 | request_profilerData = TRUE; |
599 | break; |
643 | break; |
600 | 644 | ||
601 | case 'o':// request for OSD data (FC style) |
645 | case 'o':// request for OSD data (FC style) |
602 | OSD_interval = (uint16_t) pRxData[0] * 10; |
646 | OSDDataInterval = (uint16_t) pRxData[0] * 10; |
603 | if (OSD_interval > 0) |
647 | if (OSDDataInterval > 0) |
604 | request_OSD = TRUE; |
648 | request_OSD = TRUE; |
605 | break; |
649 | break; |
606 | 650 | ||
607 | case 'v': // request for version and board release |
651 | case 'v': // request for version and board release |
608 | request_verInfo = TRUE; |
652 | request_verInfo = TRUE; |
609 | break; |
653 | break; |
610 | 654 | ||
611 | case 'x': |
655 | case 'x': |
612 | request_variables = TRUE; |
656 | request_variables = TRUE; |
613 | break; |
657 | break; |
614 | 658 | ||
615 | case 'g':// get external control data |
659 | case 'g':// get external control data |
616 | request_externalControl = TRUE; |
660 | request_externalControl = TRUE; |
617 | break; |
661 | break; |
618 | - | ||
619 | case 'd': // request for the debug data |
- | |
620 | debugData_interval = (uint16_t) pRxData[0] * 10; |
- | |
621 | if (debugData_interval > 0) |
- | |
622 | request_debugData = TRUE; |
- | |
623 | break; |
- | |
624 | - | ||
625 | case 'c': // request for the 3D data |
- | |
626 | data3D_interval = (uint16_t) pRxData[0] * 10; |
- | |
627 | if (data3D_interval > 0) |
- | |
628 | request_data3D = TRUE; |
- | |
629 | break; |
- | |
630 | 662 | ||
631 | default: |
663 | default: |
632 | //unsupported command received |
664 | //unsupported command received |
633 | break; |
665 | break; |
634 | } |
666 | } |
635 | break; // default: |
667 | break; // default: |
636 | } |
668 | } |
637 | // unlock the rxd buffer after processing |
669 | // unlock the rxd buffer after processing |
638 | pRxData = 0; |
670 | pRxData = 0; |
639 | rxDataLen = 0; |
671 | rxDataLen = 0; |
640 | rxd_buffer_locked = FALSE; |
672 | rxd_buffer_locked = FALSE; |
641 | } |
673 | } |
642 | 674 | ||
643 | /************************************************************************/ |
675 | /************************************************************************/ |
644 | /* Routine f�r die Serielle Ausgabe */ |
676 | /* Routine f�r die Serielle Ausgabe */ |
645 | /************************************************************************/ |
677 | /************************************************************************/ |
646 | int16_t uart_putchar(int8_t c) { |
678 | int uart_putchar(char c, FILE* fims) { |
647 | if (c == '\n') |
679 | if (c == '\n') |
648 | uart_putchar('\r'); |
680 | uart_putchar('\r', fims); |
649 | // wait until previous character was send |
681 | // wait until previous character was send |
650 | loop_until_bit_is_set(UCSR0A, UDRE0); |
682 | loop_until_bit_is_set(UCSR0A, UDRE0); |
651 | // send character |
683 | // send character |
652 | UDR0 = c; |
684 | UDR0 = c; |
653 | return (0); |
685 | return (0); |
654 | } |
686 | } |
655 | 687 | ||
656 | //--------------------------------------------------------------------------------------------- |
688 | //--------------------------------------------------------------------------------------------- |
657 | void usart0_transmitTxData(void) { |
689 | void usart0_transmitTxData(void) { |
658 | if (!txd_complete) |
690 | if (!txd_complete) |
659 | return; |
691 | return; |
660 | 692 | ||
661 | if (request_verInfo && txd_complete) { |
693 | if (request_verInfo && txd_complete) { |
662 | sendOutData('V', FC_ADDRESS, 1, (uint8_t *) &versionInfo, sizeof(versionInfo)); |
694 | sendData('V', FC_ADDRESS, 1, (uint8_t *) &versionInfo, sizeof(versionInfo)); |
663 | request_verInfo = FALSE; |
695 | request_verInfo = FALSE; |
664 | } |
696 | } |
665 | 697 | ||
666 | if (request_display && txd_complete) { |
698 | if (request_display && txd_complete) { |
667 | LCD_printMenu(); |
- | |
668 | sendOutData('H', FC_ADDRESS, 2, &displayLine, sizeof(displayLine), |
- | |
669 | &displayBuff[displayLine * 20], 20); |
- | |
670 | displayLine++; |
- | |
671 | if (displayLine >= 4) |
- | |
672 | displayLine = 0; |
- | |
673 | request_display = FALSE; |
699 | request_display = FALSE; |
674 | } |
700 | } |
675 | 701 | ||
676 | if (request_display1 && txd_complete) { |
702 | if (request_display1 && txd_complete) { |
677 | LCD_printMenu(); |
- | |
678 | sendOutData('L', FC_ADDRESS, 3, &menuItem, sizeof(menuItem), &maxMenuItem, |
- | |
679 | sizeof(maxMenuItem), displayBuff, sizeof(displayBuff)); |
- | |
680 | request_display1 = FALSE; |
703 | request_display1 = FALSE; |
681 | } |
704 | } |
682 | 705 | ||
683 | if (requestedDebugLabel != 0xFF && txd_complete) { // Texte f�r die Analogdaten |
706 | if (requestedAnalogLabel != 0xFF && txd_complete) { |
- | 707 | char label[17]; // local sram buffer |
|
684 | uint8_t label[16]; // local sram buffer |
708 | memset(label, ' ', sizeof(label)); |
685 | memcpy_P(label, ANALOG_LABEL[requestedDebugLabel], 16); // read lable from flash to sram buffer |
709 | strcpy_P(label, (PGM_P)pgm_read_word(&(ANALOG_LABELS[requestedAnalogLabel]))); // read label from flash to sram buffer |
686 | sendOutData('A', FC_ADDRESS, 2, (uint8_t *) &requestedDebugLabel, |
- | |
687 | sizeof(requestedDebugLabel), label, 16); |
710 | sendData('A', FC_ADDRESS, 2, (uint8_t *) &requestedAnalogLabel, sizeof(requestedAnalogLabel), label, 16); |
688 | requestedDebugLabel = 0xFF; |
711 | requestedAnalogLabel = 0xFF; |
- | 712 | } |
|
- | 713 | ||
- | 714 | if (requestedProfilerLabel != 0xFF && txd_complete) { |
|
- | 715 | char label[17]; // local sram buffer |
|
- | 716 | memset(label, ' ', sizeof(label)); |
|
- | 717 | strcpy_P(label, (PGM_P)pgm_read_word(&(PROFILER_LABELS[requestedProfilerLabel]))); // read label from flash to sram buffer |
|
- | 718 | sendData('F', FC_ADDRESS, 2, (uint8_t *) &requestedProfilerLabel, sizeof(requestedProfilerLabel), label, 16); |
|
- | 719 | requestedProfilerLabel = 0xFF; |
|
689 | } |
720 | } |
690 | 721 | ||
691 | if (confirmFrame && txd_complete) { // Datensatz ohne checksum best�tigen |
722 | if (confirmFrame && txd_complete) { // Datensatz ohne checksum best�tigen |
692 | sendOutData('B', FC_ADDRESS, 1, (uint8_t*) &confirmFrame, sizeof(confirmFrame)); |
723 | sendData('B', FC_ADDRESS, 1, (uint8_t*) &confirmFrame, sizeof(confirmFrame)); |
693 | confirmFrame = 0; |
724 | confirmFrame = 0; |
694 | } |
725 | } |
695 | 726 | ||
696 | if (((debugData_interval && checkDelay(debugData_timer)) || request_debugData) |
- | |
697 | && txd_complete) { |
727 | if (((debugDataInterval && checkDelay(debugDataTimer)) || request_debugData) && txd_complete) { |
698 | sendOutData('D', FC_ADDRESS, 1, (uint8_t *) &debugOut, sizeof(debugOut)); |
728 | sendData('D', FC_ADDRESS, 1, (uint8_t *)&debugOut, sizeof(debugOut)); |
699 | debugData_timer = setDelay(debugData_interval); |
729 | debugDataTimer = setDelay(debugDataInterval); |
700 | request_debugData = FALSE; |
730 | request_debugData = FALSE; |
701 | } |
731 | } |
702 | 732 | ||
703 | if (((data3D_interval && checkDelay(data3D_timer)) || request_data3D) && txd_complete) { |
733 | if (((profilerDataInterval && checkDelay(profilerDataTimer)) || request_profilerData) && txd_complete) { |
704 | sendOutData('C', FC_ADDRESS, 1, (uint8_t *) &data3D, sizeof(data3D)); |
734 | sendData('U', FC_ADDRESS, 2, (uint8_t *)&totalProfilerHits, sizeof(totalProfilerHits), (uint8_t *)&activitiesTimerHits, sizeof(activitiesTimerHits)); |
705 | data3D.anglePitch = (int16_t) (attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
735 | profilerDataTimer = setDelay(profilerDataInterval); |
- | 736 | request_profilerData = FALSE; |
|
- | 737 | } |
|
706 | data3D.angleRoll = (int16_t) (attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
738 | |
- | 739 | if (request_DCM_matrix && txd_complete) { |
|
- | 740 | /* |
|
707 | data3D.heading = (int16_t) (heading / (GYRO_DEG_FACTOR_YAW/10)); // convert to multiple of 0.1 deg |
741 | sendData('E', FC_ADDRESS, 1, |
- | 742 | (uint8_t *) &dcmGyro, sizeof(dcmGyro)); |
|
708 | data3D_timer = setDelay(data3D_interval); |
743 | */ |
709 | request_data3D = FALSE; |
744 | request_DCM_matrix = FALSE; |
710 | } |
745 | } |
711 | 746 | ||
712 | if (request_externalControl && txd_complete) { |
747 | if (request_externalControl && txd_complete) { |
713 | sendOutData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl, |
748 | sendData('G', FC_ADDRESS, 1, (uint8_t *) &externalControl, |
714 | sizeof(externalControl)); |
749 | sizeof(externalControl)); |
715 | request_externalControl = FALSE; |
750 | request_externalControl = FALSE; |
716 | } |
751 | } |
717 | 752 | ||
718 | #ifdef USE_DIRECT_GPS |
753 | #ifdef USE_DIRECT_GPS |
719 | if((checkDelay(toMk3MagTimer)) && txd_complete) { |
754 | if((checkDelay(toMk3MagTimer)) && txd_complete) { |
720 | toMk3Mag.attitude[0] = (int16_t)(attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // approx. 0.1 deg |
755 | toMk3Mag.attitude[0] = (int16_t)(attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // approx. 0.1 deg |
721 | toMk3Mag.attitude[1] = (int16_t)(attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // approx. 0.1 deg |
756 | toMk3Mag.attitude[1] = (int16_t)(attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // approx. 0.1 deg |
722 | toMk3Mag.userParam[0] = dynamicParams.userParams[0]; |
757 | toMk3Mag.userParam[0] = dynamicParams.userParams[0]; |
723 | toMk3Mag.userParam[1] = dynamicParams.userParams[1]; |
758 | toMk3Mag.userParam[1] = dynamicParams.userParams[1]; |
724 | toMk3Mag.calState = compassCalState; |
759 | toMk3Mag.calState = compassCalState; |
725 | sendOutData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &toMk3Mag,sizeof(toMk3Mag)); |
760 | sendData('w', MK3MAG_ADDRESS, 1,(uint8_t *) &toMk3Mag,sizeof(toMk3Mag)); |
726 | // the last state is 5 and should be send only once to avoid multiple flash writing |
761 | // the last state is 5 and should be send only once to avoid multiple flash writing |
727 | if(compassCalState > 4) compassCalState = 0; |
762 | if(compassCalState > 4) compassCalState = 0; |
728 | toMk3MagTimer = setDelay(99); |
763 | toMk3MagTimer = setDelay(99); |
729 | } |
764 | } |
730 | #endif |
765 | #endif |
731 | 766 | ||
732 | if (request_motorTest && txd_complete) { |
767 | if (request_outputTest && txd_complete) { |
733 | sendOutData('T', FC_ADDRESS, 0); |
768 | sendData('T', FC_ADDRESS, 0); |
734 | request_motorTest = FALSE; |
769 | request_outputTest = FALSE; |
735 | } |
770 | } |
736 | 771 | ||
737 | if (request_PPMChannels && txd_complete) { |
772 | if (request_PPMChannels && txd_complete) { |
738 | uint8_t length = MAX_CHANNELS; |
773 | uint8_t length = MAX_CONTROLCHANNELS; |
739 | sendOutData('P', FC_ADDRESS, 2, &length, 1, (uint8_t*)&PPM_in, sizeof(PPM_in)); |
774 | sendData('P', FC_ADDRESS, 2, &length, 1, (uint8_t*)&PPM_in, sizeof(PPM_in)); |
740 | request_PPMChannels = FALSE; |
775 | request_PPMChannels = FALSE; |
741 | } |
776 | } |
742 | 777 | ||
743 | if (request_variables && txd_complete) { |
778 | if (request_variables && txd_complete) { |
744 | sendOutData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables)); |
779 | sendData('X', FC_ADDRESS, 1, (uint8_t *) &variables, sizeof(variables)); |
745 | request_variables = FALSE; |
780 | request_variables = FALSE; |
746 | } |
781 | } |
747 | 782 | ||
748 | #ifdef USE_DIRECT_GPS |
783 | #ifdef USE_DIRECT_GPS |
749 | if (((OSD_interval && checkDelay(OSD_timer)) || request_OSD) && txd_complete) { |
784 | if (((OSD_interval && checkDelay(OSD_timer)) || request_OSD) && txd_complete) { |
750 | int32_t height = analog_getHeight(); |
785 | int32_t height = analog_getHeight(); |
751 | data3D.anglePitch = (int16_t) (attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
786 | data3D.anglePitch = (int16_t) (attitude[PITCH] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
752 | data3D.angleRoll = (int16_t) (attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
787 | data3D.angleRoll = (int16_t) (attitude[ROLL] / (GYRO_DEG_FACTOR_PITCHROLL/10)); // convert to multiple of 0.1 deg |
753 | data3D.heading = (int16_t) (heading / (GYRO_DEG_FACTOR_YAW/10)); // convert to multiple of 0.1 deg |
788 | data3D.heading = (int16_t) (heading / (GYRO_DEG_FACTOR_YAW/10)); // convert to multiple of 0.1 deg |
754 | sendOutData('O', FC_ADDRESS, 4, (uint8_t*)&data3D, sizeof(data3D), (uint8_t*)&GPSInfo, sizeof(GPSInfo), (uint8_t*)&height, sizeof(height), (uint8_t*)UBat, sizeof(UBat)); |
789 | sendData('O', FC_ADDRESS, 4, (uint8_t*)&data3D, sizeof(data3D), (uint8_t*)&GPSInfo, sizeof(GPSInfo), (uint8_t*)&height, sizeof(height), (uint8_t*)UBat, sizeof(UBat)); |
755 | OSD_timer = setDelay(OSD_interval); |
790 | OSD_timer = setDelay(OSD_interval); |
756 | request_OSD = FALSE; |
791 | request_OSD = FALSE; |
757 | } |
792 | } |
758 | #endif |
793 | #endif |
759 | } |
794 | } |
760 | 795 |