From cc399297043c05ab982337cf7b461f5cab6a94af Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Tue, 12 Nov 2019 12:00:08 +0100 Subject: [PATCH 1/6] Added fixed-length example for CC1101 --- .../CC1101_Receive_Fixed.ino | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino diff --git a/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino b/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino new file mode 100644 index 00000000..fbf20062 --- /dev/null +++ b/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino @@ -0,0 +1,98 @@ +/* + RadioLib CC1101 Receive Example (fixed length mode) + + This example receives packets using CC1101 FSK radio module. + To successfully receive data, the following settings have to be the same + on both transmitter and receiver: + - carrier frequency + - bit rate + - frequency deviation + - sync word + + For full API reference, see the GitHub Pages + https://jgromes.github.io/RadioLib/ +*/ + +// receive 9 bytes of payload after the sync word +#define FIXED_PKT_LEN 9 + +// include the library +#include + +// CC1101 has the following connections: +// NSS pin: 10 +// GDO0 pin: 2 +// GDO2 pin: 3 +CC1101 cc = new Module(10, 2, 3); + +// or using RadioShield +// https://github.com/jgromes/RadioShield +//CC1101 cc = RadioShield.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize CC1101 with default settings + Serial.print(F("[CC1101] Initializing ... ")); + // carrier frequency: 868.0 MHz + // bit rate: 4.8 kbps + // Rx bandwidth: 325.0 kHz + // frequency deviation: 48.0 kHz + // sync word: 0xD391 + int state = cc.begin(); + + // Fixed packet length mode (non-default setting) + state = cc.fixedPacketLengthMode(FIXED_PKT_LEN); + + if (state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } +} + +void loop() { + Serial.print(F("[CC1101] Waiting for incoming transmission ... ")); + + // you can receive data as an Arduino String + String str; + int state = cc.receive(str); + + // you can also receive data as byte array + /* + byte byteArr[8]; + int state = cc.receive(byteArr, 8); + */ + + if (state == ERR_NONE) { + // packet was successfully received + Serial.println(F("success!")); + + // print the data of the packet + Serial.print(F("[CC1101] Data:\t\t")); + Serial.println(str); + + // print RSSI (Received Signal Strength Indicator) + // of the last received packet + Serial.print(F("[CC1101] RSSI:\t\t")); + Serial.print(cc.getRSSI()); + Serial.println(F(" dBm")); + + // print LQI (Link Quality Indicator) + // of the last received packet, lower is better + Serial.print(F("[CC1101] LQI:\t\t")); + Serial.println(cc.getLQI()); + + } else if (state == ERR_CRC_MISMATCH) { + // packet was received, but is malformed + Serial.println(F("CRC error!")); + + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); + + } +} From 35361ce198a3f4b0cb40a9dacf6e0cfe3f7a5b26 Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Tue, 12 Nov 2019 17:56:36 +0100 Subject: [PATCH 2/6] Implemented promiscuous mode for CC1101 and RF69 --- src/modules/CC1101.cpp | 106 +++++++++++++++++++++++++++++++++++++++-- src/modules/CC1101.h | 65 ++++++++++++++++++++++++- src/modules/RF69.cpp | 85 ++++++++++++++++++++++++++++++--- src/modules/RF69.h | 49 ++++++++++++++++++- 4 files changed, 290 insertions(+), 15 deletions(-) diff --git a/src/modules/CC1101.cpp b/src/modules/CC1101.cpp index 657400a0..089323dd 100644 --- a/src/modules/CC1101.cpp +++ b/src/modules/CC1101.cpp @@ -4,6 +4,8 @@ CC1101::CC1101(Module* module) : PhysicalLayer(CC1101_CRYSTAL_FREQ, CC1101_DIV_E _mod = module; _packetLengthQueried = false; _packetLengthConfig = CC1101_LENGTH_CONFIG_VARIABLE; + + _syncWordLength = CC1101_DEFAULT_SYNC_WORD_LENGTH; } int16_t CC1101::begin(float freq, float br, float rxBw, float freqDev, int8_t power) { @@ -441,11 +443,35 @@ int16_t CC1101::setOutputPower(int8_t power) { return(SPIsetRegValue(CC1101_REG_PATABLE, powerRaw)); } -int16_t CC1101::setSyncWord(uint8_t syncH, uint8_t syncL) { - // set sync word - int16_t state = SPIsetRegValue(CC1101_REG_SYNC1, syncH); - state |= SPIsetRegValue(CC1101_REG_SYNC0, syncL); - return(state); +int16_t CC1101::setSyncWord(uint8_t* syncWord, uint8_t len, uint8_t maxErrBits) { + if(maxErrBits > 1) { + return(ERR_INVALID_SYNC_WORD); + } + + // sync word must not contain value 0x00 + for(uint8_t i = 0; i < len; i++) { + if(syncWord[i] == 0x00) { + return(ERR_INVALID_SYNC_WORD); + } + } + + _syncWordLength = len; + + // enable sync word filtering + int16_t state = enableSyncWordFiltering(maxErrBits); + if (state != ERR_NONE) { + return(state); + } + + // set sync word register + _mod->SPIwriteRegisterBurst(CC1101_REG_SYNC1, syncWord, len); + + return(ERR_NONE); +} + +int16_t CC1101::setSyncWord(uint8_t syncH, uint8_t syncL, uint8_t maxErrBits) { + uint8_t syncWord[] = { syncH, syncL }; + return(setSyncWord(syncWord, sizeof(syncWord), maxErrBits)); } int16_t CC1101::setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs) { @@ -547,6 +573,76 @@ int16_t CC1101::variablePacketLengthMode(uint8_t maxLen) { return(state); } +int16_t CC1101::enableSyncWordFiltering(uint8_t maxErrBits) { + if (maxErrBits > 1) { + return(ERR_INVALID_SYNC_WORD); + } + + if (maxErrBits == 0) { + if (_syncWordLength == 1) { + // in 16 bit sync word, expect all 16 bits + return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_16_16, 2, 0)); + } else if (_syncWordLength == 2) { + // there's no 32 of 32 case, so we resort to 30 of 32 bits required + return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_30_32, 2, 0)); + } + } + + if (maxErrBits == 1) { + if (_syncWordLength == 1) { + // in 16 bit sync word, expect at least 15 bits + return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_15_16, 2, 0)); + } else if (_syncWordLength == 2) { + // in 32 bits sync word (16 + 16), expect 30 of 32 to match + return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_30_32, 2, 0)); + } + } + + return(ERR_NONE); +} + +int16_t CC1101::disableSyncWordFiltering() { + return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_NONE, 2, 0)); +} + +int16_t CC1101::enableCrcFiltering() { + return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_ON, 2, 2)); +} + +int16_t CC1101::disableCrcFiltering() { + return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_OFF, 2, 2)); +} + +int16_t CC1101::promiscuousMode(bool promiscuous) { + int16_t state = ERR_NONE; + + if (_promiscuous == promiscuous) { + return(state); + } + + if (promiscuous == true) { + // disable preamble and sync word filtering and insertion + state = disableSyncWordFiltering(); + if (state != ERR_NONE) { + return(state); + } + + // disable CRC filtering + state = disableCrcFiltering(); + } else { + // enable preamble and sync word filtering and insertion + state = enableSyncWordFiltering(); + if (state != ERR_NONE) { + return(state); + } + + // enable CRC filtering + state = enableCrcFiltering(); + } + + return(state); +} + int16_t CC1101::config() { // enable automatic frequency synthesizer calibration int16_t state = SPIsetRegValue(CC1101_REG_MCSM0, CC1101_FS_AUTOCAL_IDLE_TO_RXTX, 5, 4); diff --git a/src/modules/CC1101.h b/src/modules/CC1101.h index c57a7e6d..2fbc0d1c 100644 --- a/src/modules/CC1101.h +++ b/src/modules/CC1101.h @@ -10,6 +10,9 @@ #define CC1101_CRYSTAL_FREQ 26.0 #define CC1101_DIV_EXPONENT 16 #define CC1101_MAX_PACKET_LENGTH 63 +#define CC1101_MAX_SYNC_WORD_LENGTH 2 +#define CC1101_DEFAULT_SYNC_WORD_LENGTH 2 +#define CC1101_DEFAULT_SYNC_WORD { 0xD3, 0x91 } // CC1101 SPI commands #define CC1101_CMD_READ 0b10000000 @@ -237,7 +240,7 @@ #define CC1101_MOD_FORMAT_MFSK 0b01110000 // 6 4 MFSK - only for data rates above 26 kBaud #define CC1101_MANCHESTER_EN_OFF 0b00000000 // 3 3 Manchester encoding: disabled (default) #define CC1101_MANCHESTER_EN_ON 0b00001000 // 3 3 enabled -#define CC1101_SYNC_MODE_NONE 0b00000000 // 2 0 synchronization: no preamble sync +#define CC1101_SYNC_MODE_NONE 0b00000000 // 2 0 synchronization: no preamble/sync #define CC1101_SYNC_MODE_15_16 0b00000001 // 2 0 15/16 sync word bits #define CC1101_SYNC_MODE_16_16 0b00000010 // 2 0 16/16 sync word bits (default) #define CC1101_SYNC_MODE_30_32 0b00000011 // 2 0 30/32 sync word bits @@ -694,9 +697,24 @@ class CC1101: public PhysicalLayer { \param syncL LSB of the sync word. + \param maxErrBits Maximum allowed number of bit errors in received sync word. Defaults to 0. + \returns \ref status_codes */ - int16_t setSyncWord(uint8_t syncH, uint8_t syncL); + int16_t setSyncWord(uint8_t syncH, uint8_t syncL, uint8_t maxErrBits = 0); + + /*! + \brief Sets 1 or 2 bytes of sync word. + + \param syncWord Pointer to the array of sync word bytes. + + \param len Sync word length in bytes. + + \param maxErrBits Maximum allowed number of bit errors in received sync word. Defaults to 0. + + \returns \ref status_codes + */ + int16_t setSyncWord(uint8_t* syncWord, uint8_t len, uint8_t maxErrBits = 0); /*! \brief Sets node and broadcast addresses. Calling this method will also enable address filtering. @@ -757,6 +775,45 @@ class CC1101: public PhysicalLayer { */ int16_t variablePacketLengthMode(uint8_t maxLen = CC1101_MAX_PACKET_LENGTH); + /*! + \brief Enable sync word filtering and generation. + + \param len Size of sync word (1 or 2 bytes). + + \returns \ref status_codes + */ + int16_t enableSyncWordFiltering(uint8_t len = CC1101_DEFAULT_SYNC_WORD_LENGTH); + + /*! + \brief Disable preamble and sync word filtering and generation. + + \returns \ref status_codes + */ + int16_t disableSyncWordFiltering(); + + /*! + \brief Enable CRC filtering and generation. + + \returns \ref status_codes + */ + int16_t enableCrcFiltering(); + + /*! + \brief Disable CRC filtering and generation. + + \returns \ref status_codes + */ + int16_t disableCrcFiltering(); + + /*! + \brief Set modem in "sniff" mode: no packet filtering (e.g., no preamble, sync word, address, CRC). + + \param promiscuous Set or unset promiscuous mode. + + \returns \ref status_codes + */ + int16_t promiscuousMode(bool promiscuous = true); + private: Module* _mod; @@ -768,6 +825,10 @@ class CC1101: public PhysicalLayer { bool _packetLengthQueried; uint8_t _packetLengthConfig; + bool _promiscuous; + + uint8_t _syncWordLength; + int16_t config(); int16_t directMode(); void getExpMant(float target, uint16_t mantOffset, uint8_t divExp, uint8_t expMax, uint8_t& exp, uint8_t& mant); diff --git a/src/modules/RF69.cpp b/src/modules/RF69.cpp index a0febe19..35fa9b9a 100644 --- a/src/modules/RF69.cpp +++ b/src/modules/RF69.cpp @@ -3,8 +3,13 @@ RF69::RF69(Module* module) : PhysicalLayer(RF69_CRYSTAL_FREQ, RF69_DIV_EXPONENT, RF69_MAX_PACKET_LENGTH) { _mod = module; _tempOffset = 0; + _packetLengthQueried = false; _packetLengthConfig = RF69_PACKET_FORMAT_VARIABLE; + + _promiscuous = false; + + _syncWordLength = RF69_DEFAULT_SYNC_WORD_LENGTH; } int16_t RF69::begin(float freq, float br, float rxBw, float freqDev, int8_t power) { @@ -87,8 +92,8 @@ int16_t RF69::begin(float freq, float br, float rxBw, float freqDev, int8_t powe } // default sync word values 0x2D01 is the same as the default in LowPowerLab RFM69 library - uint8_t syncWord[] = {0x2D, 0x01}; - state = setSyncWord(syncWord, 2); + uint8_t syncWord[] = RF69_DEFAULT_SYNC_WORD; + state = setSyncWord(syncWord, sizeof(syncWord)); if(state != ERR_NONE) { return(state); } @@ -495,7 +500,7 @@ int16_t RF69::setOutputPower(int8_t power) { int16_t RF69::setSyncWord(uint8_t* syncWord, size_t len, uint8_t maxErrBits) { // check constraints - if((maxErrBits > 7) || (len > 8)) { + if((maxErrBits > 7) || (len > RF69_MAX_SYNC_WORD_LENGTH)) { return(ERR_INVALID_SYNC_WORD); } @@ -506,13 +511,14 @@ int16_t RF69::setSyncWord(uint8_t* syncWord, size_t len, uint8_t maxErrBits) { } } - // enable sync word recognition - int16_t state = _mod->SPIsetRegValue(RF69_REG_SYNC_CONFIG, RF69_SYNC_ON | RF69_FIFO_FILL_CONDITION_SYNC | (len - 1) << 3 | maxErrBits, 7, 0); - if(state != ERR_NONE) { + _syncWordLength = len; + + int16_t state = enableSyncWordFiltering(maxErrBits); + if (state != ERR_NONE) { return(state); } - // set sync word + // set sync word register _mod->SPIwriteRegisterBurst(RF69_REG_SYNC_VALUE_1, syncWord, len); return(ERR_NONE); } @@ -636,6 +642,71 @@ int16_t RF69::variablePacketLengthMode(uint8_t maxLen) { return(state); } +int16_t RF69::enableSyncWordFiltering(uint8_t maxErrBits) { + // enable sync word recognition + int16_t state = _mod->SPIsetRegValue(RF69_REG_SYNC_CONFIG, RF69_SYNC_ON | RF69_FIFO_FILL_CONDITION_SYNC | (_syncWordLength - 1) << 3 | maxErrBits, 7, 0); + if(state != ERR_NONE) { + return(state); + } + + return(state); +} + +int16_t RF69::disableSyncWordFiltering() { + // disable preamble detection and generation + int16_t state = _mod->SPIsetRegValue(RF69_REG_PREAMBLE_LSB, 0, 7, 0); + state |= _mod->SPIsetRegValue(RF69_REG_PREAMBLE_MSB, 0, 7, 0); + if (state != ERR_NONE) { + return(state); + } + + // disable sync word detection and generation + state = _mod->SPIsetRegValue(RF69_REG_SYNC_CONFIG, RF69_SYNC_OFF | RF69_FIFO_FILL_CONDITION, 7, 6); + if (state != ERR_NONE) { + return(state); + } + + return(state); +} + +int16_t RF69::enableCrcFiltering() { + return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_ON), 4, 4); +} + +int16_t RF69::disableCrcFiltering() { + return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_OFF), 4, 4); +} + +int16_t RF69::promiscuousMode(bool promiscuous) { + int16_t state = ERR_NONE; + + if (_promiscuous == promiscuous) { + return(state); + } + + if (promiscuous == true) { + // disable preamble and sync word filtering and insertion + state = disableSyncWordFiltering(); + if (state != ERR_NONE) { + return(state); + } + + // disable CRC filtering + state = disableCrcFiltering(); + } else { + // enable preamble and sync word filtering and insertion + state = enableSyncWordFiltering(); + if (state != ERR_NONE) { + return(state); + } + + // enable CRC filtering + state = enableCrcFiltering(); + } + + return(state); +} + int16_t RF69::config() { int16_t state = ERR_NONE; diff --git a/src/modules/RF69.h b/src/modules/RF69.h index 9820d539..26c71108 100644 --- a/src/modules/RF69.h +++ b/src/modules/RF69.h @@ -10,6 +10,10 @@ #define RF69_CRYSTAL_FREQ 32.0 #define RF69_DIV_EXPONENT 19 #define RF69_MAX_PACKET_LENGTH 64 +#define RF69_MAX_PREAMBLE_LENGTH 4 +#define RF69_MAX_SYNC_WORD_LENGTH 8 +#define RF69_DEFAULT_SYNC_WORD_LENGTH 2 +#define RF69_DEFAULT_SYNC_WORD { 0x2D, 0x01 } // RF69 register map #define RF69_REG_FIFO 0x00 @@ -646,7 +650,7 @@ class RF69: public PhysicalLayer { int16_t setOutputPower(int8_t power); /*! - \brief Sets sync word. Up to 8 bytes can be set as snyc word. + \brief Sets sync word. Up to 8 bytes can be set as sync word. \param syncWord Pointer to the array of sync word bytes. @@ -724,6 +728,45 @@ class RF69: public PhysicalLayer { */ int16_t variablePacketLengthMode(uint8_t maxLen = RF69_MAX_PACKET_LENGTH); + /*! + \brief Enable sync word filtering and generation. + + \param numBits Sync word length in bits. + + \returns \ref status_codes + */ + int16_t enableSyncWordFiltering(uint8_t numBits = 16); + + /*! + \brief Disable preamble and sync word filtering and generation. + + \returns \ref status_codes + */ + int16_t disableSyncWordFiltering(); + + /*! + \brief Enable CRC filtering and generation. + + \returns \ref status_codes + */ + int16_t enableCrcFiltering(); + + /*! + \brief Disable CRC filtering and generation. + + \returns \ref status_codes + */ + int16_t disableCrcFiltering(); + + /*! + \brief Set modem in "sniff" mode: no packet filtering (e.g., no preamble, sync word, address, CRC). + + \param promiscuous Set or unset promiscuous mode. + + \returns \ref status_codes + */ + int16_t promiscuousMode(bool promiscuous = true); + protected: Module* _mod; @@ -735,6 +778,10 @@ class RF69: public PhysicalLayer { bool _packetLengthQueried; uint8_t _packetLengthConfig; + bool _promiscuous; + + uint8_t _syncWordLength; + int16_t config(); int16_t directMode(); From b2f64a03ac1101ef0dcc93dbe165be3bd069bba0 Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Wed, 13 Nov 2019 11:51:10 +0100 Subject: [PATCH 3/6] Removed CC1101 example fixed length as too specific --- .../CC1101_Receive_Fixed.ino | 98 ------------------- 1 file changed, 98 deletions(-) delete mode 100644 examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino diff --git a/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino b/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino deleted file mode 100644 index fbf20062..00000000 --- a/examples/CC1101/CC1101_Receive_Fixed/CC1101_Receive_Fixed.ino +++ /dev/null @@ -1,98 +0,0 @@ -/* - RadioLib CC1101 Receive Example (fixed length mode) - - This example receives packets using CC1101 FSK radio module. - To successfully receive data, the following settings have to be the same - on both transmitter and receiver: - - carrier frequency - - bit rate - - frequency deviation - - sync word - - For full API reference, see the GitHub Pages - https://jgromes.github.io/RadioLib/ -*/ - -// receive 9 bytes of payload after the sync word -#define FIXED_PKT_LEN 9 - -// include the library -#include - -// CC1101 has the following connections: -// NSS pin: 10 -// GDO0 pin: 2 -// GDO2 pin: 3 -CC1101 cc = new Module(10, 2, 3); - -// or using RadioShield -// https://github.com/jgromes/RadioShield -//CC1101 cc = RadioShield.ModuleA; - -void setup() { - Serial.begin(9600); - - // initialize CC1101 with default settings - Serial.print(F("[CC1101] Initializing ... ")); - // carrier frequency: 868.0 MHz - // bit rate: 4.8 kbps - // Rx bandwidth: 325.0 kHz - // frequency deviation: 48.0 kHz - // sync word: 0xD391 - int state = cc.begin(); - - // Fixed packet length mode (non-default setting) - state = cc.fixedPacketLengthMode(FIXED_PKT_LEN); - - if (state == ERR_NONE) { - Serial.println(F("success!")); - } else { - Serial.print(F("failed, code ")); - Serial.println(state); - while (true); - } -} - -void loop() { - Serial.print(F("[CC1101] Waiting for incoming transmission ... ")); - - // you can receive data as an Arduino String - String str; - int state = cc.receive(str); - - // you can also receive data as byte array - /* - byte byteArr[8]; - int state = cc.receive(byteArr, 8); - */ - - if (state == ERR_NONE) { - // packet was successfully received - Serial.println(F("success!")); - - // print the data of the packet - Serial.print(F("[CC1101] Data:\t\t")); - Serial.println(str); - - // print RSSI (Received Signal Strength Indicator) - // of the last received packet - Serial.print(F("[CC1101] RSSI:\t\t")); - Serial.print(cc.getRSSI()); - Serial.println(F(" dBm")); - - // print LQI (Link Quality Indicator) - // of the last received packet, lower is better - Serial.print(F("[CC1101] LQI:\t\t")); - Serial.println(cc.getLQI()); - - } else if (state == ERR_CRC_MISMATCH) { - // packet was received, but is malformed - Serial.println(F("CRC error!")); - - } else { - // some other error occurred - Serial.print(F("failed, code ")); - Serial.println(state); - - } -} From 2e019c370036f89a99888668942cd211dae27cf3 Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Wed, 13 Nov 2019 16:27:08 +0100 Subject: [PATCH 4/6] Added methods to keywords --- keywords.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/keywords.txt b/keywords.txt index dcec71c7..1bb6c6f8 100644 --- a/keywords.txt +++ b/keywords.txt @@ -90,6 +90,10 @@ setDataShapingOOK KEYWORD2 setCRC KEYWORD2 variablePacketLengthMode KEYWORD2 fixedPacketLengthMode KEYWORD2 +setCrcFiltering KEYWORD2 +enableSyncWordFiltering KEYWORD2 +disableSyncWordFiltering KEYWORD2 +setPromiscuous KEYWORD2 # RF69-specific setAESKey KEYWORD2 From c56a76d296130bd4cf695e5206d01887ccc2ed67 Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Wed, 13 Nov 2019 16:27:39 +0100 Subject: [PATCH 5/6] enable/disable to set + bool --- src/modules/CC1101.cpp | 18 +++++++++--------- src/modules/CC1101.h | 15 +++++---------- src/modules/RF69.cpp | 18 +++++++++--------- src/modules/RF69.h | 13 ++++--------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/modules/CC1101.cpp b/src/modules/CC1101.cpp index 089323dd..2f86d0ee 100644 --- a/src/modules/CC1101.cpp +++ b/src/modules/CC1101.cpp @@ -605,15 +605,15 @@ int16_t CC1101::disableSyncWordFiltering() { return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_NONE, 2, 0)); } -int16_t CC1101::enableCrcFiltering() { - return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_ON, 2, 2)); +int16_t CC1101::setCrcFiltering(bool crcOn) { + if (crcOn == true) { + return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_ON, 2, 2)); + } else { + return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_OFF, 2, 2)); + } } -int16_t CC1101::disableCrcFiltering() { - return(SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_OFF, 2, 2)); -} - -int16_t CC1101::promiscuousMode(bool promiscuous) { +int16_t CC1101::setPromiscuousMode(bool promiscuous) { int16_t state = ERR_NONE; if (_promiscuous == promiscuous) { @@ -628,7 +628,7 @@ int16_t CC1101::promiscuousMode(bool promiscuous) { } // disable CRC filtering - state = disableCrcFiltering(); + state = setCrcFiltering(false); } else { // enable preamble and sync word filtering and insertion state = enableSyncWordFiltering(); @@ -637,7 +637,7 @@ int16_t CC1101::promiscuousMode(bool promiscuous) { } // enable CRC filtering - state = enableCrcFiltering(); + state = setCrcFiltering(true); } return(state); diff --git a/src/modules/CC1101.h b/src/modules/CC1101.h index 2fbc0d1c..92440449 100644 --- a/src/modules/CC1101.h +++ b/src/modules/CC1101.h @@ -778,11 +778,11 @@ class CC1101: public PhysicalLayer { /*! \brief Enable sync word filtering and generation. - \param len Size of sync word (1 or 2 bytes). + \param numBits Sync word length in bits. \returns \ref status_codes */ - int16_t enableSyncWordFiltering(uint8_t len = CC1101_DEFAULT_SYNC_WORD_LENGTH); + int16_t enableSyncWordFiltering(uint8_t maxErrBits = 0); /*! \brief Disable preamble and sync word filtering and generation. @@ -794,16 +794,11 @@ class CC1101: public PhysicalLayer { /*! \brief Enable CRC filtering and generation. - \returns \ref status_codes - */ - int16_t enableCrcFiltering(); - - /*! - \brief Disable CRC filtering and generation. + \param crcOn Set or unset promiscuous mode. \returns \ref status_codes */ - int16_t disableCrcFiltering(); + int16_t setCrcFiltering(bool crcOn = true); /*! \brief Set modem in "sniff" mode: no packet filtering (e.g., no preamble, sync word, address, CRC). @@ -812,7 +807,7 @@ class CC1101: public PhysicalLayer { \returns \ref status_codes */ - int16_t promiscuousMode(bool promiscuous = true); + int16_t setPromiscuousMode(bool promiscuous = true); private: Module* _mod; diff --git a/src/modules/RF69.cpp b/src/modules/RF69.cpp index 35fa9b9a..882ca15d 100644 --- a/src/modules/RF69.cpp +++ b/src/modules/RF69.cpp @@ -669,15 +669,15 @@ int16_t RF69::disableSyncWordFiltering() { return(state); } -int16_t RF69::enableCrcFiltering() { - return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_ON), 4, 4); +int16_t RF69::setCrcFiltering(bool crcOn) { + if (crcOn == true) { + return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_ON, 4, 4)); + } else { + return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_OFF, 4, 4)); + } } -int16_t RF69::disableCrcFiltering() { - return(_mod->SPIsetRegValue(RF69_REG_PACKET_CONFIG_1, RF69_CRC_OFF), 4, 4); -} - -int16_t RF69::promiscuousMode(bool promiscuous) { +int16_t RF69::setPromiscuousMode(bool promiscuous) { int16_t state = ERR_NONE; if (_promiscuous == promiscuous) { @@ -692,7 +692,7 @@ int16_t RF69::promiscuousMode(bool promiscuous) { } // disable CRC filtering - state = disableCrcFiltering(); + state = setCrcFiltering(false); } else { // enable preamble and sync word filtering and insertion state = enableSyncWordFiltering(); @@ -701,7 +701,7 @@ int16_t RF69::promiscuousMode(bool promiscuous) { } // enable CRC filtering - state = enableCrcFiltering(); + state = setCrcFiltering(true); } return(state); diff --git a/src/modules/RF69.h b/src/modules/RF69.h index 26c71108..5318b7e8 100644 --- a/src/modules/RF69.h +++ b/src/modules/RF69.h @@ -735,7 +735,7 @@ class RF69: public PhysicalLayer { \returns \ref status_codes */ - int16_t enableSyncWordFiltering(uint8_t numBits = 16); + int16_t enableSyncWordFiltering(uint8_t maxErrBits = 0); /*! \brief Disable preamble and sync word filtering and generation. @@ -747,16 +747,11 @@ class RF69: public PhysicalLayer { /*! \brief Enable CRC filtering and generation. - \returns \ref status_codes - */ - int16_t enableCrcFiltering(); - - /*! - \brief Disable CRC filtering and generation. + \param crcOn Set or unset promiscuous mode. \returns \ref status_codes */ - int16_t disableCrcFiltering(); + int16_t setCrcFiltering(bool crcOn = true); /*! \brief Set modem in "sniff" mode: no packet filtering (e.g., no preamble, sync word, address, CRC). @@ -765,7 +760,7 @@ class RF69: public PhysicalLayer { \returns \ref status_codes */ - int16_t promiscuousMode(bool promiscuous = true); + int16_t setPromiscuousMode(bool promiscuous = true); protected: Module* _mod; From 2a09dc302af0a23197162a035ca3343cf4de2ca6 Mon Sep 17 00:00:00 2001 From: Federico Maggi Date: Wed, 13 Nov 2019 16:34:47 +0100 Subject: [PATCH 6/6] sync word length check + removed `else if` --- src/modules/CC1101.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/CC1101.cpp b/src/modules/CC1101.cpp index 2f86d0ee..658a3ba2 100644 --- a/src/modules/CC1101.cpp +++ b/src/modules/CC1101.cpp @@ -444,7 +444,7 @@ int16_t CC1101::setOutputPower(int8_t power) { } int16_t CC1101::setSyncWord(uint8_t* syncWord, uint8_t len, uint8_t maxErrBits) { - if(maxErrBits > 1) { + if((maxErrBits > 1) || (len > CC1101_MAX_SYNC_WORD_LENGTH)) { return(ERR_INVALID_SYNC_WORD); } @@ -582,7 +582,7 @@ int16_t CC1101::enableSyncWordFiltering(uint8_t maxErrBits) { if (_syncWordLength == 1) { // in 16 bit sync word, expect all 16 bits return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_16_16, 2, 0)); - } else if (_syncWordLength == 2) { + } else { // there's no 32 of 32 case, so we resort to 30 of 32 bits required return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_30_32, 2, 0)); } @@ -592,13 +592,13 @@ int16_t CC1101::enableSyncWordFiltering(uint8_t maxErrBits) { if (_syncWordLength == 1) { // in 16 bit sync word, expect at least 15 bits return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_15_16, 2, 0)); - } else if (_syncWordLength == 2) { + } else { // in 32 bits sync word (16 + 16), expect 30 of 32 to match return(SPIsetRegValue(CC1101_REG_MDMCFG2, CC1101_SYNC_MODE_30_32, 2, 0)); } } - return(ERR_NONE); + return(ERR_UNKNOWN); } int16_t CC1101::disableSyncWordFiltering() {