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 <yongcong.sin@gmail.com>
This commit is contained in:
parent
94624dc0c5
commit
fbd0cd01d5
1 changed files with 18 additions and 16 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue