Skip to content

Commit

Permalink
[SX126x] Use millis for timeouts (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgromes committed Mar 29, 2024
1 parent fb7d698 commit 88f26c4
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/modules/SX126x/SX126x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,41 +231,27 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) {
return(RADIOLIB_ERR_PACKET_TOO_LONG);
}

uint32_t timeout = 0;

// get currently active modem
uint8_t modem = getPacketType();
if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
// calculate timeout (150% of expected time-on-air)
timeout = (getTimeOnAir(len) * 3) / 2;

} else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
// calculate timeout (500% of expected time-on-air)
timeout = getTimeOnAir(len) * 5;

} else {
return(RADIOLIB_ERR_UNKNOWN);
}

RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu us", timeout);
// calculate timeout in ms (500% of expected time-on-air)
uint32_t timeout = (getTimeOnAir(len) * 5) / 1000;
RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeout);

// start transmission
state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state);

// wait for packet transmission or timeout
uint32_t start = this->mod->hal->micros();
uint32_t start = this->mod->hal->millis();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield();
if(this->mod->hal->micros() - start > timeout) {
if(this->mod->hal->millis() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
uint32_t elapsed = this->mod->hal->micros() - start;

// update data rate
this->dataRateMeasured = (len*8.0)/((float)elapsed/1000000.0);
uint32_t elapsed = this->mod->hal->millis() - start;
this->dataRateMeasured = (len*8.0)/((float)elapsed/1000.0);

return(finishTransmit());
}
Expand All @@ -282,34 +268,36 @@ int16_t SX126x::receive(uint8_t* data, size_t len) {
if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
// calculate timeout (100 LoRa symbols, the default for SX127x series)
float symbolLength = (float)(uint32_t(1) << this->spreadingFactor) / (float)this->bandwidthKhz;
timeout = (uint32_t)(symbolLength * 100.0 * 1000.0);
timeout = (uint32_t)(symbolLength * 100.0);

} else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
// calculate timeout (500 % of expected time-one-air)
size_t maxLen = len;
if(len == 0) {
maxLen = 0xFF;
}
float brBps = ((float)(RADIOLIB_SX126X_CRYSTAL_FREQ) * 1000000.0 * 32.0) / (float)this->bitRate;
timeout = (uint32_t)(((maxLen * 8.0) / brBps) * 1000000.0 * 5.0);
timeout = (uint32_t)(((maxLen * 8.0) / brBps) * 1000.0 * 5.0);

} else {
return(RADIOLIB_ERR_UNKNOWN);

}

RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu us", timeout);
RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeout);

// start reception
uint32_t timeoutValue = (uint32_t)((float)timeout / 15.625);
uint32_t timeoutValue = (uint32_t)(((float)timeout * 1000.0) / 15.625);
state = startReceive(timeoutValue);
RADIOLIB_ASSERT(state);

// wait for packet reception or timeout
bool softTimeout = false;
uint32_t start = this->mod->hal->micros();
uint32_t start = this->mod->hal->millis();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield();
// safety check, the timeout should be done by the radio
if(this->mod->hal->micros() - start > timeout) {
if(this->mod->hal->millis() - start > timeout) {
softTimeout = true;
break;
}
Expand Down

0 comments on commit 88f26c4

Please sign in to comment.