stm32/adc: Fix pyb.ADCAll.read_core_temp for G4 MCUs.

For STM32G4,
 * TS_CAL1 raw data acquired at a temperature of 30°C
 * TS_CAL2 raw data acquired at a temperature of 130°C
Also, these values are at VDDA=3.0V.

Signed-off-by: Yuuki NAGAO <wf.yn386@gmail.com>
This commit is contained in:
Yuuki NAGAO 2023-07-08 10:59:35 +09:00 committed by Damien George
parent de8035b510
commit 0ba94a67ba
1 changed files with 8 additions and 6 deletions

View File

@ -866,6 +866,12 @@ int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
STATIC volatile float adc_refcor = 1.0f;
float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
#if defined(STM32G4) || defined(STM32L1) || defined(STM32L4)
// Update the reference correction factor before reading tempsensor
// because TS_CAL1 and TS_CAL2 of STM32G4,L1/L4 are at VDDA=3.0V
adc_read_core_vref(adcHandle);
#endif
#if defined(STM32G4)
int32_t raw_value = 0;
if (adcHandle->Instance == ADC1) {
@ -873,15 +879,11 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
} else {
return 0;
}
float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 100.0f;
#else
#if defined(STM32L1) || defined(STM32L4)
// Update the reference correction factor before reading tempsensor
// because TS_CAL1 and TS_CAL2 of STM32L1/L4 are at VDDA=3.0V
adc_read_core_vref(adcHandle);
#endif
int32_t raw_value = adc_config_and_read_ref(adcHandle, ADC_CHANNEL_TEMPSENSOR);
#endif
float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 80.0f;
#endif
return (((float)raw_value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f;
}