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