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