diff --git a/components/driver/deprecated/rtc_temperature_legacy.c b/components/driver/deprecated/rtc_temperature_legacy.c index f317d7bdaa..30e59bc6e3 100644 --- a/components/driver/deprecated/rtc_temperature_legacy.c +++ b/components/driver/deprecated/rtc_temperature_legacy.c @@ -11,12 +11,14 @@ #include "esp_types.h" #include "esp_log.h" #include "esp_check.h" +#include "freertos/FreeRTOS.h" #include "soc/rtc_cntl_reg.h" #include "esp_private/regi2c_ctrl.h" #include "soc/regi2c_saradc.h" #include "esp_log.h" #include "esp_efuse_rtc_calib.h" #include "hal/temperature_sensor_ll.h" +#include "hal/regi2c_ctrl_ll.h" #include "driver/temp_sensor_types_legacy.h" #include "esp_private/periph_ctrl.h" @@ -26,6 +28,10 @@ static const char *TAG = "tsens"; #define TSENS_DAC_FACTOR (27.88) #define TSENS_SYS_OFFSET (20.52) +extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished. +#define RTC_TEMP_SENSOR_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock) +#define RTC_TEMP_SENSOR_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock) + typedef struct { int index; int offset; @@ -62,8 +68,11 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) err = ESP_ERR_INVALID_STATE; } temperature_sensor_ll_set_clk_div(tsens.clk_div); + RTC_TEMP_SENSOR_ENTER_CRITICAL(); + regi2c_ctrl_ll_i2c_saradc_enable(); temperature_sensor_ll_set_range(dac_offset[tsens.dac_offset].reg_val); temperature_sensor_ll_enable(true); + RTC_TEMP_SENSOR_EXIT_CRITICAL(); ESP_LOGI(TAG, "Config range [%d°C ~ %d°C], error < %d°C", dac_offset[tsens.dac_offset].range_min, dac_offset[tsens.dac_offset].range_max, diff --git a/components/driver/temperature_sensor.c b/components/driver/temperature_sensor.c index 4452dae906..87653814a8 100644 --- a/components/driver/temperature_sensor.c +++ b/components/driver/temperature_sensor.c @@ -25,6 +25,8 @@ #include "esp_efuse_rtc_calib.h" #include "esp_private/periph_ctrl.h" #include "hal/temperature_sensor_ll.h" +#include "hal/regi2c_ctrl_ll.h" +#include "soc/temperature_sensor_periph.h" static const char *TAG = "temperature_sensor"; @@ -42,33 +44,33 @@ static float s_deltaT = NAN; // unused number typedef struct temperature_sensor_obj_t temperature_sensor_obj_t; struct temperature_sensor_obj_t { - const temp_sensor_ll_attribute_t *tsens_attribute; + const temperature_sensor_attribute_t *tsens_attribute; temp_sensor_fsm_t fsm; temperature_sensor_clk_src_t clk_src; }; -static temp_sensor_ll_attribute_t *s_tsens_attribute_copy; +static temperature_sensor_attribute_t *s_tsens_attribute_copy; static int inline accuracy_compare(const void *p1, const void *p2) { - return ((*(temp_sensor_ll_attribute_t *)p1).error_max < (*(temp_sensor_ll_attribute_t *)p2).error_max) ? -1 : 1; + return ((*(temperature_sensor_attribute_t *)p1).error_max < (*(temperature_sensor_attribute_t *)p2).error_max) ? -1 : 1; } static esp_err_t temperature_sensor_attribute_table_sort(void) { - s_tsens_attribute_copy = (temp_sensor_ll_attribute_t *)heap_caps_malloc(sizeof(temp_sensor_ll_attributes), MALLOC_CAP_DEFAULT); + s_tsens_attribute_copy = (temperature_sensor_attribute_t *)heap_caps_malloc(sizeof(temperature_sensor_attributes), MALLOC_CAP_DEFAULT); ESP_RETURN_ON_FALSE(s_tsens_attribute_copy != NULL, ESP_ERR_NO_MEM, TAG, "No space for s_tsens_attribute_copy"); - for (int i = 0 ; i < TEMPERATURE_SENSOR_LL_RANGE_NUM; i++) { - s_tsens_attribute_copy[i] = temp_sensor_ll_attributes[i]; + for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) { + s_tsens_attribute_copy[i] = temperature_sensor_attributes[i]; } // Sort from small to large by error_max. - qsort(s_tsens_attribute_copy, TEMPERATURE_SENSOR_LL_RANGE_NUM, sizeof(s_tsens_attribute_copy[0]), accuracy_compare); + qsort(s_tsens_attribute_copy, TEMPERATURE_SENSOR_ATTR_RANGE_NUM, sizeof(s_tsens_attribute_copy[0]), accuracy_compare); return ESP_OK; } static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_t tsens, const temperature_sensor_config_t *tsens_config) { - for (int i = 0 ; i < TEMPERATURE_SENSOR_LL_RANGE_NUM; i++) { + for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) { if ((tsens_config->range_min >= s_tsens_attribute_copy[i].range_min) && (tsens_config->range_max <= s_tsens_attribute_copy[i].range_max)) { tsens->tsens_attribute = &s_tsens_attribute_copy[i]; break; @@ -102,6 +104,7 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co tsens->tsens_attribute->error_max); TEMPERATURE_SENSOR_ENTER_CRITICAL(); + regi2c_ctrl_ll_i2c_saradc_enable(); temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val); temperature_sensor_ll_enable(false); // disable the sensor by default TEMPERATURE_SENSOR_EXIT_CRITICAL(); diff --git a/components/driver/test_apps/legacy_rtc_temp_driver/README.md b/components/driver/test_apps/legacy_rtc_temp_driver/README.md index 0dfa474720..ef937345ba 100644 --- a/components/driver/test_apps/legacy_rtc_temp_driver/README.md +++ b/components/driver/test_apps/legacy_rtc_temp_driver/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | -| ----------------- | -------- | -------- | -------- | +| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 | +| ----------------- | -------- | -------- | -------- | -------- | diff --git a/components/driver/test_apps/legacy_rtc_temp_driver/pytest_legacy_temp_sensor_driver.py b/components/driver/test_apps/legacy_rtc_temp_driver/pytest_legacy_temp_sensor_driver.py index e885e9efc7..e29d1e1f90 100644 --- a/components/driver/test_apps/legacy_rtc_temp_driver/pytest_legacy_temp_sensor_driver.py +++ b/components/driver/test_apps/legacy_rtc_temp_driver/pytest_legacy_temp_sensor_driver.py @@ -8,6 +8,7 @@ from pytest_embedded import Dut @pytest.mark.esp32s2 @pytest.mark.esp32c3 @pytest.mark.esp32s3 +@pytest.mark.esp32c2 @pytest.mark.generic @pytest.mark.parametrize('config', [ 'release', diff --git a/components/driver/test_apps/temperature_sensor/README.md b/components/driver/test_apps/temperature_sensor/README.md index 0dfa474720..ef937345ba 100644 --- a/components/driver/test_apps/temperature_sensor/README.md +++ b/components/driver/test_apps/temperature_sensor/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | -| ----------------- | -------- | -------- | -------- | +| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 | +| ----------------- | -------- | -------- | -------- | -------- | diff --git a/components/driver/test_apps/temperature_sensor/pytest_temperature_sensor.py b/components/driver/test_apps/temperature_sensor/pytest_temperature_sensor.py index 9ad33be12f..12a8c4a56e 100644 --- a/components/driver/test_apps/temperature_sensor/pytest_temperature_sensor.py +++ b/components/driver/test_apps/temperature_sensor/pytest_temperature_sensor.py @@ -8,6 +8,7 @@ from pytest_embedded import Dut @pytest.mark.esp32s2 @pytest.mark.esp32c3 @pytest.mark.esp32s3 +@pytest.mark.esp32c2 @pytest.mark.generic @pytest.mark.parametrize('config', [ 'release', diff --git a/components/efuse/esp32c2/esp_efuse_rtc_calib.c b/components/efuse/esp32c2/esp_efuse_rtc_calib.c new file mode 100644 index 0000000000..998d7f4786 --- /dev/null +++ b/components/efuse/esp32c2/esp_efuse_rtc_calib.c @@ -0,0 +1,23 @@ +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "esp_efuse.h" +#include "esp_efuse_table.h" + +int esp_efuse_rtc_calib_get_ver(void) +{ + uint32_t result = 0; + esp_efuse_read_field_blob(ESP_EFUSE_BLOCK2_VERSION, &result, 3); + return result; +} + +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; + return ESP_OK; +} diff --git a/components/efuse/esp32c2/include/esp_efuse_rtc_calib.h b/components/efuse/esp32c2/include/esp_efuse_rtc_calib.h new file mode 100644 index 0000000000..d33bc76613 --- /dev/null +++ b/components/efuse/esp32c2/include/esp_efuse_rtc_calib.h @@ -0,0 +1,26 @@ +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get the temperature sensor calibration number delta_T stored in the efuse. + * + * @param tsens_cal Pointer of the specification of temperature sensor calibration number in efuse. + * + * @return ESP_OK if get the calibration value successfully. + * ESP_ERR_INVALID_ARG if can't get the calibration value. + */ +esp_err_t esp_efuse_rtc_calib_get_tsens_val(float* tsens_cal); + +#ifdef __cplusplus +} +#endif diff --git a/components/efuse/esp32c2/sources.cmake b/components/efuse/esp32c2/sources.cmake index 32e2583fcc..9dffd72008 100644 --- a/components/efuse/esp32c2/sources.cmake +++ b/components/efuse/esp32c2/sources.cmake @@ -1,3 +1,4 @@ set(EFUSE_SOC_SRCS "esp_efuse_table.c" "esp_efuse_fields.c" + "esp_efuse_rtc_calib.c" "esp_efuse_utility.c") diff --git a/components/hal/esp32c2/include/hal/clk_gate_ll.h b/components/hal/esp32c2/include/hal/clk_gate_ll.h index e9e215b34c..f83a74c981 100644 --- a/components/hal/esp32c2/include/hal/clk_gate_ll.h +++ b/components/hal/esp32c2/include/hal/clk_gate_ll.h @@ -57,6 +57,8 @@ static inline uint32_t periph_ll_get_clk_en_mask(periph_module_t periph) return SYSTEM_BT_BASEBAND_EN; case PERIPH_BT_LC_MODULE: return SYSTEM_BT_LC_EN; + case PERIPH_TEMPSENSOR_MODULE: + return SYSTEM_TSENS_CLK_EN; default: return 0; } @@ -90,6 +92,8 @@ static inline uint32_t periph_ll_get_rst_en_mask(periph_module_t periph, bool en return SYSTEM_SPI01_RST; case PERIPH_SPI2_MODULE: return SYSTEM_SPI2_RST; + case PERIPH_TEMPSENSOR_MODULE: + return SYSTEM_TSENS_RST; case PERIPH_SHA_MODULE: if (enable == true) { // Clear reset on digital signature and HMAC, otherwise SHA is held in reset @@ -119,6 +123,7 @@ static uint32_t periph_ll_get_clk_en_reg(periph_module_t periph) case PERIPH_SHA_MODULE: case PERIPH_GDMA_MODULE: case PERIPH_ECC_MODULE: + case PERIPH_TEMPSENSOR_MODULE: return SYSTEM_PERIP_CLK_EN1_REG; default: return SYSTEM_PERIP_CLK_EN0_REG; @@ -140,6 +145,7 @@ static uint32_t periph_ll_get_rst_en_reg(periph_module_t periph) case PERIPH_SHA_MODULE: case PERIPH_GDMA_MODULE: case PERIPH_ECC_MODULE: + case PERIPH_TEMPSENSOR_MODULE: return SYSTEM_PERIP_RST_EN1_REG; default: return SYSTEM_PERIP_RST_EN0_REG; diff --git a/components/hal/esp32c2/include/hal/regi2c_ctrl_ll.h b/components/hal/esp32c2/include/hal/regi2c_ctrl_ll.h index 0b9139357b..6e853f2ab0 100644 --- a/components/hal/esp32c2/include/hal/regi2c_ctrl_ll.h +++ b/components/hal/esp32c2/include/hal/regi2c_ctrl_ll.h @@ -39,6 +39,15 @@ static inline __attribute__((always_inline)) void regi2c_ctrl_ll_bbpll_calibrati REG_SET_BIT(I2C_MST_ANA_CONF0_REG, I2C_MST_BBPLL_STOP_FORCE_LOW); } +/** + * @brief Enable I2C_SAR + */ +static inline void regi2c_ctrl_ll_i2c_saradc_enable(void) +{ + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, ANA_I2C_SAR_FORCE_PD); + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_I2C_SAR_FORCE_PU); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h new file mode 100644 index 0000000000..99eef9a7c1 --- /dev/null +++ b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h @@ -0,0 +1,135 @@ +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/******************************************************************************* + * NOTICE + * The hal is not public api, don't use in application code. + * See readme.md in component/hal/readme.md + ******************************************************************************/ + +// The LL for temperature sensor + +#pragma once + +#include +#include +#include "esp_private/regi2c_ctrl.h" +#include "soc/regi2c_saradc.h" +#include "soc/apb_saradc_struct.h" +#include "soc/soc.h" +#include "soc/soc_caps.h" +#include "hal/temperature_sensor_types.h" +#include "hal/assert.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) +#define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) +#define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) + +/** + * @brief Enable the temperature sensor power. + * + * @param enable true: enable the power. + */ +static inline void temperature_sensor_ll_enable(bool enable) +{ + APB_SARADC.saradc_apb_tsens_ctrl.saradc_reg_tsens_pu = enable; +} + +/** + * @brief Enable the clock + */ +static inline void temperature_sensor_ll_clk_enable(bool enable) +{ + // No need to enable the temperature clock on esp32c2 +} + +/** + * @brief Select the clock source for temperature sensor. On ESP32-C2, temperautre sensor + * can use XTAL or FOSC. To make it convenience, suggest using XTAL all the time. + * + * @param clk_src refer to ``temperature_sensor_clk_src_t`` + */ +static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t clk_src) +{ + uint8_t clk_sel = 0; + switch (clk_src) { + case TEMPERATURE_SENSOR_CLK_SRC_XTAL: + clk_sel = 1; + break; + case TEMPERATURE_SENSOR_CLK_SRC_RC_FAST: + clk_sel = 0; + break; + default: + HAL_ASSERT(false); + break; + } + APB_SARADC.saradc_apb_tsens_ctrl2.saradc_tsens_clk_sel = clk_sel; +} + +/** + * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes`` + * + * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes`` + */ +static inline void temperature_sensor_ll_set_range(uint32_t range) +{ + REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, range); +} + +/** + * @brief Get the raw value of temperature sensor. + * + * @return uint32_t raw_value + */ +static inline uint32_t temperature_sensor_ll_get_raw_value(void) +{ + return APB_SARADC.saradc_apb_tsens_ctrl.saradc_reg_tsens_out; +} + +/** + * @brief Get the offset value of temperature sensor. + * + * @note This function is only used in legacy driver + * + * @return uint32_t offset value + */ +static inline uint32_t temperature_sensor_ll_get_offset(void) +{ + return REGI2C_READ_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC); +} + +/** + * @brief Get the clock division factor value. + * + * @note This function is only used in legacy driver + * + * @return uint32_t clock division factor + */ +static inline uint32_t temperature_sensor_ll_get_clk_div(void) +{ + return APB_SARADC.saradc_apb_tsens_ctrl.saradc_reg_tsens_clk_div; +} + +/** + * @brief Set the clock division factor value, actually this has no impact on temperature sensor. + * Suggest just keep it as default value 6. + * + * @note This function is only used in legacy driver + * + * @param clk_div clock division factor, range from 1-10 + */ +static inline void temperature_sensor_ll_set_clk_div(uint8_t clk_div) +{ + APB_SARADC.saradc_apb_tsens_ctrl.saradc_reg_tsens_clk_div = clk_div; +} + +#ifdef __cplusplus +} +#endif diff --git a/components/hal/esp32c3/include/hal/regi2c_ctrl_ll.h b/components/hal/esp32c3/include/hal/regi2c_ctrl_ll.h index 0b9139357b..6e853f2ab0 100644 --- a/components/hal/esp32c3/include/hal/regi2c_ctrl_ll.h +++ b/components/hal/esp32c3/include/hal/regi2c_ctrl_ll.h @@ -39,6 +39,15 @@ static inline __attribute__((always_inline)) void regi2c_ctrl_ll_bbpll_calibrati REG_SET_BIT(I2C_MST_ANA_CONF0_REG, I2C_MST_BBPLL_STOP_FORCE_LOW); } +/** + * @brief Enable I2C_SAR + */ +static inline void regi2c_ctrl_ll_i2c_saradc_enable(void) +{ + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, ANA_I2C_SAR_FORCE_PD); + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_I2C_SAR_FORCE_PU); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h index f34c9e930c..a98461b13e 100644 --- a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h @@ -32,25 +32,6 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) -#define TEMPERATURE_SENSOR_LL_RANGE_NUM (5) - -typedef struct { - int offset; - int reg_val; - int range_min; - int range_max; - int error_max; -} temp_sensor_ll_attribute_t; - -static const temp_sensor_ll_attribute_t temp_sensor_ll_attributes[TEMPERATURE_SENSOR_LL_RANGE_NUM] = { - /*Offset, reg_val, min, max, error */ - { -2, 5, 50, 125, 3}, - { -1, 7, 20, 100, 2}, - { 0, 15, -10, 80, 1}, - { 1, 11, -30, 50, 2}, - { 2, 10, -40, 20, 3}, -}; - /** * @brief Enable the temperature sensor power. * @@ -93,14 +74,12 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl } /** - * @brief Set the hardware range, you can refer to the table ``temp_sensor_ll_attributes`` + * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes`` * - * @param tsens_dac ``reg_val`` in table ``temp_sensor_ll_attributes`` + * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes`` */ static inline void temperature_sensor_ll_set_range(uint32_t range) { - CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, ANA_I2C_SAR_FORCE_PD); - SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_I2C_SAR_FORCE_PU); REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, range); } diff --git a/components/hal/esp32h2/include/hal/regi2c_ctrl_ll.h b/components/hal/esp32h2/include/hal/regi2c_ctrl_ll.h index 52e2158d24..27eaf05e5d 100644 --- a/components/hal/esp32h2/include/hal/regi2c_ctrl_ll.h +++ b/components/hal/esp32h2/include/hal/regi2c_ctrl_ll.h @@ -64,6 +64,15 @@ static inline __attribute__((always_inline)) bool regi2c_ctrl_ll_bbpll_calibrati return REG_GET_BIT(I2C_MST_ANA_CONF0_REG, I2C_MST_BBPLL_CAL_DONE); } +/** + * @brief Enable I2C_SAR + */ +static inline void regi2c_ctrl_ll_i2c_saradc_enable(void) +{ + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, ANA_I2C_SAR_FORCE_PD); + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_I2C_SAR_FORCE_PU); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h index d8c86351ed..d671e1588c 100644 --- a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h @@ -32,25 +32,6 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) -#define TEMPERATURE_SENSOR_LL_RANGE_NUM (5) - -typedef struct { - int offset; - int reg_val; - int range_min; - int range_max; - int error_max; -} temp_sensor_ll_attribute_t; - -static const temp_sensor_ll_attribute_t temp_sensor_ll_attributes[TEMPERATURE_SENSOR_LL_RANGE_NUM] = { - /*Offset, reg_val, min, max, error */ - { -2, 5, 50, 125, 3}, - { -1, 7, 20, 100, 2}, - { 0, 15, -10, 80, 1}, - { 1, 11, -30, 50, 2}, - { 2, 10, -40, 20, 3}, -}; - /** * @brief Enable the temperature sensor power. * @@ -93,14 +74,12 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl } /** - * @brief Set the hardware range, you can refer to the table ``temp_sensor_ll_attributes`` + * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes`` * - * @param tsens_dac ``reg_val`` in table ``temp_sensor_ll_attributes`` + * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes`` */ static inline void temperature_sensor_ll_set_range(uint32_t range) { - CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, ANA_I2C_SAR_FORCE_PD); - SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_I2C_SAR_FORCE_PU); REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, range); } diff --git a/components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h b/components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h index aa0e449607..4d390fb28f 100644 --- a/components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h +++ b/components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h @@ -38,6 +38,17 @@ static inline void regi2c_ctrl_ll_i2c_apll_enable(void) CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_APLL_M); } +/** + * @brief Enable I2C_SAR + */ +static inline void regi2c_ctrl_ll_i2c_saradc_enable(void) +{ + CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M); + SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M); + CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); + SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h index 4695c6c365..420a694c4f 100644 --- a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h @@ -30,25 +30,6 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) -#define TEMPERATURE_SENSOR_LL_RANGE_NUM (5) - -typedef struct { - int offset; - int reg_val; - int range_min; - int range_max; - int error_max; -} temp_sensor_ll_attribute_t; - -static const temp_sensor_ll_attribute_t temp_sensor_ll_attributes[TEMPERATURE_SENSOR_LL_RANGE_NUM] = { - /*Offset, reg_val, min, max, error */ - { -2, 5, 50, 125, 3}, - { -1, 7, 20, 100, 2}, - { 0, 15, -10, 80, 1}, - { 1, 11, -30, 50, 2}, - { 2, 10, -40, 20, 3}, -}; - /** * @brief Enable the temperature sensor power. * @@ -79,16 +60,12 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl } /** - * @brief Set the hardware range, you can refer to the table ``temp_sensor_ll_attributes`` + * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes`` * - * @param tsens_dac ``reg_val`` in table ``temp_sensor_ll_attributes`` + * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes`` */ static inline void temperature_sensor_ll_set_range(uint32_t tsens_dac) { - CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PD_M); - SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_FORCE_PU_M); - CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); - SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, tsens_dac); } diff --git a/components/hal/esp32s3/include/hal/regi2c_ctrl_ll.h b/components/hal/esp32s3/include/hal/regi2c_ctrl_ll.h index 73d90128e5..8c1cce5d35 100644 --- a/components/hal/esp32s3/include/hal/regi2c_ctrl_ll.h +++ b/components/hal/esp32s3/include/hal/regi2c_ctrl_ll.h @@ -35,6 +35,7 @@ static inline void regi2c_ctrl_ll_i2c_bbpll_enable(void) */ static inline void regi2c_ctrl_ll_i2c_saradc_enable(void) { + SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU); CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); } diff --git a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h index b67cfa5d31..3edea92f8b 100644 --- a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h @@ -30,25 +30,6 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) -#define TEMPERATURE_SENSOR_LL_RANGE_NUM (5) - -typedef struct { - int offset; - int reg_val; - int range_min; - int range_max; - int error_max; -} temp_sensor_ll_attribute_t; - -static const temp_sensor_ll_attribute_t temp_sensor_ll_attributes[TEMPERATURE_SENSOR_LL_RANGE_NUM] = { - /*Offset reg_val min max error */ - {-2, 5, 50, 125, 3}, - {-1, 7, 20, 100, 2}, - { 0, 15, -10, 80, 1}, - { 1, 11, -30, 50, 2}, - { 2, 10, -40, 20, 3}, -}; - /** * @brief Enable the temperature sensor power. * @@ -79,15 +60,12 @@ static inline void temperature_sensor_ll_clk_sel(temperature_sensor_clk_src_t cl } /** - * @brief Set the hardware range, you can refer to the table ``temp_sensor_ll_attributes`` + * @brief Set the hardware range, you can refer to the table ``temperature_sensor_attributes`` * - * @param tsens_dac ``reg_val`` in table ``temp_sensor_ll_attributes`` + * @param tsens_dac ``reg_val`` in table ``temperature_sensor_attributes`` */ static inline void temperature_sensor_ll_set_range(uint32_t tsens_dac) { - SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU); - CLEAR_PERI_REG_MASK(ANA_CONFIG_REG, I2C_SAR_M); - SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, tsens_dac); } diff --git a/components/soc/esp32c2/CMakeLists.txt b/components/soc/esp32c2/CMakeLists.txt index aa05ea0f13..94ab7b6b20 100644 --- a/components/soc/esp32c2/CMakeLists.txt +++ b/components/soc/esp32c2/CMakeLists.txt @@ -8,6 +8,7 @@ set(srcs "ledc_periph.c" "i2c_periph.c" "uart_periph.c" + "temperature_sensor_periph.c" "timer_periph.c") add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") diff --git a/components/soc/esp32c2/include/soc/Kconfig.soc_caps.in b/components/soc/esp32c2/include/soc/Kconfig.soc_caps.in index f7bcb4a2f2..46b634275e 100644 --- a/components/soc/esp32c2/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32c2/include/soc/Kconfig.soc_caps.in @@ -43,6 +43,10 @@ config SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK bool default y +config SOC_TEMP_SENSOR_SUPPORTED + bool + default y + config SOC_SHA_SUPPORTED bool default y diff --git a/components/soc/esp32c2/include/soc/apb_saradc_struct.h b/components/soc/esp32c2/include/soc/apb_saradc_struct.h index 0927c9593a..aa39a163be 100644 --- a/components/soc/esp32c2/include/soc/apb_saradc_struct.h +++ b/components/soc/esp32c2/include/soc/apb_saradc_struct.h @@ -684,6 +684,7 @@ typedef struct { volatile apb_saradc_apb_ctrl_date_reg_t saradc_apb_ctrl_date; } apb_dev_t; +extern apb_dev_t APB_SARADC; #ifndef __cplusplus _Static_assert(sizeof(apb_dev_t) == 0x400, "Invalid size of apb_dev_t structure"); diff --git a/components/soc/esp32c2/include/soc/periph_defs.h b/components/soc/esp32c2/include/soc/periph_defs.h index ad812d4eb1..68344e08d1 100644 --- a/components/soc/esp32c2/include/soc/periph_defs.h +++ b/components/soc/esp32c2/include/soc/periph_defs.h @@ -32,6 +32,7 @@ typedef enum { PERIPH_GDMA_MODULE, PERIPH_SYSTIMER_MODULE, PERIPH_SARADC_MODULE, + PERIPH_TEMPSENSOR_MODULE, PERIPH_MODEM_RPA_MODULE, PERIPH_MODULE_MAX } periph_module_t; diff --git a/components/soc/esp32c2/include/soc/soc_caps.h b/components/soc/esp32c2/include/soc/soc_caps.h index 1a01f5c56c..f87a11e8bf 100644 --- a/components/soc/esp32c2/include/soc/soc_caps.h +++ b/components/soc/esp32c2/include/soc/soc_caps.h @@ -35,7 +35,7 @@ #define SOC_SUPPORTS_SECURE_DL_MODE 1 #define SOC_EFUSE_KEY_PURPOSE_FIELD 0 #define SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK 1 - +#define SOC_TEMP_SENSOR_SUPPORTED 1 #define SOC_SHA_SUPPORTED 1 #define SOC_ECC_SUPPORTED 1 #define SOC_FLASH_ENC_SUPPORTED 1 diff --git a/components/soc/esp32c2/temperature_sensor_periph.c b/components/soc/esp32c2/temperature_sensor_periph.c new file mode 100644 index 0000000000..e75c60b317 --- /dev/null +++ b/components/soc/esp32c2/temperature_sensor_periph.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "soc/temperature_sensor_periph.h" + +const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = { + /*Offset reg_val min max error */ + {-2, 5, 50, 125, 3}, + {-1, 7, 20, 100, 2}, + { 0, 15, -10, 80, 1}, + { 1, 11, -30, 50, 2}, + { 2, 10, -40, 20, 3}, +}; diff --git a/components/soc/esp32c3/CMakeLists.txt b/components/soc/esp32c3/CMakeLists.txt index a59b5173c1..1e1308d5f8 100644 --- a/components/soc/esp32c3/CMakeLists.txt +++ b/components/soc/esp32c3/CMakeLists.txt @@ -11,6 +11,7 @@ set(srcs "i2s_periph.c" "i2c_periph.c" "uart_periph.c" + "temperature_sensor_periph.c" "timer_periph.c") add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") diff --git a/components/soc/esp32c3/temperature_sensor_periph.c b/components/soc/esp32c3/temperature_sensor_periph.c new file mode 100644 index 0000000000..e75c60b317 --- /dev/null +++ b/components/soc/esp32c3/temperature_sensor_periph.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "soc/temperature_sensor_periph.h" + +const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = { + /*Offset reg_val min max error */ + {-2, 5, 50, 125, 3}, + {-1, 7, 20, 100, 2}, + { 0, 15, -10, 80, 1}, + { 1, 11, -30, 50, 2}, + { 2, 10, -40, 20, 3}, +}; diff --git a/components/soc/esp32h2/CMakeLists.txt b/components/soc/esp32h2/CMakeLists.txt index 9558b2a30f..ec6f756872 100644 --- a/components/soc/esp32h2/CMakeLists.txt +++ b/components/soc/esp32h2/CMakeLists.txt @@ -11,6 +11,7 @@ set(srcs "i2s_periph.c" "i2c_periph.c" "uart_periph.c" + "temperature_sensor_periph.c" "timer_periph.c") add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") diff --git a/components/soc/esp32h2/temperature_sensor_periph.c b/components/soc/esp32h2/temperature_sensor_periph.c new file mode 100644 index 0000000000..e75c60b317 --- /dev/null +++ b/components/soc/esp32h2/temperature_sensor_periph.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "soc/temperature_sensor_periph.h" + +const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = { + /*Offset reg_val min max error */ + {-2, 5, 50, 125, 3}, + {-1, 7, 20, 100, 2}, + { 0, 15, -10, 80, 1}, + { 1, 11, -30, 50, 2}, + { 2, 10, -40, 20, 3}, +}; diff --git a/components/soc/esp32s2/CMakeLists.txt b/components/soc/esp32s2/CMakeLists.txt index 218b4bdbb8..5489dfa7bf 100644 --- a/components/soc/esp32s2/CMakeLists.txt +++ b/components/soc/esp32s2/CMakeLists.txt @@ -17,6 +17,7 @@ set(srcs "touch_sensor_periph.c" "uart_periph.c" "usb_periph.c" + "temperature_sensor_periph.c" "usb_phy_periph.c") add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") diff --git a/components/soc/esp32s2/temperature_sensor_periph.c b/components/soc/esp32s2/temperature_sensor_periph.c new file mode 100644 index 0000000000..e75c60b317 --- /dev/null +++ b/components/soc/esp32s2/temperature_sensor_periph.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "soc/temperature_sensor_periph.h" + +const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = { + /*Offset reg_val min max error */ + {-2, 5, 50, 125, 3}, + {-1, 7, 20, 100, 2}, + { 0, 15, -10, 80, 1}, + { 1, 11, -30, 50, 2}, + { 2, 10, -40, 20, 3}, +}; diff --git a/components/soc/esp32s3/CMakeLists.txt b/components/soc/esp32s3/CMakeLists.txt index 53914f113e..a1f1babd58 100644 --- a/components/soc/esp32s3/CMakeLists.txt +++ b/components/soc/esp32s3/CMakeLists.txt @@ -18,6 +18,7 @@ set(srcs "spi_periph.c" "timer_periph.c" "touch_sensor_periph.c" + "temperature_sensor_periph.c" "uart_periph.c" "usb_periph.c" "usb_phy_periph.c") diff --git a/components/soc/esp32s3/temperature_sensor_periph.c b/components/soc/esp32s3/temperature_sensor_periph.c new file mode 100644 index 0000000000..e75c60b317 --- /dev/null +++ b/components/soc/esp32s3/temperature_sensor_periph.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "soc/temperature_sensor_periph.h" + +const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM] = { + /*Offset reg_val min max error */ + {-2, 5, 50, 125, 3}, + {-1, 7, 20, 100, 2}, + { 0, 15, -10, 80, 1}, + { 1, 11, -30, 50, 2}, + { 2, 10, -40, 20, 3}, +}; diff --git a/components/soc/include/soc/temperature_sensor_periph.h b/components/soc/include/soc/temperature_sensor_periph.h new file mode 100644 index 0000000000..7374281935 --- /dev/null +++ b/components/soc/include/soc/temperature_sensor_periph.h @@ -0,0 +1,27 @@ +/* + * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define TEMPERATURE_SENSOR_ATTR_RANGE_NUM (5) + +typedef struct { + int offset; + int reg_val; + int range_min; + int range_max; + int error_max; +} temperature_sensor_attribute_t; + +extern const temperature_sensor_attribute_t temperature_sensor_attributes[TEMPERATURE_SENSOR_ATTR_RANGE_NUM]; + +#ifdef __cplusplus +} +#endif diff --git a/docs/docs_not_updated/esp32c2.txt b/docs/docs_not_updated/esp32c2.txt index 9c258653fe..36c818816d 100644 --- a/docs/docs_not_updated/esp32c2.txt +++ b/docs/docs_not_updated/esp32c2.txt @@ -86,7 +86,6 @@ api-reference/storage/index api-reference/peripherals/adc api-reference/peripherals/sdspi_host api-reference/peripherals/lcd -api-reference/peripherals/temp_sensor api-reference/kconfig api-reference/network/esp_openthread api-reference/network/esp_eth diff --git a/examples/peripherals/temp_sensor/README.md b/examples/peripherals/temp_sensor/README.md index 13ae8a2546..c0b03f9c9f 100644 --- a/examples/peripherals/temp_sensor/README.md +++ b/examples/peripherals/temp_sensor/README.md @@ -1,9 +1,9 @@ -| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | +| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C2 | +| ----------------- | -------- | -------- | -------- | -------- | # Temperature Sensor Example -The ESP32-S2/C3/S3 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC. +The ESP32-S2/C3/S3/C2 has a built-in temperature sensor. The temperature sensor module contains an 8-bit Sigma-Delta ADC and a temperature offset DAC. The conversion relationship is the first two columns of the table below. Among them, `offset = 0`(default) is the main measurement option, and other values are extended measurement options. @@ -21,7 +21,7 @@ Before project configuration and build, be sure to set the correct chip target u ### Hardware Required -* A development board with ESP32-S2/C3/S3 SoC (e.g., ESP32-S2-Saola-1, ESP32-S2-DevKitM-1, ESP32-C3-DevKitM-1, ESP32-S3-WROOM-1, etc.) +* A development board with ESP32-S2/C3/S3/C2 SoC (e.g., ESP32-S2-Saola-1, ESP32-S2-DevKitM-1, ESP32-C3-DevKitM-1, ESP32-S3-WROOM-1, etc.) * A USB cable for power supply and programming ### Build and Flash diff --git a/examples/peripherals/temp_sensor/pytest_temp_sensor_example.py b/examples/peripherals/temp_sensor/pytest_temp_sensor_example.py index 60774eeb27..161f508c50 100644 --- a/examples/peripherals/temp_sensor/pytest_temp_sensor_example.py +++ b/examples/peripherals/temp_sensor/pytest_temp_sensor_example.py @@ -8,6 +8,7 @@ from pytest_embedded.dut import Dut @pytest.mark.esp32s2 @pytest.mark.esp32c3 @pytest.mark.esp32s3 +@pytest.mark.esp32c2 @pytest.mark.generic def test_temp_sensor_example(dut: Dut) -> None: dut.expect_exact('Install temperature sensor')