[SX126x] Added method to get expected time-on-air

This commit is contained in:
jgromes 2019-07-26 09:43:24 +02:00
parent c43ba03d7d
commit 629ac5585a
3 changed files with 33 additions and 16 deletions

View file

@ -105,6 +105,7 @@ setGdo1Action KEYWORD2
setDio2Action KEYWORD2
setTCXO KEYWORD2
setDio2AsRfSwitch KEYWORD2
getTimeOnAir KEYWORD2
# ESP8266
join KEYWORD2

View file

@ -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();

View file

@ -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);