Skip to content

Commit

Permalink
drivers/sx127x: reduce use of floats
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Jun 12, 2023
1 parent 35d3896 commit 826095d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
9 changes: 5 additions & 4 deletions drivers/sx127x/sx127x_getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ void sx127x_set_syncword(sx127x_t *dev, uint8_t syncword)

uint32_t sx127x_get_channel(const sx127x_t *dev)
{
return (((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMSB) << 16) |
(sx127x_reg_read(dev, SX127X_REG_FRFMID) << 8) |
(sx127x_reg_read(dev, SX127X_REG_FRFLSB))) * LORA_FREQUENCY_RESOLUTION_DEFAULT;
uint32_t raw = ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMSB) << 16)
| ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMID) << 8)
| ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFLSB));
return (uint64_t)raw * LORA_FREQUENCY_RESOLUTION_NANOHERTZ_DEFAULT / 1000000000U;
}

void sx127x_set_channel(sx127x_t *dev, uint32_t channel)
Expand All @@ -129,7 +130,7 @@ void sx127x_set_channel(sx127x_t *dev, uint32_t channel)
/* Save current operating mode */
dev->settings.channel = channel;

channel = (uint32_t)((double)channel / (double)LORA_FREQUENCY_RESOLUTION_DEFAULT);
channel = (uint64_t)channel * 1000000000U / LORA_FREQUENCY_RESOLUTION_NANOHERTZ_DEFAULT;

/* Write frequency settings into chip */
sx127x_reg_write(dev, SX127X_REG_FRFMSB, (uint8_t)((channel >> 16) & 0xFF));
Expand Down
9 changes: 4 additions & 5 deletions drivers/sx127x/sx127x_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ void sx1276_rx_chain_calibration(sx127x_t *dev)

/* Save context */
reg_pa_config_init_val = sx127x_reg_read(dev, SX127X_REG_PACONFIG);
initial_freq = (double)(((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMSB) << 16)
| ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMID) << 8)
| ((uint32_t)sx127x_reg_read(dev,
SX127X_REG_FRFLSB))) *
(double)LORA_FREQUENCY_RESOLUTION_DEFAULT;
initial_freq = ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMSB) << 16)
| ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFMID) << 8)
| ((uint32_t)sx127x_reg_read(dev, SX127X_REG_FRFLSB));
initial_freq = (uint64_t)initial_freq * LORA_FREQUENCY_RESOLUTION_NANOHERTZ_DEFAULT / 1000000000U;

/* Cut the PA just in case, RFO output, power = -1 dBm */
sx127x_reg_write(dev, SX127X_REG_PACONFIG, 0x00);
Expand Down
10 changes: 7 additions & 3 deletions sys/include/net/lora.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ extern "C" {
* @ingroup config
* @{
*/
/** @brief Frequency resolution in Hz */
#ifndef LORA_FREQUENCY_RESOLUTION_DEFAULT
#define LORA_FREQUENCY_RESOLUTION_DEFAULT (61.03515625)
#ifndef LORA_FREQUENCY_RESOLUTION_NANOHERTZ_DEFAULT
/**
* @brief Frequency resolution in nano Hz
*
* This is the same as `(32 * 10^15) >> 19`
*/
#define LORA_FREQUENCY_RESOLUTION_NANOHERTZ_DEFAULT 61035156250
#endif

/** @brief Preamble length, same for Tx and Rx
Expand Down

0 comments on commit 826095d

Please sign in to comment.