Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions src/helpers/radiolib/CustomLLCC68Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomLLCC68Wrapper : public RadioLibWrapper {
int sf = ((CustomLLCC68 *)_radio)->spreadingFactor;
return packetScoreInt(snr, sf, packet_len);
}
uint8_t getSpreadingFactor() const override { return ((CustomLLCC68 *)_radio)->spreadingFactor; }

void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
};
2 changes: 2 additions & 0 deletions src/helpers/radiolib/CustomLR1110.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ class CustomLR1110 : public LR1110 {
bool detected = ((irq & RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID) || (irq & RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED));
return detected;
}

uint8_t getSpreadingFactor() const { return spreadingFactor; }
};
3 changes: 2 additions & 1 deletion src/helpers/radiolib/CustomLR1110Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class CustomLR1110Wrapper : public RadioLibWrapper {

void onSendFinished() override {
RadioLibWrapper::onSendFinished();
_radio->setPreambleLength(16); // overcomes weird issues with small and big pkts
_radio->setPreambleLength(preambleLengthForSF(getSpreadingFactor())); // overcomes weird issues with small and big pkts
}

float getLastRSSI() const override { return ((CustomLR1110 *)_radio)->getRSSI(); }
float getLastSNR() const override { return ((CustomLR1110 *)_radio)->getSNR(); }
uint8_t getSpreadingFactor() const override { return ((CustomLR1110 *)_radio)->getSpreadingFactor(); }
int16_t setRxBoostedGainMode(bool en) { return ((CustomLR1110 *)_radio)->setRxBoostedGainMode(en); };
};
1 change: 1 addition & 0 deletions src/helpers/radiolib/CustomSTM32WLxWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CustomSTM32WLxWrapper : public RadioLibWrapper {
int sf = ((CustomSTM32WLx *)_radio)->spreadingFactor;
return packetScoreInt(snr, sf, packet_len);
}
uint8_t getSpreadingFactor() const override { return ((CustomSTM32WLx *)_radio)->spreadingFactor; }

void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
};
1 change: 1 addition & 0 deletions src/helpers/radiolib/CustomSX1262Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomSX1262Wrapper : public RadioLibWrapper {
int sf = ((CustomSX1262 *)_radio)->spreadingFactor;
return packetScoreInt(snr, sf, packet_len);
}
uint8_t getSpreadingFactor() const override { return ((CustomSX1262 *)_radio)->spreadingFactor; }
virtual void powerOff() override {
((CustomSX1262 *)_radio)->sleep(false);
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/radiolib/CustomSX1268Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomSX1268Wrapper : public RadioLibWrapper {
int sf = ((CustomSX1268 *)_radio)->spreadingFactor;
return packetScoreInt(snr, sf, packet_len);
}
uint8_t getSpreadingFactor() const override { return ((CustomSX1268 *)_radio)->spreadingFactor; }

void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
};
1 change: 1 addition & 0 deletions src/helpers/radiolib/CustomSX1276Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class CustomSX1276Wrapper : public RadioLibWrapper {
int sf = ((CustomSX1276 *)_radio)->spreadingFactor;
return packetScoreInt(snr, sf, packet_len);
}
uint8_t getSpreadingFactor() const override { return ((CustomSX1276 *)_radio)->spreadingFactor; }
};
7 changes: 7 additions & 0 deletions src/helpers/radiolib/RadioLibWrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void setFlag(void) {

void RadioLibWrapper::begin() {
_radio->setPacketReceivedAction(setFlag); // this is also SentComplete interrupt
_preamble_sf = getSpreadingFactor();
_radio->setPreambleLength(preambleLengthForSF(_preamble_sf)); // longer preamble for lower SF improves reliability
state = STATE_IDLE;

if (_board->getStartupReason() == BD_STARTUP_RX_PACKET) { // received a LoRa packet (while in deep sleep)
Expand Down Expand Up @@ -142,6 +144,11 @@ uint32_t RadioLibWrapper::getEstAirtimeFor(int len_bytes) {

bool RadioLibWrapper::startSendRaw(const uint8_t* bytes, int len) {
_board->onBeforeTransmit();
uint8_t sf = getSpreadingFactor();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At src/Dispatcher.cpp:329, send timing and timeout budgeting are determined before transmit starts, but src/helpers/radiolib/RadioLibWrappers.cpp:147 updates the preamble length only inside startSendRaw(), after budgeting has already happened, and src/helpers/radiolib/RadioLibWrappers.cpp:142 estimates airtime using the current radio state. That means the first packet after an SF change can be scheduled with stale airtime assumptions. Since timeout/budget logic depends on airtime, this can create hard-to-reproduce send failures or premature timeout handling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I overlooked that. I'll have a look at getting the change in earlier to allow correct packet airtime calculation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in an update inside getestairtime to prevent incorrect airtime calcs

if (sf != _preamble_sf) {
_preamble_sf = sf;
_radio->setPreambleLength(preambleLengthForSF(sf)); // update preamble when SF has changed
}
int err = _radio->startTransmit((uint8_t *) bytes, len);
if (err == RADIOLIB_ERR_NONE) {
state = STATE_TX_WAIT;
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/radiolib/RadioLibWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RadioLibWrapper : public mesh::Radio {
int16_t _noise_floor, _threshold;
uint16_t _num_floor_samples;
int32_t _floor_sample_sum;
uint8_t _preamble_sf;

void idle();
void startRecv();
Expand All @@ -19,7 +20,7 @@ class RadioLibWrapper : public mesh::Radio {
virtual void doResetAGC();

public:
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board) { n_recv = n_sent = 0; }
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board), _preamble_sf(0) { n_recv = n_sent = 0; }

void begin() override;
virtual void powerOff() { _radio->sleep(); }
Expand All @@ -38,6 +39,8 @@ class RadioLibWrapper : public mesh::Radio {
}

virtual float getCurrentRSSI() =0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting the repeated ternary to a helper to keep the 4 call sites DRY:

Suggested change
virtual float getCurrentRSSI() =0;
virtual uint8_t getSpreadingFactor() const { return LORA_SF; }
static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }

virtual uint8_t getSpreadingFactor() const { return LORA_SF; }
static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }

int getNoiseFloor() const override { return _noise_floor; }
void triggerNoiseFloorCalibrate(int threshold) override;
Expand Down