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 8 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 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
126 + while ((rxfifo_cnt == 1) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
128 + rxfifo_cnt = read_cc1100_rxbytes() & 0x7f;
131 enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len
134 - if (enc[0]>=MAX_ASKSIN_MSG)
135 - enc[0] = MAX_ASKSIN_MSG-1;
137 + if (enc[0] >= MAX_ASKSIN_MSG) {
138 + // Something went horribly wrong, out of sync?
139 + rf_asksin_reset_rx();
143 + if ((enc[0] + 2) > rxfifo_cnt) {
144 + timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt);
145 + while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data
147 + rxfifo_cnt = read_cc1100_rxbytes();
149 + if (rxfifo_cnt & 0x80) { // Overflow
150 + rf_asksin_reset_rx();
154 + rxfifo_cnt &= 0x7f;
158 + // Not enough data received, out of sync?
159 + rf_asksin_reset_rx();
165 cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO );
170 rssi = cc1100_sendbyte( 0 );
175 - ccStrobe( CC1100_SFRX );
176 - ccStrobe( CC1100_SIDLE );
177 - ccStrobe( CC1100_SNOP );
178 - ccStrobe( CC1100_SRX );
179 + // We must not read the last byte from the RX fifo while RX is in progress (Errata 1)
180 + while (((read_cc1100_rxbytes() & 0x7f) == 1) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) {
184 + l = cc1100_readReg(CC1100_RXFIFO);
186 + if (!(l & 0x80)) // CRC not ok
190 dec[1] = (~enc[1]) ^ 0x89;
194 dec[l] = enc[l] ^ dec[2];
197 if (tx_report & REP_BINTIME) {
200 @@ -131,26 +205,17 @@
208 + switch(cc1100_readReg( CC1100_MARCSTATE )) {
209 + case MARCSTATE_RXFIFO_OVERFLOW:
210 + ccStrobe( CC1100_SFRX );
211 + case MARCSTATE_IDLE:
212 + ccStrobe( CC1100_SIDLE );
213 + ccStrobe( CC1100_SNOP );
214 + ccStrobe( CC1100_SRX );
219 - switch (cc1100_readReg( CC1100_MARCSTATE )) {
225 - ccStrobe( CC1100_SFRX );
226 - ccStrobe( CC1100_SIDLE );
227 - ccStrobe( CC1100_SNOP );
228 - ccStrobe( CC1100_SRX );
237 my_delay_ms(3); // 3ms: Found by trial and error
240 - ccStrobe(CC1100_SIDLE);
241 - ccStrobe(CC1100_SFRX );
242 - ccStrobe(CC1100_SFTX );
244 - if (dec[2] & (1 << 4)) { //BURST-bit set?
245 - ccStrobe(CC1100_STX ); //We need to send a burst
247 - //According to ELV, devices get activated every 300ms, so send burst for 360ms
248 - for(l = 0; l < 3; l++)
249 - my_delay_ms(120); //arg is uint_8, so loop
255 enc[1] = (~dec[1]) ^ 0x89;
259 enc[l] = dec[l] ^ dec[2];
262 + if (dec[2] & (1 << 4)) { // BURST-bit set?
263 + // According to ELV, devices get activated every 300ms, so send burst for 360ms
264 + for(l = 0; l < 3; l++)
265 + my_delay_ms(120); // arg is uint_8, so loop
272 cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO);
273 @@ -205,12 +266,17 @@
277 - ccStrobe( CC1100_SFRX );
278 - ccStrobe( CC1100_STX );
279 + // wait for TX to finish
280 + while(cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TX)
283 + if (cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TXFIFO_UNDERFLOW) {
284 + ccStrobe( CC1100_SFTX );
285 + ccStrobe( CC1100_SIDLE );
286 + ccStrobe( CC1100_SNOP );
287 + ccStrobe( CC1100_SRX );
290 - while( cc1100_readReg( CC1100_MARCSTATE ) != 1 )