]> git.zerfleddert.de Git - fhem-stuff/blob - culfw/culfw-asksin-fix.diff
rebase against r378
[fhem-stuff] / culfw / culfw-asksin-fix.diff
1 Index: clib/rf_asksin.c
2 ===================================================================
3 --- clib/rf_asksin.c (revision 378)
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[] = {
15 - 0x00, 0x07,
16 + 0x00, 0x01,
17 0x02, 0x2e,
18 0x03, 0x0d,
19 0x04, 0xE9,
20 0x05, 0xCA,
21 - 0x07, 0x0C,
22 + 0x07, 0x04,
23 0x0B, 0x06,
24 0x0D, 0x21,
25 0x0E, 0x65,
26 @@ -26,7 +29,7 @@
27 0x11, 0x93,
28 0x12, 0x03,
29 0x15, 0x34,
30 - 0x17, 0x33, // go into RX after TX, CCA; EQ3 uses 0x03
31 + 0x17, 0x3F, // always go into RX, CCA; EQ3 uses 0x03
32 0x18, 0x18,
33 0x19, 0x16,
34 0x1B, 0x43,
35 @@ -39,6 +42,7 @@
36 0x3e, 0xc3
37 };
38
39 +static inline uint8_t read_cc1100_rxbytes(void);
40 static void rf_asksin_reset_rx(void);
41
42 void
43 @@ -74,12 +78,29 @@
44 } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);
45 }
46
47 +// Workaround for CC1101 Errata 3
48 +static inline uint8_t
49 +read_cc1100_rxbytes(void)
50 +{
51 + uint8_t rxbytes, rxbytes2;
52 +
53 + rxbytes = cc1100_readReg(CC1100_RXBYTES);
54 + while((rxbytes2 = cc1100_readReg(CC1100_RXBYTES)) != rxbytes)
55 + rxbytes = rxbytes2;
56 +
57 + return rxbytes;
58 +}
59 +
60 static void
61 rf_asksin_reset_rx(void)
62 {
63 ccStrobe( CC1100_SFRX );
64 ccStrobe( CC1100_SIDLE );
65 ccStrobe( CC1100_SNOP );
66 +
67 + while (read_cc1100_rxbytes() & 0x7f)
68 + cc1100_readReg(CC1100_RXFIFO);
69 +
70 ccStrobe( CC1100_SRX );
71 }
72
73 @@ -90,13 +111,29 @@
74 uint8_t dec[MAX_ASKSIN_MSG];
75 uint8_t rssi;
76 uint8_t l;
77 + uint8_t rxfifo_cnt;
78 + uint16_t timeout;
79
80 if(!asksin_on)
81 return;
82
83 - // see if a CRC OK pkt has been arrived
84 - if (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) {
85 + // see if there is data to be read
86 + while (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) {
87 + rxfifo_cnt = read_cc1100_rxbytes();
88 +
89 + if (rxfifo_cnt & 0x80) // Overflow
90 + break;
91 +
92 + rxfifo_cnt &= 0x7f;
93 +
94 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
95 + while ((rxfifo_cnt < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
96 + my_delay_ms(1);
97 + rxfifo_cnt = read_cc1100_rxbytes() & 0x7f;
98 + }
99 +
100 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
101 + rxfifo_cnt--;
102
103 if (enc[0] >= MAX_ASKSIN_MSG) {
104 // Something went horribly wrong, out of sync?
105 @@ -104,6 +141,27 @@
106 return;
107 }
108
109 + if ((enc[0] + 2) > rxfifo_cnt) {
110 + timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt);
111 + while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data
112 + my_delay_ms(1);
113 + rxfifo_cnt = read_cc1100_rxbytes();
114 +
115 + if (rxfifo_cnt & 0x80) { // Overflow
116 + rf_asksin_reset_rx();
117 + return;
118 + }
119 +
120 + rxfifo_cnt &= 0x7f;
121 + }
122 +
123 + if (!timeout) {
124 + // Not enough data received, out of sync?
125 + rf_asksin_reset_rx();
126 + return;
127 + }
128 + }
129 +
130 CC1100_ASSERT;
131 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
132
133 @@ -112,14 +170,19 @@
134 }
135
136 rssi = cc1100_sendbyte( 0 );
137 - /* LQI = */ cc1100_sendbyte( 0 );
138
139 CC1100_DEASSERT;
140
141 - do {
142 - ccStrobe(CC1100_SRX);
143 - } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);
144 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
145 + while (((read_cc1100_rxbytes() & 0x7f) < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
146 + my_delay_ms(1);
147 + }
148
149 + l = cc1100_readReg(CC1100_RXFIFO);
150 +
151 + if (!(l & 0x80)) // CRC not ok
152 + continue;
153 +
154 dec[0] = enc[0];
155 dec[1] = (~enc[1]) ^ 0x89;
156
Impressum, Datenschutz