Skip to content

Commit

Permalink
drivers/lpsxxx: avoid float arithmetics
Browse files Browse the repository at this point in the history
Fixes #17486
  • Loading branch information
maribu committed May 19, 2023
1 parent 19ce68d commit dcb49cb
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions drivers/lpsxxx/lpsxxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "periph/i2c.h"
#include "lpsxxx.h"
#include "lpsxxx_internal.h"
#include "macros/math.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand All @@ -41,12 +42,14 @@

/**
* @brief temperature base value and divider for norming temperature output
*
* @details temperature base is given in centi-degree-celsius
*/
#if MODULE_LPS331AP || MODULE_LPS25HB
#define TEMP_BASE (42.5f)
#define TEMP_BASE (4250U) /* = 42.5 C */
#define TEMP_DIVIDER (480U)
#else
#define TEMP_BASE (0.0f)
#define TEMP_BASE (0U)
#define TEMP_DIVIDER (100U)
#endif

Expand Down Expand Up @@ -128,8 +131,8 @@ int lpsxxx_init(lpsxxx_t *dev, const lpsxxx_params_t * params)
int lpsxxx_read_temp(const lpsxxx_t *dev, int16_t *temp)
{
uint8_t tmp;
int16_t val = 0;
float res = TEMP_BASE; /* reference value -> see datasheet */
int32_t val = 0;
uint16_t res = TEMP_BASE; /* reference value -> see datasheet */

i2c_acquire(DEV_I2C);
if (i2c_read_reg(DEV_I2C, DEV_ADDR, LPSXXX_REG_TEMP_OUT_L, &tmp, 0) < 0) {
Expand All @@ -147,13 +150,15 @@ int lpsxxx_read_temp(const lpsxxx_t *dev, int16_t *temp)
i2c_release(DEV_I2C);
val |= ((uint16_t)tmp << 8);

DEBUG("[lpsxxx] read_temp: raw data %08" PRIx32 "\n", (uint32_t)val);
DEBUG("[lpsxxx] read_temp: raw data %08" PRIx32 "\n", val);

/* convert val to c°C */
val *= 100;

/* compute actual temperature value in °C */
res += ((float)val) / TEMP_DIVIDER;
/* compute actual temperature value in c°C */
res += DIV_ROUND(val, TEMP_DIVIDER);

/* return temperature in c°C */
*temp = (int16_t)(res * 100);
*temp = res;
return LPSXXX_OK;
}

Expand Down

0 comments on commit dcb49cb

Please sign in to comment.