浏览代码

temp_sensor: add calibration for esp32c3

Cao Sen Miao 5 年之前
父节点
当前提交
d92ac450a2

+ 27 - 1
components/driver/esp32c3/rtc_tempsensor.c

@@ -15,6 +15,7 @@
 #include <esp_types.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <math.h>
 #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;

+ 9 - 8
components/driver/esp32s2/rtc_tempsensor.c

@@ -14,6 +14,7 @@
 
 #include <stdlib.h>
 #include <ctype.h>
+#include <math.h>
 #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;

+ 9 - 0
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

+ 15 - 0
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;
+}

+ 1 - 1
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
 ----------------------------------

+ 1 - 1
examples/peripherals/temp_sensor_esp32s2/CMakeLists.txt → 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)

+ 0 - 0
examples/peripherals/temp_sensor_esp32s2/README.md → examples/peripherals/temp_sensor/README.md


+ 0 - 0
examples/peripherals/temp_sensor_esp32s2/main/CMakeLists.txt → examples/peripherals/temp_sensor/main/CMakeLists.txt


+ 0 - 0
examples/peripherals/temp_sensor_esp32s2/main/component.mk → examples/peripherals/temp_sensor/main/component.mk


+ 1 - 1
examples/peripherals/temp_sensor_esp32s2/main/temp_sensor_main.c → 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";

+ 0 - 8
examples/peripherals/temp_sensor_esp32s2/Makefile

@@ -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