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