]>
Commit | Line | Data |
---|---|---|
1 | Index: clib/rf_asksin.c | |
2 | =================================================================== | |
3 | --- clib/rf_asksin.c (revision 373) | |
4 | +++ clib/rf_asksin.c (working copy) | |
5 | @@ -9,15 +9,18 @@ | |
6 | ||
7 | #include "rf_asksin.h" | |
8 | ||
9 | +//we receive a new byte approximately every 8 ms... | |
10 | +#define RX_TIMEOUT_MS 10 | |
11 | + | |
12 | uint8_t asksin_on = 0; | |
13 | ||
14 | -const uint8_t PROGMEM ASKSIN_CFG[50] = { | |
15 | - 0x00, 0x07, | |
16 | +const uint8_t PROGMEM ASKSIN_CFG[] = { | |
17 | + 0x00, 0x01, | |
18 | 0x02, 0x2e, | |
19 | 0x03, 0x0d, | |
20 | 0x04, 0xE9, | |
21 | 0x05, 0xCA, | |
22 | - 0x07, 0x0C, | |
23 | + 0x07, 0x04, | |
24 | 0x0B, 0x06, | |
25 | 0x0D, 0x21, | |
26 | 0x0E, 0x65, | |
27 | @@ -26,18 +29,22 @@ | |
28 | 0x11, 0x93, | |
29 | 0x12, 0x03, | |
30 | 0x15, 0x34, | |
31 | - 0x17, 0x30, // always go into IDLE | |
32 | + 0x17, 0x3F, // always go into RX, CCA; ELV uses 0x03 | |
33 | 0x18, 0x18, | |
34 | 0x19, 0x16, | |
35 | 0x1B, 0x43, | |
36 | 0x21, 0x56, | |
37 | 0x25, 0x00, | |
38 | 0x26, 0x11, | |
39 | + 0x29, 0x59, | |
40 | + 0x2c, 0x81, | |
41 | 0x2D, 0x35, | |
42 | - 0x3e, 0xc3, | |
43 | - 0xff | |
44 | + 0x3e, 0xc3 | |
45 | }; | |
46 | ||
47 | +static inline uint8_t read_cc1100_rxbytes(void); | |
48 | +static void rf_asksin_reset_rx(void); | |
49 | + | |
50 | void | |
51 | rf_asksin_init(void) | |
52 | { | |
53 | @@ -56,20 +63,44 @@ | |
54 | my_delay_us(100); | |
55 | ||
56 | // load configuration | |
57 | - for (uint8_t i = 0; i<50; i += 2) { | |
58 | - | |
59 | - if (pgm_read_byte( &ASKSIN_CFG[i] )>0x40) | |
60 | - break; | |
61 | - | |
62 | + for (uint8_t i = 0; i < sizeof(ASKSIN_CFG); i += 2) { | |
63 | cc1100_writeReg( pgm_read_byte(&ASKSIN_CFG[i]), | |
64 | pgm_read_byte(&ASKSIN_CFG[i+1]) ); | |
65 | } | |
66 | ||
67 | ccStrobe( CC1100_SCAL ); | |
68 | ||
69 | - my_delay_ms(1); | |
70 | + my_delay_ms(4); | |
71 | + | |
72 | + ccRX(); | |
73 | } | |
74 | ||
75 | +// Workaround for CC1101 Errata 3 | |
76 | +static inline uint8_t | |
77 | +read_cc1100_rxbytes(void) | |
78 | +{ | |
79 | + uint8_t rxbytes, rxbytes2; | |
80 | + | |
81 | + rxbytes = cc1100_readReg(CC1100_RXBYTES); | |
82 | + while((rxbytes2 = cc1100_readReg(CC1100_RXBYTES)) != rxbytes) | |
83 | + rxbytes = rxbytes2; | |
84 | + | |
85 | + return rxbytes; | |
86 | +} | |
87 | + | |
88 | +static void | |
89 | +rf_asksin_reset_rx(void) | |
90 | +{ | |
91 | + ccStrobe( CC1100_SFRX ); | |
92 | + ccStrobe( CC1100_SIDLE ); | |
93 | + ccStrobe( CC1100_SNOP ); | |
94 | + | |
95 | + while (read_cc1100_rxbytes() & 0x7f) | |
96 | + cc1100_readReg(CC1100_RXFIFO); | |
97 | + | |
98 | + ccStrobe( CC1100_SRX ); | |
99 | +} | |
100 | + | |
101 | void | |
102 | rf_asksin_task(void) | |
103 | { | |
104 | @@ -77,18 +108,57 @@ | |
105 | uint8_t dec[MAX_ASKSIN_MSG]; | |
106 | uint8_t rssi; | |
107 | uint8_t l; | |
108 | + uint8_t rxfifo_cnt; | |
109 | + uint16_t timeout; | |
110 | ||
111 | if(!asksin_on) | |
112 | return; | |
113 | ||
114 | - // see if a CRC OK pkt has been arrived | |
115 | - if (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) { | |
116 | + // see if there is data to be read | |
117 | + while (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) { | |
118 | + rxfifo_cnt = read_cc1100_rxbytes(); | |
119 | ||
120 | + if (rxfifo_cnt & 0x80) // Overflow | |
121 | + break; | |
122 | + | |
123 | + rxfifo_cnt &= 0x7f; | |
124 | + | |
125 | + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1) | |
126 | + while ((rxfifo_cnt == 1) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) { | |
127 | + my_delay_ms(1); | |
128 | + rxfifo_cnt = read_cc1100_rxbytes() & 0x7f; | |
129 | + } | |
130 | + | |
131 | enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len | |
132 | + rxfifo_cnt--; | |
133 | ||
134 | - if (enc[0]>=MAX_ASKSIN_MSG) | |
135 | - enc[0] = MAX_ASKSIN_MSG-1; | |
136 | - | |
137 | + if (enc[0] >= MAX_ASKSIN_MSG) { | |
138 | + // Something went horribly wrong, out of sync? | |
139 | + rf_asksin_reset_rx(); | |
140 | + return; | |
141 | + } | |
142 | + | |
143 | + if ((enc[0] + 2) > rxfifo_cnt) { | |
144 | + timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt); | |
145 | + while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data | |
146 | + my_delay_ms(1); | |
147 | + rxfifo_cnt = read_cc1100_rxbytes(); | |
148 | + | |
149 | + if (rxfifo_cnt & 0x80) { // Overflow | |
150 | + rf_asksin_reset_rx(); | |
151 | + return; | |
152 | + } | |
153 | + | |
154 | + rxfifo_cnt &= 0x7f; | |
155 | + } | |
156 | + | |
157 | + if (!timeout) { | |
158 | + // Not enough data received, out of sync? | |
159 | + rf_asksin_reset_rx(); | |
160 | + return; | |
161 | + } | |
162 | + } | |
163 | + | |
164 | CC1100_ASSERT; | |
165 | cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO ); | |
166 | ||
167 | @@ -97,14 +167,19 @@ | |
168 | } | |
169 | ||
170 | rssi = cc1100_sendbyte( 0 ); | |
171 | - | |
172 | + | |
173 | CC1100_DEASSERT; | |
174 | ||
175 | - ccStrobe( CC1100_SFRX ); | |
176 | - ccStrobe( CC1100_SIDLE ); | |
177 | - ccStrobe( CC1100_SNOP ); | |
178 | - ccStrobe( CC1100_SRX ); | |
179 | + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1) | |
180 | + while (((read_cc1100_rxbytes() & 0x7f) == 1) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) { | |
181 | + my_delay_ms(1); | |
182 | + } | |
183 | ||
184 | + l = cc1100_readReg(CC1100_RXFIFO); | |
185 | + | |
186 | + if (!(l & 0x80)) // CRC not ok | |
187 | + continue; | |
188 | + | |
189 | dec[0] = enc[0]; | |
190 | dec[1] = (~enc[1]) ^ 0x89; | |
191 | ||
192 | @@ -113,7 +188,6 @@ | |
193 | ||
194 | dec[l] = enc[l] ^ dec[2]; | |
195 | ||
196 | - | |
197 | if (tx_report & REP_BINTIME) { | |
198 | ||
199 | DC('a'); | |
200 | @@ -131,26 +205,17 @@ | |
201 | ||
202 | DNL(); | |
203 | } | |
204 | + } | |
205 | ||
206 | - return; | |
207 | - | |
208 | + switch(cc1100_readReg( CC1100_MARCSTATE )) { | |
209 | + case MARCSTATE_RXFIFO_OVERFLOW: | |
210 | + ccStrobe( CC1100_SFRX ); | |
211 | + case MARCSTATE_IDLE: | |
212 | + ccStrobe( CC1100_SIDLE ); | |
213 | + ccStrobe( CC1100_SNOP ); | |
214 | + ccStrobe( CC1100_SRX ); | |
215 | + break; | |
216 | } | |
217 | - | |
218 | - | |
219 | - switch (cc1100_readReg( CC1100_MARCSTATE )) { | |
220 | - | |
221 | - // RX_OVERFLOW | |
222 | - case 17: | |
223 | - // IDLE | |
224 | - case 1: | |
225 | - ccStrobe( CC1100_SFRX ); | |
226 | - ccStrobe( CC1100_SIDLE ); | |
227 | - ccStrobe( CC1100_SNOP ); | |
228 | - ccStrobe( CC1100_SRX ); | |
229 | - break; | |
230 | - | |
231 | - } | |
232 | - | |
233 | } | |
234 | ||
235 | void | |
236 | @@ -173,20 +238,7 @@ | |
237 | my_delay_ms(3); // 3ms: Found by trial and error | |
238 | } | |
239 | ||
240 | - ccStrobe(CC1100_SIDLE); | |
241 | - ccStrobe(CC1100_SFRX ); | |
242 | - ccStrobe(CC1100_SFTX ); | |
243 | - | |
244 | - if (dec[2] & (1 << 4)) { //BURST-bit set? | |
245 | - ccStrobe(CC1100_STX ); //We need to send a burst | |
246 | - | |
247 | - //According to ELV, devices get activated every 300ms, so send burst for 360ms | |
248 | - for(l = 0; l < 3; l++) | |
249 | - my_delay_ms(120); //arg is uint_8, so loop | |
250 | - } | |
251 | - | |
252 | // "crypt" | |
253 | - | |
254 | enc[0] = dec[0]; | |
255 | enc[1] = (~dec[1]) ^ 0x89; | |
256 | ||
257 | @@ -195,6 +247,15 @@ | |
258 | ||
259 | enc[l] = dec[l] ^ dec[2]; | |
260 | ||
261 | + ccTX(); | |
262 | + if (dec[2] & (1 << 4)) { // BURST-bit set? | |
263 | + // According to ELV, devices get activated every 300ms, so send burst for 360ms | |
264 | + for(l = 0; l < 3; l++) | |
265 | + my_delay_ms(120); // arg is uint_8, so loop | |
266 | + } else { | |
267 | + my_delay_ms(10); | |
268 | + } | |
269 | + | |
270 | // send | |
271 | CC1100_ASSERT; | |
272 | cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); | |
273 | @@ -205,12 +266,17 @@ | |
274 | ||
275 | CC1100_DEASSERT; | |
276 | ||
277 | - ccStrobe( CC1100_SFRX ); | |
278 | - ccStrobe( CC1100_STX ); | |
279 | + // wait for TX to finish | |
280 | + while(cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TX) | |
281 | + ; | |
282 | + | |
283 | + if (cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TXFIFO_UNDERFLOW) { | |
284 | + ccStrobe( CC1100_SFTX ); | |
285 | + ccStrobe( CC1100_SIDLE ); | |
286 | + ccStrobe( CC1100_SNOP ); | |
287 | + ccStrobe( CC1100_SRX ); | |
288 | + } | |
289 | ||
290 | - while( cc1100_readReg( CC1100_MARCSTATE ) != 1 ) | |
291 | - my_delay_ms(5); | |
292 | - | |
293 | if(asksin_on) { | |
294 | ccRX(); | |
295 | } else { |