mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge #19609
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:
commit
2327d74d24
@ -30,6 +30,7 @@
|
|||||||
#include "periph/i2c.h"
|
#include "periph/i2c.h"
|
||||||
#include "lpsxxx.h"
|
#include "lpsxxx.h"
|
||||||
#include "lpsxxx_internal.h"
|
#include "lpsxxx_internal.h"
|
||||||
|
#include "macros/math.h"
|
||||||
|
|
||||||
#define ENABLE_DEBUG 0
|
#define ENABLE_DEBUG 0
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
@ -41,12 +42,14 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief temperature base value and divider for norming temperature output
|
* @brief temperature base value and divider for norming temperature output
|
||||||
|
*
|
||||||
|
* @details temperature base is given in centi-degree-celsius
|
||||||
*/
|
*/
|
||||||
#if MODULE_LPS331AP || MODULE_LPS25HB
|
#if MODULE_LPS331AP || MODULE_LPS25HB
|
||||||
#define TEMP_BASE (42.5f)
|
#define TEMP_BASE (4250U) /* = 42.5 C */
|
||||||
#define TEMP_DIVIDER (480U)
|
#define TEMP_DIVIDER (480U)
|
||||||
#else
|
#else
|
||||||
#define TEMP_BASE (0.0f)
|
#define TEMP_BASE (0U)
|
||||||
#define TEMP_DIVIDER (100U)
|
#define TEMP_DIVIDER (100U)
|
||||||
#endif
|
#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)
|
int lpsxxx_read_temp(const lpsxxx_t *dev, int16_t *temp)
|
||||||
{
|
{
|
||||||
uint8_t tmp;
|
uint8_t tmp;
|
||||||
int16_t val = 0;
|
int32_t val = 0;
|
||||||
float res = TEMP_BASE; /* reference value -> see datasheet */
|
uint16_t res = TEMP_BASE; /* reference value -> see datasheet */
|
||||||
|
|
||||||
i2c_acquire(DEV_I2C);
|
i2c_acquire(DEV_I2C);
|
||||||
if (i2c_read_reg(DEV_I2C, DEV_ADDR, LPSXXX_REG_TEMP_OUT_L, &tmp, 0) < 0) {
|
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);
|
i2c_release(DEV_I2C);
|
||||||
val |= ((uint16_t)tmp << 8);
|
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 */
|
/* convert val to c°C */
|
||||||
res += ((float)val) / TEMP_DIVIDER;
|
val *= 100;
|
||||||
|
|
||||||
/* return temperature in c°C */
|
/* compute actual temperature value in c°C */
|
||||||
*temp = (int16_t)(res * 100);
|
res += DIV_ROUND(val, TEMP_DIVIDER);
|
||||||
|
|
||||||
|
*temp = res;
|
||||||
return LPSXXX_OK;
|
return LPSXXX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user