Skip to content

Commit d173737

Browse files
mathieuchopstmkartben
authored andcommitted
drivers: sensor: stm32_vref: take calibration resolution into account
Update the driver to take the "vrefint-cal-resolution" property into account when performing the calculations. Signed-off-by: Mathieu Choplain <[email protected]>
1 parent 1d4c5ee commit d173737

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

drivers/sensor/st/stm32_vref/stm32_vref.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
LOG_MODULE_REGISTER(stm32_vref, CONFIG_SENSOR_LOG_LEVEL);
2121

22+
/* Resolution used to perform the Vref measurement */
23+
#define MEAS_RES (12U)
24+
2225
struct stm32_vref_data {
2326
const struct device *adc;
2427
const struct adc_channel_cfg adc_cfg;
@@ -32,6 +35,7 @@ struct stm32_vref_data {
3235
struct stm32_vref_config {
3336
uint16_t *cal_addr;
3437
int cal_mv;
38+
uint8_t cal_shift;
3539
};
3640

3741
static int stm32_vref_sample_fetch(const struct device *dev, enum sensor_channel chan)
@@ -103,16 +107,7 @@ static int stm32_vref_channel_get(const struct device *dev, enum sensor_channel
103107
#endif /* CONFIG_SOC_SERIES_STM32H5X */
104108

105109
/* Calculate VREF+ using VREFINT bandgap voltage and calibration data */
106-
#if defined(CONFIG_SOC_SERIES_STM32U5X)
107-
/*
108-
* The VREF CALIBRATION value is acquired on 14 bits
109-
* and the data acquired is on 12 bits
110-
* since the adc_sequence.resolution is 12
111-
*/
112-
vref = (cfg->cal_mv * (*cfg->cal_addr) >> 2) / data->raw;
113-
#else
114-
vref = cfg->cal_mv * (*cfg->cal_addr) / data->raw;
115-
#endif /* CONFIG_SOC_SERIES_STM32H5X */
110+
vref = (cfg->cal_mv * ((*cfg->cal_addr) >> cfg->cal_shift)) / data->raw;
116111

117112
#if defined(CONFIG_SOC_SERIES_STM32H5X)
118113
LL_ICACHE_Enable();
@@ -142,7 +137,7 @@ static int stm32_vref_init(const struct device *dev)
142137
.channels = BIT(data->adc_cfg.channel_id),
143138
.buffer = &data->sample_buffer,
144139
.buffer_size = sizeof(data->sample_buffer),
145-
.resolution = 12U,
140+
.resolution = MEAS_RES,
146141
};
147142

148143
return 0;
@@ -178,8 +173,13 @@ static struct stm32_vref_data stm32_vref_dev_data = {
178173
static const struct stm32_vref_config stm32_vref_dev_config = {
179174
.cal_addr = (uint16_t *)DT_INST_PROP(0, vrefint_cal_addr),
180175
.cal_mv = DT_INST_PROP(0, vrefint_cal_mv),
176+
.cal_shift = (DT_INST_PROP(0, vrefint_cal_resolution) - MEAS_RES),
181177
};
182178

179+
/* Make sure no series with unsupported configuration can be added silently */
180+
BUILD_ASSERT(DT_INST_PROP(0, vrefint_cal_resolution) >= MEAS_RES,
181+
"VREFINT calibration resolution is too low");
182+
183183
SENSOR_DEVICE_DT_INST_DEFINE(0, stm32_vref_init, NULL, &stm32_vref_dev_data, &stm32_vref_dev_config,
184184
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &stm32_vref_driver_api);
185185

0 commit comments

Comments
 (0)