Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1538 | killagreg | 1 | #include <stdlib.h> |
2 | #include "spectrum.h" |
||
3 | #include "rc.h" |
||
4 | |||
5 | uint8_t SpektrumTimer; |
||
6 | |||
7 | /* |
||
8 | Code derived from: |
||
9 | |||
10 | Copyright (c) Rainer Walther |
||
11 | RC-routines from original MK rc.c (c) H&I |
||
12 | Useful infos from Walter: http://www.rcgroups.com/forums/showthread.php?t=714299&page=2 |
||
13 | only for non-profit use |
||
14 | |||
15 | Connection of Spectrum Sattelite to SV1 of FC: |
||
16 | |||
17 | Orange: 3V from the FC (never connect 5V!) |
||
18 | Black: GND |
||
19 | Gray: RXD1 (Pin 3) |
||
20 | |||
21 | If a receiver is connected via PPM input at the same time, the PPM input will be disabled |
||
22 | if a stable signal can be captured by the uart. |
||
23 | |||
24 | Data are send at every 20 ms @ 115200 Baud 8-N-1 |
||
25 | |||
26 | DX7/DX6i: One data-frame @ 115200 Baud 8-N-1 every 22ms. |
||
27 | DX7se: One data-frame @ 115200 Baud 8-N-1 every 11ms. |
||
28 | |||
29 | Frame consist of: |
||
30 | byte1: unknown |
||
31 | byte2: unknown |
||
32 | byte3: and byte4: channel data |
||
33 | byte5: and byte6: channel data |
||
34 | byte7: and byte8: channel data |
||
35 | byte9: and byte10: channel data |
||
36 | byte11: and byte12: channel data |
||
37 | byte13: and byte14: channel data |
||
38 | byte15: and byte16: channel data |
||
39 | |||
40 | DS9 (9 Channel): One data-frame @ 115200 Baud 8-N-1 every 11ms, |
||
41 | alternating frame 1/2 for CH1-7 / CH8-9 |
||
42 | |||
43 | 1st Frame consist of: |
||
44 | byte1: unknown |
||
45 | byte2: unknown |
||
46 | byte3: and byte4: channel data |
||
47 | byte5: and byte6: channel data |
||
48 | byte7: and byte8: channel data |
||
49 | byte9: and byte10: channel data |
||
50 | byte11: and byte12: channel data |
||
51 | byte13: and byte14: channel data |
||
52 | byte15: and byte16: channel data |
||
53 | |||
54 | 2nd Frame consist of: |
||
55 | byte1: unknown |
||
56 | byte2: unknown |
||
57 | byte3: and byte4: channel data |
||
58 | byte5: and byte6: channel data |
||
59 | byte7: and byte8: 0xffff |
||
60 | byte9: and byte10: 0xffff |
||
61 | byte11: and byte12: 0xffff |
||
62 | byte13: and byte14: 0xffff |
||
63 | byte15: and byte16: 0xffff |
||
64 | |||
65 | Each channel data (16 bit = 2byte, first msb, second lsb) is arranged as: |
||
66 | |||
67 | Bits: F 0 C3 C2 C1 C0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 |
||
68 | |||
69 | |||
70 | F: 0 = indicate first frame, 1 = indicates beginning of 2nd frame for CH8-9 (DS9 only) |
||
71 | C3 to C0 is the channel number. 0 to 9 (4 bit, as assigned in the transmitter) |
||
72 | D9 to D0 is the channel data (10 bit) 0xaa..0x200..0x356 for 100% transmitter-travel |
||
73 | */ |
||
74 | |||
75 | #define MIN_FRAMEGAP 68 // 7ms |
||
76 | #define MAX_BYTEGAP 3 // 375us |
||
77 | |||
78 | void spectrum_parser(uint8_t c) |
||
79 | { |
||
80 | static uint8_t Sync = 0, FrameCnt = 0, ByteHigh = 0, ReSync = 1, Frame2 = 0; |
||
81 | uint16_t Channel, index; |
||
82 | int16_t signal; //tmp; |
||
83 | int16_t bCheckDelay; |
||
84 | |||
85 | |||
86 | if (ReSync == 1) |
||
87 | { |
||
88 | // wait for beginning of new frame |
||
89 | ReSync = 0; |
||
90 | SpektrumTimer = MIN_FRAMEGAP; |
||
91 | FrameCnt = 0; |
||
92 | Sync = 0; |
||
93 | ByteHigh = 0; |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | if(!SpektrumTimer) bCheckDelay = 1; |
||
98 | else bCheckDelay = 0; |
||
99 | if ( Sync == 0 ) |
||
100 | { |
||
101 | if(bCheckDelay) |
||
102 | { |
||
103 | // nach einer Pause von mind. 7ms erstes Sync-Character gefunden |
||
104 | // Zeichen ignorieren, da Bedeutung unbekannt |
||
105 | SpektrumTimer = MAX_BYTEGAP; |
||
106 | FrameCnt++; |
||
107 | Sync = 1; |
||
108 | } |
||
109 | else |
||
110 | { |
||
111 | // Zeichen kam vor Ablauf der 7ms Sync-Pause |
||
112 | // warten auf erstes Sync-Zeichen |
||
113 | SpektrumTimer = MIN_FRAMEGAP; |
||
114 | FrameCnt = 0; |
||
115 | Sync = 0; |
||
116 | ByteHigh = 0; |
||
117 | } |
||
118 | } |
||
119 | else if((Sync == 1) && !bCheckDelay) |
||
120 | { |
||
121 | // zweites Sync-Character ignorieren, Bedeutung unbekannt |
||
122 | SpektrumTimer = MAX_BYTEGAP; |
||
123 | Sync = 2; |
||
124 | FrameCnt++; |
||
125 | } |
||
126 | else if((Sync == 2) && !bCheckDelay) |
||
127 | { |
||
128 | // Datenbyte high |
||
129 | SpektrumTimer = MAX_BYTEGAP; |
||
130 | ByteHigh = c; |
||
131 | if (FrameCnt == 2) |
||
132 | { |
||
133 | // is 1st Byte of Channel-data |
||
134 | // Frame 1 with Channel 1-7 comming next |
||
135 | Frame2 = 0; |
||
136 | if(ByteHigh & 0x80) |
||
137 | { |
||
138 | // DS9: Frame 2 with Channel 8-9 comming next |
||
139 | Frame2 = 1; |
||
140 | } |
||
141 | } |
||
142 | Sync = 3; |
||
143 | FrameCnt ++; |
||
144 | } |
||
145 | else if((Sync == 3) && !bCheckDelay) |
||
146 | { |
||
147 | // Datenbyte low |
||
148 | // High-Byte for next channel comes next |
||
149 | SpektrumTimer = MAX_BYTEGAP; |
||
150 | Sync = 2; |
||
151 | FrameCnt ++; |
||
152 | index = (ByteHigh >> 2) & 0x0f; |
||
153 | index ++; |
||
154 | Channel = ((uint16_t)ByteHigh << 8) | c; |
||
155 | signal = Channel & 0x3ff; |
||
156 | signal -= 0x200; // Offset, range 0x000..0x3ff? |
||
157 | signal = signal/3; // scaling to fit PPM resolution |
||
158 | |||
159 | if(index >= 0 && index < MAX_CHANNELS) |
||
160 | { |
||
161 | if(RC_Channels < index) RC_Channels = index; |
||
162 | // Stabiles Signal |
||
163 | if(abs(signal - PPM_in[index]) < 6) |
||
164 | { |
||
165 | if(RC_Quality < 200) |
||
166 | { |
||
167 | RC_Quality += 10; |
||
168 | } |
||
169 | else |
||
170 | { |
||
171 | RC_Quality = 200; |
||
172 | PPM_INPUT_OFF; // disable PPM input at ICP |
||
173 | } |
||
174 | } |
||
175 | //tmp = (3 * PPM_in[index] + signal)/4; |
||
176 | //if(tmp > signal+1) tmp--; |
||
177 | //else if(tmp < signal-1) tmp++; |
||
178 | // calculate signal difference on good signal level |
||
179 | if(RC_Quality >= 180) PPM_diff[index] = ((signal - PPM_in[index]) / 3) * 3; |
||
180 | else PPM_diff[index] = 0; |
||
181 | PPM_in[index] = signal; |
||
182 | } |
||
183 | else if(index > 17) ReSync = 1; // hier stimmt was nicht: neu synchronisieren |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | // hier stimmt was nicht: neu synchronisieren |
||
188 | SpektrumTimer = MIN_FRAMEGAP; // next frame expexted after 7ms |
||
189 | ReSync = 1; |
||
190 | FrameCnt = 0; |
||
191 | Frame2 = 0; |
||
192 | } |
||
193 | |||
194 | // 16 Bytes per frame --> frame complete |
||
195 | if(FrameCnt >= 16) |
||
196 | { |
||
197 | // Frame complete |
||
198 | if(Frame2 == 0) |
||
199 | { |
||
200 | // Null bedeutet: Neue Daten |
||
201 | // nur beim ersten Frame (CH 0-7) setzen |
||
202 | if(!ReSync) NewPpmData = 0; |
||
203 | } |
||
204 | SpektrumTimer = MIN_FRAMEGAP; |
||
205 | FrameCnt = 0; |
||
206 | Frame2 = 0; |
||
207 | Sync = 0; |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 |