From 7f9d6a02d34954d216c0e1e2a38ade7300ae1540 Mon Sep 17 00:00:00 2001 From: Armando Visconti Date: Thu, 3 Jul 2025 15:24:48 +0200 Subject: [PATCH] drivers/sensor/: lis2dux12: fix ODR setting In lis2dux12_freq_to_odr_val, the loop through the array of possible ODR frequencies can break sooner than expected if power-mode is set to High Performance mode and the requested ODR is less than or equal to 25Hz. Moreover, move the "odr |= 0x10" statement used for HP mode in the chip_api set_odr_raw() API, so that we enter the HP mode even when the ODR is set from DT only. Signed-off-by: Armando Visconti --- drivers/sensor/st/lis2dux12/lis2dux12.c | 22 ++++++++++++-------- drivers/sensor/st/lis2dux12/lis2dux12_api.c | 14 ++++++++++++- drivers/sensor/st/lis2dux12/lis2duxs12_api.c | 14 ++++++++++++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/drivers/sensor/st/lis2dux12/lis2dux12.c b/drivers/sensor/st/lis2dux12/lis2dux12.c index 3d4e93eef6797..dbd1270310c52 100644 --- a/drivers/sensor/st/lis2dux12/lis2dux12.c +++ b/drivers/sensor/st/lis2dux12/lis2dux12.c @@ -55,6 +55,19 @@ static int lis2dux12_freq_to_odr_val(const struct device *dev, uint16_t freq) int odr; for (odr = LIS2DUX12_DT_ODR_OFF; odr < LIS2DUX12_DT_ODR_END; odr++) { + /* + * In case power-mode is HP, skip the ULP odrs in order to + * avoid to erroneously break the loop sooner than expected. + * In HP mode the correct ODRs must be found from + * LIS2DUX12_DT_ODR_6Hz on. + */ + if ((cfg->pm == LIS2DUX12_OPER_MODE_HIGH_PERFORMANCE) && + ((odr == LIS2DUX12_DT_ODR_1Hz_ULP) || + (odr == LIS2DUX12_DT_ODR_3Hz_ULP) || + (odr == LIS2DUX12_DT_ODR_25Hz_ULP))) { + continue; + } + if (freq <= lis2dux12_odr_map[odr]) { break; } @@ -69,15 +82,6 @@ static int lis2dux12_freq_to_odr_val(const struct device *dev, uint16_t freq) return LIS2DUX12_DT_ODR_OFF; } - /* handle high performance mode */ - if (cfg->pm == LIS2DUX12_OPER_MODE_HIGH_PERFORMANCE) { - if (odr < LIS2DUX12_DT_ODR_6Hz) { - odr = LIS2DUX12_DT_ODR_6Hz; - } - - odr |= 0x10; - } - return odr; } diff --git a/drivers/sensor/st/lis2dux12/lis2dux12_api.c b/drivers/sensor/st/lis2dux12/lis2dux12_api.c index 53b85157d0095..c42225865deab 100644 --- a/drivers/sensor/st/lis2dux12/lis2dux12_api.c +++ b/drivers/sensor/st/lis2dux12/lis2dux12_api.c @@ -17,7 +17,19 @@ static int32_t st_lis2dux12_set_odr_raw(const struct device *dev, uint8_t odr) struct lis2dux12_data *data = dev->data; const struct lis2dux12_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; - lis2dux12_md_t mode = {.odr = odr, .fs = data->range}; + lis2dux12_md_t mode; + + /* handle high performance mode */ + if (cfg->pm == LIS2DUX12_OPER_MODE_HIGH_PERFORMANCE) { + if (odr < LIS2DUX12_DT_ODR_6Hz) { + odr = LIS2DUX12_DT_ODR_6Hz; + } + + odr |= 0x10; + } + + mode.odr = odr; + mode.fs = data->range; data->odr = odr; return lis2dux12_mode_set(ctx, &mode); diff --git a/drivers/sensor/st/lis2dux12/lis2duxs12_api.c b/drivers/sensor/st/lis2dux12/lis2duxs12_api.c index 6418d1c7b7c29..7f4e272bbf3a8 100644 --- a/drivers/sensor/st/lis2dux12/lis2duxs12_api.c +++ b/drivers/sensor/st/lis2dux12/lis2duxs12_api.c @@ -17,7 +17,19 @@ static int32_t st_lis2duxs12_set_odr_raw(const struct device *dev, uint8_t odr) struct lis2dux12_data *data = dev->data; const struct lis2dux12_config *cfg = dev->config; stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx; - lis2duxs12_md_t mode = {.odr = odr, .fs = data->range}; + lis2duxs12_md_t mode; + + /* handle high performance mode */ + if (cfg->pm == LIS2DUX12_OPER_MODE_HIGH_PERFORMANCE) { + if (odr < LIS2DUX12_DT_ODR_6Hz) { + odr = LIS2DUX12_DT_ODR_6Hz; + } + + odr |= 0x10; + } + + mode.odr = odr; + mode.fs = data->range; data->odr = odr; return lis2duxs12_mode_set(ctx, &mode);