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 <aurelien@aurel32.net>
This commit is contained in:
Aurelien Jarno 2024-05-08 16:52:07 +02:00 committed by Anas Nashif
commit 99804c77c4

View file

@ -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 = {