diff --git a/sx126x.cpp b/sx126x.cpp index e401d21..1ed81a1 100644 --- a/sx126x.cpp +++ b/sx126x.cpp @@ -472,34 +472,43 @@ int sx126x::beginPacket(int implicitHeader) int sx126x::endPacket() { - setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode); + setPacketParams(_preambleLength, _implicitHeaderMode, _payloadLength, _crcMode); - // put in single TX mode - uint8_t timeout[3] = {0}; - executeOpcode(OP_TX_6X, timeout, 3); + // put in single TX mode + uint8_t timeout[3] = {0}; + executeOpcode(OP_TX_6X, timeout, 3); - uint8_t buf[2]; + uint8_t buf[2]; - buf[0] = 0x00; - buf[1] = 0x00; + buf[0] = 0x00; + buf[1] = 0x00; - executeOpcodeRead(OP_GET_IRQ_STATUS_6X, buf, 2); + executeOpcodeRead(OP_GET_IRQ_STATUS_6X, buf, 2); - // wait for TX done - while ((buf[1] & IRQ_TX_DONE_MASK_6X) == 0) { - buf[0] = 0x00; - buf[1] = 0x00; - executeOpcodeRead(OP_GET_IRQ_STATUS_6X, buf, 2); - yield(); - } + bool timed_out = false; + uint32_t w_timeout = millis()+LORA_MODEM_TIMEOUT_MS; + // wait for TX done + while ((millis() < w_timeout) && ((buf[1] & IRQ_TX_DONE_MASK_6X) == 0)) { + buf[0] = 0x00; + buf[1] = 0x00; + executeOpcodeRead(OP_GET_IRQ_STATUS_6X, buf, 2); + yield(); + } - // clear IRQ's + if (!(millis() < w_timeout)) { timed_out = true; } - uint8_t mask[2]; - mask[0] = 0x00; - mask[1] = IRQ_TX_DONE_MASK_6X; - executeOpcode(OP_CLEAR_IRQ_STATUS_6X, mask, 2); - return 1; + // clear IRQ's + + uint8_t mask[2]; + mask[0] = 0x00; + mask[1] = IRQ_TX_DONE_MASK_6X; + executeOpcode(OP_CLEAR_IRQ_STATUS_6X, mask, 2); + + if (timed_out) { + return 0; + } else { + return 1; + } } uint8_t sx126x::modemStatus() { diff --git a/sx126x.h b/sx126x.h index 5e0ff4d..0aec47a 100644 --- a/sx126x.h +++ b/sx126x.h @@ -17,6 +17,7 @@ #define LORA_DEFAULT_RXEN_PIN -1 #define LORA_DEFAULT_TXEN_PIN -1 #define LORA_DEFAULT_BUSY_PIN -1 +#define LORA_MODEM_TIMEOUT_MS 20E3 #define PA_OUTPUT_RFO_PIN 0 #define PA_OUTPUT_PA_BOOST_PIN 1