From ad88f1425cccc5052a88a261fac03725841c07d6 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 18 Oct 2025 20:19:38 +0100 Subject: [PATCH] [LR2021] Add LoRa commands --- src/modules/LR2021/LR2021.h | 21 ++++ src/modules/LR2021/LR2021_cmds_lora.cpp | 129 ++++++++++++++++++++++++ src/modules/LR2021/LR2021_commands.h | 45 +++++++++ src/modules/LR2021/LR2021_registers.h | 14 +++ 4 files changed, 209 insertions(+) create mode 100644 src/modules/LR2021/LR2021_cmds_lora.cpp create mode 100644 src/modules/LR2021/LR2021_registers.h diff --git a/src/modules/LR2021/LR2021.h b/src/modules/LR2021/LR2021.h index 5dfc2874..b2d23e5d 100644 --- a/src/modules/LR2021/LR2021.h +++ b/src/modules/LR2021/LR2021.h @@ -53,6 +53,11 @@ class LR2021: public PhysicalLayer, public LRxxxx { #endif Module* mod; + // cached LoRa parameters + bool ldroAuto = true; + uint8_t bandwidth = 0, spreadingFactor = 0, ldrOptimize = 0; + float bandwidthKhz = 0; + // chip control commands int16_t readRadioRxFifo(uint8_t* data, size_t len); int16_t writeRadioTxFifo(uint8_t* data, size_t len); @@ -115,6 +120,22 @@ class LR2021: public PhysicalLayer, public LRxxxx { // modem configuration commands int16_t setPacketType(uint8_t packetType); int16_t getPacketType(uint8_t* packetType); + + // LoRa commands + int16_t setLoRaModulationParams(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t ldro); + int16_t setLoRaPacketParams(uint16_t preambleLen, uint8_t hdrType, uint8_t payloadLen, uint8_t crcType, uint8_t invertIQ); + int16_t setLoRaSynchTimeout(uint8_t numSymbols, bool format); + int16_t setLoRaSyncword(uint16_t syncword); + int16_t setLoRaSideDetConfig(uint8_t* configs, size_t numSideDets); + int16_t setLoRaSideDetSyncword(uint8_t* syncwords, size_t numSideDets); + int16_t setLoRaCadParams(uint8_t numSymbols, bool preambleOnly, uint8_t pnrDelta, uint8_t cadExitMode, uint32_t timeout, uint8_t detPeak); + int16_t setLoRaCad(void); + int16_t getLoRaRxStats(uint16_t* pktRxTotal, uint16_t* pktCrcError, uint16_t* headerCrcError, uint16_t* falseSynch); + int16_t getLoRaPacketStatus(uint8_t* crc, uint8_t* cr, uint8_t* packetLen, float* snrPacket, float* rssiPacket, float* rssiSignalPacket); + int16_t setLoRaAddress(uint8_t addrLen, uint8_t addrPos, uint8_t* addr); + int16_t setLoRaHopping(uint8_t hopCtrl, uint16_t hopPeriod, uint32_t* freqHops, size_t numFreqHops); + int16_t setLoRaTxSync(uint8_t function, uint8_t dioNum); + int16_t setLoRaSideDetCad(uint8_t* pnrDelta, uint8_t* detPeak, size_t numSideDets); }; #endif diff --git a/src/modules/LR2021/LR2021_cmds_lora.cpp b/src/modules/LR2021/LR2021_cmds_lora.cpp new file mode 100644 index 00000000..cf92e9d5 --- /dev/null +++ b/src/modules/LR2021/LR2021_cmds_lora.cpp @@ -0,0 +1,129 @@ +#include "LR2021.h" + +#include "../LR11x0/LR_common.h" + +#include +#include + +#if !RADIOLIB_EXCLUDE_LR2021 + +int16_t LR2021::setLoRaModulationParams(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t ldro) { + // calculate symbol length and enable low data rate optimization, if auto-configuration is enabled + if(this->ldroAuto) { + float symbolLength = (float)(uint32_t(1) << this->spreadingFactor) / (float)this->bandwidthKhz; + if(symbolLength >= 16.0f) { + this->ldrOptimize = RADIOLIB_LR2021_LORA_LDRO_ENABLED; + } else { + this->ldrOptimize = RADIOLIB_LR2021_LORA_LDRO_DISABLED; + } + } else { + this->ldrOptimize = ldro; + } + + uint8_t buff[] = { (uint8_t)(((sf & 0x0F) << 4) | (bw & 0x0F)), (uint8_t)(((cr & 0x0F) << 4) | this->ldrOptimize) }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_MODULATION_PARAMS, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaPacketParams(uint16_t preambleLen, uint8_t hdrType, uint8_t payloadLen, uint8_t crcType, uint8_t invertIQ) { + uint8_t buff[] = { + (uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF), payloadLen, + (uint8_t)(((hdrType & 0x01) << 2) | ((crcType & 0x01) << 1) | (invertIQ & 0x01)), + }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_PACKET_PARAMS, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaSynchTimeout(uint8_t numSymbols, bool format) { + uint8_t buff[] = { numSymbols, (uint8_t)format }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_SYNCH_TIMEOUT, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaSyncword(uint16_t syncword) { + uint8_t buff[] = { (uint8_t)((syncword >> 8) & 0xFF), (uint8_t)(syncword & 0xFF) }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_SYNCWORD, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaSideDetConfig(uint8_t* configs, size_t numSideDets) { + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_SIDE_DET_CONFIG, true, configs, numSideDets)); +} + +int16_t LR2021::setLoRaSideDetSyncword(uint8_t* syncwords, size_t numSideDets) { + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_SIDE_DET_SYNCWORD, true, syncwords, numSideDets)); +} + +int16_t LR2021::setLoRaCadParams(uint8_t numSymbols, bool preambleOnly, uint8_t pnrDelta, uint8_t cadExitMode, uint32_t timeout, uint8_t detPeak) { + uint8_t buff[] = { + numSymbols, (uint8_t)(((uint8_t)preambleOnly << 4) | (pnrDelta & 0x0F)), cadExitMode, + (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF), + (uint8_t)(detPeak & 0x7F) + }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_CAD_PARAMS, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaCad(void) { + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_CAD, true, NULL, 0)); +} + +int16_t LR2021::getLoRaRxStats(uint16_t* pktRxTotal, uint16_t* pktCrcError, uint16_t* headerCrcError, uint16_t* falseSynch) { + uint8_t buff[8] = { 0 }; + int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_LORA_RX_STATS, false, buff, sizeof(buff)); + if(pktRxTotal) { *pktRxTotal = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; } + if(pktCrcError) { *pktCrcError = ((uint16_t)(buff[2]) << 8) | (uint16_t)buff[3]; } + if(headerCrcError) { *headerCrcError = ((uint16_t)(buff[4]) << 8) | (uint16_t)buff[5]; } + if(falseSynch) { *falseSynch = ((uint16_t)(buff[7]) << 8) | (uint16_t)buff[6]; } + return(state); +} + +int16_t LR2021::getLoRaPacketStatus(uint8_t* crc, uint8_t* cr, uint8_t* packetLen, float* snrPacket, float* rssiPacket, float* rssiSignalPacket) { + uint8_t buff[9] = { 0 }; + int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_LORA_PACKET_STATUS, false, buff, sizeof(buff)); + uint16_t raw = 0; + if(crc) { *crc = (buff[0] & 0x10) >> 4; } + if(cr) { *cr = buff[0] & 0x0F; } + if(packetLen) { *packetLen = buff[1]; } + if(snrPacket) { *snrPacket = (float)((int8_t)buff[2]) / 4.0f; } + if(rssiPacket) { + raw = (uint16_t)buff[3] << 1; + *rssiPacket = (float)raw / -2.0f; + } + if(rssiSignalPacket) { + raw = (uint16_t)buff[4] << 1; + *rssiSignalPacket = (float)raw / -2.0f; + } + return(state); +} + +int16_t LR2021::setLoRaAddress(uint8_t addrLen, uint8_t addrPos, uint8_t* addr) { + if(addrLen > 8) { return(RADIOLIB_ERR_UNKNOWN); } + uint8_t buff[9] = { (uint8_t)(((addrLen & 0x0F) << 4) | (addrPos & 0x0F)) }; + memcpy(&buff[1], addr, addrLen); + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_ADDRESS, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaHopping(uint8_t hopCtrl, uint16_t hopPeriod, uint32_t* freqHops, size_t numFreqHops) { + if(numFreqHops > 40) { return(RADIOLIB_ERR_UNKNOWN); } + uint8_t buff[2 + 160] = { (uint8_t)(hopCtrl | ((hopPeriod & 0xF00) >> 8)), (uint8_t)(hopPeriod & 0xFF) }; + for(uint8_t i = 0; i < numFreqHops; i++) { + buff[i + 2] = (freqHops[i] >> 24) & 0xFF; + buff[i + 3] = (freqHops[i] >> 16) & 0xFF; + buff[i + 4] = (freqHops[i] >> 8) & 0xFF; + buff[i + 5] = freqHops[i] & 0xFF; + } + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_HOPPING, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaTxSync(uint8_t function, uint8_t dioNum) { + uint8_t buff[] = { (uint8_t)(function | (dioNum & 0x3F)) }; + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_TX_SYNC, true, buff, sizeof(buff))); +} + +int16_t LR2021::setLoRaSideDetCad(uint8_t* pnrDelta, uint8_t* detPeak, size_t numSideDets) { + uint8_t buff[6] = { 0 }; + for(uint8_t i = 0; i < numSideDets; i++) { + if(i >= 3) { return(RADIOLIB_ERR_UNKNOWN); } + buff[2*i] = pnrDelta[i] & 0x0F; + buff[2*i + 1] = detPeak[i] & 0x7F; + } + return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_LORA_TX_SYNC, true, buff, 2*numSideDets)); +} + +#endif diff --git a/src/modules/LR2021/LR2021_commands.h b/src/modules/LR2021/LR2021_commands.h index 707bae56..a5cc323d 100644 --- a/src/modules/LR2021/LR2021_commands.h +++ b/src/modules/LR2021/LR2021_commands.h @@ -75,6 +75,7 @@ #define RADIOLIB_LR2021_CMD_SET_LORA_SIDE_DET_CONFIG (0x0224) #define RADIOLIB_LR2021_CMD_SET_LORA_SIDE_DET_SYNCWORD (0x0225) #define RADIOLIB_LR2021_CMD_SET_LORA_CAD_PARAMS (0x0227) +#define RADIOLIB_LR2021_CMD_SET_LORA_CAD (0x0228) #define RADIOLIB_LR2021_CMD_GET_LORA_RX_STATS (0x0229) #define RADIOLIB_LR2021_CMD_GET_LORA_PACKET_STATUS (0x022A) #define RADIOLIB_LR2021_CMD_SET_LORA_ADDRESS (0x022B) @@ -355,6 +356,50 @@ #define RADIOLIB_LR2021_PACKET_TYPE_Z_WAVE (0x0CUL << 0) // 7 0 Z-WAVE #define RADIOLIB_LR2021_PACKET_TYPE_OQPSK (0x0DUL << 0) // 7 0 OQPSK +// RADIOLIB_LR2021_CMD_SET_LORA_MODULATION_PARAMS +#define RADIOLIB_LR2021_LORA_BW_31 (0x02UL << 0) // 3 0 LoRa bandwidth: 31.25 kHz +#define RADIOLIB_LR2021_LORA_BW_41 (0x0AUL << 0) // 3 0 41.67 kHz +#define RADIOLIB_LR2021_LORA_BW_83 (0x0BUL << 0) // 3 0 83.34 kHz +#define RADIOLIB_LR2021_LORA_BW_62 (0x03UL << 0) // 3 0 62.50 kHz +#define RADIOLIB_LR2021_LORA_BW_125 (0x04UL << 0) // 3 0 125 kHz +#define RADIOLIB_LR2021_LORA_BW_250 (0x05UL << 0) // 3 0 250 kHz +#define RADIOLIB_LR2021_LORA_BW_500 (0x06UL << 0) // 3 0 500 kHz +#define RADIOLIB_LR2021_LORA_BW_1000 (0x07UL << 0) // 3 0 1000 kHz +#define RADIOLIB_LR2021_LORA_BW_812 (0x0FUL << 0) // 3 0 812 kHz +#define RADIOLIB_LR2021_LORA_BW_406 (0x0EUL << 0) // 3 0 406 kHz +#define RADIOLIB_LR2021_LORA_BW_203 (0x0DUL << 0) // 3 0 203 kHz +#define RADIOLIB_LR2021_LORA_BW_101 (0x0CUL << 0) // 3 0 101 kHz +#define RADIOLIB_LR2021_LORA_CR_4_5 (0x01UL << 0) // 3 0 LoRa coding rate: 4/5 +#define RADIOLIB_LR2021_LORA_CR_4_6 (0x02UL << 0) // 3 0 4/6 +#define RADIOLIB_LR2021_LORA_CR_4_7 (0x03UL << 0) // 3 0 4/7 +#define RADIOLIB_LR2021_LORA_CR_4_8 (0x04UL << 0) // 3 0 4/8 +#define RADIOLIB_LR2021_LORA_CR_4_5_LI (0x05UL << 0) // 3 0 4/5 long interleaver +#define RADIOLIB_LR2021_LORA_CR_4_6_LI (0x06UL << 0) // 3 0 4/6 long interleaver +#define RADIOLIB_LR2021_LORA_CR_4_7_LI (0x07UL << 0) // 3 0 4/7 long interleaver +#define RADIOLIB_LR2021_LORA_LDRO_DISABLED (0x00UL << 0) // 1 0 LDRO/PPM configuration: disabled +#define RADIOLIB_LR2021_LORA_LDRO_ENABLED (0x01UL << 0) // 1 0 enabled + +// RADIOLIB_LR2021_CMD_SET_LORA_PACKET_PARAMS +#define RADIOLIB_LR2021_LORA_HEADER_EXPLICIT (0x00UL << 2) // 2 2 LoRa header mode: explicit +#define RADIOLIB_LR2021_LORA_HEADER_IMPLICIT (0x01UL << 2) // 2 2 implicit +#define RADIOLIB_LR2021_LORA_CRC_DISABLED (0x00UL << 1) // 1 1 LoRa CRC: disabled +#define RADIOLIB_LR2021_LORA_CRC_ENABLED (0x01UL << 1) // 1 1 enabled +#define RADIOLIB_LR2021_LORA_IQ_STANDARD (0x00UL << 0) // 0 0 LoRa IQ: standard +#define RADIOLIB_LR2021_LORA_IQ_INVERTED (0x01UL << 0) // 0 0 inverted + +// RADIOLIB_LR2021_CMD_SET_LORA_SYNCH_TIMEOUT +#define RADIOLIB_LR2021_LORA_SYNCH_TIMEOUT_FORMAT_SYMBOLS (0x00UL << 0) // 7 0 LoRa synch timeout format: number of symbols +#define RADIOLIB_LR2021_LORA_SYNCH_TIMEOUT_FORMAT_MANT_EXP (0x01UL << 0) // 7 0 mantissa-exponent + +// RADIOLIB_LR2021_CMD_SET_LORA_HOPPING +#define RADIOLIB_LR2021_LORA_HOPPING_DISABLED (0x00UL << 6) // 7 6 LoRa intra-packet hopping: disabled +#define RADIOLIB_LR2021_LORA_HOPPING_ENABLED (0x01UL << 6) // 7 6 enabled + +// RADIOLIB_LR2021_CMD_SET_LORA_TX_SYNC +#define RADIOLIB_LR2021_LORA_TX_SYNC_DISABLED (0x00UL << 6) // 7 6 Tx sync: disabled +#define RADIOLIB_LR2021_LORA_TX_SYNC_MASTER (0x01UL << 6) // 7 6 master (wait for signal to transmit sync frame) +#define RADIOLIB_LR2021_LORA_TX_SYNC_SLAVE (0x02UL << 6) // 7 6 slave (output signal on sync frame) + #endif #endif diff --git a/src/modules/LR2021/LR2021_registers.h b/src/modules/LR2021/LR2021_registers.h new file mode 100644 index 00000000..8db1b1f3 --- /dev/null +++ b/src/modules/LR2021/LR2021_registers.h @@ -0,0 +1,14 @@ +#if !defined(RADIOLIB_LR2021_REGISTERS_H) +#define RADIOLIB_LR2021_REGISTERS_H + +#include "../../TypeDef.h" + +#if !RADIOLIB_EXCLUDE_LR2021 + +// LR2021 SPI registers +#define RADIOLIB_LR2021_REG_LORA_MODEM_TXRX_CFG0 (0xF30A14) +#define RADIOLIB_LR2021_REG_LORA_MODEM_MAIN_TX_CFG1 (0xF30A24) + +#endif + +#endif