]> git.zerfleddert.de Git - fhem-stuff/blob - culfw/culfw-asksin-fix.diff
38621e77dcd0f3bc08bcbcd80acb138fbfd0142b
[fhem-stuff] / culfw / culfw-asksin-fix.diff
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,51 @@
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 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
126 + rxfifo_cnt--;
127
128 - if (enc[0]>=MAX_ASKSIN_MSG)
129 - enc[0] = MAX_ASKSIN_MSG-1;
130 -
131 + if (enc[0] >= MAX_ASKSIN_MSG) {
132 + // Something went horribly wrong, out of sync?
133 + rf_asksin_reset_rx();
134 + return;
135 + }
136 +
137 + if ((enc[0] + 2) > rxfifo_cnt) {
138 + timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt);
139 + while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data
140 + my_delay_ms(1);
141 + rxfifo_cnt = read_cc1100_rxbytes();
142 +
143 + if (rxfifo_cnt & 0x80) { // Overflow
144 + rf_asksin_reset_rx();
145 + return;
146 + }
147 +
148 + rxfifo_cnt &= 0x7f;
149 + }
150 +
151 + if (!timeout) {
152 + // Not enough data received, out of sync?
153 + rf_asksin_reset_rx();
154 + return;
155 + }
156 + }
157 +
158 CC1100_ASSERT;
159 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
160
161 @@ -97,14 +161,19 @@
162 }
163
164 rssi = cc1100_sendbyte( 0 );
165 -
166 +
167 CC1100_DEASSERT;
168
169 - ccStrobe( CC1100_SFRX );
170 - ccStrobe( CC1100_SIDLE );
171 - ccStrobe( CC1100_SNOP );
172 - ccStrobe( CC1100_SRX );
173 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
174 + while (((read_cc1100_rxbytes() & 0x7f) == 1) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
175 + my_delay_ms(1);
176 + }
177
178 + l = cc1100_readReg(CC1100_RXFIFO);
179 +
180 + if (!(l & 0x80)) // CRC not ok
181 + continue;
182 +
183 dec[0] = enc[0];
184 dec[1] = (~enc[1]) ^ 0x89;
185
186 @@ -113,7 +182,6 @@
187
188 dec[l] = enc[l] ^ dec[2];
189
190 -
191 if (tx_report & REP_BINTIME) {
192
193 DC('a');
194 @@ -131,26 +199,17 @@
195
196 DNL();
197 }
198 + }
199
200 - return;
201 -
202 + switch(cc1100_readReg( CC1100_MARCSTATE )) {
203 + case MARCSTATE_RXFIFO_OVERFLOW:
204 + ccStrobe( CC1100_SFRX );
205 + case MARCSTATE_IDLE:
206 + ccStrobe( CC1100_SIDLE );
207 + ccStrobe( CC1100_SNOP );
208 + ccStrobe( CC1100_SRX );
209 + break;
210 }
211 -
212 -
213 - switch (cc1100_readReg( CC1100_MARCSTATE )) {
214 -
215 - // RX_OVERFLOW
216 - case 17:
217 - // IDLE
218 - case 1:
219 - ccStrobe( CC1100_SFRX );
220 - ccStrobe( CC1100_SIDLE );
221 - ccStrobe( CC1100_SNOP );
222 - ccStrobe( CC1100_SRX );
223 - break;
224 -
225 - }
226 -
227 }
228
229 void
230 @@ -173,20 +232,7 @@
231 my_delay_ms(3); // 3ms: Found by trial and error
232 }
233
234 - ccStrobe(CC1100_SIDLE);
235 - ccStrobe(CC1100_SFRX );
236 - ccStrobe(CC1100_SFTX );
237 -
238 - if (dec[2] & (1 << 4)) { //BURST-bit set?
239 - ccStrobe(CC1100_STX ); //We need to send a burst
240 -
241 - //According to ELV, devices get activated every 300ms, so send burst for 360ms
242 - for(l = 0; l < 3; l++)
243 - my_delay_ms(120); //arg is uint_8, so loop
244 - }
245 -
246 // "crypt"
247 -
248 enc[0] = dec[0];
249 enc[1] = (~dec[1]) ^ 0x89;
250
251 @@ -195,6 +241,15 @@
252
253 enc[l] = dec[l] ^ dec[2];
254
255 + ccTX();
256 + if (dec[2] & (1 << 4)) { // BURST-bit set?
257 + // According to ELV, devices get activated every 300ms, so send burst for 360ms
258 + for(l = 0; l < 3; l++)
259 + my_delay_ms(120); // arg is uint_8, so loop
260 + } else {
261 + my_delay_ms(10);
262 + }
263 +
264 // send
265 CC1100_ASSERT;
266 cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO);
267 @@ -205,12 +260,17 @@
268
269 CC1100_DEASSERT;
270
271 - ccStrobe( CC1100_SFRX );
272 - ccStrobe( CC1100_STX );
273 + // wait for TX to finish
274 + while(cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TX)
275 + ;
276 +
277 + if (cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TXFIFO_UNDERFLOW) {
278 + ccStrobe( CC1100_SFTX );
279 + ccStrobe( CC1100_SIDLE );
280 + ccStrobe( CC1100_SNOP );
281 + ccStrobe( CC1100_SRX );
282 + }
283
284 - while( cc1100_readReg( CC1100_MARCSTATE ) != 1 )
285 - my_delay_ms(5);
286 -
287 if(asksin_on) {
288 ccRX();
289 } else {
Impressum, Datenschutz