diff --git a/drivers/sensor/stm32_temp/stm32_temp.c b/drivers/sensor/stm32_temp/stm32_temp.c index 7d267aad894..fc2b12bb45a 100644 --- a/drivers/sensor/stm32_temp/stm32_temp.c +++ b/drivers/sensor/stm32_temp/stm32_temp.c @@ -24,8 +24,7 @@ LOG_MODULE_REGISTER(stm32_temp, CONFIG_SENSOR_LOG_LEVEL); struct stm32_temp_data { const struct device *adc; - uint8_t channel; - struct adc_channel_cfg adc_cfg; + const struct adc_channel_cfg adc_cfg; struct adc_sequence adc_seq; struct k_mutex mutex; int16_t sample_buffer; @@ -60,14 +59,21 @@ static int stm32_temp_sample_fetch(const struct device *dev, enum sensor_channel k_mutex_lock(&data->mutex, K_FOREVER); + rc = adc_channel_setup(data->adc, &data->adc_cfg); + if (rc) { + LOG_DBG("Setup AIN%u got %d", data->adc_cfg.channel_id, rc); + goto unlock; + } + rc = adc_read(data->adc, sp); if (rc == 0) { data->raw = data->sample_buffer; } +unlock: k_mutex_unlock(&data->mutex); - return 0; + return rc; } static int stm32_temp_channel_get(const struct device *dev, enum sensor_channel chan, @@ -110,9 +116,7 @@ static const struct sensor_driver_api stm32_temp_driver_api = { static int stm32_temp_init(const struct device *dev) { struct stm32_temp_data *data = dev->data; - struct adc_channel_cfg *accp = &data->adc_cfg; struct adc_sequence *asp = &data->adc_seq; - int rc; k_mutex_init(&data->mutex); @@ -121,19 +125,11 @@ static int stm32_temp_init(const struct device *dev) return -ENODEV; } - *accp = (struct adc_channel_cfg){ .gain = ADC_GAIN_1, - .reference = ADC_REF_INTERNAL, - .acquisition_time = ADC_ACQ_TIME_MAX, - .channel_id = data->channel, - .differential = 0 }; - rc = adc_channel_setup(data->adc, accp); - LOG_DBG("Setup AIN%u got %d", data->channel, rc); - *asp = (struct adc_sequence){ - .channels = BIT(data->channel), + .channels = BIT(data->adc_cfg.channel_id), .buffer = &data->sample_buffer, .buffer_size = sizeof(data->sample_buffer), - .resolution = 12, + .resolution = 12U, }; return 0; @@ -157,7 +153,13 @@ static const struct stm32_temp_config stm32_temp_dev_config = { static struct stm32_temp_data stm32_temp_dev_data = { .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(0)), - .channel = DT_INST_IO_CHANNELS_INPUT(0), + .adc_cfg = { + .gain = ADC_GAIN_1, + .reference = ADC_REF_INTERNAL, + .acquisition_time = ADC_ACQ_TIME_MAX, + .channel_id = DT_INST_IO_CHANNELS_INPUT(0), + .differential = 0 + }, }; DEVICE_DT_INST_DEFINE(0, stm32_temp_init, NULL, &stm32_temp_dev_data, &stm32_temp_dev_config,