Skip to content

Commit 79ef74e

Browse files
committed
add IRQ timeout logic for SX1262
1 parent 975a673 commit 79ef74e

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/helpers/radiolib/CustomSX1262.h

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <RadioLib.h>
44
#include "MeshCore.h"
55

6-
#define SX126X_IRQ_HEADER_VALID 0b0000010000 // 4 4 valid LoRa header received
7-
#define SX126X_IRQ_PREAMBLE_DETECTED 0x04
8-
96
class CustomSX1262 : public SX1262 {
7+
uint32_t _preambleMillis = 66;
8+
uint32_t _maxPayloadMillis = 3934;
9+
uint32_t _activityAt = 0;
10+
bool _headerSeen = false;
11+
1012
public:
1113
CustomSX1262(Module *mod) : SX1262(mod) { }
1214

@@ -99,9 +101,46 @@ class CustomSX1262 : public SX1262 {
99101
}
100102

101103
bool isReceiving() {
102-
uint16_t irq = getIrqFlags();
103-
bool detected = (irq & SX126X_IRQ_HEADER_VALID) || (irq & SX126X_IRQ_PREAMBLE_DETECTED);
104-
return detected;
104+
uint32_t irq = getIrqFlags();
105+
bool preamble = irq & RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED; // bit 2
106+
bool header = irq & RADIOLIB_SX126X_IRQ_HEADER_VALID; // bit 4
107+
bool hdrErr = irq & RADIOLIB_SX126X_IRQ_HEADER_ERR; // bit 5
108+
uint32_t now = millis();
109+
if (hdrErr) {
110+
clearIrqFlags(RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED | RADIOLIB_SX126X_IRQ_HEADER_VALID | RADIOLIB_SX126X_IRQ_HEADER_ERR | RADIOLIB_SX126X_IRQ_SYNC_WORD_VALID);
111+
_activityAt = 0;
112+
_headerSeen = false;
113+
return false;
114+
}
115+
if (header) {
116+
if (!_headerSeen) { _headerSeen = true; _activityAt = now; };
117+
if (now - _activityAt > _maxPayloadMillis) {
118+
MESH_DEBUG_PRINTLN("Clearing header IRQ after %ums", _maxPayloadMillis);
119+
clearIrqFlags(RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED | RADIOLIB_SX126X_IRQ_HEADER_VALID | RADIOLIB_SX126X_IRQ_HEADER_ERR | RADIOLIB_SX126X_IRQ_SYNC_WORD_VALID);
120+
_activityAt = 0; _headerSeen = false;
121+
return false;
122+
}
123+
return true;
124+
}
125+
if (preamble) {
126+
if (_activityAt == 0) _activityAt = now;
127+
if (now - _activityAt > _preambleMillis) {
128+
clearIrqStatus(RADIOLIB_IRQ_PREAMBLE_DETECTED);
129+
_activityAt = 0;
130+
MESH_DEBUG_PRINTLN("Clearing preamble IRQ after %ums", _preambleMillis);
131+
132+
return false;
133+
}
134+
return true;
135+
}
136+
_activityAt = 0; _headerSeen = false;
137+
return false;
138+
}
139+
140+
PacketMillis setMaxPacketMillis(PacketMillis maxPacketMillis) {
141+
MESH_DEBUG_PRINTLN("Setting _preambleMillis=%u, _maxPacketMillis=%u", maxPacketMillis.preambleMillis, maxPacketMillis.payloadMillis);
142+
_preambleMillis = maxPacketMillis.preambleMillis;
143+
_maxPayloadMillis = maxPacketMillis.payloadMillis;
105144
}
106145

107146
bool getRxBoostedGainMode() {

src/helpers/radiolib/CustomSX1262Wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CustomSX1262Wrapper : public RadioLibWrapper {
1818
((CustomSX1262 *)_radio)->setBandwidth(bw);
1919
((CustomSX1262 *)_radio)->setCodingRate(cr);
2020
updatePreamble(sf);
21+
((CustomSX1262 *)_radio)->setMaxPacketMillis(calcMaxPacketMillis(sf, bw, cr, preambleLengthForSF(sf)));
2122
}
2223

2324
bool isReceivingPacket() override {

0 commit comments

Comments
 (0)