temperature_sensor: Add temperature sensor support for ESP32-C2

This commit is contained in:
Cao Sen Miao 2022-06-14 14:50:35 +08:00
parent 7d68098089
commit 3a820462ac
38 changed files with 388 additions and 113 deletions

View File

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

View File

@ -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();

View File

@ -1,2 +1,2 @@
| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 |
| ----------------- | -------- | -------- | -------- |
| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 |
| ----------------- | -------- | -------- | -------- | -------- |

View File

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

View File

@ -1,2 +1,2 @@
| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 |
| ----------------- | -------- | -------- | -------- |
| Supported Targets | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 |
| ----------------- | -------- | -------- | -------- | -------- |

View File

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

View File

@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <esp_bit_defs.h>
#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;
}

View File

@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <esp_types.h>
#include <esp_err.h>
#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

View File

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

View File

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

View File

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

View File

@ -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 <stdbool.h>
#include <stdlib.h>
#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

View File

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

View File

@ -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);
}

View File

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

View File

@ -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);
}

View File

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

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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}")

View File

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

View File

@ -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");

View File

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

View File

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

View File

@ -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},
};

View File

@ -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}")

View File

@ -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},
};

View File

@ -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}")

View File

@ -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},
};

View File

@ -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}")

View File

@ -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},
};

View File

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

View File

@ -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},
};

View File

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

View File

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

View File

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

View File

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