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