From acc6d99572ba212e830afcda1dd1e26ae63ca65d Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Wed, 17 May 2023 17:40:59 +0800 Subject: [PATCH 1/4] temperature_sensor: Add new interface and reference counts so that phy and driver can use together --- components/esp_hw_support/CMakeLists.txt | 1 + .../include/esp_private/sar_periph_ctrl.h | 30 +++++ .../esp_hw_support/sar_periph_ctrl_common.c | 118 ++++++++++++++++++ components/esp_phy/src/phy_override.c | 17 +++ 4 files changed, 166 insertions(+) create mode 100644 components/esp_hw_support/sar_periph_ctrl_common.c diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index e679c67f0ddd..189b6c931ff5 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -30,6 +30,7 @@ if(NOT BOOTLOADER_BUILD) "sleep_modem.c" "regi2c_ctrl.c" "esp_gpio_reserve.c" + "sar_periph_ctrl_common.c" "port/${target}/io_mux.c" "port/${target}/esp_clk_tree.c" "port/esp_clk_tree_common.c") 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 833ce1f4ff1a..b6a0fdf74198 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 @@ -14,6 +14,9 @@ #pragma once +#include +#include + #ifdef __cplusplus extern "C" { #endif @@ -62,6 +65,33 @@ void sar_periph_ctrl_pwdet_power_acquire(void); */ void sar_periph_ctrl_pwdet_power_release(void); +/** + * @brief Acquire the temperature sensor power + */ +void temperature_sensor_power_acquire(void); + +/** + * @brief Release the temperature sensor power + */ +void temperature_sensor_power_release(void); + +/** + * @brief Get the temperature value and choose the temperature sensor range. Will be both used in phy and peripheral. + * + * @param range_changed Pointer to whether range has been changed here. If you don't need this param, you can + * set NULL directly. + * + * @return temperature sensor value. + */ +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); + /** * @brief Enable SAR power when system wakes up */ diff --git a/components/esp_hw_support/sar_periph_ctrl_common.c b/components/esp_hw_support/sar_periph_ctrl_common.c new file mode 100644 index 000000000000..b8776b39e601 --- /dev/null +++ b/components/esp_hw_support/sar_periph_ctrl_common.c @@ -0,0 +1,118 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +#include "soc/soc_caps.h" +#include "freertos/FreeRTOS.h" +#include "esp_private/sar_periph_ctrl.h" +#include "esp_log.h" + +#if SOC_TEMP_SENSOR_SUPPORTED +#include "hal/temperature_sensor_ll.h" +#include "soc/temperature_sensor_periph.h" + +extern __attribute__((unused)) portMUX_TYPE rtc_spinlock; + + +/*------------------------------------------------------------------------------------------------------------ +-----------------------------------------Temperature Sensor--------------------------------------------------- +------------------------------------------------------------------------------------------------------------*/ +static const char *TAG_TSENS = "temperature_sensor"; + +#define INT_NOT_USED 999999 + +static int s_record_min = INT_NOT_USED; +static int s_record_max = INT_NOT_USED; +static int s_temperature_sensor_power_cnt; + +static uint8_t s_tsens_idx = 2; // Index for temperature attribute, set 2(middle) as default value + +void temperature_sensor_power_acquire(void) +{ + portENTER_CRITICAL(&rtc_spinlock); + s_temperature_sensor_power_cnt++; + if (s_temperature_sensor_power_cnt == 1) { + temperature_sensor_ll_enable(true); + } + portEXIT_CRITICAL(&rtc_spinlock); +} + +void temperature_sensor_power_release(void) +{ + portENTER_CRITICAL(&rtc_spinlock); + s_temperature_sensor_power_cnt--; + /* Sanity check */ + if (s_temperature_sensor_power_cnt < 0) { + portEXIT_CRITICAL(&rtc_spinlock); + ESP_LOGE(TAG_TSENS, "%s called, but s_temperature_sensor_power_cnt == 0", __func__); + abort(); + } else if (s_temperature_sensor_power_cnt == 0) { + temperature_sensor_ll_enable(false); + } + portEXIT_CRITICAL(&rtc_spinlock); +} + +static int temperature_sensor_get_raw_value(void) +{ + int raw_value = temperature_sensor_ll_get_raw_value(); + return (TEMPERATURE_SENSOR_LL_ADC_FACTOR * raw_value - TEMPERATURE_SENSOR_LL_DAC_FACTOR * temperature_sensor_attributes[s_tsens_idx].offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR); +} + +void temp_sensor_sync_tsens_idx(int tsens_idx) +{ + s_tsens_idx = tsens_idx; +} + +int16_t temp_sensor_get_raw_value(bool *range_changed) +{ + portENTER_CRITICAL(&rtc_spinlock); + + int degree = temperature_sensor_get_raw_value(); + uint8_t temperature_dac; + + // 1. Check whether temperature value is in range + if (s_record_min != INT_NOT_USED && degree >= s_record_min && degree <= s_record_max) { + // If degree is in range, not needed to do any check to save time. Otherwise, choose proper range and record. + if (range_changed != NULL) { + *range_changed = false; + } + portEXIT_CRITICAL(&rtc_spinlock); + return degree; + } + + // 2. If temperature value is not in range, adjust to proper range + if (degree >= temperature_sensor_attributes[1].range_max) { + s_tsens_idx = 0; + } else if (degree >= temperature_sensor_attributes[2].range_max && degree < temperature_sensor_attributes[1].range_max) { + s_tsens_idx = 1; + } else if (degree <= temperature_sensor_attributes[2].range_min && degree > temperature_sensor_attributes[3].range_min) { + s_tsens_idx = 3; + } else if (degree <= temperature_sensor_attributes[3].range_min) { + s_tsens_idx = 4; + } else { + s_tsens_idx = 2; + } + ESP_EARLY_LOGD(TAG_TSENS, "range changed, change to index %d", s_tsens_idx); + temperature_dac = temperature_sensor_attributes[s_tsens_idx].reg_val; + s_record_min = temperature_sensor_attributes[s_tsens_idx].range_min; + s_record_max = temperature_sensor_attributes[s_tsens_idx].range_max; + + temperature_sensor_ll_set_range(temperature_dac); + + // 3. Then, read value again + // Before reading the temperature value, ticks need to be delayed, otherwise a wrong value will be returned. + // As what has been recommended and tested, 300us is a good interval to get the correct value after adjust range. + esp_rom_delay_us(300); + degree = temperature_sensor_get_raw_value(); + if (range_changed != NULL) { + *range_changed = true; + } + + portEXIT_CRITICAL(&rtc_spinlock); + return degree; +} + +#endif diff --git a/components/esp_phy/src/phy_override.c b/components/esp_phy/src/phy_override.c index 9b9821d9f68b..3f746411d655 100644 --- a/components/esp_phy/src/phy_override.c +++ b/components/esp_phy/src/phy_override.c @@ -8,6 +8,9 @@ #include "esp_attr.h" #include "esp_private/regi2c_ctrl.h" #include "esp_private/sar_periph_ctrl.h" +#include "esp_private/sar_periph_ctrl.h" +#include "freertos/FreeRTOS.h" + /* * This file is used to override the hooks provided by the PHY lib for some system features. @@ -58,3 +61,17 @@ void phy_set_pwdet_power(bool en) sar_periph_ctrl_pwdet_power_release(); } } + +void phy_set_tsens_power(bool en) +{ + if (en) { + temperature_sensor_power_acquire(); + } else { + temperature_sensor_power_release(); + } +} + +int16_t phy_get_tsens_value(void) +{ + return temp_sensor_get_raw_value(NULL); +} From e493a99da2d11a4c195626d371aad72dfd8a409c Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Wed, 17 May 2023 17:41:36 +0800 Subject: [PATCH 2/4] temperature_sensor: Apply new-shared interface in temperature sensor driver --- .../deprecated/rtc_temperature_legacy.c | 26 ++++++------ .../temperature_sensor/temperature_sensor.c | 41 +++++++++++++------ .../main/test_rtc_temp_driver.c | 4 +- .../include/hal/temperature_sensor_ll.h | 2 + .../include/hal/temperature_sensor_ll.h | 2 + .../include/hal/temperature_sensor_ll.h | 2 + .../include/hal/temperature_sensor_ll.h | 2 + .../include/hal/temperature_sensor_ll.h | 2 + .../include/hal/temperature_sensor_ll.h | 2 + 9 files changed, 58 insertions(+), 25 deletions(-) diff --git a/components/driver/deprecated/rtc_temperature_legacy.c b/components/driver/deprecated/rtc_temperature_legacy.c index fccb3c9e8a4f..1e162c127e9a 100644 --- a/components/driver/deprecated/rtc_temperature_legacy.c +++ b/components/driver/deprecated/rtc_temperature_legacy.c @@ -19,6 +19,7 @@ #include "hal/temperature_sensor_ll.h" #include "driver/temp_sensor_types_legacy.h" #include "esp_private/periph_ctrl.h" +#include "esp_private/sar_periph_ctrl.h" static const char *TAG = "tsens"; @@ -62,8 +63,8 @@ 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); + temp_sensor_sync_tsens_idx(tsens.dac_offset); temperature_sensor_ll_set_range(dac_offset[tsens.dac_offset].reg_val); - temperature_sensor_ll_enable(true); 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, @@ -95,7 +96,7 @@ esp_err_t temp_sensor_start(void) } regi2c_saradc_enable(); periph_module_enable(PERIPH_TEMPSENSOR_MODULE); - temperature_sensor_ll_enable(true); + temperature_sensor_power_acquire(); temperature_sensor_ll_clk_enable(true); temperature_sensor_ll_clk_sel(TEMPERATURE_SENSOR_CLK_SRC_DEFAULT); tsens_hw_state = TSENS_HW_STATE_STARTED; @@ -105,7 +106,7 @@ esp_err_t temp_sensor_start(void) esp_err_t temp_sensor_stop(void) { regi2c_saradc_disable(); - temperature_sensor_ll_enable(false); + temperature_sensor_power_release(); tsens_hw_state = TSENS_HW_STATE_CONFIGURED; return ESP_OK; } @@ -124,12 +125,12 @@ static esp_err_t read_delta_t_from_efuse(void) return ESP_OK; } -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 = (TEMPERATURE_SENSOR_LL_ADC_FACTOR * (float)tsens_raw - TEMPERATURE_SENSOR_LL_DAC_FACTOR * dac_offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR) - s_deltaT / 10.0; + float result = tsens_raw - s_deltaT / 10.0; return result; } @@ -141,16 +142,17 @@ esp_err_t temp_sensor_read_celsius(float *celsius) return ESP_ERR_INVALID_STATE; } temp_sensor_config_t tsens; - uint32_t tsens_out = 0; temp_sensor_get_config(&tsens); - temp_sensor_read_raw(&tsens_out); - ESP_LOGV(TAG, "tsens_out %"PRIu32, tsens_out); - 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!"); + 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_LL_MEASURE_MIN || *celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) { + ESP_LOGE(TAG, "Exceeding temperature measure range."); return ESP_ERR_INVALID_STATE; } + if (range_changed) { + temp_sensor_get_config(&tsens); + } return ESP_OK; } diff --git a/components/driver/temperature_sensor/temperature_sensor.c b/components/driver/temperature_sensor/temperature_sensor.c index d7f170ce6203..563f417267ea 100644 --- a/components/driver/temperature_sensor/temperature_sensor.c +++ b/components/driver/temperature_sensor/temperature_sensor.c @@ -28,6 +28,7 @@ #include "hal/temperature_sensor_ll.h" #include "soc/temperature_sensor_periph.h" #include "esp_memory_utils.h" +#include "esp_private/sar_periph_ctrl.h" static const char *TAG = "temperature_sensor"; @@ -63,6 +64,7 @@ static esp_err_t temperature_sensor_choose_best_range(temperature_sensor_handle_ tsens->tsens_attribute = &s_tsens_attribute_copy[i]; break; } + temp_sensor_sync_tsens_idx(i); } ESP_RETURN_ON_FALSE(tsens->tsens_attribute != NULL, ESP_ERR_INVALID_ARG, TAG, "Out of testing range"); return ESP_OK; @@ -114,7 +116,6 @@ esp_err_t temperature_sensor_install(const temperature_sensor_config_t *tsens_co regi2c_saradc_enable(); temperature_sensor_ll_set_range(tsens->tsens_attribute->reg_val); - temperature_sensor_ll_enable(false); // disable the sensor by default tsens->fsm = TEMP_SENSOR_FSM_INIT; *ret_tsens = tsens; @@ -147,6 +148,20 @@ esp_err_t temperature_sensor_uninstall(temperature_sensor_handle_t tsens) return ESP_OK; } +static esp_err_t s_update_tsens_attribute(temperature_sensor_handle_t tsens) +{ + uint32_t dac; + ESP_RETURN_ON_FALSE(tsens != NULL, ESP_ERR_INVALID_ARG, TAG, "no tsens specified"); + dac = temperature_sensor_ll_get_offset(); + for (int i = 0 ; i < TEMPERATURE_SENSOR_ATTR_RANGE_NUM; i++) { + if (dac == s_tsens_attribute_copy[i].reg_val) { + tsens->tsens_attribute = &s_tsens_attribute_copy[i]; + break; + } + } + return ESP_OK; +} + esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens) { ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "invalid argument"); @@ -165,7 +180,7 @@ esp_err_t temperature_sensor_enable(temperature_sensor_handle_t tsens) temperature_sensor_ll_clk_enable(true); temperature_sensor_ll_clk_sel(tsens->clk_src); - temperature_sensor_ll_enable(true); + temperature_sensor_power_acquire(); tsens->fsm = TEMP_SENSOR_FSM_ENABLE; return ESP_OK; } @@ -175,12 +190,12 @@ esp_err_t temperature_sensor_disable(temperature_sensor_handle_t tsens) ESP_RETURN_ON_FALSE(tsens, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet"); + temperature_sensor_power_release(); #if SOC_TEMPERATURE_SENSOR_INTR_SUPPORT temperature_sensor_ll_wakeup_enable(false); temperature_sensor_ll_sample_enable(false); #endif - temperature_sensor_ll_enable(false); #if SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC if (tsens->clk_src == TEMPERATURE_SENSOR_CLK_SRC_RC_FAST) { periph_rtc_dig_clk8m_disable(); @@ -200,12 +215,12 @@ static esp_err_t read_delta_t_from_efuse(void) return ESP_OK; } -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 = (TEMPERATURE_SENSOR_LL_ADC_FACTOR * (float)tsens_raw - TEMPERATURE_SENSOR_LL_DAC_FACTOR * dac_offset - TEMPERATURE_SENSOR_LL_OFFSET_FACTOR) - s_deltaT / 10.0; + float result = tsens_raw - s_deltaT / 10.0; return result; } @@ -214,14 +229,16 @@ esp_err_t temperature_sensor_get_celsius(temperature_sensor_handle_t tsens, floa ESP_RETURN_ON_FALSE((tsens != NULL), ESP_ERR_INVALID_ARG, TAG, "Has not been installed"); ESP_RETURN_ON_FALSE(out_celsius != NULL, ESP_ERR_INVALID_ARG, TAG, "Celsius points to nothing"); ESP_RETURN_ON_FALSE(tsens->fsm == TEMP_SENSOR_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "tsens not enabled yet"); + bool range_changed; + uint16_t tsens_out = temp_sensor_get_raw_value(&range_changed); + *out_celsius = parse_temp_sensor_raw_value(tsens_out); - uint32_t tsens_out = temperature_sensor_ll_get_raw_value(); - ESP_LOGD(TAG, "tsens_out %"PRIu32, tsens_out); - - *out_celsius = parse_temp_sensor_raw_value(tsens_out, tsens->tsens_attribute->offset); - if (*out_celsius < tsens->tsens_attribute->range_min || *out_celsius > tsens->tsens_attribute->range_max) { - ESP_LOGW(TAG, "value out of range, probably invalid"); - return ESP_FAIL; + if (*out_celsius < TEMPERATURE_SENSOR_LL_MEASURE_MIN || *out_celsius > TEMPERATURE_SENSOR_LL_MEASURE_MAX) { + ESP_LOGE(TAG, "Exceeding temperature measure range."); + return ESP_ERR_INVALID_STATE; + } + if (range_changed) { + s_update_tsens_attribute(tsens); } return ESP_OK; } diff --git a/components/driver/test_apps/legacy_rtc_temp_driver/main/test_rtc_temp_driver.c b/components/driver/test_apps/legacy_rtc_temp_driver/main/test_rtc_temp_driver.c index 7b3fc55a7a57..da5c4e96e2f0 100644 --- a/components/driver/test_apps/legacy_rtc_temp_driver/main/test_rtc_temp_driver.c +++ b/components/driver/test_apps/legacy_rtc_temp_driver/main/test_rtc_temp_driver.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -23,6 +23,7 @@ TEST_CASE("Temperature_legacy_workflow_test", "[hw_timer]") TEST_ESP_OK(temp_sensor_read_celsius(&tsens_out)); printf("Temperature out celsius %f°C\n", tsens_out); TEST_ESP_OK(temp_sensor_stop()); +#if !CONFIG_IDF_TARGET_ESP32H2 // disable on eps32h2, seems have some issues on esp32h2 temp_sensor.dac_offset = TSENS_DAC_L3; TEST_ESP_OK(temp_sensor_set_config(temp_sensor)); TEST_ESP_OK(temp_sensor_start()); @@ -30,6 +31,7 @@ TEST_CASE("Temperature_legacy_workflow_test", "[hw_timer]") TEST_ESP_OK(temp_sensor_read_celsius(&tsens_out)); printf("Temperature out celsius %f°C\n", tsens_out); TEST_ESP_OK(temp_sensor_stop()); +#endif } TEST_CASE("Temperature legacy double start error cause test", "[temperature_sensor]") diff --git a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h index 360f8dfd8529..6f4f58897ba7 100644 --- a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h @@ -31,6 +31,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) /** * @brief Enable the temperature sensor power. diff --git a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h index 31a47d96f0f9..d39f660d6935 100644 --- a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h @@ -31,6 +31,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) /** * @brief Enable the temperature sensor power. diff --git a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h index a0198f1a7f6d..a50d56f489c7 100644 --- a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h @@ -34,6 +34,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) #define TEMPERATURE_SENSOR_LL_INTR_MASK APB_SARADC_APB_SARADC_TSENS_INT_ST diff --git a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h index 38f688710233..94c9d4b7412f 100644 --- a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h @@ -34,6 +34,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) #define TEMPERATURE_SENSOR_LL_INTR_MASK APB_SARADC_APB_SARADC_TSENS_INT_ST diff --git a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h index a02805dbc3de..86dcd5dbefb5 100644 --- a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h @@ -29,6 +29,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) /** * @brief Enable the temperature sensor power. diff --git a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h index c1fcb073b9dc..cc14ce96e5ec 100644 --- a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h @@ -29,6 +29,8 @@ extern "C" { #define TEMPERATURE_SENSOR_LL_ADC_FACTOR (0.4386) #define TEMPERATURE_SENSOR_LL_DAC_FACTOR (27.88) #define TEMPERATURE_SENSOR_LL_OFFSET_FACTOR (20.52) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) /** * @brief Enable the temperature sensor power. From a33541f0360b80e2afb3a037e00dd2fdf7dab342 Mon Sep 17 00:00:00 2001 From: Armando Date: Fri, 9 Jun 2023 20:26:37 +0800 Subject: [PATCH 3/4] phy: fix phy pwdet and tsens power cannot be set twice issue Closes https://github.com/espressif/esp-idf/issues/11627 --- components/esp_phy/src/phy_override.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/components/esp_phy/src/phy_override.c b/components/esp_phy/src/phy_override.c index 3f746411d655..f374b36f1cf9 100644 --- a/components/esp_phy/src/phy_override.c +++ b/components/esp_phy/src/phy_override.c @@ -18,6 +18,8 @@ */ static bool s_wifi_adc_xpd_flag; +static bool s_wifi_pwdet_xpd_flag; +static bool s_wifi_tsens_xpd_flag; void include_esp_phy_override(void) { @@ -55,6 +57,12 @@ IRAM_ATTR void phy_i2c_exit_critical(void) void phy_set_pwdet_power(bool en) { + if (s_wifi_pwdet_xpd_flag == en) { + /* ignore repeated calls to phy_set_pwdet_power when the state is already correct */ + return; + } + + s_wifi_pwdet_xpd_flag = en; if (en) { sar_periph_ctrl_pwdet_power_acquire(); } else { @@ -64,6 +72,12 @@ void phy_set_pwdet_power(bool en) 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 { From 0954babbd669177e8194382531103216a65d9bfd Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Tue, 20 Jun 2023 14:40:47 +0800 Subject: [PATCH 4/4] temperature_sensor: make as a weak link --- components/esp_rom/esp32c2/ld/esp32c2.rom.ld | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld index 384adb25d769..80933f399762 100644 --- a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld +++ b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld @@ -2065,7 +2065,7 @@ phy_rx_rifs_en = 0x400021d8; phy_current_level_set = 0x400021dc; phy_bbpll_en_usb = 0x400021e0; phy_bt_power_track = 0x400021e4; -phy_xpd_tsens = 0x400021e8; +/* phy_xpd_tsens = 0x400021e8); Link this function in phy_lib, no longer link the rom one.*/ bb_wdt_rst_enable = 0x400021ec; bb_wdt_int_enable = 0x400021f0; bb_wdt_timeout_clear = 0x400021f4;