From 99804c77c4d8e0f60f398718b0c78844d50f0470 Mon Sep 17 00:00:00 2001 From: Aurelien Jarno Date: Wed, 8 May 2024 16:52:07 +0200 Subject: [PATCH] drivers: sensor: stm32_vbat: get rid of floating point computation Instead of using floating point operations to compute the vbat voltage, it is possible to do the computation using 32-bit variables by reordering operations and using the sensor_value_from_milli() function. On a STM32G0, this saves 140 bytes of flash, excluding the FP library needed on a FPU less MCU. Signed-off-by: Aurelien Jarno --- drivers/sensor/st/stm32_vbat/stm32_vbat.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/sensor/st/stm32_vbat/stm32_vbat.c b/drivers/sensor/st/stm32_vbat/stm32_vbat.c index 8379670fab3..84511c1a4cc 100644 --- a/drivers/sensor/st/stm32_vbat/stm32_vbat.c +++ b/drivers/sensor/st/stm32_vbat/stm32_vbat.c @@ -77,18 +77,16 @@ static int stm32_vbat_channel_get(const struct device *dev, enum sensor_channel { struct stm32_vbat_data *data = dev->data; const struct stm32_vbat_config *cfg = dev->config; - float voltage; + int32_t voltage; if (chan != SENSOR_CHAN_VOLTAGE) { return -ENOTSUP; } - /* Sensor value in millivolts */ - voltage = data->raw * adc_ref_internal(data->adc) / 0x0FFF; - /* considering the vbat input through a resistor bridge */ - voltage = voltage * cfg->ratio / 1000; /* value of SENSOR_CHAN_VOLTAGE in Volt */ + /* Sensor value in millivolts considering the vbat input through a resistor bridge */ + voltage = data->raw * adc_ref_internal(data->adc) * cfg->ratio / 0x0FFF; - return sensor_value_from_double(val, voltage); + return sensor_value_from_milli(val, voltage); } static const struct sensor_driver_api stm32_vbat_driver_api = {