1 Index: clib/rf_asksin.c
2 ===================================================================
3 --- clib/rf_asksin.c (revision 373)
4 +++ clib/rf_asksin.c (working copy)
9 +//we receive a new byte approximately every 7 ms...
10 +#define RX_TIMEOUT_MS 10
12 uint8_t asksin_on = 0;
14 -const uint8_t PROGMEM ASKSIN_CFG[50] = {
16 +const uint8_t PROGMEM ASKSIN_CFG[] = {
31 - 0x17, 0x30, // always go into IDLE
32 + 0x17, 0x3F, // always go into RX, CCA; ELV uses 0x03
47 +static inline uint8_t read_cc1100_rxbytes(void);
48 +static void rf_asksin_reset_rx(void);
57 - for (uint8_t i = 0; i<50; i += 2) {
59 - if (pgm_read_byte( &ASKSIN_CFG[i] )>0x40)
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]) );
67 ccStrobe( CC1100_SCAL );
75 +// Workaround for CC1101 Errata 3
76 +static inline uint8_t
77 +read_cc1100_rxbytes(void)
79 + uint8_t rxbytes, rxbytes2;
81 + rxbytes = cc1100_readReg(CC1100_RXBYTES);
82 + while((rxbytes2 = cc1100_readReg(CC1100_RXBYTES)) != rxbytes)
89 +rf_asksin_reset_rx(void)
91 + ccStrobe( CC1100_SFRX );
92 + ccStrobe( CC1100_SIDLE );
93 + ccStrobe( CC1100_SNOP );
95 + while (read_cc1100_rxbytes() & 0x7f)
96 + cc1100_readReg(CC1100_RXFIFO);
98 + ccStrobe( CC1100_SRX );
105 uint8_t dec[MAX_ASKSIN_MSG];
108 + uint8_t rxfifo_cnt;
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();
120 + if (rxfifo_cnt & 0x80) // Overflow
123 + rxfifo_cnt &= 0x7f;
125 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
128 - if (enc[0]>=MAX_ASKSIN_MSG)
129 - enc[0] = MAX_ASKSIN_MSG-1;
131 + if (enc[0] >= MAX_ASKSIN_MSG) {
132 + // Something went horribly wrong, out of sync?
133 + rf_asksin_reset_rx();
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
141 + rxfifo_cnt = read_cc1100_rxbytes();
143 + if (rxfifo_cnt & 0x80) { // Overflow
144 + rf_asksin_reset_rx();
148 + rxfifo_cnt &= 0x7f;
152 + // Not enough data received, out of sync?
153 + rf_asksin_reset_rx();
159 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
164 rssi = cc1100_sendbyte( 0 );
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))) {
178 + l = cc1100_readReg(CC1100_RXFIFO);
180 + if (!(l & 0x80)) // CRC not ok
184 dec[1] = (~enc[1]) ^ 0x89;
188 dec[l] = enc[l] ^ dec[2];
191 if (tx_report & REP_BINTIME) {
194 @@ -131,26 +199,17 @@
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 );
213 - switch (cc1100_readReg( CC1100_MARCSTATE )) {
219 - ccStrobe( CC1100_SFRX );
220 - ccStrobe( CC1100_SIDLE );
221 - ccStrobe( CC1100_SNOP );
222 - ccStrobe( CC1100_SRX );
231 my_delay_ms(3); // 3ms: Found by trial and error
234 - ccStrobe(CC1100_SIDLE);
235 - ccStrobe(CC1100_SFRX );
236 - ccStrobe(CC1100_SFTX );
238 - if (dec[2] & (1 << 4)) { //BURST-bit set?
239 - ccStrobe(CC1100_STX ); //We need to send a burst
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
249 enc[1] = (~dec[1]) ^ 0x89;
253 enc[l] = dec[l] ^ dec[2];
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
266 cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO);
267 @@ -205,12 +260,17 @@
271 - ccStrobe( CC1100_SFRX );
272 - ccStrobe( CC1100_STX );
273 + // wait for TX to finish
274 + while(cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TX)
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 );
284 - while( cc1100_readReg( CC1100_MARCSTATE ) != 1 )