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