From c9e7ca3cac8ccf924cfba53fa454fca78a62969c Mon Sep 17 00:00:00 2001 From: Wouter Cappelle Date: Mon, 10 Jan 2022 17:38:20 +0100 Subject: [PATCH] sensors: STM32: Prepare for different handling of temp conversions moving the conversion from adc value to the get function which will be used for different handling of stm32 temp sensors Signed-off-by: Wouter Cappelle --- drivers/sensor/stm32_temp/stm32_temp.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/sensor/stm32_temp/stm32_temp.c b/drivers/sensor/stm32_temp/stm32_temp.c index d4c47c40439..697f71e9fa8 100644 --- a/drivers/sensor/stm32_temp/stm32_temp.c +++ b/drivers/sensor/stm32_temp/stm32_temp.c @@ -20,7 +20,7 @@ struct stm32_temp_data { struct adc_sequence adc_seq; struct k_mutex mutex; int16_t sample_buffer; - int32_t mv; /* Sensor value in millivolts */ + int16_t raw; /* raw adc Sensor value */ }; struct stm32_temp_config { @@ -33,7 +33,6 @@ struct stm32_temp_config { static int stm32_temp_sample_fetch(const struct device *dev, enum sensor_channel chan) { - const struct stm32_temp_config *cfg = dev->config; struct stm32_temp_data *data = dev->data; struct adc_sequence *sp = &data->adc_seq; int rc; @@ -47,7 +46,7 @@ static int stm32_temp_sample_fetch(const struct device *dev, rc = adc_read(data->adc, sp); sp->calibrate = false; if (rc == 0) { - data->mv = data->sample_buffer * cfg->tsv_mv / 0x0FFF; + data->raw = data->sample_buffer; } k_mutex_unlock(&data->mutex); @@ -67,10 +66,12 @@ static int stm32_temp_channel_get(const struct device *dev, return -ENOTSUP; } + int32_t mv = data->raw * cfg->tsv_mv / 0x0FFF; /* Sensor value in millivolts */ + if (cfg->is_ntc) { - temp = (float)(cfg->v25_mv - data->mv); + temp = (float)(cfg->v25_mv - mv); } else { - temp = (float)(data->mv - cfg->v25_mv); + temp = (float)(mv - cfg->v25_mv); } temp = (temp/cfg->avgslope)*10; temp += 25;