Skip to content

Commit fbd0cd0

Browse files
ycsincarlescufi
authored andcommitted
drivers: sensor: stm32_temp: setup channel before adc_read
Currently the driver only setup the ADC to read from the internal temperature channel on init. However, it is possible that some other application that uses the ADC can setup the ADC to read from some other channel and therefore subsequent stm32_temp_sample_fetch will fail to read the targeted channel. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent 94624dc commit fbd0cd0

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

drivers/sensor/stm32_temp/stm32_temp.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ LOG_MODULE_REGISTER(stm32_temp, CONFIG_SENSOR_LOG_LEVEL);
2424

2525
struct stm32_temp_data {
2626
const struct device *adc;
27-
uint8_t channel;
28-
struct adc_channel_cfg adc_cfg;
27+
const struct adc_channel_cfg adc_cfg;
2928
struct adc_sequence adc_seq;
3029
struct k_mutex mutex;
3130
int16_t sample_buffer;
@@ -60,14 +59,21 @@ static int stm32_temp_sample_fetch(const struct device *dev, enum sensor_channel
6059

6160
k_mutex_lock(&data->mutex, K_FOREVER);
6261

62+
rc = adc_channel_setup(data->adc, &data->adc_cfg);
63+
if (rc) {
64+
LOG_DBG("Setup AIN%u got %d", data->adc_cfg.channel_id, rc);
65+
goto unlock;
66+
}
67+
6368
rc = adc_read(data->adc, sp);
6469
if (rc == 0) {
6570
data->raw = data->sample_buffer;
6671
}
6772

73+
unlock:
6874
k_mutex_unlock(&data->mutex);
6975

70-
return 0;
76+
return rc;
7177
}
7278

7379
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 = {
110116
static int stm32_temp_init(const struct device *dev)
111117
{
112118
struct stm32_temp_data *data = dev->data;
113-
struct adc_channel_cfg *accp = &data->adc_cfg;
114119
struct adc_sequence *asp = &data->adc_seq;
115-
int rc;
116120

117121
k_mutex_init(&data->mutex);
118122

@@ -121,19 +125,11 @@ static int stm32_temp_init(const struct device *dev)
121125
return -ENODEV;
122126
}
123127

124-
*accp = (struct adc_channel_cfg){ .gain = ADC_GAIN_1,
125-
.reference = ADC_REF_INTERNAL,
126-
.acquisition_time = ADC_ACQ_TIME_MAX,
127-
.channel_id = data->channel,
128-
.differential = 0 };
129-
rc = adc_channel_setup(data->adc, accp);
130-
LOG_DBG("Setup AIN%u got %d", data->channel, rc);
131-
132128
*asp = (struct adc_sequence){
133-
.channels = BIT(data->channel),
129+
.channels = BIT(data->adc_cfg.channel_id),
134130
.buffer = &data->sample_buffer,
135131
.buffer_size = sizeof(data->sample_buffer),
136-
.resolution = 12,
132+
.resolution = 12U,
137133
};
138134

139135
return 0;
@@ -157,7 +153,13 @@ static const struct stm32_temp_config stm32_temp_dev_config = {
157153

158154
static struct stm32_temp_data stm32_temp_dev_data = {
159155
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(0)),
160-
.channel = DT_INST_IO_CHANNELS_INPUT(0),
156+
.adc_cfg = {
157+
.gain = ADC_GAIN_1,
158+
.reference = ADC_REF_INTERNAL,
159+
.acquisition_time = ADC_ACQ_TIME_MAX,
160+
.channel_id = DT_INST_IO_CHANNELS_INPUT(0),
161+
.differential = 0
162+
},
161163
};
162164

163165
DEVICE_DT_INST_DEFINE(0, stm32_temp_init, NULL, &stm32_temp_dev_data, &stm32_temp_dev_config,

0 commit comments

Comments
 (0)