1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

driver, lsm6dsl: fix temperature reading

This commit is contained in:
smlng 2017-07-02 11:28:15 +02:00
parent ed227c7d34
commit a7d9547d5e
3 changed files with 24 additions and 12 deletions

View File

@ -132,8 +132,12 @@ int lsm6dsl_read_gyro(const lsm6dsl_t *dev, lsm6dsl_3d_data_t *data);
/**
* @brief Read temperature data
*
* @note To avoid floating point data types but still provide high resolution
* for temperature readings, resulting values are scale by factor 100.
*
*
* @param[in] dev device to read
* @param[out] data temperature value
* @param[out] data temperature value, in °C x 100
*
* @return 0 on success
* @return < 0 on error

View File

@ -149,6 +149,11 @@ extern "C" {
#define LSM6DSL_FIFO_CTRL3_GYRO_DEC_SHIFT (3)
/** @} */
/**
* @brief Offset for temperature calculation
*/
#define LSM6DSL_TEMP_OFFSET (0x1900)
#ifdef __cplusplus
}
#endif

View File

@ -198,21 +198,24 @@ int lsm6dsl_read_gyro(const lsm6dsl_t *dev, lsm6dsl_3d_data_t *data)
int lsm6dsl_read_temp(const lsm6dsl_t *dev, int16_t *data)
{
int res;
uint8_t tmp;
uint16_t traw;
/* read raw temperature */
i2c_acquire(dev->params.i2c);
res = i2c_read_reg(dev->params.i2c, dev->params.addr, LSM6DSL_REG_OUT_TEMP_L, &tmp);
*data = tmp;
res += i2c_read_reg(dev->params.i2c, dev->params.addr, LSM6DSL_REG_OUT_TEMP_H, &tmp);
*data |= tmp << 8;
if (i2c_read_reg(dev->params.i2c, dev->params.addr, LSM6DSL_REG_OUT_TEMP_L, &tmp) != 1) {
i2c_release(dev->params.i2c);
if (res < 2) {
return -1;
}
traw = tmp;
if (i2c_read_reg(dev->params.i2c, dev->params.addr, LSM6DSL_REG_OUT_TEMP_H, &tmp) != 1) {
i2c_release(dev->params.i2c);
return -1;
}
traw |= (uint16_t)tmp << 8;
i2c_release(dev->params.i2c);
/* convert temperature */
traw += LSM6DSL_TEMP_OFFSET;
*data = (int16_t)(((int32_t)traw * 100) / 256);
return 0;
}