]> git.tnoah.ca Git - lora-radio.git/commitdiff
Detect modem communication timeouts for sx126x chips
authorMark Qvist <mark@unsigned.io>
Mon, 7 Oct 2024 18:43:16 +0000 (20:43 +0200)
committerMark Qvist <mark@unsigned.io>
Mon, 7 Oct 2024 18:43:16 +0000 (20:43 +0200)
sx126x.cpp
sx126x.h

index e401d219edaa1bb18991a5d02de6ed8a9ae1c893..1ed81a1e83b8e17d17911a1725ea15fe47d0c377 100644 (file)
@@ -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() {
index 5e0ff4d4f9e0925525eca182b743226881754577..0aec47adaddd19976038d6637c90ef486a35545e 100644 (file)
--- 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