19
19
20
20
LOG_MODULE_REGISTER (stm32_vref , CONFIG_SENSOR_LOG_LEVEL );
21
21
22
+ /* Resolution used to perform the Vref measurement */
23
+ #define MEAS_RES (12U)
24
+
22
25
struct stm32_vref_data {
23
26
const struct device * adc ;
24
27
const struct adc_channel_cfg adc_cfg ;
@@ -32,6 +35,7 @@ struct stm32_vref_data {
32
35
struct stm32_vref_config {
33
36
uint16_t * cal_addr ;
34
37
int cal_mv ;
38
+ uint8_t cal_shift ;
35
39
};
36
40
37
41
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
103
107
#endif /* CONFIG_SOC_SERIES_STM32H5X */
104
108
105
109
/* 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 ;
116
111
117
112
#if defined(CONFIG_SOC_SERIES_STM32H5X )
118
113
LL_ICACHE_Enable ();
@@ -142,7 +137,7 @@ static int stm32_vref_init(const struct device *dev)
142
137
.channels = BIT (data -> adc_cfg .channel_id ),
143
138
.buffer = & data -> sample_buffer ,
144
139
.buffer_size = sizeof (data -> sample_buffer ),
145
- .resolution = 12U ,
140
+ .resolution = MEAS_RES ,
146
141
};
147
142
148
143
return 0 ;
@@ -178,8 +173,13 @@ static struct stm32_vref_data stm32_vref_dev_data = {
178
173
static const struct stm32_vref_config stm32_vref_dev_config = {
179
174
.cal_addr = (uint16_t * )DT_INST_PROP (0 , vrefint_cal_addr ),
180
175
.cal_mv = DT_INST_PROP (0 , vrefint_cal_mv ),
176
+ .cal_shift = (DT_INST_PROP (0 , vrefint_cal_resolution ) - MEAS_RES ),
181
177
};
182
178
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
+
183
183
SENSOR_DEVICE_DT_INST_DEFINE (0 , stm32_vref_init , NULL , & stm32_vref_dev_data , & stm32_vref_dev_config ,
184
184
POST_KERNEL , CONFIG_SENSOR_INIT_PRIORITY , & stm32_vref_driver_api );
185
185
0 commit comments