Browse Source

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>
pull/11966/head
Yuuki NAGAO 1 year ago
committed by Damien George
parent
commit
0ba94a67ba
  1. 14
      ports/stm32/adc.c

14
ports/stm32/adc.c

@ -866,6 +866,12 @@ int adc_read_core_temp(ADC_HandleTypeDef *adcHandle) {
STATIC volatile float adc_refcor = 1.0f; STATIC volatile float adc_refcor = 1.0f;
float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) { 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) #if defined(STM32G4)
int32_t raw_value = 0; int32_t raw_value = 0;
if (adcHandle->Instance == ADC1) { if (adcHandle->Instance == ADC1) {
@ -873,15 +879,11 @@ float adc_read_core_temp_float(ADC_HandleTypeDef *adcHandle) {
} else { } else {
return 0; return 0;
} }
float core_temp_avg_slope = (*ADC_CAL2 - *ADC_CAL1) / 100.0f;
#else #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); 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; 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; return (((float)raw_value * adc_refcor - *ADC_CAL1) / core_temp_avg_slope) + 30.0f;
} }

Loading…
Cancel
Save