diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 261e9814..731b77d9 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -147,17 +147,8 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) { int16_t modem = getActiveModem(); uint32_t start = 0; if(modem == RADIOLIB_SX127X_LORA) { - // calculate timeout (150 % of expected time-one-air) - float symbolLength = (float)(uint32_t(1) <<_sf) / (float)_bw; - float de = 0; - if(symbolLength >= 16.0) { - de = 1; - } - float ih = (float)_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, 0, 0); - float crc = (float)(_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, 2, 2) >> 2); - float n_pre = (float)((_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB) << 8) | _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB)); - float n_pay = 8.0 + max(ceil((8.0 * (float)len - 4.0 * (float)_sf + 28.0 + 16.0 * crc - 20.0 * ih)/(4.0 * (float)_sf - 8.0 * de)) * (float)_cr, 0.0); - uint32_t timeout = ceil(symbolLength * (n_pre + n_pay + 4.25) * 1500.0); + // calculate timeout (150 % of expected time-on-air) + uint32_t timeout = getTimeOnAir(len) * 1.5; // start transmission state = startTransmit(data, len, addr); @@ -175,7 +166,7 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) { } else if(modem == RADIOLIB_SX127X_FSK_OOK) { // calculate timeout (5ms + 500 % of expected time-on-air) - uint32_t timeout = 5000000 + (uint32_t)((((float)(len * 8)) / (_br * 1000.0)) * 5000000.0); + uint32_t timeout = 5000 + getTimeOnAir(len) * 5; // start transmission state = startTransmit(data, len, addr); @@ -227,8 +218,8 @@ int16_t SX127x::receive(uint8_t* data, size_t len) { } } else if(modem == RADIOLIB_SX127X_FSK_OOK) { - // calculate timeout (500 % of expected time-one-air) - uint32_t timeout = (uint32_t)((((float)(len * 8)) / (_br * 1000.0)) * 5000000.0); + // calculate timeout (500 % of expected time-on-air) + uint32_t timeout = getTimeOnAir(len) * 5; // set mode to receive state = startReceive(len, RADIOLIB_SX127X_RX); @@ -1047,6 +1038,52 @@ int16_t SX127x::variablePacketLengthMode(uint8_t maxLen) { return(SX127x::setPacketMode(RADIOLIB_SX127X_PACKET_VARIABLE, maxLen)); } +uint32_t SX127x::getTimeOnAir(size_t len) { + // check active modem + uint8_t modem = getActiveModem(); + if (modem == RADIOLIB_SX127X_LORA) { + // Get symbol length in us + float symbolLength = (float) (uint32_t(1) << _sf) / (float) _bw; + // Get Low Data Rate optimization flag + float de = 0; + if (symbolLength >= 16.0) { + de = 1; + } + // Get explicit/implicit header enabled flag + float ih = (float) _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_1, 0, 0); + // Get CRC enabled flag + float crc = (float) (_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_MODEM_CONFIG_2, 2, 2) >> 2); + // Get number of bits preamble + float n_pre = (float) ((_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB) << 8) | _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB)); + // Get number of bits payload + float n_pay = 8.0 + max(ceil((8.0 * (float) len - 4.0 * (float) _sf + 28.0 + 16.0 * crc - 20.0 * ih) / (4.0 * (float) _sf - 8.0 * de)) * (float) _cr, 0.0); + + // Get time-on-air in us + return ceil(symbolLength * (n_pre + n_pay + 4.25)) * 1000; + } else if(modem == RADIOLIB_SX127X_FSK_OOK) { + // Get number of bits preamble + float n_pre = (float) ((_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_MSB_FSK) << 8) | _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PREAMBLE_LSB_FSK)) * 8; + //Get the number of bits of the sync word + float n_syncWord = (float) (_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, 2, 0) + 1) * 8; + //Get CRC bits + float crc = (_mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_1, 4, 4) == RADIOLIB_SX127X_CRC_ON) * 16; + + if (_packetLengthConfig == RADIOLIB_SX127X_PACKET_FIXED) { + //If Packet size fixed -> len = fixed packet length + len = _mod->SPIgetRegValue(RADIOLIB_SX127X_REG_PAYLOAD_LENGTH_FSK); + } else { + //if packet variable -> Add 1 extra byte for payload length + 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) + return (uint32_t) (((crc + n_syncWord + n_pre + (float) (len * 8)) / (_br * 1000.0)) * 1000000.0); + } else { + return(RADIOLIB_ERR_UNKNOWN); + } + +} + int16_t SX127x::setCrcFiltering(bool crcOn) { _crcOn = crcOn; diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index 2597695e..15ba7154 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -980,6 +980,15 @@ class SX127x: public PhysicalLayer { */ int16_t variablePacketLengthMode(uint8_t maxLen = RADIOLIB_SX127X_MAX_PACKET_LENGTH_FSK); + /*! + \brief Get expected time-on-air for a given size of payload + + \param len Payload length in bytes. + + \returns Expected time-on-air in microseconds. + */ + uint32_t getTimeOnAir(size_t len); + /*! \brief Enable CRC filtering and generation.