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