1 Index: clib/rf_asksin.c
2 ===================================================================
3 --- clib/rf_asksin.c (revision 377)
4 +++ clib/rf_asksin.c (working copy)
9 +//we receive a new byte approximately every 8 ms...
10 +#define RX_TIMEOUT_MS 10
12 uint8_t asksin_on = 0;
14 const uint8_t PROGMEM ASKSIN_CFG[] = {
30 - 0x17, 0x33, // go into RX after TX, CCA; EQ3 uses 0x03
31 + 0x17, 0x3F, // always go into RX, CCA; EQ3 uses 0x03
39 +static inline uint8_t read_cc1100_rxbytes(void);
40 static void rf_asksin_reset_rx(void);
48 + // enable RX, but don't enable the interrupt
50 + ccStrobe(CC1100_SRX);
51 + } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);
54 +// Workaround for CC1101 Errata 3
55 +static inline uint8_t
56 +read_cc1100_rxbytes(void)
58 + uint8_t rxbytes, rxbytes2;
60 + rxbytes = cc1100_readReg(CC1100_RXBYTES);
61 + while((rxbytes2 = cc1100_readReg(CC1100_RXBYTES)) != rxbytes)
68 rf_asksin_reset_rx(void)
70 ccStrobe( CC1100_SFRX );
71 ccStrobe( CC1100_SIDLE );
72 ccStrobe( CC1100_SNOP );
74 + while (read_cc1100_rxbytes() & 0x7f)
75 + cc1100_readReg(CC1100_RXFIFO);
77 ccStrobe( CC1100_SRX );
81 uint8_t dec[MAX_ASKSIN_MSG];
90 - // see if a CRC OK pkt has been arrived
91 - if (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) {
92 + // see if there is data to be read
93 + while (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) {
94 + rxfifo_cnt = read_cc1100_rxbytes();
96 + if (rxfifo_cnt & 0x80) // Overflow
101 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
102 + while ((rxfifo_cnt < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
104 + rxfifo_cnt = read_cc1100_rxbytes() & 0x7f;
107 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
110 if (enc[0] >= MAX_ASKSIN_MSG) {
111 // Something went horribly wrong, out of sync?
116 + if ((enc[0] + 2) > rxfifo_cnt) {
117 + timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt);
118 + while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data
120 + rxfifo_cnt = read_cc1100_rxbytes();
122 + if (rxfifo_cnt & 0x80) { // Overflow
123 + rf_asksin_reset_rx();
127 + rxfifo_cnt &= 0x7f;
131 + // Not enough data received, out of sync?
132 + rf_asksin_reset_rx();
138 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
140 @@ -109,12 +170,19 @@
143 rssi = cc1100_sendbyte( 0 );
144 - /* LQI = */ cc1100_sendbyte( 0 );
148 - ccStrobe( CC1100_SRX );
149 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
150 + while (((read_cc1100_rxbytes() & 0x7f) < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
154 + l = cc1100_readReg(CC1100_RXFIFO);
156 + if (!(l & 0x80)) // CRC not ok
160 dec[1] = (~enc[1]) ^ 0x89;
164 enc[l] = dec[l] ^ dec[2];
167 + // enable TX, wait for CCA
168 + while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_TX) {
169 + ccStrobe(CC1100_STX);
172 if (dec[2] & (1 << 4)) { // BURST-bit set?
173 // According to ELV, devices get activated every 300ms, so send burst for 360ms
174 for(l = 0; l < 3; l++)
175 @@ -209,11 +281,12 @@
176 ccStrobe( CC1100_SFTX );
177 ccStrobe( CC1100_SIDLE );
178 ccStrobe( CC1100_SNOP );
179 - ccStrobe( CC1100_SRX );
185 + ccStrobe(CC1100_SRX);
186 + } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX);