Rev 1738 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1171 | hbuss | 1 | /*####################################################################################### |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
||
3 | #######################################################################################*/ |
||
4 | |||
1426 | ingob | 5 | #include "Spektrum.h" |
1171 | hbuss | 6 | #include "main.h" |
1680 | holgerb | 7 | // Achtung: RECEIVER_SPEKTRUM_EXP wird in der Main.h gesetzt |
1506 | holgerb | 8 | |
1320 | hbuss | 9 | unsigned char SpektrumTimer = 0; |
1171 | hbuss | 10 | |
1506 | holgerb | 11 | #ifdef RECEIVER_SPEKTRUM_EXP |
1680 | holgerb | 12 | unsigned char s_excnt = 0; // Counter for Spektrum-Expander |
13 | unsigned char s_exparity = 0; // Parity Bit for Spektrum-Expander |
||
14 | signed char s_exdata[11]; // Data for Spektrum-Expander |
||
1506 | holgerb | 15 | #endif |
1213 | ingob | 16 | //--------------------------------------------------------------// |
17 | //--------------------------------------------------------------// |
||
1738 | - | 18 | |
19 | /* // MartinR: deaktiviert, da der Aufruf in der main.c auch deaktiviert ist |
||
20 | |||
1213 | ingob | 21 | void SpektrumBinding(void) |
22 | { |
||
23 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
||
24 | unsigned char connected = 0; |
||
25 | unsigned int delaycounter; |
||
1391 | killagreg | 26 | |
1213 | ingob | 27 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
28 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
||
29 | PORTD &= ~(1 << PORTD2); // disable pull-up |
||
1391 | killagreg | 30 | |
1213 | ingob | 31 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
1391 | killagreg | 32 | |
1213 | ingob | 33 | while(!CheckDelay(timerTimeout)) |
34 | { |
||
35 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
||
36 | } |
||
1391 | killagreg | 37 | |
38 | if (connected) |
||
39 | { |
||
40 | |||
41 | printf("ok.\n\r"); |
||
1213 | ingob | 42 | DDRD |= (1 << DDD2); // Rx as output |
43 | |||
1680 | holgerb | 44 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
45 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
||
46 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1391 | killagreg | 47 | |
1680 | holgerb | 48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 49 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 50 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
51 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1391 | killagreg | 52 | |
1680 | holgerb | 53 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 54 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 55 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
56 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1213 | ingob | 57 | |
1680 | holgerb | 58 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 59 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 60 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
1391 | killagreg | 61 | |
1213 | ingob | 62 | } |
1391 | killagreg | 63 | else |
64 | { printf("Timeout.\n\r"); |
||
65 | |||
66 | |||
1213 | ingob | 67 | } |
1391 | killagreg | 68 | |
1213 | ingob | 69 | DDRD &= ~(1 << DDD2); // RX as input |
1391 | killagreg | 70 | PORTD &= ~(1 << PORTD2); |
1213 | ingob | 71 | |
1424 | ingob | 72 | SpektrumUartInit(); // init Uart again |
1213 | ingob | 73 | } |
74 | |||
1738 | - | 75 | */ |
1171 | hbuss | 76 | //############################################################################ |
77 | // USART1 initialisation from killagreg |
||
1424 | ingob | 78 | void SpektrumUartInit(void) |
1171 | hbuss | 79 | //############################################################################ |
80 | { |
||
1680 | holgerb | 81 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
82 | // USART1 Control and Status Register A, B, C and baud rate register |
||
83 | uint8_t sreg = SREG; |
||
84 | |||
85 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
||
86 | |||
87 | // disable all interrupts before reconfiguration |
||
88 | cli(); |
||
89 | // disable RX-Interrupt |
||
90 | UCSR1B &= ~(1 << RXCIE1); |
||
91 | // disable TX-Interrupt |
||
92 | UCSR1B &= ~(1 << TXCIE1); |
||
93 | // disable DRE-Interrupt |
||
94 | UCSR1B &= ~(1 << UDRIE1); |
||
95 | // set direction of RXD1 and TXD1 pins |
||
96 | // set RXD1 (PD2) as an input pin |
||
97 | PORTD |= (1 << PORTD2); |
||
98 | DDRD &= ~(1 << DDD2); |
||
1431 | ingob | 99 | |
1680 | holgerb | 100 | // set TXD1 (PD3) as an output pin |
101 | PORTD |= (1 << PORTD3); |
||
102 | DDRD |= (1 << DDD3); |
||
103 | |||
104 | // USART0 Baud Rate Register |
||
105 | // set clock divider |
||
106 | UBRR1H = (uint8_t)(ubrr>>8); |
||
107 | UBRR1L = (uint8_t)ubrr; |
||
108 | // enable double speed operation |
||
109 | UCSR1A |= (1 << U2X1); |
||
110 | // enable receiver and transmitter |
||
111 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
||
1391 | killagreg | 112 | |
1680 | holgerb | 113 | UCSR1B = (1<<RXEN1); |
114 | // set asynchronous mode |
||
115 | UCSR1C &= ~(1 << UMSEL11); |
||
116 | UCSR1C &= ~(1 << UMSEL10); |
||
117 | // no parity |
||
118 | UCSR1C &= ~(1 << UPM11); |
||
119 | UCSR1C &= ~(1 << UPM10); |
||
120 | // 1 stop bit |
||
121 | UCSR1C &= ~(1 << USBS1); |
||
122 | // 8-bit |
||
123 | UCSR1B &= ~(1 << UCSZ12); |
||
124 | UCSR1C |= (1 << UCSZ11); |
||
125 | UCSR1C |= (1 << UCSZ10); |
||
126 | // flush receive buffer explicit |
||
127 | while(UCSR1A & (1<<RXC1)) UDR1; |
||
128 | // enable RX-interrupts at the end |
||
129 | UCSR1B |= (1 << RXCIE1); |
||
130 | // -- End of USART1 initialisation |
||
131 | // restore global interrupt flags |
||
1431 | ingob | 132 | |
1680 | holgerb | 133 | SREG = sreg; |
1171 | hbuss | 134 | return; |
135 | } |
||
1391 | killagreg | 136 | |
1171 | hbuss | 137 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
138 | // + Copyright (c) Rainer Walther |
||
139 | // + RC-routines from original MK rc.c (c) H&I |
||
140 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
||
141 | // + only for non-profit use |
||
142 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
143 | // |
||
144 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
||
145 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
||
146 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
||
147 | // Replace AR6100-coding with original composit-signal routines |
||
148 | // |
||
149 | // --- |
||
150 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
||
151 | // Binding is not implemented. Bind with external Receiver. |
||
152 | // Servo output J3, J4, J5 not serviced |
||
153 | // |
||
154 | // Anschuß Spektrum Receiver |
||
1680 | holgerb | 155 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
156 | // Schwarz: GND |
||
157 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
||
1171 | hbuss | 158 | // |
159 | // --- |
||
160 | // Satellite-Reciever connected on USART1: |
||
161 | // |
||
162 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
||
163 | // DX7se: One data-frame at 115200 baud every 11ms. |
||
1680 | holgerb | 164 | // byte1: unknown |
165 | // byte2: unknown |
||
166 | // byte3: and byte4: channel data (FLT-Mode) |
||
167 | // byte5: and byte6: channel data (Roll) |
||
168 | // byte7: and byte8: channel data (Nick) |
||
169 | // byte9: and byte10: channel data (Gier) |
||
170 | // byte11: and byte12: channel data (Gear Switch) |
||
171 | // byte13: and byte14: channel data (Gas) |
||
172 | // byte15: and byte16: channel data (AUX2) |
||
1171 | hbuss | 173 | // |
174 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
||
175 | // 1st Frame: |
||
1680 | holgerb | 176 | // byte1: unknown |
177 | // byte2: unknown |
||
178 | // byte3: and byte4: channel data |
||
179 | // byte5: and byte6: channel data |
||
180 | // byte7: and byte8: channel data |
||
181 | // byte9: and byte10: channel data |
||
182 | // byte11: and byte12: channel data |
||
183 | // byte13: and byte14: channel data |
||
184 | // byte15: and byte16: channel data |
||
1171 | hbuss | 185 | // 2nd Frame: |
1680 | holgerb | 186 | // byte1: unknown |
187 | // byte2: unknown |
||
188 | // byte3: and byte4: channel data |
||
189 | // byte5: and byte6: channel data |
||
190 | // byte7: and byte8: 0xffff |
||
191 | // byte9: and byte10: 0xffff |
||
192 | // byte11: and byte12: 0xffff |
||
193 | // byte13: and byte14: 0xffff |
||
194 | // byte15: and byte16: 0xffff |
||
1171 | hbuss | 195 | // |
196 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
||
197 | // |
||
198 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
||
199 | // |
||
200 | // 0 means a '0' bit |
||
201 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
||
202 | // C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
||
203 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
||
204 | // |
||
205 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
206 | |||
1322 | hbuss | 207 | #define MIN_FRAMEGAP 68 // 7ms |
208 | #define MAX_BYTEGAP 3 // 310us |
||
1320 | hbuss | 209 | |
1419 | ingob | 210 | |
1171 | hbuss | 211 | //############################################################################ |
1481 | holgerb | 212 | // Wird im UART-Interrupt aufgerufen |
1171 | hbuss | 213 | //############################################################################ |
1419 | ingob | 214 | void SpektrumParser(unsigned char c) |
1171 | hbuss | 215 | { |
1320 | hbuss | 216 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
1680 | holgerb | 217 | unsigned int Channel, index = 0; |
218 | signed int signal = 0, tmp; |
||
219 | int bCheckDelay; |
||
220 | // c = UDR1; // get data byte |
||
221 | if(ReSync == 1) |
||
222 | { |
||
223 | // wait for beginning of new frame |
||
224 | ReSync = 0; |
||
225 | SpektrumTimer = MIN_FRAMEGAP; |
||
226 | FrameCnt = 0; |
||
227 | Sync = 0; |
||
228 | ByteHigh = 0; |
||
229 | } |
||
1171 | hbuss | 230 | else |
1391 | killagreg | 231 | { |
1680 | holgerb | 232 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
233 | if ( Sync == 0 ) |
||
234 | { |
||
235 | if(bCheckDelay) |
||
236 | { |
||
237 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
||
238 | // Zeichen ignorieren, da Bedeutung unbekannt |
||
239 | Sync = 1; |
||
240 | FrameCnt ++; |
||
241 | SpektrumTimer = MAX_BYTEGAP; |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
||
246 | // warten auf erstes Sync-Zeichen |
||
247 | SpektrumTimer = MIN_FRAMEGAP; |
||
248 | FrameCnt = 0; |
||
249 | Sync = 0; |
||
250 | ByteHigh = 0; |
||
251 | } |
||
252 | } |
||
253 | else if((Sync == 1) && !bCheckDelay) |
||
254 | { |
||
255 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
||
256 | Sync = 2; |
||
257 | FrameCnt ++; |
||
258 | SpektrumTimer = MAX_BYTEGAP; |
||
259 | } |
||
260 | else if((Sync == 2) && !bCheckDelay) |
||
261 | { |
||
262 | SpektrumTimer = MAX_BYTEGAP; |
||
263 | // Datenbyte high |
||
264 | ByteHigh = c; |
||
265 | if (FrameCnt == 2) |
||
266 | { |
||
267 | // is 1st Byte of Channel-data |
||
268 | // Frame 1 with Channel 1-7 comming next |
||
269 | Frame2 = 0; |
||
270 | if(ByteHigh & 0x80) |
||
271 | { |
||
272 | // DS9: Frame 2 with Channel 8-9 comming next |
||
273 | Frame2 = 1; |
||
274 | } |
||
275 | } |
||
276 | Sync = 3; |
||
277 | FrameCnt ++; |
||
278 | } |
||
279 | else if((Sync == 3) && !bCheckDelay) |
||
280 | { |
||
281 | // Datenbyte low |
||
282 | // High-Byte for next channel comes next |
||
283 | SpektrumTimer = MAX_BYTEGAP; |
||
284 | Sync = 2; |
||
285 | FrameCnt ++; |
||
286 | Channel = ((unsigned int)ByteHigh << 8) | c; |
||
287 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM) |
||
288 | { |
||
289 | signal = Channel & 0x3ff; |
||
290 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
||
291 | signal = signal/3; // scaling to fit PPM resolution |
||
292 | index = (ByteHigh >> 2) & 0x0f; |
||
293 | } |
||
294 | else |
||
295 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_HI_RES) |
||
296 | { |
||
297 | signal = Channel & 0x7ff; |
||
298 | signal -= 0x400; // Offset, range 0x000..0x7ff? |
||
299 | signal = signal/6; // scaling to fit PPM resolution |
||
300 | index = (ByteHigh >> 3) & 0x0f; |
||
301 | } |
||
302 | else |
||
303 | //if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_LOW_RES) |
||
304 | { |
||
305 | signal = Channel & 0x3ff; |
||
306 | signal -= 360; // Offset, range 0x000..0x3ff? |
||
307 | signal = signal/2; // scaling to fit PPM resolution |
||
308 | index = (ByteHigh >> 2) & 0x0f; |
||
309 | } |
||
1391 | killagreg | 310 | |
1680 | holgerb | 311 | index++; |
312 | if(index < 13) |
||
313 | { |
||
314 | // Stabiles Signal |
||
1506 | holgerb | 315 | #ifdef RECEIVER_SPEKTRUM_EXP |
1680 | holgerb | 316 | if (index == 2) index = 4; // Analog channel reassigment (2 <-> 4) for logical numbering (1,2,3,4) |
317 | else if (index == 4) index = 2; |
||
1506 | holgerb | 318 | #endif |
1680 | holgerb | 319 | if(abs(signal - PPM_in[index]) < 6) |
320 | { |
||
321 | if(SenderOkay < 200) SenderOkay += 10; |
||
322 | else |
||
323 | { |
||
324 | SenderOkay = 200; |
||
325 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
||
326 | } |
||
327 | } |
||
328 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
||
329 | if(tmp > signal+1) tmp--; else |
||
330 | if(tmp < signal-1) tmp++; |
||
331 | |||
1506 | holgerb | 332 | #ifdef RECEIVER_SPEKTRUM_EXP |
1680 | holgerb | 333 | if(index == 6) // FLIGHT-MODE - The channel used for our data uplink |
1506 | holgerb | 334 | { |
1680 | holgerb | 335 | if (signal > 100) // SYNC received |
1506 | holgerb | 336 | { |
1680 | holgerb | 337 | if (s_exdata[s_excnt] == 125) s_exparity = ~s_exparity; // Bit = 1 -> Re-Invert parity bit |
338 | if ((s_excnt == 6 && ((s_exparity != 0 && s_exdata[s_excnt] == -125) || (s_exparity == 0 && s_exdata[s_excnt] == 125))) || (s_excnt == 9 && ((s_exparity == 0 && s_exdata[s_excnt] == -125) || (s_exparity != 0 && s_exdata[s_excnt] == 125)))) // Parity check |
||
339 | { |
||
340 | if (s_exdata[1] == 125 && s_exdata[2] == -125) PPM_in[5] = -125; // Reconstruct tripole Flight-Mode value (CH5) |
||
341 | else if (s_exdata[1] == -125 && s_exdata[2] == -125) PPM_in[5] = 0; // Reconstruct tripole Flight-Mode value (CH5) |
||
342 | else if (s_exdata[1] == -125 && s_exdata[2] == 125) PPM_in[5] = 125; // Reconstruct tripole Flight-Mode value (CH5) |
||
343 | PPM_in[6] = s_exdata[3]; // Elevator (CH6) |
||
344 | PPM_in[11] = s_exdata[4]; // Aileron (CH11) |
||
345 | PPM_in[12] = s_exdata[5]; // Rudder (CH12) |
||
346 | |||
347 | if (s_excnt == 9) // New Mode (12 Channels) |
||
348 | { |
||
349 | if (s_exdata[7] == 125) PPM_in[8] += 5; // Hover Pitch UP (CH8) |
||
350 | if (s_exdata[8] == 125) PPM_in[8] -= 5; // Hover Pitch DN (CH8) |
||
351 | if (PPM_in[8] < -125) PPM_in[8] = -125; // Range-Limit |
||
352 | else if (PPM_in[8] > 125) PPM_in[8] = 125; // Range-Limit |
||
353 | PPM_in[10] = s_exdata[6]; // AUX2 (CH10) |
||
354 | } |
||
355 | } |
||
356 | |||
357 | s_excnt = 0; // Reset bitcounter |
||
358 | s_exparity = 0; // Reset parity bit |
||
1506 | holgerb | 359 | } |
360 | |||
1680 | holgerb | 361 | if (signal < 10) s_exdata[++s_excnt] = -125; // Bit = 0 -> value = -125 (min) |
362 | if (s_excnt == 10) s_excnt = 0; // Overflow protection |
||
1506 | holgerb | 363 | if (signal < -100) |
364 | { |
||
1680 | holgerb | 365 | s_exdata[s_excnt] = 125; // Bit = 1 -> value = 125 (max) |
366 | s_exparity = ~s_exparity; // Bit = 1 -> Invert parity bit |
||
1506 | holgerb | 367 | } |
1680 | holgerb | 368 | |
1506 | holgerb | 369 | } |
370 | #endif |
||
371 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
||
1680 | holgerb | 372 | else PPM_diff[index] = 0; |
1506 | holgerb | 373 | |
374 | #ifdef RECEIVER_SPEKTRUM_EXP |
||
1680 | holgerb | 375 | if (index < 5 ) PPM_in[index] = tmp; // Update normal potis (CH1-4) |
376 | else if (index == 5) PPM_in[7] = signal; // Gear (CH7) |
||
377 | else if (index == 7) PPM_in[9] = signal; // Hover Throttle (CH9) |
||
1506 | holgerb | 378 | #else |
379 | PPM_in[index] = tmp; |
||
380 | #endif |
||
1171 | hbuss | 381 | } |
1506 | holgerb | 382 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
1680 | holgerb | 383 | } |
384 | else |
||
385 | { |
||
386 | // hier stimmt was nicht: neu synchronisieren |
||
387 | ReSync = 1; |
||
388 | FrameCnt = 0; |
||
389 | Frame2 = 0; |
||
390 | // new frame next, nach fruehestens 7ms erwartet |
||
391 | SpektrumTimer = MIN_FRAMEGAP; |
||
392 | } |
||
1391 | killagreg | 393 | |
1680 | holgerb | 394 | // 16 Bytes eingetroffen -> Komplett |
395 | if(FrameCnt >= 16) |
||
396 | { |
||
397 | // Frame complete |
||
398 | if(Frame2 == 0) |
||
399 | { |
||
400 | // Null bedeutet: Neue Daten |
||
401 | // nur beim ersten Frame (CH 0-7) setzen |
||
402 | if(!ReSync) NewPpmData = 0; |
||
403 | } |
||
404 | FrameCnt = 0; |
||
405 | Frame2 = 0; |
||
406 | Sync = 0; |
||
407 | SpektrumTimer = MIN_FRAMEGAP; |
||
408 | } |
||
1171 | hbuss | 409 | } |
1391 | killagreg | 410 | } |