Skip to content

Commit bc71207

Browse files
committed
Issue #931 - Changed RegionCommonComputeSymbolTimeLoRa, RegionCommonComputeSymbolTimeFsk and RegionCommonComputeRxWindowParameters API implementations to use integer divisions instead of double division.
1 parent c91cf25 commit bc71207

File tree

12 files changed

+74
-56
lines changed

12 files changed

+74
-56
lines changed

src/mac/region/RegionAS923.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,22 +579,22 @@ bool RegionAS923ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
579579

580580
void RegionAS923ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
581581
{
582-
double tSymbol = 0.0;
582+
uint32_t tSymbolInUs = 0;
583583

584584
// Get the datarate, perform a boundary check
585585
rxConfigParams->Datarate = MIN( datarate, AS923_RX_MAX_DATARATE );
586586
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsAS923 );
587587

588588
if( rxConfigParams->Datarate == DR_7 )
589589
{ // FSK
590-
tSymbol = RegionCommonComputeSymbolTimeFsk( DataratesAS923[rxConfigParams->Datarate] );
590+
tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesAS923[rxConfigParams->Datarate] );
591591
}
592592
else
593593
{ // LoRa
594-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesAS923[rxConfigParams->Datarate], BandwidthsAS923[rxConfigParams->Datarate] );
594+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesAS923[rxConfigParams->Datarate], BandwidthsAS923[rxConfigParams->Datarate] );
595595
}
596596

597-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
597+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
598598
}
599599

600600
bool RegionAS923RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionAU915.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,15 @@ bool RegionAU915ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
556556

557557
void RegionAU915ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
558558
{
559-
double tSymbol = 0.0;
559+
uint32_t tSymbolInUs = 0;
560560

561561
// Get the datarate, perform a boundary check
562562
rxConfigParams->Datarate = MIN( datarate, AU915_RX_MAX_DATARATE );
563563
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsAU915 );
564564

565-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesAU915[rxConfigParams->Datarate], BandwidthsAU915[rxConfigParams->Datarate] );
565+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesAU915[rxConfigParams->Datarate], BandwidthsAU915[rxConfigParams->Datarate] );
566566

567-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
567+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
568568
}
569569

570570
bool RegionAU915RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionCN470.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,15 +452,15 @@ bool RegionCN470ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
452452

453453
void RegionCN470ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
454454
{
455-
double tSymbol = 0.0;
455+
uint32_t tSymbolInUs = 0;
456456

457457
// Get the datarate, perform a boundary check
458458
rxConfigParams->Datarate = MIN( datarate, CN470_RX_MAX_DATARATE );
459459
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsCN470 );
460460

461-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesCN470[rxConfigParams->Datarate], BandwidthsCN470[rxConfigParams->Datarate] );
461+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesCN470[rxConfigParams->Datarate], BandwidthsCN470[rxConfigParams->Datarate] );
462462

463-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
463+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
464464
}
465465

466466
bool RegionCN470RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionCN779.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,22 +474,22 @@ bool RegionCN779ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
474474

475475
void RegionCN779ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
476476
{
477-
double tSymbol = 0.0;
477+
uint32_t tSymbolInUs = 0;
478478

479479
// Get the datarate, perform a boundary check
480480
rxConfigParams->Datarate = MIN( datarate, CN779_RX_MAX_DATARATE );
481481
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsCN779 );
482482

483483
if( rxConfigParams->Datarate == DR_7 )
484484
{ // FSK
485-
tSymbol = RegionCommonComputeSymbolTimeFsk( DataratesCN779[rxConfigParams->Datarate] );
485+
tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesCN779[rxConfigParams->Datarate] );
486486
}
487487
else
488488
{ // LoRa
489-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesCN779[rxConfigParams->Datarate], BandwidthsCN779[rxConfigParams->Datarate] );
489+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesCN779[rxConfigParams->Datarate], BandwidthsCN779[rxConfigParams->Datarate] );
490490
}
491491

