From 7a5832ab8adcd072656136b4c20147c0f00423c7 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Wed, 7 Oct 2020 13:43:51 +0800 Subject: [PATCH] SX1262: fix serious bug with detecting if we have a rx packet in progress Could cause hangs on the way into sleep (and enormous power consumption). Instead of checking for rx packet length (which only changes at completion) check if we've received preamble bits but haven't yet received a completed packet interrupt. notes: wait to sleep loop problem radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 Can not send yet, busyRx radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 vs normal run radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 radio wait to sleep, txEmpty=0 Starting low level send (id=0x53fe1dd0 Fr0xe5 To0xff, WantAck0, HopLim3 encrypted) Completed sending (id=0x53fe1dd0 Fr0xe5 To0xff, WantAck0, HopLim3 encrypted) --- docs/software/gps-todo.txt | 38 ++---------------------------------- platformio.ini | 2 +- src/mesh/RF95Interface.cpp | 6 +++--- src/mesh/SX1262Interface.cpp | 14 ++++++++++--- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/docs/software/gps-todo.txt b/docs/software/gps-todo.txt index 48c0ebb3a..79503ebcd 100644 --- a/docs/software/gps-todo.txt +++ b/docs/software/gps-todo.txt @@ -12,44 +12,10 @@ ublox parsing failure record power measurements and update spreadsheet -wait to sleep loop problem -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -Can not send yet, busyRx -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 - -vs normal run -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -radio wait to sleep, txEmpty=0 -Starting low level send (id=0x53fe1dd0 Fr0xe5 To0xff, WantAck0, HopLim3 encrypted) -Completed sending (id=0x53fe1dd0 Fr0xe5 To0xff, WantAck0, HopLim3 encrypted) - fix has_gps based on new logic +make sure we are turning off lora radio in deep sleep + don't send locations if the user has forbidden that (lie to phone so phone won't either) have loop methods return allowable sleep time (from their perspective) diff --git a/platformio.ini b/platformio.ini index 035a8e297..b48dc3938 100644 --- a/platformio.ini +++ b/platformio.ini @@ -63,7 +63,7 @@ lib_deps = 1202 ; CRC32, explicitly needed because dependency is missing in the ble ota update lib https://github.com/meshtastic/arduino-fsm.git https://github.com/meshtastic/SparkFun_Ublox_Arduino_Library.git#cb8353dfddd1b0e205098f5e70d5f2a5f74b4838 - https://github.com/meshtastic/RadioLib.git#ac7feac00f5e0bd95a3ac5d5852b4cc7344cf95c + https://github.com/meshtastic/RadioLib.git#1083c2e76f9906c5f80dfec726facebf8413eef0 https://github.com/meshtastic/TinyGPSPlus.git https://github.com/meshtastic/AXP202X_Library.git#8404abb6d4b486748636bc6ad72d2a47baaf5460 Wire ; explicitly needed here because the AXP202 library forgets to add it diff --git a/src/mesh/RF95Interface.cpp b/src/mesh/RF95Interface.cpp index 594cec91f..babd9475f 100644 --- a/src/mesh/RF95Interface.cpp +++ b/src/mesh/RF95Interface.cpp @@ -42,7 +42,7 @@ bool RF95Interface::init() power = MAX_POWER; limitPower(); - + iface = lora = new RadioLibRF95(&module); #ifdef RF95_TCXO @@ -158,7 +158,7 @@ void RF95Interface::startReceive() isReceiving = true; - // Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits + // Must be done AFTER, starting receive, because startReceive clears (possibly stale) interrupt pending register bits enableInterrupt(isrRxLevel0); } @@ -171,7 +171,7 @@ bool RF95Interface::isActivelyReceiving() bool RF95Interface::sleep() { // put chipset into sleep mode - disableInterrupt(); + setStandby(); // First cancel any active receving/sending lora->sleep(); return true; diff --git a/src/mesh/SX1262Interface.cpp b/src/mesh/SX1262Interface.cpp index 130f340c3..b17acd9fe 100644 --- a/src/mesh/SX1262Interface.cpp +++ b/src/mesh/SX1262Interface.cpp @@ -179,9 +179,17 @@ void SX1262Interface::startReceive() /** Could we send right now (i.e. either not actively receving or transmitting)? */ bool SX1262Interface::isActivelyReceiving() { - // return false; // FIXME - // FIXME this is not correct? - often always true - need to add an extra conditional - return lora.getPacketLength() > 0; + // The IRQ status will be cleared when we start our read operation. Check if we've started a preamble, but haven't yet + // received and handled the interrupt for reading the packet/handling errors. + + uint16_t irq = lora.getIrqStatus(); + bool hasPreamble = (irq & SX126X_IRQ_PREAMBLE_DETECTED); + + // this is not correct - often always true - need to add an extra conditional + // size_t bytesPending = lora.getPacketLength(); + + // if (hasPreamble || bytesPending) DEBUG_MSG("rx hasPre %d, bytes %d\n", hasPreamble, bytesPending); + return hasPreamble; } bool SX1262Interface::sleep()