From a4ec7cd919c2b30983888c93f13e6202b5676736 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 16:22:18 +0100 Subject: [PATCH 1/9] Update CC1101.h Add Max packet size for FIFO Refills --- src/modules/CC1101/CC1101.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/CC1101/CC1101.h b/src/modules/CC1101/CC1101.h index 576f8d739..3b4128e97 100644 --- a/src/modules/CC1101/CC1101.h +++ b/src/modules/CC1101/CC1101.h @@ -8,6 +8,7 @@ // CC1101 physical layer properties #define RADIOLIB_CC1101_FREQUENCY_STEP_SIZE 396.7285156 +#define RADIOLIB_CC1101_MAX_PACKET_LENGTH 255 #define RADIOLIB_CC1101_MAX_PACKET_LENGTH 64 #define RADIOLIB_CC1101_CRYSTAL_FREQ 26.0f #define RADIOLIB_CC1101_DIV_EXPONENT 16 From bf0f27208bce38af270dd0bf860f50f4abbcbb2d Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 16:24:48 +0100 Subject: [PATCH 2/9] Define FIFO Size, Max packet Length for FIFO refills --- src/modules/CC1101/CC1101.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/CC1101/CC1101.h b/src/modules/CC1101/CC1101.h index 3b4128e97..b77edb323 100644 --- a/src/modules/CC1101/CC1101.h +++ b/src/modules/CC1101/CC1101.h @@ -9,7 +9,7 @@ // CC1101 physical layer properties #define RADIOLIB_CC1101_FREQUENCY_STEP_SIZE 396.7285156 #define RADIOLIB_CC1101_MAX_PACKET_LENGTH 255 -#define RADIOLIB_CC1101_MAX_PACKET_LENGTH 64 +#define RADIOLIB_CC1101_FIFO_SIZE 64 #define RADIOLIB_CC1101_CRYSTAL_FREQ 26.0f #define RADIOLIB_CC1101_DIV_EXPONENT 16 From 346585d620fe7e3132174fa7d217337e02b7a9e6 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 16:53:20 +0100 Subject: [PATCH 3/9] FIFO REFILL - Go through FSTXON State - Check MARCSTATE to ensure ready to tx - Initial FIFO fill - Check FIFO bytes twice in accordance with errata - Refill FIFO - Check MARCSTATE is idle before returning --- src/modules/CC1101/CC1101.cpp | 38 ++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index 1c0222c16..c04c661c3 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -236,23 +236,39 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { // flush Tx FIFO SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_TX); + // Turn on freq oscilator + SPIsendCommand(RADIOLIB_CC1101_CMD_FSTXON); + + // Check MARCSTATE and wait until ready to tx + While(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x12) {}; + // set GDO0 mapping int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_SYNC_WORD_SENT_OR_PKT_RECEIVED, 5, 0); RADIOLIB_ASSERT(state); + // data put on FIFO + uint8_t dataSent = 0; + // optionally write packet length if(this->packetLengthConfig == RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE) { + if (len > RADIOLIB_CC1101_MAX_PACKET_LENGTH - 1) { + return(RADIOLIB_ERR_PACKET_TOO_LONG); + } SPIwriteRegister(RADIOLIB_CC1101_REG_FIFO, len); + dataSent+= 1; } // check address filtering uint8_t filter = SPIgetRegValue(RADIOLIB_CC1101_REG_PKTCTRL1, 1, 0); if(filter != RADIOLIB_CC1101_ADR_CHK_NONE) { SPIwriteRegister(RADIOLIB_CC1101_REG_FIFO, addr); + dataSent += 1; } // fill the FIFO - SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(data), len); + uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); + SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(data), initialWrite); + dataSent += initialWrite; // set RF switch (if present) this->mod->setRfSwitchState(Module::MODE_TX); @@ -260,6 +276,26 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { // set mode to transmit SPIsendCommand(RADIOLIB_CC1101_CMD_TX); + // Keep feeding the FIFO until the packet is done + While (dataSent < len) { + uint8_t fifoBytes = 0; + uint8_t prevFifobytes = 0; + + // Check number of bytes on FIFO twice due to the CC1101 errata. Block until two reads are equal. + do{ + fifoBytes = SPIgetRegValue(RADIOLIB_CC1101_REG_TXBYTES, 6, 0); + prevFifobytes = SPIgetRegValue(RADIOLIB_CC1101_REG_TXBYTES, 6, 0); + } while (fifoBytes != prevFifobytes) + + //If there is room add more data to the FIFO + if (fifoBytes < RADIOLIB_CC1101_FIFO_SIZE) { + uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); + SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(&data[dataSent]), bytesToWrite); + dataSent += bytesToWrite; + } + } + // Check MARCSTATE for Idle + while(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x01) {}; return(state); } From 0a230e6daa4e80778f6ef52d02291576dbfaa876 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:12:08 +0100 Subject: [PATCH 4/9] Fix typos --- src/modules/CC1101/CC1101.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index c04c661c3..b52958b51 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -240,7 +240,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { SPIsendCommand(RADIOLIB_CC1101_CMD_FSTXON); // Check MARCSTATE and wait until ready to tx - While(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x12) {}; + while(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x12) {}; // set GDO0 mapping int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_SYNC_WORD_SENT_OR_PKT_RECEIVED, 5, 0); @@ -277,7 +277,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { SPIsendCommand(RADIOLIB_CC1101_CMD_TX); // Keep feeding the FIFO until the packet is done - While (dataSent < len) { + while (dataSent < len) { uint8_t fifoBytes = 0; uint8_t prevFifobytes = 0; From 08a884cfb8cae99a8507bfee0d47ef3fdb317d38 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:15:08 +0100 Subject: [PATCH 5/9] Fix another typo --- src/modules/CC1101/CC1101.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index b52958b51..c737493ff 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -285,7 +285,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { do{ fifoBytes = SPIgetRegValue(RADIOLIB_CC1101_REG_TXBYTES, 6, 0); prevFifobytes = SPIgetRegValue(RADIOLIB_CC1101_REG_TXBYTES, 6, 0); - } while (fifoBytes != prevFifobytes) + } while (fifoBytes != prevFifobytes); //If there is room add more data to the FIFO if (fifoBytes < RADIOLIB_CC1101_FIFO_SIZE) { From af8ca5263649045ae53d3bdd7ca3b28c620f87b3 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:19:19 +0100 Subject: [PATCH 6/9] min -> std::min per build check --- src/modules/CC1101/CC1101.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index c737493ff..1a946a536 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -266,7 +266,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { } // fill the FIFO - uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); + uint8_t initialWrite = std::min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(data), initialWrite); dataSent += initialWrite; @@ -289,7 +289,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { //If there is room add more data to the FIFO if (fifoBytes < RADIOLIB_CC1101_FIFO_SIZE) { - uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); + uint8_t bytesToWrite = std::min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(&data[dataSent]), bytesToWrite); dataSent += bytesToWrite; } From 76407a12f67bd68c6fa482933ef00f692f8845c3 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 17:30:24 +0100 Subject: [PATCH 7/9] Revert std::min back to min --- src/modules/CC1101/CC1101.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index 1a946a536..c737493ff 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -266,7 +266,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { } // fill the FIFO - uint8_t initialWrite = std::min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); + uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(data), initialWrite); dataSent += initialWrite; @@ -289,7 +289,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { //If there is room add more data to the FIFO if (fifoBytes < RADIOLIB_CC1101_FIFO_SIZE) { - uint8_t bytesToWrite = std::min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); + uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(&data[dataSent]), bytesToWrite); dataSent += bytesToWrite; } From d4ef5f7bc96966ae235c8189ab7f1c8cc327fdbd Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Tue, 28 Jan 2025 19:26:19 +0100 Subject: [PATCH 8/9] Use RADIOLIB_MIN Macro instead of min --- src/modules/CC1101/CC1101.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index c737493ff..23e05bfd1 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -266,7 +266,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { } // fill the FIFO - uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); + uint8_t initialWrite = RADIOLIB_MIN((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(data), initialWrite); dataSent += initialWrite; @@ -289,7 +289,7 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { //If there is room add more data to the FIFO if (fifoBytes < RADIOLIB_CC1101_FIFO_SIZE) { - uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); + uint8_t bytesToWrite = RADIOLIB_MIN((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - fifoBytes), (uint8_t)(len - dataSent)); SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, const_cast(&data[dataSent]), bytesToWrite); dataSent += bytesToWrite; } From 34f0d2fd2b954968e423395254564f7c4dc44349 Mon Sep 17 00:00:00 2001 From: Crsarmv7l <85343771+Crsarmv7l@users.noreply.github.com> Date: Sat, 1 Feb 2025 17:02:20 +0100 Subject: [PATCH 9/9] Move MARC State check for Idle to finishTransmit function Change allows startTransmit to stop blocking once the last bytes are added to the FIFO --- src/modules/CC1101/CC1101.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/modules/CC1101/CC1101.cpp b/src/modules/CC1101/CC1101.cpp index 23e05bfd1..807b776db 100644 --- a/src/modules/CC1101/CC1101.cpp +++ b/src/modules/CC1101/CC1101.cpp @@ -294,13 +294,16 @@ int16_t CC1101::startTransmit(const uint8_t* data, size_t len, uint8_t addr) { dataSent += bytesToWrite; } } - // Check MARCSTATE for Idle - while(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x01) {}; return(state); } int16_t CC1101::finishTransmit() { // set mode to standby to disable transmitter/RF switch + + // Check MARCSTATE for Idle + while(SPIgetRegValue(RADIOLIB_CC1101_REG_MARCSTATE, 4, 0) != 0x01) {}; + //TODO: Timeout + int16_t state = standby(); RADIOLIB_ASSERT(state);