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