Skip to content

Commit 7325b3e

Browse files
author
Hasnain Virk
committed
Changing double precision to float
We shouldn't use double precision as most of te embedded processors may not support it.
1 parent bf1244e commit 7325b3e

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

SX1272/SX1272_LoRaRadio.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ bool SX1272_LoRaRadio::check_rf_frequency(uint32_t frequency)
284284
void SX1272_LoRaRadio::set_channel(uint32_t freq)
285285
{
286286
_rf_settings.channel = freq;
287-
freq = (uint32_t) ((double) freq / (double) FREQ_STEP);
287+
freq = (uint32_t) ((float) freq / (float) FREQ_STEP);
288288
write_to_register(REG_FRFMSB, (uint8_t) ((freq >> 16) & 0xFF));
289289
write_to_register(REG_FRFMID, (uint8_t) ((freq >> 8) & 0xFF));
290290
write_to_register(REG_FRFLSB, (uint8_t) (freq & 0xFF));
@@ -416,7 +416,7 @@ void SX1272_LoRaRadio::set_rx_config(radio_modems_t modem, uint32_t bandwidth,
416416
_rf_settings.fsk.preamble_len = preamble_len;
417417
_rf_settings.fsk.rx_single_timeout = (symb_timeout + 1) / 2; // dividing by 2 as our detector size is 2 symbols (16 bytes)
418418

419-
datarate = (uint16_t) ((double) XTAL_FREQ / (double) datarate);
419+
datarate = (uint16_t) ((float) XTAL_FREQ / (float) datarate);
420420
write_to_register(REG_BITRATEMSB, (uint8_t) (datarate >> 8));
421421
write_to_register(REG_BITRATELSB, (uint8_t) (datarate & 0xFF));
422422

@@ -545,11 +545,11 @@ void SX1272_LoRaRadio::set_tx_config(radio_modems_t modem, int8_t power,
545545
_rf_settings.fsk.iq_inverted = iq_inverted;
546546
_rf_settings.fsk.tx_timeout = timeout;
547547

548-
fdev = (uint16_t) ((double) fdev / (double) FREQ_STEP);
548+
fdev = (uint16_t) ((float) fdev / (float) FREQ_STEP);
549549
write_to_register( REG_FDEVMSB, (uint8_t) (fdev >> 8));
550550
write_to_register( REG_FDEVLSB, (uint8_t) (fdev & 0xFF));
551551

552-
datarate = (uint16_t) ((double) XTAL_FREQ / (double) datarate);
552+
datarate = (uint16_t) ((float) XTAL_FREQ / (float) datarate);
553553
write_to_register( REG_BITRATEMSB, (uint8_t) (datarate >> 8));
554554
write_to_register( REG_BITRATELSB, (uint8_t) (datarate & 0xFF));
555555

@@ -656,48 +656,48 @@ uint32_t SX1272_LoRaRadio::time_on_air(radio_modems_t modem, uint8_t pkt_len)
656656
+ ((read_register( REG_SYNCCONFIG)
657657
& ~RF_SYNCCONFIG_SYNCSIZE_MASK) + 1)
658658
+ ((_rf_settings.fsk.fix_len == 0x01) ?
659-
0.0 : 1.0)
659+
0.0f : 1.0f)
660660
+ (((read_register( REG_PACKETCONFIG1)
661661
& ~RF_PACKETCONFIG1_ADDRSFILTERING_MASK)
662-
!= 0x00) ? 1.0 : 0) + pkt_len
662+
!= 0x00) ? 1.0f : 0) + pkt_len
663663
+ ((_rf_settings.fsk.crc_on == 0x01) ?
664-
2.0 : 0))
665-
/ _rf_settings.fsk.datarate) * 1e3);
664+
2.0f : 0))
665+
/ _rf_settings.fsk.datarate) * 1000);
666666
}
667667
break;
668668
case MODEM_LORA: {
669-
double bw = 0.0;
669+
float bw = 0.0f;
670670
switch (_rf_settings.lora.bandwidth) {
671671
case 0: // 125 kHz
672-
bw = 125e3;
672+
bw = 125000;
673673
break;
674674
case 1: // 250 kHz
675-
bw = 250e3;
675+
bw = 250000;
676676
break;
677677
case 2: // 500 kHz
678-
bw = 500e3;
678+
bw = 500000;
679679
break;
680680
}
681681

682682
// Symbol rate : time for one symbol (secs)
683-
double rs = bw / (1 << _rf_settings.lora.datarate);
684-
double ts = 1 / rs;
683+
float rs = bw / (1 << _rf_settings.lora.datarate);
684+
float ts = 1 / rs;
685685
// time of preamble
686-
double preamble_time = (_rf_settings.lora.preamble_len + 4.25) * ts;
686+
float preamble_time = (_rf_settings.lora.preamble_len + 4.25f) * ts;
687687
// Symbol length of payload and time
688-
double tmp = ceil((8 * pkt_len - 4 * _rf_settings.lora.datarate + 28
688+
float tmp = ceil((8 * pkt_len - 4 * _rf_settings.lora.datarate + 28
689689
+ 16 * _rf_settings.lora.crc_on -
690690
(_rf_settings.lora.fix_len ? 20 : 0))
691-
/ (double) (4 * (_rf_settings.lora.datarate -
691+
/ (float) (4 * (_rf_settings.lora.datarate -
692692
((_rf_settings.lora.low_datarate_optimize
693693
> 0) ? 2 : 0)))) *
694694
(_rf_settings.lora.coderate + 4);
695-
double n_payload = 8 + ((tmp > 0) ? tmp : 0);
696-
double t_payload = n_payload * ts;
695+
float n_payload = 8 + ((tmp > 0) ? tmp : 0);
696+
float t_payload = n_payload * ts;
697697
// Time on air
698-
double t_onair = preamble_time + t_payload;
698+
float t_onair = preamble_time + t_payload;
699699
// return ms secs
700-
airtime = floor(t_onair * 1e3 + 0.999);
700+
airtime = floor(t_onair * 1000 + 0.999f);
701701
}
702702
break;
703703
}
@@ -1735,9 +1735,9 @@ void SX1272_LoRaRadio::handle_dio0_irq()
17351735
// We can have a snapshot of RSSI here as at this point it
17361736
// should be more smoothed out.
17371737
_rf_settings.fsk_packet_handler.rssi_value = -(read_register(REG_RSSIVALUE) >> 1);
1738-
_rf_settings.fsk_packet_handler.afc_value = (int32_t)(double)(((uint16_t)read_register(REG_AFCMSB) << 8) |
1738+
_rf_settings.fsk_packet_handler.afc_value = (int32_t)(float)(((uint16_t)read_register(REG_AFCMSB) << 8) |
17391739
(uint16_t)read_register(REG_AFCLSB)) *
1740-
(double)FREQ_STEP;
1740+
(float)FREQ_STEP;
17411741
_rf_settings.fsk_packet_handler.rx_gain = (read_register(REG_LNA) >> 5) & 0x07;
17421742

17431743
// Read received packet size

SX1276/SX1276_LoRaRadio.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ uint8_t SX1276_LoRaRadio::get_status(void)
310310
void SX1276_LoRaRadio::set_channel(uint32_t freq)
311311
{
312312
_rf_settings.channel = freq;
313-
freq = (uint32_t) ((double) freq / (double) FREQ_STEP);
313+
freq = (uint32_t) ((float) freq / (float) FREQ_STEP);
314314
write_to_register(REG_FRFMSB, (uint8_t) ((freq >> 16) & 0xFF));
315315
write_to_register(REG_FRFMID, (uint8_t) ((freq >> 8) & 0xFF));
316316
write_to_register(REG_FRFLSB, (uint8_t) (freq & 0xFF));
@@ -389,7 +389,7 @@ void SX1276_LoRaRadio::set_rx_config(radio_modems_t modem, uint32_t bandwidth,
389389
_rf_settings.fsk.preamble_len = preamble_len;
390390
_rf_settings.fsk.rx_single_timeout = (symb_timeout + 1) / 2; // dividing by 2 as our detector size is 2 symbols (16 bytes)
391391

392-
datarate = (uint16_t) ((double) XTAL_FREQ / (double) datarate);
392+
datarate = (uint16_t) ((float) XTAL_FREQ / (float) datarate);
393393
write_to_register(REG_BITRATEMSB, (uint8_t) (datarate >> 8));
394394
write_to_register(REG_BITRATELSB, (uint8_t) (datarate & 0xFF));
395395

@@ -554,11 +554,11 @@ void SX1276_LoRaRadio::set_tx_config(radio_modems_t modem, int8_t power,
554554
_rf_settings.fsk.iq_inverted = iq_inverted;
555555
_rf_settings.fsk.tx_timeout = timeout;
556556

557-
fdev = (uint16_t) ((double) fdev / (double) FREQ_STEP);
557+
fdev = (uint16_t) ((float) fdev / (float) FREQ_STEP);
558558
write_to_register( REG_FDEVMSB, (uint8_t) (fdev >> 8));
559559
write_to_register( REG_FDEVLSB, (uint8_t) (fdev & 0xFF));
560560

561-
datarate = (uint16_t) ((double) XTAL_FREQ / (double) datarate);
561+
datarate = (uint16_t) ((float) XTAL_FREQ / (float) datarate);
562562
write_to_register( REG_BITRATEMSB, (uint8_t) (datarate >> 8));
563563
write_to_register( REG_BITRATELSB, (uint8_t) (datarate & 0xFF));
564564

@@ -667,17 +667,17 @@ uint32_t SX1276_LoRaRadio::time_on_air(radio_modems_t modem, uint8_t pkt_len)
667667
+ ((read_register( REG_SYNCCONFIG)
668668
& ~RF_SYNCCONFIG_SYNCSIZE_MASK) + 1)
669669
+ ((_rf_settings.fsk.fix_len == 0x01) ?
670-
0.0 : 1.0)
670+
0.0f : 1.0f)
671671
+ (((read_register( REG_PACKETCONFIG1)
672672
& ~RF_PACKETCONFIG1_ADDRSFILTERING_MASK)
673-
!= 0x00) ? 1.0 : 0) + pkt_len
673+
!= 0x00) ? 1.0f : 0) + pkt_len
674674
+ ((_rf_settings.fsk.crc_on == 0x01) ?
675675
2.0 : 0))
676-
/ _rf_settings.fsk.datarate) * 1e3);
676+
/ _rf_settings.fsk.datarate) * 1000);
677677

