diff --git a/components/driver/esp32c3/include/driver/temp_sensor.h b/components/driver/esp32c3/include/driver/temp_sensor.h index c9abd0e794..028e207c58 100644 --- a/components/driver/esp32c3/include/driver/temp_sensor.h +++ b/components/driver/esp32c3/include/driver/temp_sensor.h @@ -31,13 +31,16 @@ typedef enum { TSENS_DAC_DEFAULT = TSENS_DAC_L2, } temp_sensor_dac_offset_t; +/** + * @brief tsens dac offset, internal use only + */ typedef struct { - int index; - int offset; - int set_val; - int range_min; - int range_max; - int error_max; + int index; /*!< temperature dac offset index */ + int offset; /*!< temperature dac offset */ + int set_val; /*!< temperature dac set value */ + int range_min; /*!< temperature current range minimum */ + int range_max; /*!< temperature current range maximum */ + int error_max; /*!< temperature current range error */ } tsens_dac_offset_t; extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX]; diff --git a/components/driver/esp32c3/rtc_tempsensor.c b/components/driver/esp32c3/rtc_tempsensor.c index e1537708d7..1d7cbfd0bd 100644 --- a/components/driver/esp32c3/rtc_tempsensor.c +++ b/components/driver/esp32c3/rtc_tempsensor.c @@ -28,6 +28,7 @@ #include "regi2c_ctrl.h" #include "esp32c3/rom/ets_sys.h" #include "esp32c3/esp_efuse_rtc_calib.h" +#include "esp_private/sar_periph_ctrl.h" static const char *TAG = "tsens"; @@ -38,6 +39,8 @@ static const char *TAG = "tsens"; } \ }) #define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */ +#define TEMPERATURE_SENSOR_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_MEASURE_MIN (-40) const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { /* DAC Offset reg_val min max error */ @@ -58,7 +61,7 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val); APB_SARADC.apb_tsens_ctrl.tsens_clk_div = tsens.clk_div; APB_SARADC.apb_tsens_ctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT; - APB_SARADC.apb_tsens_ctrl2.tsens_xpd_force = 1; + temp_sensor_sync_tsens_idx(tsens.dac_offset); ESP_LOGD(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C", dac_offset[tsens.dac_offset].range_min, dac_offset[tsens.dac_offset].range_max, @@ -86,13 +89,13 @@ esp_err_t temp_sensor_start(void) { REG_SET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_TSENS_CLK_EN); APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 1; - APB_SARADC.apb_tsens_ctrl.tsens_pu = 1; + temperature_sensor_power_acquire(); return ESP_OK; } esp_err_t temp_sensor_stop(void) { - APB_SARADC.apb_tsens_ctrl.tsens_pu = 0; + temperature_sensor_power_release(); APB_SARADC.apb_tsens_ctrl2.tsens_clk_sel = 0; return ESP_OK; } @@ -117,12 +120,12 @@ static void read_delta_t_from_efuse(void) ESP_LOGD(TAG, "s_deltaT = %f", s_deltaT); } -static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset) +static float parse_temp_sensor_raw_value(uint32_t tsens_raw) { 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; + float result = tsens_raw - s_deltaT / 10.0; return result; } @@ -130,18 +133,16 @@ esp_err_t temp_sensor_read_celsius(float *celsius) { TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG); temp_sensor_config_t tsens; - uint32_t tsens_out = 0; - esp_err_t ret = temp_sensor_get_config(&tsens); - if (ret == ESP_OK) { - ret = temp_sensor_read_raw(&tsens_out); - 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 = 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; - } + temp_sensor_get_config(&tsens); + bool range_changed; + uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed); + *celsius = parse_temp_sensor_raw_value(tsens_out); + if (*celsius < TEMPERATURE_SENSOR_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_MEASURE_MAX) { + ESP_LOGE(TAG, "Exceeding temperature measure range."); + return ESP_ERR_INVALID_STATE; } - return ret; + if (range_changed) { + temp_sensor_get_config(&tsens); + } + return ESP_OK; } diff --git a/components/driver/esp32s2/include/driver/temp_sensor.h b/components/driver/esp32s2/include/driver/temp_sensor.h index 1023c83105..8b5f08cd32 100644 --- a/components/driver/esp32s2/include/driver/temp_sensor.h +++ b/components/driver/esp32s2/include/driver/temp_sensor.h @@ -42,13 +42,16 @@ typedef struct { uint8_t clk_div; /*!< Default: 6 */ } temp_sensor_config_t; +/** + * @brief tsens dac offset, internal use only + */ typedef struct { - int index; - int offset; - int set_val; - int range_min; - int range_max; - int error_max; + int index; /*!< temperature dac offset index */ + int offset; /*!< temperature dac offset */ + int set_val; /*!< temperature dac set value */ + int range_min; /*!< temperature current range minimum */ + int range_max; /*!< temperature current range maximum */ + int error_max; /*!< temperature current range error */ } tsens_dac_offset_t; extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX]; diff --git a/components/driver/esp32s2/rtc_tempsensor.c b/components/driver/esp32s2/rtc_tempsensor.c index ffbc7d0b41..5cb48d10f6 100644 --- a/components/driver/esp32s2/rtc_tempsensor.c +++ b/components/driver/esp32s2/rtc_tempsensor.c @@ -28,6 +28,7 @@ #include "regi2c_ctrl.h" #include "esp_log.h" #include "esp32s2/esp_efuse_rtc_table.h" +#include "esp_private/sar_periph_ctrl.h" static const char *TAG = "tsens"; @@ -38,6 +39,9 @@ static const char *TAG = "tsens"; } \ }) #define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */ +#define TEMPERATURE_SENSOR_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_MEASURE_MIN (-40) + const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { /* DAC Offset reg_val min max error */ @@ -60,11 +64,10 @@ esp_err_t temp_sensor_set_config(temp_sensor_config_t tsens) SET_PERI_REG_MASK(ANA_CONFIG2_REG, ANA_SAR_CFG2_M); REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, dac_offset[tsens.dac_offset].set_val); SENS.sar_tctrl.tsens_clk_div = tsens.clk_div; - SENS.sar_tctrl.tsens_power_up_force = 1; SENS.sar_tctrl2.tsens_xpd_wait = TSENS_XPD_WAIT_DEFAULT; - SENS.sar_tctrl2.tsens_xpd_force = 1; SENS.sar_tctrl2.tsens_reset = 1;// Reset the temp sensor. SENS.sar_tctrl2.tsens_reset = 0;// Clear the reset status. + temp_sensor_sync_tsens_idx(tsens.dac_offset); ESP_LOGI(TAG, "Config temperature range [%d°C ~ %d°C], error < %d°C", dac_offset[tsens.dac_offset].range_min, dac_offset[tsens.dac_offset].range_max, @@ -96,15 +99,15 @@ esp_err_t temp_sensor_start(void) rtc_tsens_mux = xSemaphoreCreateMutex(); } TSENS_CHECK(rtc_tsens_mux != NULL, ESP_ERR_NO_MEM); + temperature_sensor_power_acquire(); SENS.sar_tctrl.tsens_dump_out = 0; SENS.sar_tctrl2.tsens_clkgate_en = 1; - SENS.sar_tctrl.tsens_power_up = 1; return ESP_OK; } esp_err_t temp_sensor_stop(void) { - SENS.sar_tctrl.tsens_power_up = 0; + temperature_sensor_power_release(); SENS.sar_tctrl2.tsens_clkgate_en = 0; if (rtc_tsens_mux != NULL) { vSemaphoreDelete(rtc_tsens_mux); @@ -139,12 +142,12 @@ static void read_delta_t_from_efuse(void) ESP_LOGD(TAG, "s_deltaT = %f\n", s_deltaT); } -static float parse_temp_sensor_raw_value(uint32_t tsens_raw, const int dac_offset) +static float parse_temp_sensor_raw_value(uint32_t tsens_raw) { 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; + float result = tsens_raw - s_deltaT / 10.0; return result; } @@ -152,17 +155,17 @@ esp_err_t temp_sensor_read_celsius(float *celsius) { TSENS_CHECK(celsius != NULL, ESP_ERR_INVALID_ARG); temp_sensor_config_t tsens; - uint32_t tsens_out = 0; - esp_err_t ret = temp_sensor_get_config(&tsens); - if (ret == ESP_OK) { - 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->offset); - if (*celsius < dac->range_min || *celsius > dac->range_max) { - ESP_LOGW(TAG, "Exceeding the temperature range!"); - ret = ESP_ERR_INVALID_STATE; - } + temp_sensor_get_config(&tsens); + bool range_changed; + uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed); + *celsius = parse_temp_sensor_raw_value(tsens_out); + if (*celsius < TEMPERATURE_SENSOR_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_MEASURE_MAX) { + ESP_LOGE(TAG, "Exceeding temperature measure range."); + return ESP_ERR_INVALID_STATE; } - return ret; + if (range_changed) { + temp_sensor_get_config(&tsens); + } + return ESP_OK; + } diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index e3cd878a41..733618e76f 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -9,8 +9,6 @@ set(srcs "compare_set.c" "cpu_util.c") if(NOT BOOTLOADER_BUILD) list(APPEND srcs "clk_ctrl_os.c" "regi2c_ctrl.c") - - list(APPEND requires driver freertos) endif() idf_component_register(SRCS ${srcs} diff --git a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h index d0329c4fd2..42e8ccec11 100644 --- a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h +++ b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h @@ -41,6 +41,14 @@ void temperature_sensor_power_release(void); */ int16_t temp_sensor_get_raw_value(bool *range_changed); +/** + * @brief Synchronize the tsens_idx between sar_periph and driver + * + * @param tsens_idx index value of temperature sensor attribute + */ +void temp_sensor_sync_tsens_idx(int tsens_idx); + + #ifdef __cplusplus } #endif diff --git a/components/esp_hw_support/port/esp32c3/CMakeLists.txt b/components/esp_hw_support/port/esp32c3/CMakeLists.txt index a6300b6297..42f471bda7 100644 --- a/components/esp_hw_support/port/esp32c3/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32c3/CMakeLists.txt @@ -7,7 +7,7 @@ set(srcs "cpu_util_esp32c3.c" "rtc_time.c" ) -if (NOT BOOTLOADER_BUILD) +if(NOT BOOTLOADER_BUILD) list(APPEND srcs "sar_periph_ctrl.c") endif() diff --git a/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c index 2c48cd9646..c8b43f441c 100644 --- a/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c @@ -17,7 +17,6 @@ extern __attribute__((unused)) portMUX_TYPE rtc_spinlock; - /*------------------------------------------------------------------------------------------------------------ -----------------------------------------Temperature Sensor--------------------------------------------------- ------------------------------------------------------------------------------------------------------------*/ diff --git a/components/esp_hw_support/port/esp32s2/CMakeLists.txt b/components/esp_hw_support/port/esp32s2/CMakeLists.txt index 15d60d4832..f86e94b138 100644 --- a/components/esp_hw_support/port/esp32s2/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32s2/CMakeLists.txt @@ -11,7 +11,7 @@ set(srcs "rtc_wdt.c" "regi2c_ctrl.c") -if (NOT BOOTLOADER_BUILD) +if(NOT BOOTLOADER_BUILD) list(APPEND srcs "sar_periph_ctrl.c") endif() diff --git a/components/esp_wifi/src/phy_override.c b/components/esp_wifi/src/phy_override.c index 8b32811cac..cc9ef85507 100644 --- a/components/esp_wifi/src/phy_override.c +++ b/components/esp_wifi/src/phy_override.c @@ -16,6 +16,7 @@ */ static bool s_wifi_adc_xpd_flag; +static bool s_wifi_tsens_xpd_flag; void include_esp_phy_override(void) { @@ -56,6 +57,13 @@ IRAM_ATTR void phy_i2c_exit_critical(void) void phy_set_tsens_power(bool en) { + if (s_wifi_tsens_xpd_flag == en) { + /* ignore repeated calls to phy_set_tsens_power when the state is already correct */ + return; + } + + s_wifi_tsens_xpd_flag = en; + if (en) { temperature_sensor_power_acquire(); } else {