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