Skip to content

Commit

Permalink
drivers: sensor: LIS3MDL: make die temperature reading optional
Browse files Browse the repository at this point in the history
The LIS3MDL sensor driver is slowed down by an additional
transaction to fetch the die temperature. This commit allowes to
enable/disable this via a KConfig option. Default is enables in
order to preserve backwards compatability.

Signed-off-by: De Murlot <demurlotpierre@gmail.com>
  • Loading branch information
DeMurlot committed Sep 16, 2024
1 parent 6fc525c commit e3f8ba0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions drivers/sensor/st/lis3mdl/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ menuconfig LIS3MDL

if LIS3MDL

config LIS3MDL_DIE_TEMP_EN
bool "Enable die temperature sensor"
default y

choice LIS3MDL_TRIGGER_MODE
prompt "Trigger mode"
default LIS3MDL_TRIGGER_GLOBAL_THREAD
Expand Down
18 changes: 14 additions & 4 deletions drivers/sensor/st/lis3mdl/lis3mdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ static int lis3mdl_channel_get(const struct device *dev,
} else if (chan == SENSOR_CHAN_MAGN_Z) {
lis3mdl_convert(val, drv_data->z_sample,
lis3mdl_magn_gain[LIS3MDL_FS_IDX]);
#ifdef CONFIG_LIS3MDL_DIE_TEMP_EN
} else if (chan == SENSOR_CHAN_DIE_TEMP) {
/* temp_val = 25 + sample / 8 */
lis3mdl_convert(val, drv_data->temp_sample, 8);
val->val1 += 25;
#endif /*CONFIG_LIS3MDL_DIE_TEMP_EN*/
} else {
return -ENOTSUP;
}
Expand All @@ -63,18 +65,22 @@ static int lis3mdl_channel_get(const struct device *dev,
int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan)
{
struct lis3mdl_data *drv_data = dev->data;
const struct lis3mdl_config *config = dev->config;
#ifdef CONFIG_LIS3MDL_DIE_TEMP_EN
int16_t buf[4];
#else /*CONFIG_LIS3MDL_DIE_TEMP_EN*/
int16_t buf[3];
#endif /*CONFIG_LIS3MDL_DIE_TEMP_EN*/

__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

/* fetch magnetometer sample */
if (i2c_burst_read_dt(&config->i2c, LIS3MDL_REG_SAMPLE_START,
(uint8_t *)buf, 8) < 0) {
(uint8_t *)buf, 6) < 0) {
LOG_DBG("Failed to fetch magnetometer sample.");
return -EIO;
}

#ifdef CONFIG_LIS3MDL_DIE_TEMP_EN
/*
* the chip doesn't allow fetching temperature data in
* the same read as magnetometer data, so do another
Expand All @@ -85,11 +91,12 @@ int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan)
LOG_DBG("Failed to fetch temperature sample.");
return -EIO;
}
drv_data->temp_sample = sys_le16_to_cpu(buf[3]);
#endif /*CONFIG_LIS3MDL_DIE_TEMP_EN*/

drv_data->x_sample = sys_le16_to_cpu(buf[0]);
drv_data->y_sample = sys_le16_to_cpu(buf[1]);
drv_data->z_sample = sys_le16_to_cpu(buf[2]);
drv_data->temp_sample = sys_le16_to_cpu(buf[3]);

return 0;
}
Expand Down Expand Up @@ -138,7 +145,10 @@ int lis3mdl_init(const struct device *dev)

/* Configure sensor */
chip_cfg[0] = LIS3MDL_REG_CTRL1;
chip_cfg[1] = LIS3MDL_TEMP_EN_MASK | lis3mdl_odr_bits[idx];
chip_cfg[1] = lis3mdl_odr_bits[idx];
#ifdef CONFIG_LIS3MDL_DIE_TEMP_EN
chip_cfg[1] |= LIS3MDL_TEMP_EN_MASK;
#endif /*LIS3MDL_DIE_TEMP_EN*/
chip_cfg[2] = LIS3MDL_FS_IDX << LIS3MDL_FS_SHIFT;
chip_cfg[3] = LIS3MDL_MD_CONTINUOUS;
chip_cfg[4] = ((lis3mdl_odr_bits[idx] & LIS3MDL_OM_MASK) >>
Expand Down
6 changes: 4 additions & 2 deletions drivers/sensor/st/lis3mdl/lis3mdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ struct lis3mdl_data {
int16_t x_sample;
int16_t y_sample;
int16_t z_sample;
#ifdef CONFIG_LIS3MDL_DIE_TEMP_EN
int16_t temp_sample;
#endif /*CONFIG_LIS3MDL_DIE_TEMP_EN*/

#ifdef CONFIG_LIS3MDL_TRIGGER
const struct device *dev;
Expand All @@ -137,7 +139,7 @@ struct lis3mdl_config {
struct i2c_dt_spec i2c;
#ifdef CONFIG_LIS3MDL_TRIGGER
struct gpio_dt_spec irq_gpio;
#endif
#endif /*CONFIG_LIS3MDL_TRIGGER*/
};

#ifdef CONFIG_LIS3MDL_TRIGGER
Expand All @@ -148,6 +150,6 @@ int lis3mdl_trigger_set(const struct device *dev,
int lis3mdl_sample_fetch(const struct device *dev, enum sensor_channel chan);

int lis3mdl_init_interrupt(const struct device *dev);
#endif
#endif /*CONFIG_LIS3MDL_TRIGGER*/

#endif /* __SENSOR_LIS3MDL__ */

0 comments on commit e3f8ba0

Please sign in to comment.