From e60d2624f20fa6122c08741cf026b38502e8d34a Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 12 Oct 2025 21:12:27 +0200 Subject: [PATCH] [LR11x0] Move functions common with LR2021 --- src/modules/LR11x0/LR11x0.h | 5 ++- src/modules/LR11x0/LR11x0_commands.cpp | 43 ++------------------------ src/modules/LR11x0/LR11x0_crypto.cpp | 4 ++- src/modules/LR11x0/LR_common.cpp | 39 +++++++++++++++++++++++ src/modules/LR11x0/LR_common.h | 8 +++++ 5 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 src/modules/LR11x0/LR_common.cpp create mode 100644 src/modules/LR11x0/LR_common.h diff --git a/src/modules/LR11x0/LR11x0.h b/src/modules/LR11x0/LR11x0.h index b3b6599d..b8e34ac9 100644 --- a/src/modules/LR11x0/LR11x0.h +++ b/src/modules/LR11x0/LR11x0.h @@ -301,8 +301,8 @@ // RADIOLIB_LR11X0_CMD_SET_SLEEP #define RADIOLIB_LR11X0_SLEEP_RETENTION_DISABLED (0x00UL << 0) // 0 0 configuration retention in sleep mode: disabled #define RADIOLIB_LR11X0_SLEEP_RETENTION_ENABLED (0x01UL << 0) // 0 0 enabled -#define RADIOLIB_LR11X0_SLEEP_WAKEUP_DISABLED (0x00UL << 0) // 1 1 automated wakeup: disabled -#define RADIOLIB_LR11X0_SLEEP_WAKEUP_ENABLED (0x01UL << 0) // 1 1 enabled +#define RADIOLIB_LR11X0_SLEEP_WAKEUP_DISABLED (0x00UL << 1) // 1 1 automated wakeup: disabled +#define RADIOLIB_LR11X0_SLEEP_WAKEUP_ENABLED (0x01UL << 1) // 1 1 enabled // RADIOLIB_LR11X0_CMD_SET_STANDBY #define RADIOLIB_LR11X0_STANDBY_RC (0x00UL << 0) // 7 0 standby mode: RC oscillator @@ -1881,7 +1881,6 @@ class LR11x0: public PhysicalLayer { // common methods to avoid some copy-paste int16_t bleBeaconCommon(uint16_t cmd, uint8_t chan, const uint8_t* payload, size_t len); - int16_t writeCommon(uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile); int16_t cryptoCommon(uint16_t cmd, uint8_t keyId, const uint8_t* dataIn, size_t len, uint8_t* dataOut); }; diff --git a/src/modules/LR11x0/LR11x0_commands.cpp b/src/modules/LR11x0/LR11x0_commands.cpp index e430bbe0..95bf66e9 100644 --- a/src/modules/LR11x0/LR11x0_commands.cpp +++ b/src/modules/LR11x0/LR11x0_commands.cpp @@ -2,6 +2,7 @@ #include "../../utils/CRC.h" #include "../../utils/Cryptography.h" +#include "LR_common.h" #include #include @@ -55,7 +56,7 @@ int16_t LR11x0::writeRegMem32(uint32_t addr, const uint32_t* data, size_t len) { if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) { return(RADIOLIB_ERR_SPI_CMD_INVALID); } - return(this->writeCommon(RADIOLIB_LR11X0_CMD_WRITE_REG_MEM, addr, data, len, false)); + return(LR_writeCommon(this->mod, RADIOLIB_LR11X0_CMD_WRITE_REG_MEM, addr, data, len, false)); } int16_t LR11x0::readRegMem32(uint32_t addr, uint32_t* data, size_t len) { @@ -829,7 +830,7 @@ int16_t LR11x0::bootWriteFlashEncrypted(uint32_t offset, const uint32_t* data, s if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) { return(RADIOLIB_ERR_SPI_CMD_INVALID); } - return(this->writeCommon(RADIOLIB_LR11X0_CMD_BOOT_WRITE_FLASH_ENCRYPTED, offset, data, len, nonvolatile)); + return(LR_writeCommon(this->mod, RADIOLIB_LR11X0_CMD_BOOT_WRITE_FLASH_ENCRYPTED, offset, data, len, nonvolatile)); } int16_t LR11x0::bootReboot(bool stay) { @@ -852,42 +853,4 @@ int16_t LR11x0::bootGetJoinEui(uint8_t* eui) { return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_JOIN_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN)); } -int16_t LR11x0::writeCommon(uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile) { - // build buffers - later we need to ensure endians are correct, - // so there is probably no way to do this without copying buffers and iterating - size_t buffLen = sizeof(uint32_t) + len*sizeof(uint32_t); - #if RADIOLIB_STATIC_ONLY - uint8_t dataBuff[sizeof(uint32_t) + RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN]; - #else - uint8_t* dataBuff = new uint8_t[buffLen]; - #endif - - // set the address or offset - dataBuff[0] = (uint8_t)((addrOffset >> 24) & 0xFF); - dataBuff[1] = (uint8_t)((addrOffset >> 16) & 0xFF); - dataBuff[2] = (uint8_t)((addrOffset >> 8) & 0xFF); - dataBuff[3] = (uint8_t)(addrOffset & 0xFF); - - // convert endians - for(size_t i = 0; i < len; i++) { - uint32_t bin = 0; - if(nonvolatile) { - uint32_t* ptr = const_cast(data) + i; - bin = RADIOLIB_NONVOLATILE_READ_DWORD(ptr); - } else { - bin = data[i]; - } - dataBuff[4 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 24) & 0xFF); - dataBuff[5 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 16) & 0xFF); - dataBuff[6 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 8) & 0xFF); - dataBuff[7 + i*sizeof(uint32_t)] = (uint8_t)(bin & 0xFF); - } - - int16_t state = this->mod->SPIwriteStream(cmd, dataBuff, buffLen, true, false); - #if !RADIOLIB_STATIC_ONLY - delete[] dataBuff; - #endif - return(state); -} - #endif diff --git a/src/modules/LR11x0/LR11x0_crypto.cpp b/src/modules/LR11x0/LR11x0_crypto.cpp index 6d586622..9730db5b 100644 --- a/src/modules/LR11x0/LR11x0_crypto.cpp +++ b/src/modules/LR11x0/LR11x0_crypto.cpp @@ -1,6 +1,8 @@ #include "LR11x0.h" #include "../../utils/Cryptography.h" +#include "LR_common.h" + #include #if !RADIOLIB_EXCLUDE_LR11X0 @@ -176,7 +178,7 @@ int16_t LR11x0::cryptoCheckEncryptedFirmwareImage(uint32_t offset, const uint32_ if(len > (RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) { return(RADIOLIB_ERR_SPI_CMD_INVALID); } - return(this->writeCommon(RADIOLIB_LR11X0_CMD_CRYPTO_CHECK_ENCRYPTED_FIRMWARE_IMAGE, offset, data, len, nonvolatile)); + return(LR_writeCommon(this->mod, RADIOLIB_LR11X0_CMD_CRYPTO_CHECK_ENCRYPTED_FIRMWARE_IMAGE, offset, data, len, nonvolatile)); } int16_t LR11x0::cryptoCheckEncryptedFirmwareImageResult(bool* result) { diff --git a/src/modules/LR11x0/LR_common.cpp b/src/modules/LR11x0/LR_common.cpp new file mode 100644 index 00000000..ed211ff2 --- /dev/null +++ b/src/modules/LR11x0/LR_common.cpp @@ -0,0 +1,39 @@ +#include "LR_common.h" + +int16_t LR_writeCommon(Module* mod, uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile) { + // build buffers - later we need to ensure endians are correct, + // so there is probably no way to do this without copying buffers and iterating + size_t buffLen = sizeof(uint32_t) + len*sizeof(uint32_t); + #if RADIOLIB_STATIC_ONLY + uint8_t dataBuff[sizeof(uint32_t) + RADIOLIB_LR11X0_SPI_MAX_READ_WRITE_LEN]; + #else + uint8_t* dataBuff = new uint8_t[buffLen]; + #endif + + // set the address or offset + dataBuff[0] = (uint8_t)((addrOffset >> 24) & 0xFF); + dataBuff[1] = (uint8_t)((addrOffset >> 16) & 0xFF); + dataBuff[2] = (uint8_t)((addrOffset >> 8) & 0xFF); + dataBuff[3] = (uint8_t)(addrOffset & 0xFF); + + // convert endians + for(size_t i = 0; i < len; i++) { + uint32_t bin = 0; + if(nonvolatile) { + uint32_t* ptr = const_cast(data) + i; + bin = RADIOLIB_NONVOLATILE_READ_DWORD(ptr); + } else { + bin = data[i]; + } + dataBuff[4 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 24) & 0xFF); + dataBuff[5 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 16) & 0xFF); + dataBuff[6 + i*sizeof(uint32_t)] = (uint8_t)((bin >> 8) & 0xFF); + dataBuff[7 + i*sizeof(uint32_t)] = (uint8_t)(bin & 0xFF); + } + + int16_t state = mod->SPIwriteStream(cmd, dataBuff, buffLen, true, false); + #if !RADIOLIB_STATIC_ONLY + delete[] dataBuff; + #endif + return(state); +} diff --git a/src/modules/LR11x0/LR_common.h b/src/modules/LR11x0/LR_common.h new file mode 100644 index 00000000..74c75b9a --- /dev/null +++ b/src/modules/LR11x0/LR_common.h @@ -0,0 +1,8 @@ +#if !defined(RADIOLIB_LR_COMMON_H) +#define RADIOLIB_LR_COMMON_H + +#include "../../Module.h" + +int16_t LR_writeCommon(Module* mod, uint16_t cmd, uint32_t addrOffset, const uint32_t* data, size_t len, bool nonvolatile); + +#endif