diff --git a/components/driver/esp32c3/rtc_tempsensor.c b/components/driver/esp32c3/rtc_tempsensor.c index dc3e118f82..e7c42cd06a 100644 --- a/components/driver/esp32c3/rtc_tempsensor.c +++ b/components/driver/esp32c3/rtc_tempsensor.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "esp_log.h" @@ -26,6 +27,7 @@ #include "driver/temp_sensor.h" #include "regi2c_ctrl.h" #include "esp32c3/rom/ets_sys.h" +#include "esp32c3/esp_efuse_rtc_calib.h" static const char *TAG = "tsens"; @@ -58,6 +60,8 @@ static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { {TSENS_DAC_L4, 2, 10, -40, 20, 3}, }; +static float s_deltaT = NAN; // unused number + esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) { REG_SET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_TSENS_CLK_EN); @@ -112,6 +116,28 @@ esp_err_t temp_sensor_read_raw(uint32_t *tsens_out) return ESP_OK; } +static void read_delta_t_from_efuse(void) +{ + uint32_t version = esp_efuse_rtc_calib_get_ver(); + if (version == 1) { + // fetch calibration value for temp sensor from eFuse + s_deltaT = esp_efuse_rtc_calib_get_cal_temp(version); + } else { + // no value to fetch, use 0. + s_deltaT = 0; + } + ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT); +} + +static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset) +{ + if (isnan(s_deltaT)) { //suggests that the value is not initialized + read_delta_t_from_efuse(); + } + float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT / 10.0; + return result; +} + esp_err_t temp_sensor_read_celsius(float *celsius) { TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG); @@ -123,7 +149,7 @@ esp_err_t temp_sensor_read_celsius(float *celsius) printf("tsens_out %d\r\n", tsens_out); TSENS_CHECK(ret == ESP_OK, ret); const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset]; - *celsius = (TSENS_ADC_FACTOR * (float)tsens_out - TSENS_DAC_FACTOR * dac->offset - TSENS_SYS_OFFSET); + *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset); if (*celsius < dac->range_min || *celsius > dac->range_max) { ESP_LOGW(TAG, "Exceeding the temperature range!"); ret = ESP_ERR_INVALID_STATE; diff --git a/components/driver/esp32s2/rtc_tempsensor.c b/components/driver/esp32s2/rtc_tempsensor.c index b47d1626e0..4975faaa63 100644 --- a/components/driver/esp32s2/rtc_tempsensor.c +++ b/components/driver/esp32s2/rtc_tempsensor.c @@ -14,6 +14,7 @@ #include #include +#include #include "esp_types.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" @@ -61,7 +62,7 @@ static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { static SemaphoreHandle_t rtc_tsens_mux = NULL; -static float deltaT = 1000; // greater than range +static float s_deltaT = NAN; // Unused number esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) { @@ -142,20 +143,20 @@ static void read_delta_t_from_efuse(void) uint32_t version = esp_efuse_rtc_table_read_calib_version(); if (version == 1 || version == 2) { // fetch calibration value for temp sensor from eFuse - deltaT = esp_efuse_rtc_table_get_parsed_efuse_value(RTCCALIB_IDX_TMPSENSOR, false) / 10.0; + s_deltaT = esp_efuse_rtc_table_get_parsed_efuse_value(RTCCALIB_IDX_TMPSENSOR, false) / 10.0; } else { // no value to fetch, use 0. - deltaT = 0; + s_deltaT = 0; } - ESP_LOGD(TAG, "deltaT = %f\n", deltaT); + ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT); } -static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const tsens_dac_offset_t *dac) +static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset) { - if (deltaT > 512) { //suggests that the value is not initialized + if (isnan(s_deltaT)) { //suggests that the value is not initialized read_delta_t_from_efuse(); } - float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac->offset - TSENS_SYS_OFFSET) - deltaT; + float result = (TSENS_ADC_FACTOR * (float)tsens_raw - TSENS_DAC_FACTOR * dac_offset - TSENS_SYS_OFFSET) - s_deltaT; return result; } @@ -169,7 +170,7 @@ esp_err_t temp_sensor_read_celsius(float *celsius) ret = temp_sensor_read_raw(&tsens_out); TSENS_CHECK(ret == ESP_OK, ret); const tsens_dac_offset_t *dac = &dac_offset[tsens.dac_offset]; - *celsius = parse_temp_sensor_raw_value(tsens_out, dac); + *celsius = parse_temp_sensor_raw_value(tsens_out, dac->offset); if (*celsius < dac->range_min || *celsius > dac->range_max) { ESP_LOGW(TAG, "Exceeding the temperature range!"); ret = ESP_ERR_INVALID_STATE; diff --git a/components/efuse/include/esp32c3/esp_efuse_rtc_calib.h b/components/efuse/include/esp32c3/esp_efuse_rtc_calib.h index 75a0bb397e..5dc007f8c5 100644 --- a/components/efuse/include/esp32c3/esp_efuse_rtc_calib.h +++ b/components/efuse/include/esp32c3/esp_efuse_rtc_calib.h @@ -48,6 +48,15 @@ uint16_t esp_efuse_rtc_calib_get_init_code(int version, int atten); */ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, int atten, uint32_t* out_digi, uint32_t* out_vol_mv); +/** + * @brief Get the temperature sensor calibration number delta_T stored in the efuse. + * + * @param version Version of the stored efuse + * + * @return The specification of temperature sensor calibration number in efuse. + */ +float esp_efuse_rtc_calib_get_cal_temp(int version); + #ifdef __cplusplus } #endif diff --git a/components/efuse/src/esp32c3/esp_efuse_rtc_calib.c b/components/efuse/src/esp32c3/esp_efuse_rtc_calib.c index 7f0460b41e..844463b6f3 100644 --- a/components/efuse/src/esp32c3/esp_efuse_rtc_calib.c +++ b/components/efuse/src/esp32c3/esp_efuse_rtc_calib.c @@ -82,3 +82,18 @@ esp_err_t esp_efuse_rtc_calib_get_cal_voltage(int version, int atten, uint32_t* *out_vol_mv = calib_vol_expected_mv; return ESP_OK; } + +float esp_efuse_rtc_calib_get_cal_temp(int version) +{ + assert(version == 1); + 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); + assert(err == ESP_OK); + // BIT(8) stands for sign: 1: negtive, 0: positive + return ((cal_temp & BIT(8)) != 0)? -(uint8_t)cal_temp: (uint8_t)cal_temp; +} diff --git a/docs/en/api-reference/peripherals/temp_sensor.rst b/docs/en/api-reference/peripherals/temp_sensor.rst index 89754cb0cb..9fa4a977cf 100644 --- a/docs/en/api-reference/peripherals/temp_sensor.rst +++ b/docs/en/api-reference/peripherals/temp_sensor.rst @@ -25,7 +25,7 @@ The conversion relationship is the first columns of the table below. Among them, Application Example ------------------- -Temperature sensor reading example: :example:`peripherals/temp_sensor_esp32s2`. +Temperature sensor reading example: :example:`peripherals/temp_sensor`. API Reference - Normal Temp Sensor ---------------------------------- diff --git a/examples/peripherals/temp_sensor_esp32s2/CMakeLists.txt b/examples/peripherals/temp_sensor/CMakeLists.txt similarity index 88% rename from examples/peripherals/temp_sensor_esp32s2/CMakeLists.txt rename to examples/peripherals/temp_sensor/CMakeLists.txt index 61bb8c9c57..bd14a9425f 100644 --- a/examples/peripherals/temp_sensor_esp32s2/CMakeLists.txt +++ b/examples/peripherals/temp_sensor/CMakeLists.txt @@ -3,4 +3,4 @@ cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(temp_sensor_esp32s2) +project(temp_sensor) diff --git a/examples/peripherals/temp_sensor_esp32s2/README.md b/examples/peripherals/temp_sensor/README.md similarity index 100% rename from examples/peripherals/temp_sensor_esp32s2/README.md rename to examples/peripherals/temp_sensor/README.md diff --git a/examples/peripherals/temp_sensor_esp32s2/main/CMakeLists.txt b/examples/peripherals/temp_sensor/main/CMakeLists.txt similarity index 100% rename from examples/peripherals/temp_sensor_esp32s2/main/CMakeLists.txt rename to examples/peripherals/temp_sensor/main/CMakeLists.txt diff --git a/examples/peripherals/temp_sensor_esp32s2/main/component.mk b/examples/peripherals/temp_sensor/main/component.mk similarity index 100% rename from examples/peripherals/temp_sensor_esp32s2/main/component.mk rename to examples/peripherals/temp_sensor/main/component.mk diff --git a/examples/peripherals/temp_sensor_esp32s2/main/temp_sensor_main.c b/examples/peripherals/temp_sensor/main/temp_sensor_main.c similarity index 96% rename from examples/peripherals/temp_sensor_esp32s2/main/temp_sensor_main.c rename to examples/peripherals/temp_sensor/main/temp_sensor_main.c index 3cf1697d33..2605899d22 100644 --- a/examples/peripherals/temp_sensor_esp32s2/main/temp_sensor_main.c +++ b/examples/peripherals/temp_sensor/main/temp_sensor_main.c @@ -14,7 +14,7 @@ /* Note: ESP32 don't support temperature sensor */ -#if CONFIG_IDF_TARGET_ESP32S2 +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 #include "driver/temp_sensor.h" static const char *TAG = "TempSensor"; diff --git a/examples/peripherals/temp_sensor_esp32s2/Makefile b/examples/peripherals/temp_sensor_esp32s2/Makefile deleted file mode 100644 index 8f95084302..0000000000 --- a/examples/peripherals/temp_sensor_esp32s2/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# - -PROJECT_NAME := temp_sensor_esp32s2 - -include $(IDF_PATH)/make/project.mk