]> git.zerfleddert.de Git - fhem-stuff/blame - culfw/culfw-asksin-fix.diff
rebase against r378
[fhem-stuff] / culfw / culfw-asksin-fix.diff
CommitLineData
b2720844
MG
1Index: clib/rf_asksin.c
2===================================================================
ce15a465 3--- clib/rf_asksin.c (revision 378)
b2720844 4+++ clib/rf_asksin.c (working copy)
f59d1d47 5@@ -9,15 +9,18 @@
b2720844 6
f59d1d47
MG
7 #include "rf_asksin.h"
8
9+//we receive a new byte approximately every 8 ms...
10+#define RX_TIMEOUT_MS 10
11+
b2720844
MG
12 uint8_t asksin_on = 0;
13
82f42c48 14 const uint8_t PROGMEM ASKSIN_CFG[] = {
532a7bf0 15- 0x00, 0x07,
532a7bf0 16+ 0x00, 0x01,
b2720844
MG
17 0x02, 0x2e,
18 0x03, 0x0d,
532a7bf0 19 0x04, 0xE9,
f59d1d47
MG
20 0x05, 0xCA,
21- 0x07, 0x0C,
22+ 0x07, 0x04,
23 0x0B, 0x06,
24 0x0D, 0x21,
25 0x0E, 0x65,
82f42c48 26@@ -26,7 +29,7 @@
b2720844
MG
27 0x11, 0x93,
28 0x12, 0x03,
29 0x15, 0x34,
82f42c48
MG
30- 0x17, 0x33, // go into RX after TX, CCA; EQ3 uses 0x03
31+ 0x17, 0x3F, // always go into RX, CCA; EQ3 uses 0x03
b2720844
MG
32 0x18, 0x18,
33 0x19, 0x16,
34 0x1B, 0x43,
82f42c48
MG
35@@ -39,6 +42,7 @@
36 0x3e, 0xc3
b2720844
MG
37 };
38
f59d1d47 39+static inline uint8_t read_cc1100_rxbytes(void);
82f42c48 40 static void rf_asksin_reset_rx(void);
b2720844 41
82f42c48 42 void
ce15a465
MG
43@@ -74,12 +78,29 @@
44 } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);
b2720844
MG
45 }
46
f59d1d47
MG
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+
82f42c48
MG
60 static void
61 rf_asksin_reset_rx(void)
62 {
63 ccStrobe( CC1100_SFRX );
64 ccStrobe( CC1100_SIDLE );
65 ccStrobe( CC1100_SNOP );
f59d1d47
MG
66+
67+ while (read_cc1100_rxbytes() & 0x7f)
68+ cc1100_readReg(CC1100_RXFIFO);
69+
82f42c48
MG
70 ccStrobe( CC1100_SRX );
71 }
72
ce15a465 73@@ -90,13 +111,29 @@
f59d1d47
MG
74 uint8_t dec[MAX_ASKSIN_MSG];
75 uint8_t rssi;
76 uint8_t l;
77+ uint8_t rxfifo_cnt;
78+ uint16_t timeout;
cf4474a9 79
f59d1d47
MG
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();
82f42c48 88+
f59d1d47
MG
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)
82f42c48 95+ while ((rxfifo_cnt < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
f59d1d47
MG
96+ my_delay_ms(1);
97+ rxfifo_cnt = read_cc1100_rxbytes() & 0x7f;
98+ }
99+
532a7bf0 100 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
f59d1d47 101+ rxfifo_cnt--;
cf4474a9 102
82f42c48
MG
103 if (enc[0] >= MAX_ASKSIN_MSG) {
104 // Something went horribly wrong, out of sync?
ce15a465 105@@ -104,6 +141,27 @@
82f42c48
MG
106 return;
107 }
108
f59d1d47
MG
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+ }
532a7bf0 129+
db4f8119
MG
130 CC1100_ASSERT;
131 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
532a7bf0 132
ce15a465 133@@ -112,14 +170,19 @@
532a7bf0
MG
134 }
135
136 rssi = cc1100_sendbyte( 0 );
82f42c48
MG
137- /* LQI = */ cc1100_sendbyte( 0 );
138
532a7bf0 139 CC1100_DEASSERT;
b2720844 140
ce15a465
MG
141- do {
142- ccStrobe(CC1100_SRX);
143- } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);
f59d1d47 144+ // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
82f42c48 145+ while (((read_cc1100_rxbytes() & 0x7f) < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
f59d1d47
MG
146+ my_delay_ms(1);
147+ }
b74270d7 148
f59d1d47
MG
149+ l = cc1100_readReg(CC1100_RXFIFO);
150+
151+ if (!(l & 0x80)) // CRC not ok
152+ continue;
153+
532a7bf0 154 dec[0] = enc[0];
f59d1d47
MG
155 dec[1] = (~enc[1]) ^ 0x89;
156
Impressum, Datenschutz