Skip to content

Commit bd994b3

Browse files
authored
Merge pull request #3302 from bcostm/fix_issue_1685
STM32F4 AnalogIn - Clear VBATE and TSVREFE bits before configuring ADC channels
2 parents d4e23e1 + a3baf2d commit bd994b3

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

targets/TARGET_STM/TARGET_STM32F4/analogin_api.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,19 @@ static inline uint16_t adc_read(analogin_t *obj)
183183
return 0;
184184
}
185185

186+
// Measuring VBAT sets the ADC_CCR_VBATE bit in ADC->CCR, and there is not
187+
// possibility with the ST HAL driver to clear it. If it isn't cleared,
188+
// VBAT remains connected to the ADC channel in preference to temperature,
189+
// so VBAT readings are returned in place of temperature.
190+
ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE);
191+
186192
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
187193

188194
HAL_ADC_Start(&AdcHandle); // Start conversion
189195

190196
// Wait end of conversion and get value
191197
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
192-
return (HAL_ADC_GetValue(&AdcHandle));
198+
return (uint16_t)HAL_ADC_GetValue(&AdcHandle);
193199
} else {
194200
return 0;
195201
}

0 commit comments

Comments
 (0)