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