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