492-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
492+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
493493
}
494494

495495
bool RegionCN779RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionCommon.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@
4545
#define DUTY_CYCLE_TIME_PERIOD 3600000
4646
#endif
4747

48+
/*!
49+
* \brief Returns `N / D` rounded to the smallest integer value greater than or equal to `N / D`
50+
*
51+
* \warning when `D == 0`, the result is undefined
52+
*
53+
* \remark `N` and `D` can be signed or unsigned
54+
*
55+
* \param [IN] N the numerator, which can have any sign
56+
* \param [IN] D the denominator, which can have any sign
57+
* \retval N / D with any fractional part rounded to the smallest integer value greater than or equal to `N / D`
58+
*/
59+
#define DIV_CEIL( N, D ) \
60+
( \
61+
( N > 0 ) ? \
62+
( ( ( N ) + ( D ) - 1 ) / ( D ) ) : \
63+
( ( N ) / ( D ) ) \
64+
)
65+
4866
static uint16_t GetDutyCycle( Band_t* band, bool joined, SysTime_t elapsedTimeSinceStartup )
4967
{
5068
uint16_t joinDutyCycle = RegionCommonGetJoinDc( elapsedTimeSinceStartup );
@@ -410,20 +428,22 @@ uint8_t RegionCommonLinkAdrReqVerifyParams( RegionCommonLinkAdrReqVerifyParams_t
410428
return status;
411429
}
412430

413-
double RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidth )
431+
uint32_t RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidthInHz )
414432
{
415-
return ( ( double )( 1 << phyDr ) / ( double )bandwidth ) * 1000;
433+
return ( 1 << phyDr ) * 1000000 / bandwidthInHz;
416434
}
417435

418-
double RegionCommonComputeSymbolTimeFsk( uint8_t phyDr )
436+
uint32_t RegionCommonComputeSymbolTimeFsk( uint8_t phyDrInKbps )
419437
{
420-
return ( 8.0 / ( double )phyDr ); // 1 symbol equals 1 byte
438+
return 8000 / ( uint32_t )phyDrInKbps; // 1 symbol equals 1 byte
421439
}
422440

423-
void RegionCommonComputeRxWindowParameters( double tSymbol, uint8_t minRxSymbols, uint32_t rxError, uint32_t wakeUpTime, uint32_t* windowTimeout, int32_t* windowOffset )
441+
void RegionCommonComputeRxWindowParameters( uint32_t tSymbolInUs, uint8_t minRxSymbols, uint32_t rxErrorInMs, uint32_t wakeUpTimeInMs, uint32_t* windowTimeoutInSymbols, int32_t* windowOffsetInMs )
424442
{
425-
*windowTimeout = MAX( ( uint32_t )ceil( ( ( 2 * minRxSymbols - 8 ) * tSymbol + 2 * rxError ) / tSymbol ), minRxSymbols ); // Computed number of symbols
426-
*windowOffset = ( int32_t )ceil( ( 4.0 * tSymbol ) - ( ( *windowTimeout * tSymbol ) / 2.0 ) - wakeUpTime );
443+
*windowTimeoutInSymbols = MAX( DIV_CEIL( ( ( 2 * minRxSymbols - 8 ) * tSymbolInUs + 2 * ( rxErrorInMs * 1000 ) ), tSymbolInUs ), minRxSymbols ); // Computed number of symbols
444+
*windowOffsetInMs = ( int32_t )DIV_CEIL( ( int32_t )( 4 * tSymbolInUs ) -
445+
( int32_t )DIV_CEIL( ( *windowTimeoutInSymbols * tSymbolInUs ), 2 ) -
446+
( int32_t )( wakeUpTimeInMs * 1000 ), 1000 );
427447
}
428448

429449
int8_t RegionCommonComputeTxPower( int8_t txPowerIndex, float maxEirp, float antennaGain )

