From 77e69fa03629397991bbbb74ec3dd9b4e818b7f7 Mon Sep 17 00:00:00 2001 From: "C.S.M" Date: Mon, 4 Nov 2024 18:09:24 +0800 Subject: [PATCH] feat(temperature_sensor): Add temperature sensor calibration support --- .../efuse/esp32c2/esp_efuse_rtc_calib.c | 17 +++++++++++++--- .../efuse/esp32c6/esp_efuse_rtc_calib.c | 17 +++++++++++++--- .../efuse/esp32h2/esp_efuse_rtc_calib.c | 17 +++++++++++++--- .../efuse/esp32p4/esp_efuse_rtc_calib.c | 20 +++++++++++++++---- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/components/efuse/esp32c2/esp_efuse_rtc_calib.c b/components/efuse/esp32c2/esp_efuse_rtc_calib.c index 79d91b2319..673d0e1deb 100644 --- a/components/efuse/esp32c2/esp_efuse_rtc_calib.c +++ b/components/efuse/esp32c2/esp_efuse_rtc_calib.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -111,7 +111,18 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, in esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal) { - // Currently calibration is not supported on ESP32-C2, IDF-5236 - *tsens_cal = 0.0; + const esp_efuse_desc_t** cal_temp_efuse; + cal_temp_efuse = ESP_EFUSE_TEMP_CALIB; + int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse); + assert(cal_temp_size == 9); + + uint32_t cal_temp = 0; + esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size); + if (err != ESP_OK) { + *tsens_cal = 0.0; + return err; + } + // BIT(8) stands for sign: 1: negative, 0: positive + *tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp; return ESP_OK; } diff --git a/components/efuse/esp32c6/esp_efuse_rtc_calib.c b/components/efuse/esp32c6/esp_efuse_rtc_calib.c index 5477b095a4..0f4defa9e1 100644 --- a/components/efuse/esp32c6/esp_efuse_rtc_calib.c +++ b/components/efuse/esp32c6/esp_efuse_rtc_calib.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -132,7 +132,18 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, in esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal) { - // Currently calibration is not supported on ESP32-C6, IDF-5236 - *tsens_cal = 0; + const esp_efuse_desc_t** cal_temp_efuse; + cal_temp_efuse = ESP_EFUSE_TEMP_CALIB; + int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse); + assert(cal_temp_size == 9); + + uint32_t cal_temp = 0; + esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size); + if (err != ESP_OK) { + *tsens_cal = 0.0; + return err; + } + // BIT(8) stands for sign: 1: negative, 0: positive + *tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp; return ESP_OK; } diff --git a/components/efuse/esp32h2/esp_efuse_rtc_calib.c b/components/efuse/esp32h2/esp_efuse_rtc_calib.c index 2ff20ea3a3..26f4426175 100644 --- a/components/efuse/esp32h2/esp_efuse_rtc_calib.c +++ b/components/efuse/esp32h2/esp_efuse_rtc_calib.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -124,7 +124,18 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, uint32_t adc_unit, in esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal) { - // Currently calibration is not supported on ESP32-H2, IDF-5236 - *tsens_cal = 0; + const esp_efuse_desc_t** cal_temp_efuse; + cal_temp_efuse = ESP_EFUSE_TEMP_CALIB; + int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse); + assert(cal_temp_size == 9); + + uint32_t cal_temp = 0; + esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size); + if (err != ESP_OK) { + *tsens_cal = 0.0; + return err; + } + // BIT(8) stands for sign: 1: negative, 0: positive + *tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp; return ESP_OK; } diff --git a/components/efuse/esp32p4/esp_efuse_rtc_calib.c b/components/efuse/esp32p4/esp_efuse_rtc_calib.c index 47ac9117d7..4dbd5a9e6c 100644 --- a/components/efuse/esp32p4/esp_efuse_rtc_calib.c +++ b/components/efuse/esp32p4/esp_efuse_rtc_calib.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,6 +7,7 @@ #include #include "esp_efuse.h" #include "esp_efuse_table.h" +#include "esp_efuse_rtc_calib.h" int esp_efuse_rtc_calib_get_ver(void) { @@ -35,7 +36,18 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, int atten, uint32_t* esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal) { - //TODO: IDF-7482 - *tsens_cal = 0; - return ESP_ERR_NOT_SUPPORTED; + const esp_efuse_desc_t** cal_temp_efuse; + cal_temp_efuse = ESP_EFUSE_TEMPERATURE_SENSOR; + int cal_temp_size = esp_efuse_get_field_size(cal_temp_efuse); + assert(cal_temp_size == 9); + + uint32_t cal_temp = 0; + esp_err_t err = esp_efuse_read_field_blob(cal_temp_efuse, &cal_temp, cal_temp_size); + if (err != ESP_OK) { + *tsens_cal = 0.0; + return err; + } + // BIT(8) stands for sign: 1: negative, 0: positive + *tsens_cal = ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp; + return ESP_OK; }