[SX127x] Use millis for timeouts

This commit is contained in:
jgromes 2024-03-29 08:36:05 +01:00
parent 8098bea254
commit fb7d698007

View file

@ -149,13 +149,14 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) {
int16_t modem = getActiveModem(); int16_t modem = getActiveModem();
uint32_t start = 0; uint32_t start = 0;
uint32_t timeout = 0; uint32_t timeout = 0;
uint32_t toa = getTimeOnAir(len);
if(modem == RADIOLIB_SX127X_LORA) { if(modem == RADIOLIB_SX127X_LORA) {
// calculate timeout (150 % of expected time-on-air) // calculate timeout in ms (150 % of expected time-on-air)
timeout = getTimeOnAir(len) * 1.5; timeout = (toa * 1.5) / 1000;
} else if(modem == RADIOLIB_SX127X_FSK_OOK) { } else if(modem == RADIOLIB_SX127X_FSK_OOK) {
// calculate timeout (5ms + 500 % of expected time-on-air) // calculate timeout in ms (5ms + 500 % of expected time-on-air)
timeout = 5000 + getTimeOnAir(len) * 5; timeout = 5 + (toa * 5) / 1000;
} else { } else {
return(RADIOLIB_ERR_UNKNOWN); return(RADIOLIB_ERR_UNKNOWN);
@ -163,22 +164,23 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) {
} }
// start transmission // start transmission
RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeout);
state = startTransmit(data, len, addr); state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for packet transmission or timeout // wait for packet transmission or timeout
start = this->mod->hal->micros(); start = this->mod->hal->millis();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) { while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield(); this->mod->hal->yield();
if(this->mod->hal->micros() - start > timeout) { if(this->mod->hal->millis() - start > timeout) {
finishTransmit(); finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT); return(RADIOLIB_ERR_TX_TIMEOUT);
} }
} }
// update data rate // update data rate
uint32_t elapsed = this->mod->hal->micros() - start; uint32_t elapsed = this->mod->hal->millis() - start;
this->dataRate = (len*8.0)/((float)elapsed/1000000.0); this->dataRate = (len*8.0)/((float)elapsed/1000.0);
return(finishTransmit()); return(finishTransmit());
} }
@ -198,17 +200,17 @@ int16_t SX127x::receive(uint8_t* data, size_t len) {
uint32_t timeout = 0; uint32_t timeout = 0;
if(this->mod->getGpio() == RADIOLIB_NC) { if(this->mod->getGpio() == RADIOLIB_NC) {
float symbolLength = (float) (uint32_t(1) << this->spreadingFactor) / (float) this->bandwidth; float symbolLength = (float) (uint32_t(1) << this->spreadingFactor) / (float) this->bandwidth;
timeout = (uint32_t)(symbolLength * 100.0 * 1000.0); timeout = (uint32_t)(symbolLength * 100.0);
} }
// wait for packet reception or timeout // wait for packet reception or timeout
uint32_t start = this->mod->hal->micros(); uint32_t start = this->mod->hal->millis();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) { while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield(); this->mod->hal->yield();
if(this->mod->getGpio() == RADIOLIB_NC) { if(this->mod->getGpio() == RADIOLIB_NC) {
// no GPIO pin provided, use software timeout // no GPIO pin provided, use software timeout
if(this->mod->hal->micros() - start > timeout) { if(this->mod->hal->millis() - start > timeout) {
clearIRQFlags(); clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT); return(RADIOLIB_ERR_RX_TIMEOUT);
} }
@ -223,18 +225,18 @@ int16_t SX127x::receive(uint8_t* data, size_t len) {
} }
} else if(modem == RADIOLIB_SX127X_FSK_OOK) { } else if(modem == RADIOLIB_SX127X_FSK_OOK) {
// calculate timeout (500 % of expected time-on-air) // calculate timeout in ms (500 % of expected time-on-air)
uint32_t timeout = getTimeOnAir(len) * 5; uint32_t timeout = (getTimeOnAir(len) * 5) / 1000;
// set mode to receive // set mode to receive
state = startReceive(len, RADIOLIB_SX127X_RX); state = startReceive(len, RADIOLIB_SX127X_RX);
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for packet reception or timeout // wait for packet reception or timeout
uint32_t start = this->mod->hal->micros(); uint32_t start = this->mod->hal->millis();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) { while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield(); this->mod->hal->yield();
if(this->mod->hal->micros() - start > timeout) { if(this->mod->hal->millis() - start > timeout) {
clearIRQFlags(); clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT); return(RADIOLIB_ERR_RX_TIMEOUT);
} }
@ -1252,31 +1254,30 @@ uint32_t SX127x::getTimeOnAir(size_t len) {
// get number of symbols // get number of symbols
float n_sym = getNumSymbols(len); float n_sym = getNumSymbols(len);
// Get time-on-air in us // get time-on-air in us
return ceil((double)symbolLength * (double)n_sym) * 1000; return ceil((double)symbolLength * (double)n_sym) * 1000;
} else if(modem == RADIOLIB_SX127X_FSK_OOK) { } else if(modem == RADIOLIB_SX127X_FSK_OOK) {
// Get number of bits preamble // get number of bits preamble
float n_pre = (float) ((this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB_FSK) << 8) | this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB_FSK)) * 8; float n_pre = (float) ((this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB_FSK) << 8) | this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB_FSK)) * 8;
//Get the number of bits of the sync word // get the number of bits of the sync word
float n_syncWord = (float) (this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, 2, 0) + 1) * 8; float n_syncWord = (float) (this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, 2, 0) + 1) * 8;
//Get CRC bits // get CRC bits
float crc = (this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, 4, 4) == RADIOLIB_SX127X_CRC_ON) * 16; float crc = (this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, 4, 4) == RADIOLIB_SX127X_CRC_ON) * 16;
if (this->packetLengthConfig == RADIOLIB_SX127X_PACKET_FIXED) { if (this->packetLengthConfig == RADIOLIB_SX127X_PACKET_FIXED) {
//If Packet size fixed -> len = fixed packet length // if packet size fixed -> len = fixed packet length
len = this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PAYLOAD_LENGTH_FSK); len = this->mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PAYLOAD_LENGTH_FSK);
} else { } else {
//if packet variable -> Add 1 extra byte for payload length // if packet variable -> Add 1 extra byte for payload length
len += 1; len += 1;
} }
// Calculate time-on-air in us {[(length in bytes) * (8 bits / 1 byte)] / [(Bit Rate in kbps) * (1000 bps / 1 kbps)]} * (1000000 us in 1 sec) // calculate time-on-air in us {[(length in bytes) * (8 bits / 1 byte)] / [(Bit Rate in kbps) * (1000 bps / 1 kbps)]} * (1000000 us in 1 sec)
return (uint32_t) (((crc + n_syncWord + n_pre + (float) (len * 8)) / (this->bitRate * 1000.0)) * 1000000.0); return((uint32_t) (((crc + n_syncWord + n_pre + (float) (len * 8)) / (this->bitRate * 1000.0)) * 1000000.0));
} else {
return(RADIOLIB_ERR_UNKNOWN);
} }
return(RADIOLIB_ERR_UNKNOWN);
} }
uint32_t SX127x::calculateRxTimeout(uint32_t timeoutUs) { uint32_t SX127x::calculateRxTimeout(uint32_t timeoutUs) {