1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19609: drivers/lpsxxx: avoid float arithmetics r=aabadie a=maribu

### Contribution description

Perform computation directly in centi-degree-celsius to avoid floating point arithmetics. In addition, use scientific rounding in the division.


Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
This commit is contained in:
bors[bot] 2023-05-19 07:49:57 +00:00 committed by GitHub
commit 2327d74d24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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"
@ -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
@ -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) {
@ -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);
/* compute actual temperature value in °C */
res += ((float)val) / TEMP_DIVIDER;
/* convert val to c°C */
val *= 100;
/* return temperature in c°C */
*temp = (int16_t)(res * 100);
/* compute actual temperature value in c°C */
res += DIV_ROUND(val, TEMP_DIVIDER);
*temp = res;
return LPSXXX_OK;
}