From 63ab20056705fc07fbcbb8ab4e4280ffb7e2a8af Mon Sep 17 00:00:00 2001 From: Jeppe Odgaard Date: Fri, 17 Oct 2025 12:37:34 +0200 Subject: [PATCH] drivers: sensor: qdec_stm32: check counts/revoultion compile time Use build assert to check counts_per_revolution DTS value compile time to prevent runtime failure and also decrease flash usage slightly. Signed-off-by: Jeppe Odgaard --- drivers/sensor/st/qdec_stm32/qdec_stm32.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/sensor/st/qdec_stm32/qdec_stm32.c b/drivers/sensor/st/qdec_stm32/qdec_stm32.c index d3ed5b4b1f2e9..01bddb810a4ec 100644 --- a/drivers/sensor/st/qdec_stm32/qdec_stm32.c +++ b/drivers/sensor/st/qdec_stm32/qdec_stm32.c @@ -114,12 +114,6 @@ static int qdec_stm32_initialize(const struct device *dev) return retval; } - if (dev_cfg->counts_per_revolution < 1) { - LOG_ERR("Invalid number of counts per revolution (%d)", - dev_cfg->counts_per_revolution); - return -EINVAL; - } - /* Ensure that the counter will always count up to a multiple of counts_per_revolution */ if (IS_TIM_32B_COUNTER_INSTANCE(dev_cfg->timer_inst)) { max_counter_value = UINT32_MAX - (UINT32_MAX % dev_cfg->counts_per_revolution) - 1; @@ -146,6 +140,9 @@ static DEVICE_API(sensor, qdec_stm32_driver_api) = { }; #define QDEC_STM32_INIT(n) \ + BUILD_ASSERT(DT_INST_PROP(n, st_counts_per_revolution) > 0, \ + "Counts per revolution must be above 0"); \ + \ BUILD_ASSERT(!(DT_INST_PROP(n, st_encoder_mode) & ~TIM_SMCR_SMS), \ "Encoder mode is not supported by this MCU"); \ \