Rev 1481 | Rev 1680 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1481 | Rev 1506 | ||
---|---|---|---|
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
2 | Decodieren eines RC Summen Signals oder Spektrum Empfänger-Satellit |
3 | #######################################################################################*/ |
3 | #######################################################################################*/ |
4 | 4 | ||
5 | #include "Spektrum.h" |
5 | #include "Spektrum.h" |
6 | #include "main.h" |
6 | #include "main.h" |
- | 7 | //#define RECEIVER_SPEKTRUM_EXP |
|
- | 8 | ||
7 | unsigned char SpektrumTimer = 0; |
9 | unsigned char SpektrumTimer = 0; |
- | 10 | ||
- | 11 | #ifdef RECEIVER_SPEKTRUM_EXP |
|
- | 12 | unsigned char sexcnt; // Counter for Spektrum-Expander |
|
- | 13 | unsigned char sexparity; // Parity Bit for Spektrum-Expander |
|
- | 14 | signed char sexdata[7]; // Data for Spektrum-Expander |
|
8 | 15 | #endif |
|
9 | //--------------------------------------------------------------// |
16 | //--------------------------------------------------------------// |
10 | //--------------------------------------------------------------// |
17 | //--------------------------------------------------------------// |
11 | void SpektrumBinding(void) |
18 | void SpektrumBinding(void) |
12 | { |
19 | { |
13 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
20 | unsigned int timerTimeout = SetDelay(10000); // Timeout 10 sec. |
14 | unsigned char connected = 0; |
21 | unsigned char connected = 0; |
15 | unsigned int delaycounter; |
22 | unsigned int delaycounter; |
16 | 23 | ||
17 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
24 | UCSR1B &= ~(1 << RXCIE1); // disable rx-interrupt |
18 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
25 | UCSR1B &= ~(1<<RXEN1); // disable Uart-Rx |
19 | PORTD &= ~(1 << PORTD2); // disable pull-up |
26 | PORTD &= ~(1 << PORTD2); // disable pull-up |
20 | 27 | ||
21 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
28 | printf("\n\rPlease connect Spektrum receiver for binding NOW..."); |
22 | 29 | ||
23 | while(!CheckDelay(timerTimeout)) |
30 | while(!CheckDelay(timerTimeout)) |
24 | { |
31 | { |
25 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
32 | if (PIND & (1 << PORTD2)) { timerTimeout = SetDelay(90); connected = 1; break; } |
26 | } |
33 | } |
27 | 34 | ||
28 | if (connected) |
35 | if (connected) |
29 | { |
36 | { |
30 | 37 | ||
31 | printf("ok.\n\r"); |
38 | printf("ok.\n\r"); |
32 | DDRD |= (1 << DDD2); // Rx as output |
39 | DDRD |= (1 << DDD2); // Rx as output |
33 | 40 | ||
34 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
41 | while(!CheckDelay(timerTimeout)); // delay after startup of RX |
35 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
42 | for (delaycounter = 0; delaycounter < 100; delaycounter++) PORTD |= (1 << PORTD2); |
36 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
43 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
37 | 44 | ||
38 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
45 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
39 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
46 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
40 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
47 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
41 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
48 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
42 | 49 | ||
43 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
44 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
51 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
45 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
52 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
46 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
53 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD &= ~(1 << PORTD2); |
47 | 54 | ||
48 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
55 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD |= (1 << PORTD2); |
49 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
56 | for (delaycounter = 0; delaycounter < 10; delaycounter++) PORTD &= ~(1 << PORTD2); |
50 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
57 | for (delaycounter = 0; delaycounter < 400; delaycounter++) PORTD |= (1 << PORTD2); |
51 | 58 | ||
52 | } |
59 | } |
53 | else |
60 | else |
54 | { printf("Timeout.\n\r"); |
61 | { printf("Timeout.\n\r"); |
55 | 62 | ||
56 | 63 | ||
57 | } |
64 | } |
58 | 65 | ||
59 | DDRD &= ~(1 << DDD2); // RX as input |
66 | DDRD &= ~(1 << DDD2); // RX as input |
60 | PORTD &= ~(1 << PORTD2); |
67 | PORTD &= ~(1 << PORTD2); |
61 | 68 | ||
62 | SpektrumUartInit(); // init Uart again |
69 | SpektrumUartInit(); // init Uart again |
63 | } |
70 | } |
64 | 71 | ||
65 | //############################################################################ |
72 | //############################################################################ |
66 | // USART1 initialisation from killagreg |
73 | // USART1 initialisation from killagreg |
67 | void SpektrumUartInit(void) |
74 | void SpektrumUartInit(void) |
68 | //############################################################################ |
75 | //############################################################################ |
69 | { |
76 | { |
70 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
77 | // -- Start of USART1 initialisation for Spekturm seriell-mode |
71 | // USART1 Control and Status Register A, B, C and baud rate register |
78 | // USART1 Control and Status Register A, B, C and baud rate register |
72 | uint8_t sreg = SREG; |
79 | uint8_t sreg = SREG; |
73 | 80 | ||
74 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
81 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * 115200) - 1); |
75 | 82 | ||
76 | // disable all interrupts before reconfiguration |
83 | // disable all interrupts before reconfiguration |
77 | cli(); |
84 | cli(); |
78 | // disable RX-Interrupt |
85 | // disable RX-Interrupt |
79 | UCSR1B &= ~(1 << RXCIE1); |
86 | UCSR1B &= ~(1 << RXCIE1); |
80 | // disable TX-Interrupt |
87 | // disable TX-Interrupt |
81 | UCSR1B &= ~(1 << TXCIE1); |
88 | UCSR1B &= ~(1 << TXCIE1); |
82 | // disable DRE-Interrupt |
89 | // disable DRE-Interrupt |
83 | UCSR1B &= ~(1 << UDRIE1); |
90 | UCSR1B &= ~(1 << UDRIE1); |
84 | // set direction of RXD1 and TXD1 pins |
91 | // set direction of RXD1 and TXD1 pins |
85 | // set RXD1 (PD2) as an input pin |
92 | // set RXD1 (PD2) as an input pin |
86 | PORTD |= (1 << PORTD2); |
93 | PORTD |= (1 << PORTD2); |
87 | DDRD &= ~(1 << DDD2); |
94 | DDRD &= ~(1 << DDD2); |
88 | 95 | ||
89 | // set TXD1 (PD3) as an output pin |
96 | // set TXD1 (PD3) as an output pin |
90 | PORTD |= (1 << PORTD3); |
97 | PORTD |= (1 << PORTD3); |
91 | DDRD |= (1 << DDD3); |
98 | DDRD |= (1 << DDD3); |
92 | 99 | ||
93 | // USART0 Baud Rate Register |
100 | // USART0 Baud Rate Register |
94 | // set clock divider |
101 | // set clock divider |
95 | UBRR1H = (uint8_t)(ubrr>>8); |
102 | UBRR1H = (uint8_t)(ubrr>>8); |
96 | UBRR1L = (uint8_t)ubrr; |
103 | UBRR1L = (uint8_t)ubrr; |
97 | // enable double speed operation |
104 | // enable double speed operation |
98 | UCSR1A |= (1 << U2X1); |
105 | UCSR1A |= (1 << U2X1); |
99 | // enable receiver and transmitter |
106 | // enable receiver and transmitter |
100 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
107 | //UCSR1B = (1<<RXEN1)|(1<<TXEN1); |
101 | 108 | ||
102 | UCSR1B = (1<<RXEN1); |
109 | UCSR1B = (1<<RXEN1); |
103 | // set asynchronous mode |
110 | // set asynchronous mode |
104 | UCSR1C &= ~(1 << UMSEL11); |
111 | UCSR1C &= ~(1 << UMSEL11); |
105 | UCSR1C &= ~(1 << UMSEL10); |
112 | UCSR1C &= ~(1 << UMSEL10); |
106 | // no parity |
113 | // no parity |
107 | UCSR1C &= ~(1 << UPM11); |
114 | UCSR1C &= ~(1 << UPM11); |
108 | UCSR1C &= ~(1 << UPM10); |
115 | UCSR1C &= ~(1 << UPM10); |
109 | // 1 stop bit |
116 | // 1 stop bit |
110 | UCSR1C &= ~(1 << USBS1); |
117 | UCSR1C &= ~(1 << USBS1); |
111 | // 8-bit |
118 | // 8-bit |
112 | UCSR1B &= ~(1 << UCSZ12); |
119 | UCSR1B &= ~(1 << UCSZ12); |
113 | UCSR1C |= (1 << UCSZ11); |
120 | UCSR1C |= (1 << UCSZ11); |
114 | UCSR1C |= (1 << UCSZ10); |
121 | UCSR1C |= (1 << UCSZ10); |
115 | // flush receive buffer explicit |
122 | // flush receive buffer explicit |
116 | while(UCSR1A & (1<<RXC1)) UDR1; |
123 | while(UCSR1A & (1<<RXC1)) UDR1; |
117 | // enable RX-interrupts at the end |
124 | // enable RX-interrupts at the end |
118 | UCSR1B |= (1 << RXCIE1); |
125 | UCSR1B |= (1 << RXCIE1); |
119 | // -- End of USART1 initialisation |
126 | // -- End of USART1 initialisation |
120 | // restore global interrupt flags |
127 | // restore global interrupt flags |
121 | 128 | ||
122 | SREG = sreg; |
129 | SREG = sreg; |
123 | return; |
130 | return; |
124 | } |
131 | } |
125 | 132 | ||
126 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
133 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
127 | // + Copyright (c) Rainer Walther |
134 | // + Copyright (c) Rainer Walther |
128 | // + RC-routines from original MK rc.c (c) H&I |
135 | // + 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 |
136 | // + Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
130 | // + only for non-profit use |
137 | // + only for non-profit use |
131 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
138 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
132 | // |
139 | // |
133 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
140 | // 20080808 rw Modified for Spektrum AR6100 (PPM) |
134 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
141 | // 20080823 rw Add Spektrum satellite receiver on USART1 (644P only) |
135 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
142 | // 20081213 rw Add support for Spektrum DS9 Air-Tx-Module (9 channels) |
136 | // Replace AR6100-coding with original composit-signal routines |
143 | // Replace AR6100-coding with original composit-signal routines |
137 | // |
144 | // |
138 | // --- |
145 | // --- |
139 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
146 | // Entweder Summensignal ODER Spektrum-Receiver anschließen. Nicht beides gleichzeitig betreiben! |
140 | // Binding is not implemented. Bind with external Receiver. |
147 | // Binding is not implemented. Bind with external Receiver. |
141 | // Servo output J3, J4, J5 not serviced |
148 | // Servo output J3, J4, J5 not serviced |
142 | // |
149 | // |
143 | // Anschuß Spektrum Receiver |
150 | // Anschuß Spektrum Receiver |
144 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
151 | // Orange: 3V von der FC (keinesfalls an 5V anschließen!) |
145 | // Schwarz: GND |
152 | // Schwarz: GND |
146 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
153 | // Grau: RXD1 (Pin 3) auf 10-Pol FC-Stecker |
147 | // |
154 | // |
148 | // --- |
155 | // --- |
149 | // Satellite-Reciever connected on USART1: |
156 | // Satellite-Reciever connected on USART1: |
150 | // |
157 | // |
151 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
158 | // DX7/DX6i: One data-frame at 115200 baud every 22ms. |
152 | // DX7se: One data-frame at 115200 baud every 11ms. |
159 | // DX7se: One data-frame at 115200 baud every 11ms. |
153 | // byte1: unknown |
160 | // byte1: unknown |
154 | // byte2: unknown |
161 | // byte2: unknown |
155 | // byte3: and byte4: channel data (FLT-Mode) |
162 | // byte3: and byte4: channel data (FLT-Mode) |
156 | // byte5: and byte6: channel data (Roll) |
163 | // byte5: and byte6: channel data (Roll) |
157 | // byte7: and byte8: channel data (Nick) |
164 | // byte7: and byte8: channel data (Nick) |
158 | // byte9: and byte10: channel data (Gier) |
165 | // byte9: and byte10: channel data (Gier) |
159 | // byte11: and byte12: channel data (Gear Switch) |
166 | // byte11: and byte12: channel data (Gear Switch) |
160 | // byte13: and byte14: channel data (Gas) |
167 | // byte13: and byte14: channel data (Gas) |
161 | // byte15: and byte16: channel data (AUX2) |
168 | // byte15: and byte16: channel data (AUX2) |
162 | // |
169 | // |
163 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
170 | // DS9 (9 Channel): One data-frame at 115200 baud every 11ms, alternating frame 1/2 for CH1-7 / CH8-9 |
164 | // 1st Frame: |
171 | // 1st Frame: |
165 | // byte1: unknown |
172 | // byte1: unknown |
166 | // byte2: unknown |
173 | // byte2: unknown |
167 | // byte3: and byte4: channel data |
174 | // byte3: and byte4: channel data |
168 | // byte5: and byte6: channel data |
175 | // byte5: and byte6: channel data |
169 | // byte7: and byte8: channel data |
176 | // byte7: and byte8: channel data |
170 | // byte9: and byte10: channel data |
177 | // byte9: and byte10: channel data |
171 | // byte11: and byte12: channel data |
178 | // byte11: and byte12: channel data |
172 | // byte13: and byte14: channel data |
179 | // byte13: and byte14: channel data |
173 | // byte15: and byte16: channel data |
180 | // byte15: and byte16: channel data |
174 | // 2nd Frame: |
181 | // 2nd Frame: |
175 | // byte1: unknown |
182 | // byte1: unknown |
176 | // byte2: unknown |
183 | // byte2: unknown |
177 | // byte3: and byte4: channel data |
184 | // byte3: and byte4: channel data |
178 | // byte5: and byte6: channel data |
185 | // byte5: and byte6: channel data |
179 | // byte7: and byte8: 0xffff |
186 | // byte7: and byte8: 0xffff |
180 | // byte9: and byte10: 0xffff |
187 | // byte9: and byte10: 0xffff |
181 | // byte11: and byte12: 0xffff |
188 | // byte11: and byte12: 0xffff |
182 | // byte13: and byte14: 0xffff |
189 | // byte13: and byte14: 0xffff |
183 | // byte15: and byte16: 0xffff |
190 | // byte15: and byte16: 0xffff |
184 | // |
191 | // |
185 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
192 | // Each channel data (16 bit= 2byte, first msb, second lsb) is arranged as: |
186 | // |
193 | // |
187 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
194 | // Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
188 | // |
195 | // |
189 | // 0 means a '0' bit |
196 | // 0 means a '0' bit |
190 | // F: 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
197 | // 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) |
198 | // 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 |
199 | // D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
193 | // |
200 | // |
194 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
201 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
195 | 202 | ||
196 | #define MIN_FRAMEGAP 68 // 7ms |
203 | #define MIN_FRAMEGAP 68 // 7ms |
197 | #define MAX_BYTEGAP 3 // 310us |
204 | #define MAX_BYTEGAP 3 // 310us |
198 | 205 | ||
199 | 206 | ||
200 | //############################################################################ |
207 | //############################################################################ |
201 | // Wird im UART-Interrupt aufgerufen |
208 | // Wird im UART-Interrupt aufgerufen |
202 | //############################################################################ |
209 | //############################################################################ |
203 | void SpektrumParser(unsigned char c) |
210 | void SpektrumParser(unsigned char c) |
204 | { |
211 | { |
205 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
212 | static unsigned char Sync=0, FrameCnt=0, ByteHigh=0, ReSync=1, Frame2=0; |
206 | unsigned int Channel, index = 0; |
213 | unsigned int Channel, index = 0; |
207 | signed int signal = 0, tmp; |
214 | signed int signal = 0, tmp; |
208 | int bCheckDelay; |
215 | int bCheckDelay; |
209 | // c = UDR1; // get data byte |
216 | // c = UDR1; // get data byte |
210 | if(ReSync == 1) |
217 | if(ReSync == 1) |
211 | { |
218 | { |
212 | // wait for beginning of new frame |
219 | // wait for beginning of new frame |
213 | ReSync = 0; |
220 | ReSync = 0; |
214 | SpektrumTimer = MIN_FRAMEGAP; |
221 | SpektrumTimer = MIN_FRAMEGAP; |
215 | FrameCnt = 0; |
222 | FrameCnt = 0; |
216 | Sync = 0; |
223 | Sync = 0; |
217 | ByteHigh = 0; |
224 | ByteHigh = 0; |
218 | } |
225 | } |
219 | else |
226 | else |
220 | { |
227 | { |
221 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
228 | if(!SpektrumTimer) bCheckDelay = 1; else bCheckDelay = 0;//CheckDelay(FrameTimer); |
222 | if ( Sync == 0 ) |
229 | if ( Sync == 0 ) |
223 | { |
230 | { |
224 | if(bCheckDelay) |
231 | if(bCheckDelay) |
225 | { |
232 | { |
226 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
233 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
227 | // Zeichen ignorieren, da Bedeutung unbekannt |
234 | // Zeichen ignorieren, da Bedeutung unbekannt |
228 | Sync = 1; |
235 | Sync = 1; |
229 | FrameCnt ++; |
236 | FrameCnt ++; |
230 | SpektrumTimer = MAX_BYTEGAP; |
237 | SpektrumTimer = MAX_BYTEGAP; |
231 | } |
238 | } |
232 | else |
239 | else |
233 | { |
240 | { |
234 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
241 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
235 | // warten auf erstes Sync-Zeichen |
242 | // warten auf erstes Sync-Zeichen |
236 | SpektrumTimer = MIN_FRAMEGAP; |
243 | SpektrumTimer = MIN_FRAMEGAP; |
237 | FrameCnt = 0; |
244 | FrameCnt = 0; |
238 | Sync = 0; |
245 | Sync = 0; |
239 | ByteHigh = 0; |
246 | ByteHigh = 0; |
240 | } |
247 | } |
241 | } |
248 | } |
242 | else if((Sync == 1) && !bCheckDelay) |
249 | else if((Sync == 1) && !bCheckDelay) |
243 | { |
250 | { |
244 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
251 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
245 | Sync = 2; |
252 | Sync = 2; |
246 | FrameCnt ++; |
253 | FrameCnt ++; |
247 | SpektrumTimer = MAX_BYTEGAP; |
254 | SpektrumTimer = MAX_BYTEGAP; |
248 | } |
255 | } |
249 | else if((Sync == 2) && !bCheckDelay) |
256 | else if((Sync == 2) && !bCheckDelay) |
250 | { |
257 | { |
251 | SpektrumTimer = MAX_BYTEGAP; |
258 | SpektrumTimer = MAX_BYTEGAP; |
252 | // Datenbyte high |
259 | // Datenbyte high |
253 | ByteHigh = c; |
260 | ByteHigh = c; |
254 | if (FrameCnt == 2) |
261 | if (FrameCnt == 2) |
255 | { |
262 | { |
256 | // is 1st Byte of Channel-data |
263 | // is 1st Byte of Channel-data |
257 | // Frame 1 with Channel 1-7 comming next |
264 | // Frame 1 with Channel 1-7 comming next |
258 | Frame2 = 0; |
265 | Frame2 = 0; |
259 | if(ByteHigh & 0x80) |
266 | if(ByteHigh & 0x80) |
260 | { |
267 | { |
261 | // DS9: Frame 2 with Channel 8-9 comming next |
268 | // DS9: Frame 2 with Channel 8-9 comming next |
262 | Frame2 = 1; |
269 | Frame2 = 1; |
263 | } |
270 | } |
264 | } |
271 | } |
265 | Sync = 3; |
272 | Sync = 3; |
266 | FrameCnt ++; |
273 | FrameCnt ++; |
267 | } |
274 | } |
268 | else if((Sync == 3) && !bCheckDelay) |
275 | else if((Sync == 3) && !bCheckDelay) |
269 | { |
276 | { |
270 | // Datenbyte low |
277 | // Datenbyte low |
271 | // High-Byte for next channel comes next |
278 | // High-Byte for next channel comes next |
272 | SpektrumTimer = MAX_BYTEGAP; |
279 | SpektrumTimer = MAX_BYTEGAP; |
273 | Sync = 2; |
280 | Sync = 2; |
274 | FrameCnt ++; |
281 | FrameCnt ++; |
275 | Channel = ((unsigned int)ByteHigh << 8) | c; |
282 | Channel = ((unsigned int)ByteHigh << 8) | c; |
276 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM) |
283 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM) |
277 | { |
284 | { |
278 | signal = Channel & 0x3ff; |
285 | signal = Channel & 0x3ff; |
279 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
286 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
280 | signal = signal/3; // scaling to fit PPM resolution |
287 | signal = signal/3; // scaling to fit PPM resolution |
281 | index = (ByteHigh >> 2) & 0x0f; |
288 | index = (ByteHigh >> 2) & 0x0f; |
282 | } |
289 | } |
283 | else |
290 | else |
284 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_HI_RES) |
291 | if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_HI_RES) |
285 | { |
292 | { |
286 | signal = Channel & 0x7ff; |
293 | signal = Channel & 0x7ff; |
287 | signal -= 0x400; // Offset, range 0x000..0x7ff? |
294 | signal -= 0x400; // Offset, range 0x000..0x7ff? |
288 | signal = signal/6; // scaling to fit PPM resolution |
295 | signal = signal/6; // scaling to fit PPM resolution |
289 | index = (ByteHigh >> 3) & 0x0f; |
296 | index = (ByteHigh >> 3) & 0x0f; |
290 | } |
297 | } |
291 | else |
298 | else |
292 | //if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_LOW_RES) |
299 | //if(EE_Parameter.Receiver == RECEIVER_SPEKTRUM_LOW_RES) |
293 | { |
300 | { |
294 | signal = Channel & 0x3ff; |
301 | signal = Channel & 0x3ff; |
295 | signal -= 360; // Offset, range 0x000..0x3ff? |
302 | signal -= 360; // Offset, range 0x000..0x3ff? |
296 | signal = signal/2; // scaling to fit PPM resolution |
303 | signal = signal/2; // scaling to fit PPM resolution |
297 | index = (ByteHigh >> 2) & 0x0f; |
304 | index = (ByteHigh >> 2) & 0x0f; |
298 | } |
305 | } |
299 | 306 | ||
300 | index++; |
307 | index++; |
301 | if(index < 13) |
308 | if(index < 13) |
302 | { |
309 | { |
303 | // Stabiles Signal |
310 | // Stabiles Signal |
- | 311 | #ifdef RECEIVER_SPEKTRUM_EXP |
|
- | 312 | if (index == 2) index = 4; // Analog channel reassigment (2 <-> 4) for logical numbering (1,2,3,4) |
|
- | 313 | else if (index == 4) index = 2; |
|
- | 314 | #endif |
|
304 | if(abs(signal - PPM_in[index]) < 6) |
315 | if(abs(signal - PPM_in[index]) < 6) |
305 | { |
316 | { |
306 | if(SenderOkay < 200) SenderOkay += 10; |
317 | if(SenderOkay < 200) SenderOkay += 10; |
307 | else |
318 | else |
308 | { |
319 | { |
309 | SenderOkay = 200; |
320 | SenderOkay = 200; |
310 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
321 | TIMSK1 &= ~_BV(ICIE1); // disable PPM-Input |
311 | } |
322 | } |
312 | } |
323 | } |
313 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
324 | tmp = (3 * (PPM_in[index]) + signal) / 4; |
314 | if(tmp > signal+1) tmp--; else |
325 | if(tmp > signal+1) tmp--; else |
315 | if(tmp < signal-1) tmp++; |
326 | if(tmp < signal-1) tmp++; |
- | 327 | ||
- | 328 | #ifdef RECEIVER_SPEKTRUM_EXP |
|
- | 329 | if(index == 6) // FLIGHT-MODE - The channel used for our data uplink |
|
- | 330 | { |
|
- | 331 | if (signal > 100) // SYNC received |
|
- | 332 | { |
|
- | 333 | sexcnt = 0; // Reset bitcounter |
|
- | 334 | sexparity = 0; // Reset parity bit |
|
- | 335 | } |
|
- | 336 | ||
- | 337 | if (signal < 10) |
|
- | 338 | { |
|
- | 339 | sexcnt++; // Increase counter only for non-sync bits |
|
- | 340 | sexdata[sexcnt] = -113; // Bit = 0 -> value = -113 (min) |
|
- | 341 | } |
|
- | 342 | ||
- | 343 | if (sexcnt == 7) sexcnt = 0; // Overflow protection |
|
- | 344 | ||
- | 345 | if (signal < -100) |
|
- | 346 | { |
|
- | 347 | sexdata[sexcnt] = 114; // Bit = 1 -> value = 114 (max) |
|
- | 348 | if (sexcnt < 6) sexparity = ~sexparity; // Bit = 1 -> Invert parity bit (without itself) |
|
- | 349 | } |
|
- | 350 | ||
- | 351 | if (sexcnt == 6) // Wait for complete frame |
|
- | 352 | { |
|
- | 353 | if ((sexparity != 0 && sexdata[6] == -113) || (sexparity == 0 && sexdata[6] == 114)) // Parity check |
|
- | 354 | { |
|
- | 355 | if (sexdata[1] == 114 && sexdata[2] == -113) PPM_in[5] = -113;// Reconstruct tripole Flight-Mode value (CH5) |
|
- | 356 | if (sexdata[1] == -113 && sexdata[2] == -113) PPM_in[5] = 0; // Reconstruct tripole Flight-Mode value (CH5) |
|
- | 357 | if (sexdata[1] == -113 && sexdata[2] == 114) PPM_in[5] = 114; // Reconstruct tripole Flight-Mode value (CH5) |
|
- | 358 | PPM_in[6] = sexdata[3]; // Elevator (CH6) |
|
- | 359 | PPM_in[9] = sexdata[4]; // Aileron (CH9) |
|
- | 360 | PPM_in[10] = sexdata[5]; // Rudder (CH10) |
|
- | 361 | } |
|
- | 362 | } |
|
- | 363 | } |
|
- | 364 | #endif |
|
316 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
365 | if(SenderOkay >= 180) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3; |
- | 366 | ||
317 | else PPM_diff[index] = 0; |
367 | else PPM_diff[index] = 0; |
- | 368 | ||
- | 369 | #ifdef RECEIVER_SPEKTRUM_EXP |
|
- | 370 | if (index < 5 ) PPM_in[index] = tmp; // Update normal potis (CH1-4) |
|
- | 371 | if (index == 5) PPM_in[7] = signal; // Gear (CH7) |
|
- | 372 | if (index == 7) PPM_in[8] = signal; // AUX2 (CH8) |
|
- | 373 | #else |
|
318 | PPM_in[index] = tmp; |
374 | PPM_in[index] = tmp; |
- | 375 | #endif |
|
319 | } |
376 | } |
320 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
377 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
321 | } |
378 | } |
322 | else |
379 | else |
323 | { |
380 | { |
324 | // hier stimmt was nicht: neu synchronisieren |
381 | // hier stimmt was nicht: neu synchronisieren |
325 | ReSync = 1; |
382 | ReSync = 1; |
326 | FrameCnt = 0; |
383 | FrameCnt = 0; |
327 | Frame2 = 0; |
384 | Frame2 = 0; |
328 | // new frame next, nach fruehestens 7ms erwartet |
385 | // new frame next, nach fruehestens 7ms erwartet |
329 | SpektrumTimer = MIN_FRAMEGAP; |
386 | SpektrumTimer = MIN_FRAMEGAP; |
330 | } |
387 | } |
331 | 388 | ||
332 | // 16 Bytes eingetroffen -> Komplett |
389 | // 16 Bytes eingetroffen -> Komplett |
333 | if(FrameCnt >= 16) |
390 | if(FrameCnt >= 16) |
334 | { |
391 | { |
335 | // Frame complete |
392 | // Frame complete |
336 | if(Frame2 == 0) |
393 | if(Frame2 == 0) |
337 | { |
394 | { |
338 | // Null bedeutet: Neue Daten |
395 | // Null bedeutet: Neue Daten |
339 | // nur beim ersten Frame (CH 0-7) setzen |
396 | // nur beim ersten Frame (CH 0-7) setzen |
340 | if(!ReSync) NewPpmData = 0; |
397 | if(!ReSync) NewPpmData = 0; |
341 | } |
398 | } |
342 | FrameCnt = 0; |
399 | FrameCnt = 0; |
343 | Frame2 = 0; |
400 | Frame2 = 0; |
344 | Sync = 0; |
401 | Sync = 0; |
345 | SpektrumTimer = MIN_FRAMEGAP; |
402 | SpektrumTimer = MIN_FRAMEGAP; |
346 | } |
403 | } |
347 | } |
404 | } |
348 | } |
405 | } |
349 | 406 | ||
350 | 407 | ||
351 | 408 |