Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/helpers/radiolib/CustomLR2021.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,10 @@ class CustomLR2021 : public LR2021 {
uint32_t irq = getIrqStatus();
bool preamble = irq & RADIOLIB_LR2021_IRQ_PREAMBLE_DETECTED; // bit 5
bool header = irq & RADIOLIB_LR2021_IRQ_LORA_HEADER_VALID; // bit 6
bool hdrErr = irq & RADIOLIB_LR2021_IRQ_LORA_HDR_CRC_ERROR; // bit 9
uint32_t now = millis();
if (hdrErr) {
clearIrqFlags(RADIOLIB_LR2021_IRQ_PREAMBLE_DETECTED | RADIOLIB_LR2021_IRQ_LORA_HEADER_VALID | RADIOLIB_LR2021_IRQ_LORA_HDR_CRC_ERROR);
_activityAt = 0;
_headerSeen = false;
return false;
}
if (!header && _headerSeen) {
// something cleared the header flag, reset our state.
_activityAt = 0; _headerSeen = false;
return false;
}

if (header) {
Expand All @@ -116,6 +108,17 @@ class CustomLR2021 : public LR2021 {
_activityAt = 0; _headerSeen = false;
return false;
}

int16_t readData(uint8_t* data, size_t len) override {
uint16_t fifoLevel = 0;
getRxFifoLevel(&fifoLevel);
// If there's too few bytes it could be a ghost RX_DONE (0 bytes in buffer), late read with another whole packet in the buffer,
// late and we're mid writing the next packet to the fifo, or leftover data from last event clearing the buffer halfway through.
int16_t state = LR2021::readData(data, len);
RADIOLIB_ASSERT(state);
if (fifoLevel != len) { return RADIOLIB_ERR_PACKET_TOO_SHORT; }
return state;
}

void setPreambleMillis(uint32_t preambleMillis) {
_preambleMillis = preambleMillis;
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/radiolib/RadioLibWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
}
}
#if defined(USE_LR2021)
state = STATE_RX; // LR2021 stays in Rx after readData, calling startReceive while still in Rx throws -706 errors
state = STATE_RX; // LR2021 stays in Rx after readData, calling startReceive when not in standby throws -706 errors
_radio->clearIrqFlags(RADIOLIB_LR2021_IRQ_RX_DONE); // Ensure we never get stuck
#else
state = STATE_IDLE; // need another startReceive()
#endif
Expand Down