From 29dd65d7adbfd4248e2065f25ef36e9b81c0c6ec Mon Sep 17 00:00:00 2001 From: jgromes Date: Wed, 28 Aug 2024 19:10:05 +0200 Subject: [PATCH] [SX127x] Make Rx mode implicit based on timeout --- src/modules/SX127x/SX127x.cpp | 40 +++++++++++++++-------------------- src/modules/SX127x/SX127x.h | 9 -------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index c0d51c2f..7e00c898 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -217,7 +217,7 @@ int16_t SX127x::receive(uint8_t* data, size_t len) { int16_t modem = getActiveModem(); if(modem == RADIOLIB_SX127X_LORA) { // set mode to receive - state = startReceive(len, RADIOLIB_SX127X_RXSINGLE); + state = startReceive(100, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, len); RADIOLIB_ASSERT(state); // if no DIO1 is provided, use software timeout (100 LoRa symbols, same as hardware timeout) @@ -253,7 +253,7 @@ int16_t SX127x::receive(uint8_t* data, size_t len) { RadioLibTime_t timeout = (getTimeOnAir(len) * 5) / 1000; // set mode to receive - state = startReceive(len, RADIOLIB_SX127X_RX); + state = startReceive(0, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, len); RADIOLIB_ASSERT(state); // wait for packet reception or timeout @@ -386,10 +386,12 @@ int16_t SX127x::packetMode() { } int16_t SX127x::startReceive() { - return(this->startReceive(0, RADIOLIB_SX127X_RXCONTINUOUS, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK)); + return(this->startReceive(0, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, 0)); } -int16_t SX127x::startReceive(uint8_t len, uint8_t mode, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) { +int16_t SX127x::startReceive(uint32_t timeout, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask, size_t len) { + uint8_t mode = RADIOLIB_SX127X_RXCONTINUOUS; + // set mode to standby int16_t state = setMode(RADIOLIB_SX127X_STANDBY); RADIOLIB_ASSERT(state); @@ -400,6 +402,16 @@ int16_t SX127x::startReceive(uint8_t len, uint8_t mode, RadioLibIrqFlags_t irqFl int16_t modem = getActiveModem(); if(modem == RADIOLIB_SX127X_LORA) { + if(timeout != 0) { + // for non-zero timeout value, change mode to Rx single and set the timeout + mode = RADIOLIB_SX127X_RXSINGLE; + uint8_t msb_sym = (timeout > 0x3FF) ? 0x3 : (uint8_t)(timeout >> 8); + uint8_t lsb_sym = (timeout > 0x3FF) ? 0xFF : (uint8_t)(timeout & 0xFF); + int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, msb_sym, 1, 0); + state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYMB_TIMEOUT_LSB, lsb_sym); + RADIOLIB_ASSERT(state); + } + // in FHSS mode, enable channel change interrupt if(this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_HOP_PERIOD) > RADIOLIB_SX127X_HOP_PERIOD_OFF) { state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_1, RADIOLIB_SX127X_DIO1_LORA_FHSS_CHANGE_CHANNEL, 5, 4); @@ -427,11 +439,7 @@ int16_t SX127x::startReceive(uint8_t len, uint8_t mode, RadioLibIrqFlags_t irqFl clearIrqFlags(RADIOLIB_SX127X_FLAGS_ALL); // FSK modem does not distinguish between Rx single and continuous - if(mode == RADIOLIB_SX127X_RXCONTINUOUS) { - // set RF switch (if present) - this->mod->setRfSwitchState(Module::MODE_RX); - return(setMode(RADIOLIB_SX127X_RX)); - } + mode = RADIOLIB_SX127X_RX; } // set RF switch (if present) @@ -441,20 +449,6 @@ int16_t SX127x::startReceive(uint8_t len, uint8_t mode, RadioLibIrqFlags_t irqFl return(setMode(mode)); } -int16_t SX127x::startReceive(uint32_t timeout, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask, size_t len) { - uint8_t mode = RADIOLIB_SX127X_RXCONTINUOUS; - if(timeout != 0) { - // for non-zero timeout value, change mode to Rx single and set the timeout - mode = RADIOLIB_SX127X_RXSINGLE; - uint8_t msb_sym = (timeout > 0x3FF) ? 0x3 : (uint8_t)(timeout >> 8); - uint8_t lsb_sym = (timeout > 0x3FF) ? 0xFF : (uint8_t)(timeout & 0xFF); - int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, msb_sym, 1, 0); - state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYMB_TIMEOUT_LSB, lsb_sym); - RADIOLIB_ASSERT(state); - } - return(startReceive((uint8_t)len, mode, irqFlags, irqMask)); -} - void SX127x::setDio0Action(void (*func)(void), uint32_t dir) { this->mod->hal->attachInterrupt(this->mod->hal->pinToInterrupt(this->mod->getIrq()), func, dir); } diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index 86f5144b..a1df97e0 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -811,15 +811,6 @@ class SX127x: public PhysicalLayer { \returns \ref status_codes */ int16_t startReceive() override; - - /*! - \brief Interrupt-driven receive method. DIO0 will be activated when full valid packet is received. - \param len Expected length of packet to be received, or 0 when unused. - Defaults to 0, non-zero required for LoRa spreading factor 6. - \param mode Receive mode to be used. Defaults to RxContinuous. - \returns \ref status_codes - */ - int16_t startReceive(uint8_t len, uint8_t mode = RADIOLIB_SX127X_RXCONTINUOUS, RadioLibIrqFlags_t irqFlags = RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RadioLibIrqFlags_t irqMask = RADIOLIB_IRQ_RX_DEFAULT_MASK); /*! \brief Interrupt-driven receive method, implemented for compatibility with PhysicalLayer.