678678
break;
679679
case MODEM_LORA:
680-
double bw = 0.0;
680+
float bw = 0.0f;
681681
// REMARK: When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
682682
switch (_rf_settings.lora.bandwidth) {
683683
//case 0: // 7.8 kHz
@@ -702,36 +702,36 @@ uint32_t SX1276_LoRaRadio::time_on_air(radio_modems_t modem, uint8_t pkt_len)
702702
// bw = 625e2;
703703
// break;
704704
case 7: // 125 kHz
705-
bw = 125e3;
705+
bw = 125000;
706706
break;
707707
case 8: // 250 kHz
708-
bw = 250e3;
708+
bw = 250000;
709709
break;
710710
case 9: // 500 kHz
711-
bw = 500e3;
711+
bw = 500000;
712712
break;
713713
}
714714

715715
// Symbol rate : time for one symbol (secs)
716-
double rs = bw / (1 << _rf_settings.lora.datarate);
717-
double ts = 1 / rs;
716+
float rs = bw / (1 << _rf_settings.lora.datarate);
717+
float ts = 1 / rs;
718718
// time of preamble
719-
double tPreamble = (_rf_settings.lora.preamble_len + 4.25) * ts;
719+
float tPreamble = (_rf_settings.lora.preamble_len + 4.25f) * ts;
720720
// Symbol length of payload and time
721-
double tmp = ceil((8 * pkt_len - 4 * _rf_settings.lora.datarate + 28
721+
float tmp = ceil((8 * pkt_len - 4 * _rf_settings.lora.datarate + 28
722722
+ 16 * _rf_settings.lora.crc_on
723723
- (_rf_settings.lora.fix_len ? 20 : 0))
724-
/ (double) (4
724+
/ (float) (4
725725
* (_rf_settings.lora.datarate
726726
- ((_rf_settings.lora.low_datarate_optimize > 0)
727727
? 2 : 0))))
728728
* (_rf_settings.lora.coderate + 4);
729-
double nPayload = 8 + ((tmp > 0) ? tmp : 0);
730-
double tPayload = nPayload * ts;
729+
float nPayload = 8 + ((tmp > 0) ? tmp : 0);
730+
float tPayload = nPayload * ts;
731731
// Time on air
732-
double tOnAir = tPreamble + tPayload;
732+
float tOnAir = tPreamble + tPayload;
733733
// return ms secs
734-
airTime = floor(tOnAir * 1e3 + 0.999);
734+
airTime = floor(tOnAir * 1000 + 0.999f);
735735

736736
break;
737737
}
@@ -1054,7 +1054,6 @@ void SX1276_LoRaRadio::set_public_network(bool enable)
10541054
// Change lora modem SyncWord
10551055
write_to_register(REG_LR_SYNCWORD, LORA_MAC_PRIVATE_SYNCWORD);
10561056
}
1057-
10581057
}
10591058

