From 36530b00fc3515ba33caf89699aa6f85cb119371 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 24 Jun 2023 19:23:26 +0200 Subject: [PATCH] [SX128x] Fixed blocking receive --- src/modules/SX128x/SX128x.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index f3eb20d6..bf464218 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -353,16 +353,30 @@ int16_t SX128x::receive(uint8_t* data, size_t len) { RADIOLIB_ASSERT(state); // wait for packet reception or timeout + bool softTimeout = false; uint32_t start = this->mod->hal->micros(); while(!this->mod->hal->digitalRead(this->mod->getIrq())) { this->mod->hal->yield(); + // safety check, the timeout should be done by the radio if(this->mod->hal->micros() - start > timeout) { - clearIrqStatus(); - standby(); - return(RADIOLIB_ERR_RX_TIMEOUT); + softTimeout = true; + break; } } + // if it was a timeout, this will return an error code + state = standby(); + if((state != RADIOLIB_ERR_NONE) && (state != RADIOLIB_ERR_SPI_CMD_TIMEOUT)) { + return(state); + } + + // check whether this was a timeout or not + if((getIrqStatus() & RADIOLIB_SX128X_IRQ_RX_TX_TIMEOUT) || softTimeout) { + standby(); + clearIrqStatus(); + return(RADIOLIB_ERR_RX_TIMEOUT); + } + // read the received data return(readData(data, len)); }