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