From 8f3a5c7430c815bf5371512c1e2a68f16464fedc Mon Sep 17 00:00:00 2001 From: jgromes Date: Mon, 28 Oct 2024 18:52:07 +0000 Subject: [PATCH] [LR11x0] Use array as LR-FHSS sync word --- .../LR11x0_LR_FHSS_Modem.ino | 3 +- src/modules/LR11x0/LR11x0.cpp | 48 +++++++++++-------- src/modules/LR11x0/LR11x0.h | 6 +-- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/examples/LR11x0/LR11x0_LR_FHSS_Modem/LR11x0_LR_FHSS_Modem.ino b/examples/LR11x0/LR11x0_LR_FHSS_Modem/LR11x0_LR_FHSS_Modem.ino index a7de634d..f1c7c198 100644 --- a/examples/LR11x0/LR11x0_LR_FHSS_Modem/LR11x0_LR_FHSS_Modem.ino +++ b/examples/LR11x0/LR11x0_LR_FHSS_Modem/LR11x0_LR_FHSS_Modem.ino @@ -62,7 +62,8 @@ void setup() { 3, // header count 0x13A); // hopping sequence seed state = radio.setOutputPower(10.0); - state = radio.setSyncWord(0x12345678); + uint8_t syncWord[] = {0x01, 0x23, 0x45, 0x67}; + state = radio.setSyncWord(syncWord, 4); if (state != RADIOLIB_ERR_NONE) { Serial.print(F("Unable to set configuration, code ")); Serial.println(state); diff --git a/src/modules/LR11x0/LR11x0.cpp b/src/modules/LR11x0/LR11x0.cpp index f87a7f88..cedf5b5b 100644 --- a/src/modules/LR11x0/LR11x0.cpp +++ b/src/modules/LR11x0/LR11x0.cpp @@ -110,7 +110,8 @@ int16_t LR11x0::beginLRFHSS(uint8_t bw, uint8_t cr, bool narrowGrid, float tcxoV state = setLrFhssConfig(bw, cr); RADIOLIB_ASSERT(state); - state = setSyncWord(0x12AD101B); + uint8_t syncWord[] = { 0x12, 0xAD, 0x10, 0x1B }; + state = setSyncWord(syncWord, 4); RADIOLIB_ASSERT(state); state = setRegulatorLDO(); @@ -758,20 +759,16 @@ int16_t LR11x0::setCodingRate(uint8_t cr, bool longInterleave) { return(setModulationParamsLoRa(this->spreadingFactor, this->bandwidth, this->codingRate, this->ldrOptimize)); } -int16_t LR11x0::setSyncWord(uint32_t syncWord) { +int16_t LR11x0::setSyncWord(uint8_t syncWord) { // check active modem uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE; int16_t state = getPacketType(&type); RADIOLIB_ASSERT(state); - if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) { - return(setLoRaSyncWord(syncWord & 0xFF)); - - } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) { - return(lrFhssSetSyncWord(syncWord)); - + if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) { + return(RADIOLIB_ERR_WRONG_MODEM); } - return(RADIOLIB_ERR_WRONG_MODEM); + return(setLoRaSyncWord(syncWord)); } int16_t LR11x0::setBitRate(float br) { @@ -885,27 +882,36 @@ int16_t LR11x0::setSyncWord(uint8_t* syncWord, size_t len) { uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE; int16_t state = getPacketType(&type); RADIOLIB_ASSERT(state); - if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) { + if(type == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) { + // update sync word length + this->syncWordLength = len*8; + state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening); + RADIOLIB_ASSERT(state); + + // sync word is passed most-significant byte first + uint8_t fullSyncWord[RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN] = { 0 }; + memcpy(fullSyncWord, syncWord, len); + return(setGfskSyncWord(fullSyncWord)); + + } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) { // with length set to 1 and LoRa modem active, assume it is the LoRa sync word if(len > 1) { return(RADIOLIB_ERR_INVALID_SYNC_WORD); } return(setSyncWord(syncWord[0])); - } else if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) { - return(RADIOLIB_ERR_WRONG_MODEM); + } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) { + // with length set to 4 and LR-FHSS modem active, assume it is the LR-FHSS sync word + if(len != sizeof(uint32_t)) { + return(RADIOLIB_ERR_INVALID_SYNC_WORD); + } + uint32_t sync = 0; + memcpy(&sync, syncWord, sizeof(uint32_t)); + return(lrFhssSetSyncWord(sync)); } - // update sync word length - this->syncWordLength = len*8; - state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening); - RADIOLIB_ASSERT(state); - - // sync word is passed most-significant byte first - uint8_t fullSyncWord[RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN] = { 0 }; - memcpy(fullSyncWord, syncWord, len); - return(setGfskSyncWord(fullSyncWord)); + return(RADIOLIB_ERR_WRONG_MODEM); } int16_t LR11x0::setSyncBits(uint8_t *syncWord, uint8_t bitsLen) { diff --git a/src/modules/LR11x0/LR11x0.h b/src/modules/LR11x0/LR11x0.h index 974e8e3b..9ee985d5 100644 --- a/src/modules/LR11x0/LR11x0.h +++ b/src/modules/LR11x0/LR11x0.h @@ -1176,11 +1176,11 @@ class LR11x0: public PhysicalLayer { int16_t setCodingRate(uint8_t cr, bool longInterleave = false); /*! - \brief Sets LoRa or LR-FHSS sync word. - \param syncWord LoRa or LR-FHSS sync word to be set. For LoRa, only 8 least significant bits will be used + \brief Sets LoRa sync word. + \param syncWord LoRa sync word to be set. \returns \ref status_codes */ - int16_t setSyncWord(uint32_t syncWord); + int16_t setSyncWord(uint8_t syncWord); /*! \brief Sets GFSK bit rate. Allowed values range from 0.6 to 300.0 kbps.