From 3ca1e62b1f4d07472f92b6cdb2d5403f26ab5a7b Mon Sep 17 00:00:00 2001 From: GUVWAF <78759985+GUVWAF@users.noreply.github.com> Date: Mon, 6 Mar 2023 22:53:59 +0100 Subject: [PATCH] SX126x/8x: Add HEADER_VALID IRQ flag for actively receiving check (#2333) * Add HEADER_VALID IRQ flag for SX126x/8x to detect actively receiving Needs new RadioLib commit * Update comments * Use latest RadioLib release --------- Co-authored-by: Ben Meadors --- platformio.ini | 3 +-- src/mesh/SX126xInterface.cpp | 20 ++++++++------------ src/mesh/SX128xInterface.cpp | 10 ++++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/platformio.ini b/platformio.ini index af18a6f8b..fa119eccc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -66,8 +66,7 @@ lib_deps = https://github.com/meshtastic/ArduinoThread.git#72921ac222eed6f526ba1682023cee290d9aa1b3 nanopb/Nanopb@^0.4.6 erriez/ErriezCRC32@^1.0.1 -; jgromes/RadioLib@^5.5.1 - https://github.com/jgromes/RadioLib.git#1afa947030c5637f71f6563bc22aa75032e53a57 + jgromes/RadioLib@^5.7.0 ; Used for the code analysis in PIO Home / Inspect check_tool = cppcheck diff --git a/src/mesh/SX126xInterface.cpp b/src/mesh/SX126xInterface.cpp index 1036042eb..54bc70a73 100644 --- a/src/mesh/SX126xInterface.cpp +++ b/src/mesh/SX126xInterface.cpp @@ -212,9 +212,10 @@ template void SX126xInterface::startReceive() setStandby(); - // int err = lora.startReceive(); - int err = lora.startReceiveDutyCycleAuto(); // We use a 32 bit preamble so this should save some power by letting radio sit in - // standby mostly. + // We use a 16 bit preamble so this should save some power by letting radio sit in standby mostly. + // Furthermore, we need the HEADER_VALID IRQ flag to detect whether we are actively receiving + int err = + lora.startReceiveDutyCycleAuto(preambleLength, 8, RADIOLIB_SX126X_IRQ_RX_DEFAULT | RADIOLIB_SX126X_IRQ_HEADER_VALID); assert(err == RADIOLIB_ERR_NONE); isReceiving = true; @@ -224,7 +225,7 @@ template void SX126xInterface::startReceive() #endif } -/** Could we send right now (i.e. either not actively receving or transmitting)? */ +/** Is the channel currently active? */ template bool SX126xInterface::isChannelActive() { // check if we can detect a LoRa preamble on the current channel @@ -245,17 +246,12 @@ template bool SX126xInterface::isActivelyReceiving() { // The IRQ status will be cleared when we start our read operation. Check if we've started a header, but haven't yet // received and handled the interrupt for reading the packet/handling errors. - // FIXME: it would be better to check for preamble, but we currently have our ISR not set to fire for packets that - // never even get a valid header, so we don't want preamble to get set and stay set due to noise on the network. uint16_t irq = lora.getIrqStatus(); - bool hasPreamble = (irq & RADIOLIB_SX126X_IRQ_HEADER_VALID); + bool headerValid = (irq & RADIOLIB_SX126X_IRQ_HEADER_VALID); - // this is not correct - often always true - need to add an extra conditional - // size_t bytesPending = lora.getPacketLength(); - - // if (hasPreamble) LOG_DEBUG("rx hasPreamble\n"); - return hasPreamble; + // if (headerValid) LOG_DEBUG("rx headerValid\n"); + return headerValid; } template bool SX126xInterface::sleep() diff --git a/src/mesh/SX128xInterface.cpp b/src/mesh/SX128xInterface.cpp index 19001a0de..308e4a52a 100644 --- a/src/mesh/SX128xInterface.cpp +++ b/src/mesh/SX128xInterface.cpp @@ -203,7 +203,9 @@ template void SX128xInterface::startReceive() digitalWrite(SX128X_TXEN, LOW); #endif - int err = lora.startReceive(); + // We use the HEADER_VALID IRQ flag to detect whether we are actively receiving + int err = + lora.startReceive(RADIOLIB_SX128X_RX_TIMEOUT_INF, RADIOLIB_SX128X_IRQ_RX_DEFAULT | RADIOLIB_SX128X_IRQ_HEADER_VALID); assert(err == RADIOLIB_ERR_NONE); @@ -214,7 +216,7 @@ template void SX128xInterface::startReceive() #endif } -/** Could we send right now (i.e. either not actively receving or transmitting)? */ +/** Is the channel currently active? */ template bool SX128xInterface::isChannelActive() { // check if we can detect a LoRa preamble on the current channel @@ -234,8 +236,8 @@ template bool SX128xInterface::isChannelActive() template bool SX128xInterface::isActivelyReceiving() { uint16_t irq = lora.getIrqStatus(); - bool hasPreamble = (irq & RADIOLIB_SX128X_IRQ_HEADER_VALID); - return hasPreamble; + bool hasHeader = (irq & RADIOLIB_SX128X_IRQ_HEADER_VALID); + return hasHeader; } template bool SX128xInterface::sleep()