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