X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/fhem-stuff/blobdiff_plain/cf4474a9a93f454bc7af0db64c8cf0303741a4d1..54f699f34a726721c5092279b34ee216d6bcbae6:/culfw/culfw-asksin-fix.diff diff --git a/culfw/culfw-asksin-fix.diff b/culfw/culfw-asksin-fix.diff index 5c8db33..19e6598 100644 --- a/culfw/culfw-asksin-fix.diff +++ b/culfw/culfw-asksin-fix.diff @@ -1,250 +1,189 @@ Index: clib/rf_asksin.c =================================================================== ---- clib/rf_asksin.c (revision 373) +--- clib/rf_asksin.c (revision 377) +++ clib/rf_asksin.c (working copy) -@@ -11,7 +11,7 @@ +@@ -9,15 +9,18 @@ + #include "rf_asksin.h" + ++//we receive a new byte approximately every 8 ms... ++#define RX_TIMEOUT_MS 10 ++ uint8_t asksin_on = 0; --const uint8_t PROGMEM ASKSIN_CFG[50] = { -+const uint8_t PROGMEM ASKSIN_CFG[] = { - 0x00, 0x07, + const uint8_t PROGMEM ASKSIN_CFG[] = { +- 0x00, 0x07, ++ 0x00, 0x01, 0x02, 0x2e, 0x03, 0x0d, -@@ -26,16 +26,17 @@ + 0x04, 0xE9, + 0x05, 0xCA, +- 0x07, 0x0C, ++ 0x07, 0x04, + 0x0B, 0x06, + 0x0D, 0x21, + 0x0E, 0x65, +@@ -26,7 +29,7 @@ 0x11, 0x93, 0x12, 0x03, 0x15, 0x34, -- 0x17, 0x30, // always go into IDLE -+ 0x17, 0x3F, // always go into RX after TX, CCA, ELV uses 0x03 +- 0x17, 0x33, // go into RX after TX, CCA; EQ3 uses 0x03 ++ 0x17, 0x3F, // always go into RX, CCA; EQ3 uses 0x03 0x18, 0x18, 0x19, 0x16, 0x1B, 0x43, - 0x21, 0x56, - 0x25, 0x00, - 0x26, 0x11, -+ 0x29, 0x59, -+ 0x2c, 0x81, - 0x2D, 0x35, -- 0x3e, 0xc3, -- 0xff -+ 0x3e, 0xc3 +@@ -39,6 +42,7 @@ + 0x3e, 0xc3 }; ++static inline uint8_t read_cc1100_rxbytes(void); + static void rf_asksin_reset_rx(void); + void -@@ -56,18 +57,16 @@ - my_delay_us(100); - - // load configuration -- for (uint8_t i = 0; i<50; i += 2) { -- -- if (pgm_read_byte( &ASKSIN_CFG[i] )>0x40) -- break; -- -+ for (uint8_t i = 0; i < sizeof(ASKSIN_CFG); i += 2) { - cc1100_writeReg( pgm_read_byte(&ASKSIN_CFG[i]), - pgm_read_byte(&ASKSIN_CFG[i+1]) ); - } - - ccStrobe( CC1100_SCAL ); +@@ -68,15 +72,35 @@ -- my_delay_ms(1); -+ my_delay_ms(4); + my_delay_ms(4); + +- ccRX(); ++ // enable RX, but don't enable the interrupt ++ do { ++ ccStrobe(CC1100_SRX); ++ } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX); + } + ++// Workaround for CC1101 Errata 3 ++static inline uint8_t ++read_cc1100_rxbytes(void) ++{ ++ uint8_t rxbytes, rxbytes2; ++ ++ rxbytes = cc1100_readReg(CC1100_RXBYTES); ++ while((rxbytes2 = cc1100_readReg(CC1100_RXBYTES)) != rxbytes) ++ rxbytes = rxbytes2; ++ ++ return rxbytes; ++} ++ + static void + rf_asksin_reset_rx(void) + { + ccStrobe( CC1100_SFRX ); + ccStrobe( CC1100_SIDLE ); + ccStrobe( CC1100_SNOP ); + -+ ccRX(); ++ while (read_cc1100_rxbytes() & 0x7f) ++ cc1100_readReg(CC1100_RXFIFO); ++ + ccStrobe( CC1100_SRX ); } - void -@@ -77,80 +76,74 @@ +@@ -87,13 +111,29 @@ uint8_t dec[MAX_ASKSIN_MSG]; uint8_t rssi; uint8_t l; + uint8_t rxfifo_cnt; ++ uint16_t timeout; if(!asksin_on) return; - // see if a CRC OK pkt has been arrived - if (bit_is_set( CC1100_IN_PORT, CC1100_IN_PIN )) { -+ while((rxfifo_cnt = cc1100_readReg(CC1100_RXBYTES)) != 0x00) { -+ if (rxfifo_cnt & 0x80) // Overflow -+ break; - -- enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len -+ enc[0] = cc1100_readReg( CC1100_RXFIFO ) & 0x7f; // read len - -- if (enc[0]>=MAX_ASKSIN_MSG) -- enc[0] = MAX_ASKSIN_MSG-1; -- -- CC1100_ASSERT; -- cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO ); -- -- for (uint8_t i=0; i=MAX_ASKSIN_MSG) -+ enc[0] = MAX_ASKSIN_MSG-1; -+ -+ CC1100_ASSERT; -+ cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO ); -+ -+ for (uint8_t i=0; i= MAX_ASKSIN_MSG) { + // Something went horribly wrong, out of sync? +@@ -101,6 +141,27 @@ + return; + } + ++ if ((enc[0] + 2) > rxfifo_cnt) { ++ timeout = RX_TIMEOUT_MS * ((enc[0] + 2) - rxfifo_cnt); ++ while (timeout-- && ((enc[0] + 2) > rxfifo_cnt)) { // Wait for more data ++ my_delay_ms(1); ++ rxfifo_cnt = read_cc1100_rxbytes(); ++ ++ if (rxfifo_cnt & 0x80) { // Overflow ++ rf_asksin_reset_rx(); ++ return; ++ } + -+ dec[0] = enc[0]; -+ dec[1] = (~enc[1]) ^ 0x89; - -- DC('a'); -- for (uint8_t i=0; i<=dec[0]; i++) -- DC( dec[i] ); -- -- } else { -- DC('A'); -+ for (l = 2; l < dec[0]; l++) -+ dec[l] = (enc[l-1] + 0xdc) ^ enc[l]; - -- for (uint8_t i=0; i<=dec[0]; i++) -- DH2( dec[i] ); -+ dec[l] = enc[l] ^ dec[2]; - -- if (tx_report & REP_RSSI) -- DH2(rssi); -- -- DNL(); -+ if (tx_report & REP_BINTIME) { -+ -+ DC('a'); -+ for (uint8_t i=0; i<=dec[0]; i++) -+ DC( dec[i] ); -+ -+ } else { -+ DC('A'); -+ -+ for (uint8_t i=0; i<=dec[0]; i++) -+ DH2( dec[i] ); -+ -+ if (tx_report & REP_RSSI) -+ DH2(rssi); -+ -+ DNL(); ++ rxfifo_cnt &= 0x7f; + } ++ ++ if (!timeout) { ++ // Not enough data received, out of sync? ++ rf_asksin_reset_rx(); ++ return; ++ } ++ } ++ + CC1100_ASSERT; + cc1100_sendbyte( CC1100_READ_BURST | CC1100_RXFIFO ); + +@@ -109,12 +170,19 @@ } -- -- return; -- - } -- -- -- switch (cc1100_readReg( CC1100_MARCSTATE )) { -- -- // RX_OVERFLOW -- case 17: -- // IDLE -- case 1: -- ccStrobe( CC1100_SFRX ); -- ccStrobe( CC1100_SIDLE ); -- ccStrobe( CC1100_SNOP ); -- ccStrobe( CC1100_SRX ); -- break; -- -- } - -+ switch(cc1100_readReg( CC1100_MARCSTATE )) { -+ case MARCSTATE_RXFIFO_OVERFLOW: -+ ccStrobe( CC1100_SFRX ); -+ case MARCSTATE_IDLE: -+ ccStrobe( CC1100_SIDLE ); -+ ccStrobe( CC1100_SNOP ); -+ ccStrobe( CC1100_SRX ); -+ break; -+ } - } + + rssi = cc1100_sendbyte( 0 ); +- /* LQI = */ cc1100_sendbyte( 0 ); - void -@@ -173,20 +166,7 @@ - my_delay_ms(3); // 3ms: Found by trial and error - } + CC1100_DEASSERT; -- ccStrobe(CC1100_SIDLE); -- ccStrobe(CC1100_SFRX ); -- ccStrobe(CC1100_SFTX ); -- -- if (dec[2] & (1 << 4)) { //BURST-bit set? -- ccStrobe(CC1100_STX ); //We need to send a burst -- -- //According to ELV, devices get activated every 300ms, so send burst for 360ms -- for(l = 0; l < 3; l++) -- my_delay_ms(120); //arg is uint_8, so loop -- } -- - // "crypt" -- - enc[0] = dec[0]; - enc[1] = (~dec[1]) ^ 0x89; - -@@ -195,6 +175,15 @@ +- ccStrobe( CC1100_SRX ); ++ // We must not read the last byte from the RX fifo while RX is in progress (Errata 1) ++ while (((read_cc1100_rxbytes() & 0x7f) < 2) && (cc1100_readReg(CC1100_PKTSTATUS) & (1 << 3))) { ++ my_delay_ms(1); ++ } + ++ l = cc1100_readReg(CC1100_RXFIFO); ++ ++ if (!(l & 0x80)) // CRC not ok ++ continue; ++ + dec[0] = enc[0]; + dec[1] = (~enc[1]) ^ 0x89; + +@@ -182,7 +250,11 @@ enc[l] = dec[l] ^ dec[2]; -+ ccTX(); -+ if (dec[2] & (1 << 4)) { // BURST-bit set? -+ // According to ELV, devices get activated every 300ms, so send burst for 360ms -+ for(l = 0; l < 3; l++) -+ my_delay_ms(120); // arg is uint_8, so loop -+ } else { -+ my_delay_ms(10); +- ccTX(); ++ // enable TX, wait for CCA ++ while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_TX) { ++ ccStrobe(CC1100_STX); + } + - // send - CC1100_ASSERT; - cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); -@@ -205,12 +194,17 @@ - - CC1100_DEASSERT; - -- ccStrobe( CC1100_SFRX ); -- ccStrobe( CC1100_STX ); -+ // wait for TX to finish -+ while(cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TX) -+ ; -+ -+ if (cc1100_readReg( CC1100_MARCSTATE ) == MARCSTATE_TXFIFO_UNDERFLOW) { -+ ccStrobe( CC1100_SFTX ); -+ ccStrobe( CC1100_SIDLE ); -+ ccStrobe( CC1100_SNOP ); -+ ccStrobe( CC1100_SRX ); -+ } + if (dec[2] & (1 << 4)) { // BURST-bit set? + // According to ELV, devices get activated every 300ms, so send burst for 360ms + for(l = 0; l < 3; l++) +@@ -209,11 +281,12 @@ + ccStrobe( CC1100_SFTX ); + ccStrobe( CC1100_SIDLE ); + ccStrobe( CC1100_SNOP ); +- ccStrobe( CC1100_SRX ); + } -- while( cc1100_readReg( CC1100_MARCSTATE ) != 1 ) -- my_delay_ms(5); -- if(asksin_on) { - ccRX(); +- ccRX(); ++ do { ++ ccStrobe(CC1100_SRX); ++ } while (cc1100_readReg(CC1100_MARCSTATE) != MARCSTATE_RX); } else { + set_txrestore(); + }