Rev 1793 | 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 | //--------------------------------------------------------------// |
||
1799 | - | 18 | |
1743 | holgerb | 19 | /* |
1213 | ingob | 20 | void SpektrumBinding(void) |
21 | { |
||
22 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
||
23 | unsigned char connected = 0; |
||
24 | unsigned int delaycounter; |
||
1391 | killagreg | 25 | |
1213 | ingob | 26 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
27 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
||
28 | PORTD &= ~(1 << PORTD2); // disable pull-up |
||
1391 | killagreg | 29 | |
1213 | ingob | 30 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
1391 | killagreg | 31 | |
1213 | ingob | 32 | while(!CheckDelay(timerTimeout)) |
33 | { |
||
34 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
||
35 | } |
||
1391 | killagreg | 36 | |
37 | if (connected) |
||
38 | { |
||
39 | |||
40 | printf("ok.\n\r"); |
||
1213 | ingob | 41 | DDRD |= (1 << DDD2); // Rx as output |
42 | |||
1680 | holgerb | 43 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
44 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
||
45 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1391 | killagreg | 46 | |
1680 | holgerb | 47 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 49 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1391 | killagreg | 51 | |
1680 | holgerb | 52 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 53 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 54 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
55 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
||
1213 | ingob | 56 | |
1680 | holgerb | 57 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
1213 | ingob | 58 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
1680 | holgerb | 59 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
1391 | killagreg | 60 | |
1213 | ingob | 61 | } |
1391 | killagreg | 62 | else |
63 | { printf("Timeout.\n\r"); |
||
64 | |||
65 | |||
1213 | ingob | 66 | } |
1391 | killagreg | 67 | |
1213 | ingob | 68 | DDRD &= ~(1 << DDD2); // RX as input |
1391 | killagreg | 69 | PORTD &= ~(1 << PORTD2); |
1213 | ingob | 70 | |
1424 | ingob | 71 | SpektrumUartInit(); // init Uart again |
1213 | ingob | 72 | } |
1743 | holgerb | 73 | */ |
1171 | hbuss | 74 | //############################################################################ |
75 | // USART1 initialisation from killagreg |
||
1424 | ingob | 76 | void SpektrumUartInit(void) |
1171 | hbuss | 77 | //############################################################################ |
78 | { |
||
1799 | - | 79 | /* MartinW Codesize red |
1680 | holgerb | 80 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
81 | // USART1 Control and Status Register A, B, C and baud rate register |
||
82 | uint8_t sreg = SREG; |
||
83 | |||
84 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
||
85 | |||
86 | // disable all interrupts before reconfiguration |
||
87 | cli(); |
||
88 | // disable RX-Interrupt |
||
89 | UCSR1B &= ~(1 << RXCIE1); |
||
90 | // disable TX-Interrupt |
||
91 | UCSR1B &= ~(1 << TXCIE1); |
||
92 | // disable DRE-Interrupt |
||
93 | UCSR1B &= ~(1 << UDRIE1); |
||
94 | // set direction of RXD1 and TXD1 pins |
||
95 | // set RXD1 (PD2) as an input pin |
||
96 | PORTD |= (1 << PORTD2); |
||
97 | DDRD &= ~(1 << DDD2); |
||
1431 | ingob | 98 | |
1680 | holgerb | 99 | // set TXD1 (PD3) as an output pin |
100 | PORTD |= (1 << PORTD3); |
||
101 | DDRD |= (1 << DDD3); |
||
102 | |||
103 | // USART0 Baud Rate Register |
||
104 | // set clock divider |
||
105 | UBRR1H = (uint8_t)(ubrr>>8); |
||
106 | UBRR1L = (uint8_t)ubrr; |
||
107 | // enable double speed operation |
||
108 | UCSR1A |= (1 << U2X1); |
||
109 | // enable receiver and transmitter |
||
110 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
||
1391 | killagreg | 111 | |
1680 | holgerb | 112 | UCSR1B = (1<<RXEN1); |
113 | // set asynchronous mode |
||
114 | UCSR1C &= ~(1 << UMSEL11); |
||
115 | UCSR1C &= ~(1 << UMSEL10); |
||
116 | // no parity |
||
117 | UCSR1C &= ~(1 << UPM11); |
||
118 | UCSR1C &= ~(1 << UPM10); |
||
119 | // 1 stop bit |
||
120 | UCSR1C &= ~(1 << USBS1); |
||
121 | // 8-bit |
||
122 | UCSR1B &= ~(1 << UCSZ12); |
||
123 | UCSR1C |= (1 << UCSZ11); |
||
124 | UCSR1C |= (1 << UCSZ10); |
||
125 | // flush receive buffer explicit |
||
126 | while(UCSR1A & (1<<RXC1)) UDR1; |
||
127 | // enable RX-interrupts at the end |
||
128 | UCSR1B |= (1 << RXCIE1); |
||
129 | // -- End of USART1 initialisation |
||
130 | // restore global interrupt flags |
||
1431 | ingob | 131 | |
1680 | holgerb | 132 | SREG = sreg; |
1799 | - | 133 | */ //MartinW Codesize red |
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 | { |
1799 | - | 216 | /* //MartinW Codesize red |
217 | |||
1320 | hbuss | 218 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
1680 | holgerb | 219 | unsigned int Channel, index = 0; |
220 | signed int signal = 0, tmp; |
||
221 | int bCheckDelay; |
||
222 | // c = UDR1; // get data byte |
||
223 | if(ReSync == 1) |
||
224 | { |
||
225 | // wait for beginning of new frame |
||
226 | ReSync = 0; |
||
227 | SpektrumTimer = MIN_FRAMEGAP; |
||
228 | FrameCnt = 0; |
||
229 | Sync = 0; |
||
230 | ByteHigh = 0; |
||
231 | } |
||
1171 | hbuss | 232 | else |
1391 | killagreg | 233 | { |
1680 | holgerb | 234 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
235 | if ( Sync == 0 ) |
||
236 | { |
||
237 | if(bCheckDelay) |
||
238 | { |
||
239 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
||
240 | // Zeichen ignorieren, da Bedeutung unbekannt |
||
241 | Sync = 1; |
||
242 | FrameCnt ++; |
||
243 | SpektrumTimer = MAX_BYTEGAP; |
||
244 | } |
||
245 | else |
||
246 | { |
||
247 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
||
248 | // warten auf erstes Sync-Zeichen |
||
249 | SpektrumTimer = MIN_FRAMEGAP; |
||
250 | FrameCnt = 0; |
||
251 | Sync = 0; |
||
252 | ByteHigh = 0; |
||
253 | } |
||
254 | } |
||
255 | else if((Sync == 1) && !bCheckDelay) |
||
256 | { |
||
257 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
||
258 | Sync = 2; |
||
259 | FrameCnt ++; |
||
260 | SpektrumTimer = MAX_BYTEGAP; |
||
261 | } |
||
262 | else if((Sync == 2) && !bCheckDelay) |
||
263 | { |
||
264 | SpektrumTimer = MAX_BYTEGAP; |
||
265 | // Datenbyte high |
||
266 | ByteHigh = c; |
||
267 | if (FrameCnt == 2) |
||
268 | { |
||
269 | // is 1st Byte of Channel-data |
||
270 | // Frame 1 with Channel 1-7 comming next |
||
271 | Frame2 = 0; |
||
272 | if(ByteHigh & 0x80) |
||
273 | { |
||
274 | // DS9: Frame 2 with Channel 8-9 comming next |
||
275 | Frame2 = 1; |
||
276 | } |
||
277 | } |
||
278 | Sync = 3; |
||
279 | FrameCnt ++; |
||
280 | } |
||
281 | else if((Sync == 3) && !bCheckDelay) |
||
282 | { |
||
283 | // Datenbyte low |
||
284 | // High-Byte for next channel comes next |
||
285 | SpektrumTimer = MAX_BYTEGAP; |
||
286 | Sync = 2; |
||
287 | FrameCnt ++; |
||
288 | Channel = ((unsigned int)ByteHigh << 8) | c; |
||
289 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM) |
||
290 | { |
||
291 | signal = Channel & 0x3ff; |
||
292 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
||
293 | signal = signal/3; // scaling to fit PPM resolution |
||
294 | index = (ByteHigh >> 2) & 0x0f; |
||
295 | } |
||
296 | else |
||
297 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_HI_RES) |
||
298 | { |
||
299 | signal = Channel & 0x7ff; |
||
300 | signal -= 0x400; // Offset, range 0x000..0x7ff? |
||
301 | signal = signal/6; // scaling to fit PPM resolution |
||
302 | index = (ByteHigh >> 3) & 0x0f; |
||
303 | } |
||
304 | else |
||
305 | //if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_LOW_RES) |
||
306 | { |
||
307 | signal = Channel & 0x3ff; |
||
308 | signal -= 360; // Offset, range 0x000..0x3ff? |
||
309 | signal = signal/2; // scaling to fit PPM resolution |
||
310 | index = (ByteHigh >> 2) & 0x0f; |
||
311 | } |
||
1391 | killagreg | 312 | |
1680 | holgerb | 313 | index++; |
314 | if(index < 13) |
||
315 | { |
||
316 | // Stabiles Signal |
||
1506 | holgerb | 317 | #ifdef RECEIVER_SPEKTRUM_EXP |
1680 | holgerb | 318 | if (index == 2) index = 4; // Analog channel reassigment (2 <-> 4) for logical numbering (1,2,3,4) |
319 | else if (index == 4) index = 2; |
||
1506 | holgerb | 320 | #endif |
1680 | holgerb | 321 | if(abs(signal - PPM_in[index]) < 6) |
322 | { |
||
323 | if(SenderOkay < 200) SenderOkay += 10; |
||
324 | else |
||
325 | { |
||
326 | SenderOkay = 200; |
||
327 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
||
328 | } |
||
329 | } |
||
330 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
||
331 | if(tmp > signal+1) tmp--; else |
||
332 | if(tmp < signal-1) tmp++; |
||
333 | |||
1506 | holgerb | 334 | #ifdef RECEIVER_SPEKTRUM_EXP |
1680 | holgerb | 335 | if(index == 6) // FLIGHT-MODE - The channel used for our data uplink |
1506 | holgerb | 336 | { |
1680 | holgerb | 337 | if (signal > 100) // SYNC received |
1506 | holgerb | 338 | { |
1680 | holgerb | 339 | if (s_exdata[s_excnt] == 125) s_exparity = ~s_exparity; // Bit = 1 -> Re-Invert parity bit |
340 | 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 |
||
341 | { |
||
342 | if (s_exdata[1] == 125 && s_exdata[2] == -125) PPM_in[5] = -125; // Reconstruct tripole Flight-Mode value (CH5) |
||
343 | else if (s_exdata[1] == -125 && s_exdata[2] == -125) PPM_in[5] = 0; // Reconstruct tripole Flight-Mode value (CH5) |
||
344 | else if (s_exdata[1] == -125 && s_exdata[2] == 125) PPM_in[5] = 125; // Reconstruct tripole Flight-Mode value (CH5) |
||
345 | PPM_in[6] = s_exdata[3]; // Elevator (CH6) |
||
346 | PPM_in[11] = s_exdata[4]; // Aileron (CH11) |
||
347 | PPM_in[12] = s_exdata[5]; // Rudder (CH12) |
||
348 | |||
349 | if (s_excnt == 9) // New Mode (12 Channels) |
||
350 | { |
||
351 | if (s_exdata[7] == 125) PPM_in[8] += 5; // Hover Pitch UP (CH8) |
||
352 | if (s_exdata[8] == 125) PPM_in[8] -= 5; // Hover Pitch DN (CH8) |
||
353 | if (PPM_in[8] < -125) PPM_in[8] = -125; // Range-Limit |
||
354 | else if (PPM_in[8] > 125) PPM_in[8] = 125; // Range-Limit |
||
355 | PPM_in[10] = s_exdata[6]; // AUX2 (CH10) |
||
356 | } |
||
357 | } |
||
358 | |||
359 | s_excnt = 0; // Reset bitcounter |
||
360 | s_exparity = 0; // Reset parity bit |
||
1506 | holgerb | 361 | } |
362 | |||
1680 | holgerb | 363 | if (signal < 10) s_exdata[++s_excnt] = -125; // Bit = 0 -> value = -125 (min) |
364 | if (s_excnt == 10) s_excnt = 0; // Overflow protection |
||
1506 | holgerb | 365 | if (signal < -100) |
366 | { |
||
1680 | holgerb | 367 | s_exdata[s_excnt] = 125; // Bit = 1 -> value = 125 (max) |
368 | s_exparity = ~s_exparity; // Bit = 1 -> Invert parity bit |
||
1506 | holgerb | 369 | } |
1680 | holgerb | 370 | |
1506 | holgerb | 371 | } |
372 | #endif |
||
373 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
||
1680 | holgerb | 374 | else PPM_diff[index] = 0; |
1506 | holgerb | 375 | |
376 | #ifdef RECEIVER_SPEKTRUM_EXP |
||
1680 | holgerb | 377 | if (index < 5 ) PPM_in[index] = tmp; // Update normal potis (CH1-4) |
378 | else if (index == 5) PPM_in[7] = signal; // Gear (CH7) |
||
379 | else if (index == 7) PPM_in[9] = signal; // Hover Throttle (CH9) |
||
1506 | holgerb | 380 | #else |
381 | PPM_in[index] = tmp; |
||
382 | #endif |
||
1171 | hbuss | 383 | } |
1506 | holgerb | 384 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
1680 | holgerb | 385 | } |
386 | else |
||
387 | { |
||
388 | // hier stimmt was nicht: neu synchronisieren |
||
389 | ReSync = 1; |
||
390 | FrameCnt = 0; |
||
391 | Frame2 = 0; |
||
392 | // new frame next, nach fruehestens 7ms erwartet |
||
393 | SpektrumTimer = MIN_FRAMEGAP; |
||
394 | } |
||
1391 | killagreg | 395 | |
1680 | holgerb | 396 | // 16 Bytes eingetroffen -> Komplett |
397 | if(FrameCnt >= 16) |
||
398 | { |
||
399 | // Frame complete |
||
400 | if(Frame2 == 0) |
||
401 | { |
||
402 | // Null bedeutet: Neue Daten |
||
403 | // nur beim ersten Frame (CH 0-7) setzen |
||
404 | if(!ReSync) NewPpmData = 0; |
||
405 | } |
||
406 | FrameCnt = 0; |
||
407 | Frame2 = 0; |
||
408 | Sync = 0; |
||
409 | SpektrumTimer = MIN_FRAMEGAP; |
||
410 | } |
||
1171 | hbuss | 411 | } |
1799 | - | 412 | */ //MartinW Codesize red |
1391 | killagreg | 413 | } |