src/mac/region/RegionCommon.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -483,38 +483,36 @@ uint8_t RegionCommonLinkAdrReqVerifyParams( RegionCommonLinkAdrReqVerifyParams_t
483483
*
484484
* \param [IN] bandwidth Bandwidth to use.
485485
*
486-
* \retval Returns the symbol time.
486+
* \retval Returns the symbol time in microseconds.
487487
*/
488-
double RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidth );
488+
uint32_t RegionCommonComputeSymbolTimeLoRa( uint8_t phyDr, uint32_t bandwidthInHz );
489489

490490
/*!
491491
* \brief Computes the symbol time for FSK modulation.
492492
*
493493
* \param [IN] phyDr Physical datarate to use.
494494
*
495-
* \param [IN] bandwidth Bandwidth to use.
496-
*
497-
* \retval Returns the symbol time.
495+
* \retval Returns the symbol time in microseconds.
498496
*/
499-
double RegionCommonComputeSymbolTimeFsk( uint8_t phyDr );
497+
uint32_t RegionCommonComputeSymbolTimeFsk( uint8_t phyDrInKbps );
500498

501499
/*!
502500
* \brief Computes the RX window timeout and the RX window offset.
503501
*
504-
* \param [IN] tSymbol Symbol timeout.
502+
* \param [IN] tSymbolInUs Symbol timeout.
505503
*
506504
* \param [IN] minRxSymbols Minimum required number of symbols to detect an Rx frame.
507505
*
508-
* \param [IN] rxError System maximum timing error of the receiver. In milliseconds
509-
* The receiver will turn on in a [-rxError : +rxError] ms interval around RxOffset.
506+
* \param [IN] rxErrorInMs System maximum timing error of the receiver. In milliseconds
507+
* The receiver will turn on in a [-rxErrorInMs : +rxErrorInMs] ms interval around RxOffset.
510508
*
511-
* \param [IN] wakeUpTime Wakeup time of the system.
509+
* \param [IN] wakeUpTimeInMs Wakeup time of the system.
512510
*
513-
* \param [OUT] windowTimeout RX window timeout.
511+
* \param [OUT] windowTimeoutInSymbols RX window timeout.
514512
*
515-
* \param [OUT] windowOffset RX window time offset to be applied to the RX delay.
513+
* \param [OUT] windowOffsetInMs RX window time offset to be applied to the RX delay.
516514
*/
517-
void RegionCommonComputeRxWindowParameters( double tSymbol, uint8_t minRxSymbols, uint32_t rxError, uint32_t wakeUpTime, uint32_t* windowTimeout, int32_t* windowOffset );
515+
void RegionCommonComputeRxWindowParameters( uint32_t tSymbolInUs, uint8_t minRxSymbols, uint32_t rxErrorInMs, uint32_t wakeUpTimeInMs, uint32_t* windowTimeoutInSymbols, int32_t* windowOffsetInMs );
518516

