Skip to content

Commit 0bd871c

Browse files
committed
add IRQ timeout logic for LR11X0
1 parent 79ef74e commit 0bd871c

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

src/helpers/radiolib/CustomLR1110.h

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "MeshCore.h"
55

66
class CustomLR1110 : public LR1110 {
7+
uint32_t _preambleMillis = 66;
8+
uint32_t _maxPayloadMillis = 3934;
9+
uint32_t _activityAt = 0;
10+
bool _headerSeen = false;
711
bool _rx_boosted = false;
812

913
public:
@@ -32,9 +36,46 @@ class CustomLR1110 : public LR1110 {
3236
bool getRxBoostedGainMode() const { return _rx_boosted; }
3337

3438
bool isReceiving() {
35-
uint16_t irq = getIrqStatus();
36-
bool detected = ((irq & RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID) || (irq & RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED));
37-
return detected;
39+
uint32_t irq = getIrqStatus();
40+
bool preamble = irq & RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED; // bit 4
41+
bool header = irq & RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID; // bit 5
42+
bool hdrErr = irq & RADIOLIB_LR11X0_IRQ_HEADER_ERR; // bit 6
43+
uint32_t now = millis();
44+
if (hdrErr) {
45+
clearIrqState(RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED | RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID | RADIOLIB_LR11X0_IRQ_HEADER_ERR);
46+
_activityAt = 0;
47+
_headerSeen = false;
48+
return false;
49+
}
50+
if (header) {
51+
if (!_headerSeen) { _headerSeen = true; _activityAt = now; };
52+
if (now - _activityAt > _maxPayloadMillis) {
53+
MESH_DEBUG_PRINTLN("Clearing header IRQ after %ums", _maxPayloadMillis);
54+
clearIrqState(RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED | RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID | RADIOLIB_LR11X0_IRQ_HEADER_ERR);
55+
_activityAt = 0; _headerSeen = false;
56+
return false;
57+
}
58+
return true;
59+
}
60+
if (preamble) {
61+
if (_activityAt == 0) _activityAt = now;
62+
if (now - _activityAt > _preambleMillis) {
63+
clearIrqState(RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED);
64+
_activityAt = 0;
65+
MESH_DEBUG_PRINTLN("Clearing preamble IRQ after %ums", _preambleMillis);
66+
67+
return false;
68+
}
69+
return true;
70+
}
71+
_activityAt = 0; _headerSeen = false;
72+
return false;
73+
}
74+
75+
PacketMillis setMaxPacketMillis(PacketMillis maxPacketMillis) {
76+
MESH_DEBUG_PRINTLN("Setting _preambleMillis=%u, _maxPacketMillis=%u", maxPacketMillis.preambleMillis, maxPacketMillis.payloadMillis);
77+
_preambleMillis = maxPacketMillis.preambleMillis;
78+
_maxPayloadMillis = maxPacketMillis.payloadMillis;
3879
}
3980

4081
uint8_t getSpreadingFactor() const { return spreadingFactor; }

src/helpers/radiolib/CustomLR1110Wrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class CustomLR1110Wrapper : public RadioLibWrapper {
1414
((CustomLR1110 *)_radio)->setBandwidth(bw);
1515
((CustomLR1110 *)_radio)->setCodingRate(cr);
1616
updatePreamble(sf);
17+
((CustomLR1110 *)_radio)->setMaxPacketMillis(calcMaxPacketMillis(sf, bw, cr, preambleLengthForSF(sf)));
18+
1719
}
1820

1921
void doResetAGC() override { lr11x0ResetAGC((LR11x0 *)_radio, ((CustomLR1110 *)_radio)->getFreqMHz()); }

0 commit comments

Comments
 (0)