From 0ba94a67ba72f2e04bfc26dcf0ed687addd0d740 Mon Sep 17 00:00:00 2001 From: Yuuki NAGAO Date: Sat, 8 Jul 2023 10:59:35 +0900 Subject: [PATCH] stm32/adc: Fix pyb.ADCAll.read_core_temp for G4 MCUs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ports/stm32/adc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ports/stm32/adc.c b/ports/stm32/adc.c index e2eb5f7e5a..fc0882286c 100644 --- a/ports/stm32/adc.c +++ b/ports/stm32/adc.c @@ -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; }