diff --git a/components/driver/esp32c3/include/driver/temp_sensor.h b/components/driver/esp32c3/include/driver/temp_sensor.h index 2eccefcf0f..c9abd0e794 100644 --- a/components/driver/esp32c3/include/driver/temp_sensor.h +++ b/components/driver/esp32c3/include/driver/temp_sensor.h @@ -31,6 +31,21 @@ typedef enum { TSENS_DAC_DEFAULT = TSENS_DAC_L2, } temp_sensor_dac_offset_t; +typedef struct { + int index; + int offset; + int set_val; + int range_min; + int range_max; + int error_max; +} tsens_dac_offset_t; + +extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX]; + +#define TSENS_ADC_FACTOR (0.4386) +#define TSENS_DAC_FACTOR (27.88) +#define TSENS_SYS_OFFSET (20.52) + /** * @brief Configuration for temperature sensor reading */ diff --git a/components/driver/esp32c3/rtc_tempsensor.c b/components/driver/esp32c3/rtc_tempsensor.c index e7c42cd06a..e1537708d7 100644 --- a/components/driver/esp32c3/rtc_tempsensor.c +++ b/components/driver/esp32c3/rtc_tempsensor.c @@ -38,20 +38,8 @@ static const char *TAG = "tsens"; } \ }) #define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */ -#define TSENS_ADC_FACTOR (0.4386) -#define TSENS_DAC_FACTOR (27.88) -#define TSENS_SYS_OFFSET (20.52) -typedef struct { - int index; - int offset; - int set_val; - int range_min; - int range_max; - int error_max; -} tsens_dac_offset_t; - -static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { +const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { /* DAC Offset reg_val min max error */ {TSENS_DAC_L0, -2, 5, 50, 125, 3}, {TSENS_DAC_L1, -1, 7, 20, 100, 2}, diff --git a/components/driver/esp32s2/include/driver/temp_sensor.h b/components/driver/esp32s2/include/driver/temp_sensor.h index 56907b236c..1023c83105 100644 --- a/components/driver/esp32s2/include/driver/temp_sensor.h +++ b/components/driver/esp32s2/include/driver/temp_sensor.h @@ -42,6 +42,21 @@ typedef struct { uint8_t clk_div; /*!< Default: 6 */ } temp_sensor_config_t; +typedef struct { + int index; + int offset; + int set_val; + int range_min; + int range_max; + int error_max; +} tsens_dac_offset_t; + +extern const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX]; + +#define TSENS_ADC_FACTOR (0.4386) +#define TSENS_DAC_FACTOR (27.88) +#define TSENS_SYS_OFFSET (20.52) + /** * @brief temperature sensor default setting. */ diff --git a/components/driver/esp32s2/rtc_tempsensor.c b/components/driver/esp32s2/rtc_tempsensor.c index 4975faaa63..ffbc7d0b41 100644 --- a/components/driver/esp32s2/rtc_tempsensor.c +++ b/components/driver/esp32s2/rtc_tempsensor.c @@ -38,20 +38,8 @@ static const char *TAG = "tsens"; } \ }) #define TSENS_XPD_WAIT_DEFAULT 0xFF /* Set wait cycle time(8MHz) from power up to reset enable. */ -#define TSENS_ADC_FACTOR (0.4386) -#define TSENS_DAC_FACTOR (27.88) -#define TSENS_SYS_OFFSET (20.52) -typedef struct { - int index; - int offset; - int set_val; - int range_min; - int range_max; - int error_max; -} tsens_dac_offset_t; - -static const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { +const tsens_dac_offset_t dac_offset[TSENS_DAC_MAX] = { /* DAC Offset reg_val min max error */ {TSENS_DAC_L0, -2, 5, 50, 125, 3}, {TSENS_DAC_L1, -1, 7, 20, 100, 2}, diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index 733618e76f..e3cd878a41 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -9,6 +9,8 @@ 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 new file mode 100644 index 0000000000..d0329c4fd2 --- /dev/null +++ b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h @@ -0,0 +1,46 @@ +/* + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * SAR related peripherals are interdependent. This file + * provides a united control to these registers, as multiple + * components require these controls. + * + * See target/sar_periph_ctrl.c to know involved peripherals + */ + +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @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); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_hw_support/port/esp32c3/CMakeLists.txt b/components/esp_hw_support/port/esp32c3/CMakeLists.txt index f8dc50fed7..a6300b6297 100644 --- a/components/esp_hw_support/port/esp32c3/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32c3/CMakeLists.txt @@ -7,6 +7,10 @@ set(srcs "cpu_util_esp32c3.c" "rtc_time.c" ) +if (NOT BOOTLOADER_BUILD) + list(APPEND srcs "sar_periph_ctrl.c") +endif() + add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") diff --git a/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c new file mode 100644 index 0000000000..2c48cd9646 --- /dev/null +++ b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c @@ -0,0 +1,117 @@ +/* + * 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" +#include "soc/apb_saradc_struct.h" +#include "private_include/regi2c_saradc.h" +#include "driver/temp_sensor.h" +#include "regi2c_ctrl.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) { + APB_SARADC.apb_tsens_ctrl.tsens_pu = 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) { + APB_SARADC.apb_tsens_ctrl.tsens_pu = false; + } + portEXIT_CRITICAL(&rtc_spinlock); +} + +static int temperature_sensor_get_raw_value(void) +{ + int raw_value = APB_SARADC.apb_tsens_ctrl.tsens_out; + return (TSENS_ADC_FACTOR * raw_value - TSENS_DAC_FACTOR * dac_offset[s_tsens_idx].offset - TSENS_SYS_OFFSET); +} + +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 >= dac_offset[1].range_max) { + s_tsens_idx = 0; + } else if (degree >= dac_offset[2].range_max && degree < dac_offset[1].range_max) { + s_tsens_idx = 1; + } else if (degree <= dac_offset[2].range_min && degree > dac_offset[3].range_min) { + s_tsens_idx = 3; + } else if (degree <= dac_offset[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 = dac_offset[s_tsens_idx].set_val; + s_record_min = dac_offset[s_tsens_idx].range_min; + s_record_max = dac_offset[s_tsens_idx].range_max; + + REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, 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; +} diff --git a/components/esp_hw_support/port/esp32s2/CMakeLists.txt b/components/esp_hw_support/port/esp32s2/CMakeLists.txt index 28dc4ab6f9..15d60d4832 100644 --- a/components/esp_hw_support/port/esp32s2/CMakeLists.txt +++ b/components/esp_hw_support/port/esp32s2/CMakeLists.txt @@ -11,6 +11,10 @@ set(srcs "rtc_wdt.c" "regi2c_ctrl.c") +if (NOT BOOTLOADER_BUILD) + list(APPEND srcs "sar_periph_ctrl.c") +endif() + add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}") target_sources(${COMPONENT_LIB} PRIVATE "${srcs}") diff --git a/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c new file mode 100644 index 0000000000..f0275cf523 --- /dev/null +++ b/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c @@ -0,0 +1,126 @@ +/* + * 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" +#include "soc/apb_saradc_struct.h" +#include "soc/sens_struct.h" +#include "private_include/regi2c_saradc.h" +#include "driver/temp_sensor.h" +#include "regi2c_ctrl.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) { + SENS.sar_tctrl.tsens_power_up_force = true; + SENS.sar_tctrl2.tsens_xpd_force = true; + SENS.sar_tctrl.tsens_power_up = 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) { + SENS.sar_tctrl.tsens_power_up_force = false; + SENS.sar_tctrl2.tsens_xpd_force = false; + SENS.sar_tctrl.tsens_power_up = false; + } + portEXIT_CRITICAL(&rtc_spinlock); +} + +static int temperature_sensor_get_raw_value(void) +{ + SENS.sar_tctrl.tsens_dump_out = 1; + while (!SENS.sar_tctrl.tsens_ready) { + } + SENS.sar_tctrl.tsens_dump_out = 0; + int raw_value = SENS.sar_tctrl.tsens_out; + return (TSENS_ADC_FACTOR * raw_value - TSENS_DAC_FACTOR * dac_offset[s_tsens_idx].offset - TSENS_SYS_OFFSET); +} + +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 >= dac_offset[1].range_max) { + s_tsens_idx = 0; + } else if (degree >= dac_offset[2].range_max && degree < dac_offset[1].range_max) { + s_tsens_idx = 1; + } else if (degree <= dac_offset[2].range_min && degree > dac_offset[3].range_min) { + s_tsens_idx = 3; + } else if (degree <= dac_offset[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 = dac_offset[s_tsens_idx].set_val; + s_record_min = dac_offset[s_tsens_idx].range_min; + s_record_max = dac_offset[s_tsens_idx].range_max; + + REGI2C_WRITE_MASK(I2C_SAR_ADC, I2C_SARADC_TSENS_DAC, 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; +} diff --git a/components/esp_wifi/src/phy_override.c b/components/esp_wifi/src/phy_override.c index ce7c0f8512..8b32811cac 100644 --- a/components/esp_wifi/src/phy_override.c +++ b/components/esp_wifi/src/phy_override.c @@ -7,6 +7,8 @@ #include #include "esp_attr.h" #include "driver/adc.h" +#include "esp_private/sar_periph_ctrl.h" + /* * This file is used to override the hooks provided by the PHY lib for some system features. @@ -51,3 +53,17 @@ IRAM_ATTR void phy_i2c_exit_critical(void) { regi2c_exit_critical(); } + +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); +}