519517
/*!
520518
* \brief Computes the txPower, based on the max EIRP and the antenna gain.

src/mac/region/RegionEU433.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,22 +474,22 @@ bool RegionEU433ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
474474

475475
void RegionEU433ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
476476
{
477-
double tSymbol = 0.0;
477+
uint32_t tSymbolInUs = 0;
478478

479479
// Get the datarate, perform a boundary check
480480
rxConfigParams->Datarate = MIN( datarate, EU433_RX_MAX_DATARATE );
481481
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsEU433 );
482482

483483
if( rxConfigParams->Datarate == DR_7 )
484484
{ // FSK
485-
tSymbol = RegionCommonComputeSymbolTimeFsk( DataratesEU433[rxConfigParams->Datarate] );
485+
tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesEU433[rxConfigParams->Datarate] );
486486
}
487487
else
488488
{ // LoRa
489-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesEU433[rxConfigParams->Datarate], BandwidthsEU433[rxConfigParams->Datarate] );
489+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesEU433[rxConfigParams->Datarate], BandwidthsEU433[rxConfigParams->Datarate] );
490490
}
491491

492-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
492+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
493493
}
494494

495495
bool RegionEU433RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionEU868.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,22 +505,22 @@ bool RegionEU868ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
505505

506506
void RegionEU868ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
507507
{
508-
double tSymbol = 0.0;
508+
uint32_t tSymbolInUs = 0;
509509

510510
// Get the datarate, perform a boundary check
511511
rxConfigParams->Datarate = MIN( datarate, EU868_RX_MAX_DATARATE );
512512
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsEU868 );
513513

514514
if( rxConfigParams->Datarate == DR_7 )
515515
{ // FSK
516-
tSymbol = RegionCommonComputeSymbolTimeFsk( DataratesEU868[rxConfigParams->Datarate] );
516+
tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesEU868[rxConfigParams->Datarate] );
517517
}
518518
else
519519
{ // LoRa
520-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesEU868[rxConfigParams->Datarate], BandwidthsEU868[rxConfigParams->Datarate] );
520+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesEU868[rxConfigParams->Datarate], BandwidthsEU868[rxConfigParams->Datarate] );
521521
}
522522

523-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
523+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
524524
}
525525

526526
bool RegionEU868RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionIN865.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,22 +500,22 @@ bool RegionIN865ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
500500

501501
void RegionIN865ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
502502
{
503-
double tSymbol = 0.0;
503+
uint32_t tSymbolInUs = 0;
504504

505505
// Get the datarate, perform a boundary check
506506
rxConfigParams->Datarate = MIN( datarate, IN865_RX_MAX_DATARATE );
507507
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsIN865 );
508508

509509
if( rxConfigParams->Datarate == DR_7 )
510510
{ // FSK
511-
tSymbol = RegionCommonComputeSymbolTimeFsk( DataratesIN865[rxConfigParams->Datarate] );
511+
tSymbolInUs = RegionCommonComputeSymbolTimeFsk( DataratesIN865[rxConfigParams->Datarate] );
512512
}
513513
else
514514
{ // LoRa
515-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesIN865[rxConfigParams->Datarate], BandwidthsIN865[rxConfigParams->Datarate] );
515+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesIN865[rxConfigParams->Datarate], BandwidthsIN865[rxConfigParams->Datarate] );
516516
}
517517

518-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
518+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
519519
}
520520

521521
bool RegionIN865RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

src/mac/region/RegionKR920.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,15 @@ bool RegionKR920ChanMaskSet( ChanMaskSetParams_t* chanMaskSet )
494494

495495
void RegionKR920ComputeRxWindowParameters( int8_t datarate, uint8_t minRxSymbols, uint32_t rxError, RxConfigParams_t *rxConfigParams )
496496
{
497-
double tSymbol = 0.0;
497+
uint32_t tSymbolInUs = 0;
498498

499499
// Get the datarate, perform a boundary check
500500
rxConfigParams->Datarate = MIN( datarate, KR920_RX_MAX_DATARATE );
501501
rxConfigParams->Bandwidth = RegionCommonGetBandwidth( rxConfigParams->Datarate, BandwidthsKR920 );
502502

503-
tSymbol = RegionCommonComputeSymbolTimeLoRa( DataratesKR920[rxConfigParams->Datarate], BandwidthsKR920[rxConfigParams->Datarate] );
503+
tSymbolInUs = RegionCommonComputeSymbolTimeLoRa( DataratesKR920[rxConfigParams->Datarate], BandwidthsKR920[rxConfigParams->Datarate] );
504504

505-
RegionCommonComputeRxWindowParameters( tSymbol, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
505+
RegionCommonComputeRxWindowParameters( tSymbolInUs, minRxSymbols, rxError, Radio.GetWakeupTime( ), &rxConfigParams->WindowTimeout, &rxConfigParams->WindowOffset );
506506
}
507507

508508
bool RegionKR920RxConfig( RxConfigParams_t* rxConfig, int8_t* datarate )

0 commit comments

Comments
 (0)