10601059
/**
@@ -1134,7 +1133,7 @@ void SX1276_LoRaRadio::set_tx_continuous_wave(uint32_t freq, int8_t power,
11341133
write_to_register( REG_DIOMAPPING2, RF_DIOMAPPING2_DIO4_10 | RF_DIOMAPPING2_DIO5_10 );
11351134

11361135
_rf_settings.state = RF_TX_RUNNING;
1137-
tx_timeout_timer.attach_us(callback(this, &SX1276_LoRaRadio::timeout_irq_isr), time*1e3);
1136+
tx_timeout_timer.attach_us(callback(this, &SX1276_LoRaRadio::timeout_irq_isr), time * 1000);
11381137
set_operation_mode(RF_OPMODE_TRANSMITTER);
11391138
}
11401139

@@ -1386,12 +1385,12 @@ void SX1276_LoRaRadio::rx_chain_calibration(void)
13861385

13871386
// Save context
13881387
regPaConfigInitVal = read_register( REG_PACONFIG );
1389-
initialFreq = ( double )( ( ( uint32_t )this->read_register( REG_FRFMSB ) << 16 ) |
1390-
( ( uint32_t )this->read_register( REG_FRFMID ) << 8 ) |
1391-
( ( uint32_t )this->read_register( REG_FRFLSB ) ) ) * ( double )FREQ_STEP;
1388+
initialFreq = (float) (((uint32_t) this->read_register(REG_FRFMSB) << 16) |
1389+
((uint32_t) this->read_register(REG_FRFMID) << 8 ) |
1390+
((uint32_t)this->read_register(REG_FRFLSB))) * (float) FREQ_STEP;
13921391

13931392
// Cut the PA just in case, RFO output, power = -1 dBm
1394-
write_to_register( REG_PACONFIG, 0x00 );
1393+
write_to_register(REG_PACONFIG, 0x00);
13951394

13961395
// Launch Rx chain calibration for LF band
13971396
write_to_register (REG_IMAGECAL, (read_register(REG_IMAGECAL)
@@ -1582,7 +1581,7 @@ void SX1276_LoRaRadio::transmit(uint32_t timeout)
15821581

15831582
_rf_settings.state = RF_TX_RUNNING;
15841583
tx_timeout_timer.attach_us(callback(this,
1585-
&SX1276_LoRaRadio::timeout_irq_isr), timeout*1e3);
1584+
&SX1276_LoRaRadio::timeout_irq_isr), timeout * 1000);
15861585
set_operation_mode(RF_OPMODE_TRANSMITTER);
15871586
}
15881587

@@ -1870,9 +1869,9 @@ void SX1276_LoRaRadio::handle_dio0_irq()
18701869
-(read_register(REG_RSSIVALUE) >> 1);
18711870

18721871
_rf_settings.fsk_packet_handler.afc_value =
1873-
(int32_t) (double) (((uint16_t) read_register(REG_AFCMSB) << 8)
1872+
(int32_t) (float) (((uint16_t) read_register(REG_AFCMSB) << 8)
18741873
| (uint16_t) read_register( REG_AFCLSB))
1875-
* (double) FREQ_STEP;
1874+
* (float) FREQ_STEP;
18761875
_rf_settings.fsk_packet_handler.rx_gain =
18771876
(read_register( REG_LNA) >> 5) & 0x07;
18781877

0 commit comments

Comments
 (0)