From 629ac5585aedb0993acfee66e7ba58dac312eb27 Mon Sep 17 00:00:00 2001 From: jgromes Date: Fri, 26 Jul 2019 09:43:24 +0200 Subject: [PATCH] [SX126x] Added method to get expected time-on-air --- keywords.txt | 1 + src/modules/SX126x.cpp | 39 +++++++++++++++++++++++---------------- src/modules/SX126x.h | 9 +++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/keywords.txt b/keywords.txt index 7a47ab48..1e47e76e 100644 --- a/keywords.txt +++ b/keywords.txt @@ -105,6 +105,7 @@ setGdo1Action KEYWORD2 setDio2Action KEYWORD2 setTCXO KEYWORD2 setDio2AsRfSwitch KEYWORD2 +getTimeOnAir KEYWORD2 # ESP8266 join KEYWORD2 diff --git a/src/modules/SX126x.cpp b/src/modules/SX126x.cpp index 93327118..94b9c581 100644 --- a/src/modules/SX126x.cpp +++ b/src/modules/SX126x.cpp @@ -157,25 +157,11 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) { uint8_t modem = getPacketType(); if(modem == SX126X_PACKET_TYPE_LORA) { // calculate timeout (150% of expected time-on-air) - float symbolLength = (float)((uint32_t)(1) << _sf) / (float)_bwKhz; - float sfCoeff1 = 4.25; - float sfCoeff2 = 8.0; - if(_sf == 5 || _sf == 6) { - sfCoeff1 = 6.25; - sfCoeff2 = 0.0; - } - uint8_t sfDivisor = 4*_sf; - if(symbolLength >= 16.0) { - sfDivisor = 4*(_sf - 2); - } - float nSymbol = _preambleLength + sfCoeff1 + 8 + ceil(max(8.0 * len + (_crcType * 16.0) - 4.0 * _sf + sfCoeff2 + 20.0, 0.0) / sfDivisor) * (_cr + 4); - timeout = (uint32_t)(symbolLength * nSymbol * 1500.0); + timeout = (float)getTimeOnAir(len) * 1.5; } else if(modem == SX126X_PACKET_TYPE_GFSK) { - // calculate timeout (500% of expected time-on-air) - float brBps = ((float)(SX126X_CRYSTAL_FREQ) * 1000000.0 * 32.0) / (float)_br; - timeout = (uint32_t)(((len * 8.0) / brBps) * 1000000.0 * 5.0); + timeout = (float)getTimeOnAir(len) * 5.0; } else { return(ERR_UNKNOWN); @@ -877,6 +863,27 @@ size_t SX126x::getPacketLength(bool update) { return((size_t)rxBufStatus[0]); } +uint32_t SX126x::getTimeOnAir(size_t len) { + if(getPacketType() == SX126X_PACKET_TYPE_LORA) { + float symbolLength = (float)((uint32_t)(1) << _sf) / (float)_bwKhz; + float sfCoeff1 = 4.25; + float sfCoeff2 = 8.0; + if(_sf == 5 || _sf == 6) { + sfCoeff1 = 6.25; + sfCoeff2 = 0.0; + } + uint8_t sfDivisor = 4*_sf; + if(symbolLength >= 16.0) { + sfDivisor = 4*(_sf - 2); + } + float nSymbol = _preambleLength + sfCoeff1 + 8 + ceil(max(8.0 * len + (_crcType * 16.0) - 4.0 * _sf + sfCoeff2 + 20.0, 0.0) / sfDivisor) * (_cr + 4); + return((uint32_t)(symbolLength * nSymbol * 1000.0)); + } else { + float brBps = ((float)(SX126X_CRYSTAL_FREQ) * 1000000.0 * 32.0) / (float)_br; + return((uint32_t)(((len * 8.0) / brBps) * 1000000.0)); + } +} + int16_t SX126x::setTCXO(float voltage, uint32_t timeout) { // set mode to standby standby(); diff --git a/src/modules/SX126x.h b/src/modules/SX126x.h index 9f9253b4..134325bd 100644 --- a/src/modules/SX126x.h +++ b/src/modules/SX126x.h @@ -690,6 +690,15 @@ class SX126x: public PhysicalLayer { */ size_t getPacketLength(bool update = true); + /*! + \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); + protected: // SX1276x SPI command implementations int16_t setTx(uint32_t timeout = 0);