drivers: adc: stm32: move internal path setting to sensor drivers

On some STM32 families (such as F4), temperature and Vbat sensor share the
same ADC channel, which can lead to conflict when reading them, and wrong
measurement can follow.

To alleviate this problem, this commit moves the setting of the common
path internal channel to the sensor drivers themselves instead of doing
it in the ADC driver.

The teardown is still done in the ADC driver, systematically, instead of
channel by channel (which has the same result).

By moving this logic in the sensor drivers, the properties temp-channel,
vbat-channel and vref-channel becomes useless and are thus removed.

Signed-off-by: Guillaume Gautier <guillaume.gautier-ext@st.com>
This commit is contained in:
Guillaume Gautier 2023-09-06 15:06:04 +02:00 committed by Carles Cufí
commit a1adc17b31
4 changed files with 27 additions and 96 deletions

View file

@ -9,6 +9,7 @@
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/logging/log.h>
#include <stm32_ll_adc.h>
LOG_MODULE_REGISTER(stm32_vbat, CONFIG_SENSOR_LOG_LEVEL);
@ -21,6 +22,7 @@ LOG_MODULE_REGISTER(stm32_vbat, CONFIG_SENSOR_LOG_LEVEL);
struct stm32_vbat_data {
const struct device *adc;
const struct adc_channel_cfg adc_cfg;
ADC_TypeDef *adc_base;
struct adc_sequence adc_seq;
struct k_mutex mutex;
int16_t sample_buffer;
@ -50,6 +52,9 @@ static int stm32_vbat_sample_fetch(const struct device *dev, enum sensor_channel
goto unlock;
}
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(data->adc_base),
LL_ADC_PATH_INTERNAL_VBAT);
rc = adc_read(data->adc, sp);
if (rc == 0) {
data->raw = data->sample_buffer;
@ -110,6 +115,7 @@ static int stm32_vbat_init(const struct device *dev)
#define STM32_VBAT_DEFINE(inst) \
static struct stm32_vbat_data stm32_vbat_dev_data_##inst = { \
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)), \
.adc_base = (ADC_TypeDef *)DT_REG_ADDR(DT_INST_IO_CHANNELS_CTLR(0)), \
.adc_cfg = { \
.gain = ADC_GAIN_1, \
.reference = ADC_REF_INTERNAL, \