From 439bc719fece03a1c15d4fe9ada53efe738c571c Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Fri, 22 Dec 2023 11:26:53 +0800 Subject: [PATCH] feat(temperature_sensor): Add temperature sensor support on esp32p4 --- .../legacy_rtc_temp_driver/README.md | 4 +- .../esp_driver_tsens/src/temperature_sensor.c | 2 +- .../test_apps/temperature_sensor/README.md | 4 +- .../esp_hw_support/sar_periph_ctrl_common.c | 18 +- .../include/hal/temperature_sensor_ll.h | 22 +- .../include/hal/temperature_sensor_ll.h | 22 +- .../include/hal/temperature_sensor_ll.h | 17 +- .../include/hal/temperature_sensor_ll.h | 16 +- .../include/hal/temperature_sensor_ll.h | 251 ++++++++++++++++++ .../include/hal/temperature_sensor_ll.h | 19 +- .../include/hal/temperature_sensor_ll.h | 19 +- .../soc/esp32c6/include/soc/interrupts.h | 3 +- .../soc/esp32h2/include/soc/interrupts.h | 3 +- .../esp32p4/include/soc/Kconfig.soc_caps.in | 8 +- .../soc/esp32p4/include/soc/clk_tree_defs.h | 15 ++ .../soc/esp32p4/include/soc/interrupts.h | 3 +- .../soc/esp32p4/include/soc/regi2c_defs.h | 5 +- .../soc/esp32p4/include/soc/regi2c_saradc.h | 7 + components/soc/esp32p4/include/soc/soc_caps.h | 6 +- .../soc/esp32p4/include/soc/tsens_struct.h | 1 + .../soc/esp32p4/ld/esp32p4.peripherals.ld | 1 + .../soc/esp32p4/temperature_sensor_periph.c | 7 +- .../temperature_sensor/temp_sensor/README.md | 4 +- .../temp_sensor_monitor/README.md | 4 +- 24 files changed, 424 insertions(+), 37 deletions(-) create mode 100644 components/hal/esp32p4/include/hal/temperature_sensor_ll.h 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 d2553ff6db..4fc69c16b6 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-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/esp_driver_tsens/src/temperature_sensor.c b/components/esp_driver_tsens/src/temperature_sensor.c index 1e0a3a7e10..2146c84751 100644 --- a/components/esp_driver_tsens/src/temperature_sensor.c +++ b/components/esp_driver_tsens/src/temperature_sensor.c @@ -304,7 +304,7 @@ esp_err_t temperature_sensor_register_callbacks(temperature_sensor_handle_t tsen // lazy install interrupt service. if (!tsens->temp_sensor_isr_handle) { - ret = esp_intr_alloc_intrstatus(ETS_APB_ADC_INTR_SOURCE, isr_flags, + ret = esp_intr_alloc_intrstatus(ETS_TEMPERATURE_SENSOR_INTR_SOURCE, isr_flags, (uint32_t)temperature_sensor_ll_get_intr_status(), TEMPERATURE_SENSOR_LL_INTR_MASK, temperature_sensor_isr, tsens, &tsens->temp_sensor_isr_handle); } diff --git a/components/esp_driver_tsens/test_apps/temperature_sensor/README.md b/components/esp_driver_tsens/test_apps/temperature_sensor/README.md index d2553ff6db..4fc69c16b6 100644 --- a/components/esp_driver_tsens/test_apps/temperature_sensor/README.md +++ b/components/esp_driver_tsens/test_apps/temperature_sensor/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/esp_hw_support/sar_periph_ctrl_common.c b/components/esp_hw_support/sar_periph_ctrl_common.c index 1173a82cef..0bd4b99554 100644 --- a/components/esp_hw_support/sar_periph_ctrl_common.c +++ b/components/esp_hw_support/sar_periph_ctrl_common.c @@ -26,6 +26,12 @@ static const char *TAG_TSENS = "temperature_sensor"; #define INT_NOT_USED 999999 +#if !SOC_RCC_IS_INDEPENDENT +#define TSENS_RCC_ATOMIC() PERIPH_RCC_ATOMIC() +#else +#define TSENS_RCC_ATOMIC() +#endif + static int s_record_min = INT_NOT_USED; static int s_record_max = INT_NOT_USED; static int s_temperature_sensor_power_cnt; @@ -37,10 +43,11 @@ void temperature_sensor_power_acquire(void) portENTER_CRITICAL(&rtc_spinlock); s_temperature_sensor_power_cnt++; if (s_temperature_sensor_power_cnt == 1) { - periph_module_enable(PERIPH_TEMPSENSOR_MODULE); - periph_module_reset(PERIPH_TEMPSENSOR_MODULE); regi2c_saradc_enable(); - temperature_sensor_ll_clk_enable(true); + TSENS_RCC_ATOMIC() { + temperature_sensor_ll_bus_clk_enable(true); + temperature_sensor_ll_reset_module(); + } temperature_sensor_ll_enable(true); } portEXIT_CRITICAL(&rtc_spinlock); @@ -56,10 +63,11 @@ void temperature_sensor_power_release(void) 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_clk_enable(false); temperature_sensor_ll_enable(false); + TSENS_RCC_ATOMIC() { + temperature_sensor_ll_bus_clk_enable(false); + } regi2c_saradc_disable(); - periph_module_disable(PERIPH_TEMPSENSOR_MODULE); } portEXIT_CRITICAL(&rtc_spinlock); } diff --git a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h index 685f77b794..6153c012b3 100644 --- a/components/hal/esp32c2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c2/include/hal/temperature_sensor_ll.h @@ -21,6 +21,7 @@ #include "soc/apb_saradc_struct.h" #include "soc/soc.h" #include "soc/soc_caps.h" +#include "soc/system_struct.h" #include "hal/temperature_sensor_types.h" #include "hal/assert.h" #include "hal/misc.h" @@ -48,11 +49,28 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { - // No need to enable the temperature clock on esp32c2 + SYSTEM.perip_clk_en1.tsens_clk_en = enable; } +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_bus_clk_enable(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_bus_clk_enable(__VA_ARGS__);} while(0) + +/** + * @brief Reset the Temperature sensor module + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + SYSTEM.perip_rst_en1.tsens_rst = 1; + SYSTEM.perip_rst_en1.tsens_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_reset_module(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_reset_module(__VA_ARGS__);} while(0) + /** * @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. diff --git a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h index 4dac4061ae..38902bac73 100644 --- a/components/hal/esp32c3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c3/include/hal/temperature_sensor_ll.h @@ -21,6 +21,7 @@ #include "soc/apb_saradc_struct.h" #include "soc/soc.h" #include "soc/soc_caps.h" +#include "soc/system_struct.h" #include "hal/temperature_sensor_types.h" #include "hal/assert.h" #include "hal/misc.h" @@ -48,11 +49,28 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { - // No need to enable the temperature clock on esp32c3 + SYSTEM.perip_clk_en1.reg_tsens_clk_en = enable; } +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_bus_clk_enable(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_bus_clk_enable(__VA_ARGS__);} while(0) + +/** + * @brief Reset the Temperature sensor module + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + SYSTEM.perip_rst_en1.reg_tsens_rst = 1; + SYSTEM.perip_rst_en1.reg_tsens_rst = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_reset_module(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_reset_module(__VA_ARGS__);} while(0) + /** * @brief Select the clock source for temperature sensor. On ESP32-C3, temperautre sensor * can use XTAL or FOSC. To make it convenience, suggest using XTAL all the time. diff --git a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h index 9ecd97727e..17ac106e08 100644 --- a/components/hal/esp32c6/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32c6/include/hal/temperature_sensor_ll.h @@ -20,9 +20,11 @@ #include "soc/regi2c_saradc.h" #include "soc/apb_saradc_struct.h" #include "soc/apb_saradc_reg.h" +#include "soc/pcr_struct.h" #include "soc/soc.h" #include "soc/soc_caps.h" #include "soc/pcr_struct.h" +#include "soc/interrupts.h" #include "hal/temperature_sensor_types.h" #include "hal/assert.h" #include "hal/misc.h" @@ -57,9 +59,18 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { - // clock enable duplicated with periph enable, no need to enable it again. + PCR.tsens_clk_conf.tsens_clk_en = enable; +} + +/** + * @brief Reset the Temperature sensor module + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + PCR.tsens_clk_conf.tsens_rst_en = 1; + PCR.tsens_clk_conf.tsens_rst_en = 0; } /** @@ -150,7 +161,7 @@ static inline void temperature_sensor_ll_set_clk_div(uint8_t clk_div) * * @param mode 0: Absolute value mode. 1: Difference mode. */ -static inline void temperature_sensor_ll_wakeup_mode(uint8_t mode) +static inline void temperature_sensor_ll_wakeup_mode(temperature_sensor_ll_wakeup_mode_t mode) { APB_SARADC.tsens_wake.saradc_wakeup_mode = mode; } diff --git a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h index 9ecd97727e..6007f05c51 100644 --- a/components/hal/esp32h2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32h2/include/hal/temperature_sensor_ll.h @@ -23,6 +23,7 @@ #include "soc/soc.h" #include "soc/soc_caps.h" #include "soc/pcr_struct.h" +#include "soc/interrupts.h" #include "hal/temperature_sensor_types.h" #include "hal/assert.h" #include "hal/misc.h" @@ -57,9 +58,18 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { - // clock enable duplicated with periph enable, no need to enable it again. + PCR.tsens_clk_conf.tsens_clk_en = enable; +} + +/** + * @brief Reset the Temperature sensor module + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + PCR.tsens_clk_conf.tsens_rst_en = 1; + PCR.tsens_clk_conf.tsens_rst_en = 0; } /** @@ -150,7 +160,7 @@ static inline void temperature_sensor_ll_set_clk_div(uint8_t clk_div) * * @param mode 0: Absolute value mode. 1: Difference mode. */ -static inline void temperature_sensor_ll_wakeup_mode(uint8_t mode) +static inline void temperature_sensor_ll_wakeup_mode(temperature_sensor_ll_wakeup_mode_t mode) { APB_SARADC.tsens_wake.saradc_wakeup_mode = mode; } diff --git a/components/hal/esp32p4/include/hal/temperature_sensor_ll.h b/components/hal/esp32p4/include/hal/temperature_sensor_ll.h new file mode 100644 index 0000000000..637bcb697c --- /dev/null +++ b/components/hal/esp32p4/include/hal/temperature_sensor_ll.h @@ -0,0 +1,251 @@ +/* + * SPDX-FileCopyrightText: 2023 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 "hal/regi2c_ctrl.h" +#include "soc/regi2c_saradc.h" +#include "soc/tsens_struct.h" +#include "soc/lpperi_struct.h" +#include "soc/tsens_reg.h" +#include "soc/soc.h" +#include "soc/soc_caps.h" +#include "soc/interrupts.h" +#include "hal/temperature_sensor_types.h" +#include "hal/assert.h" +#include "hal/misc.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) +#define TEMPERATURE_SENSOR_LL_MEASURE_MAX (125) +#define TEMPERATURE_SENSOR_LL_MEASURE_MIN (-40) + +#define TEMPERATURE_SENSOR_LL_INTR_MASK TSENS_COCPU_TSENS_WAKE_INT_ST + +typedef enum { + TEMPERATURE_SENSOR_LL_WAKE_ABSOLUTE = 0, + TEMPERATURE_SENSOR_LL_WAKE_DELTA = 1, +} temperature_sensor_ll_wakeup_mode_t; + +/** + * @brief Enable the temperature sensor power. + * + * @param enable true: enable the power. + */ +static inline void temperature_sensor_ll_enable(bool enable) +{ + LP_TSENS.ctrl.power_up = enable; +} + +/** + * @brief Enable the clock + */ +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) +{ + LPPERI.clk_en.ck_en_lp_tsens = enable; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_bus_clk_enable(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_bus_clk_enable(__VA_ARGS__);} while(0) + +/** + * @brief Reset the Temperature sensor module + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + LPPERI.reset_en.rst_en_lp_tsens = 1; + LPPERI.reset_en.rst_en_lp_tsens = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_reset_module(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_reset_module(__VA_ARGS__);} while(0) + +/** + * @brief Select the clock source for temperature sensor. + * + * @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) +{ + // Only LP_PLL can be used as clock source on P4. +} + +/** + * @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 + */ +__attribute__((always_inline)) +static inline uint32_t temperature_sensor_ll_get_raw_value(void) +{ + return HAL_FORCE_READ_U32_REG_FIELD(LP_TSENS.ctrl, 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 HAL_FORCE_READ_U32_REG_FIELD(LP_TSENS.ctrl, 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) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(LP_TSENS.ctrl, clk_div, clk_div); +} + +/** + * @brief Choose the wake-up mode for temperature sensor + * + * @param mode 0: Absolute value mode. 1: Difference mode. + */ +static inline void temperature_sensor_ll_wakeup_mode(temperature_sensor_ll_wakeup_mode_t mode) +{ + LP_TSENS.wakeup_ctrl.wakeup_mode = mode; +} + +/** + * @brief Get temperature sensor interrupt/wakeup in which reason + * + * @return uint8_t 0: temperature value lower than low threshold 1: otherwise, higher than high threshold. + */ +__attribute__((always_inline)) +static inline uint8_t temperature_sensor_ll_get_wakeup_reason(void) +{ + return LP_TSENS.wakeup_ctrl.wakeup_over_upper_th; +} + +/** + * @brief Configure whether to enable temperature sensor wake up + * + * @param en true: enable, false: disable. + */ +static inline void temperature_sensor_ll_wakeup_enable(bool en) +{ + LP_TSENS.wakeup_ctrl.wakeup_en = en; +} + +/** + * @brief Configures the low threshold for temperature sensor to wakeup + * + * @param th_low low threshold value. + */ +static inline void temperature_sensor_ll_set_th_low_val(uint8_t th_low) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(LP_TSENS.wakeup_ctrl, wakeup_th_low, th_low); +} + +/** + * @brief Configures the high threshold for temperature sensor to wakeup + * + * @param th_high high threshold value. + */ +static inline void temperature_sensor_ll_set_th_high_val(uint8_t th_high) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(LP_TSENS.wakeup_ctrl, wakeup_th_high, th_high); +} + +/** + * @brief Enable temperature sensor interrupt + * + * @param enable true: enable. false: disable + */ +static inline void temperature_sensor_ll_enable_intr(bool enable) +{ + LP_TSENS.int_ena.cocpu_tsens_wake_int_ena = enable; +} + +/** + * @brief Clear temperature sensor interrupt + */ +__attribute__((always_inline)) +static inline void temperature_sensor_ll_clear_intr(void) +{ + LP_TSENS.int_clr.cocpu_tsens_wake_int_clr = 1; +} + +/** + * @brief Get temperature sensor interrupt status. + */ +static inline volatile void *temperature_sensor_ll_get_intr_status(void) +{ + return &LP_TSENS.int_st; +} + +/** + * @brief Configure whether to enable hardware sampling + * + * @param en true: enable, false: disable + */ +static inline void temperature_sensor_ll_sample_enable(bool en) +{ + LP_TSENS.ctrl.sample_en = en; +} + +/** + * @brief Configures the hardware sampling rate + * + * @param rate sampling rate + */ +static inline void temperature_sensor_ll_set_sample_rate(uint16_t rate) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(LP_TSENS.sample_rate, sample_rate, rate); +} + +#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 ca444f528b..9e356e163f 100644 --- a/components/hal/esp32s2/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/temperature_sensor_ll.h @@ -48,11 +48,28 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { SENS.sar_tctrl2.tsens_clkgate_en = enable; } +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_bus_clk_enable(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_bus_clk_enable(__VA_ARGS__);} while(0) + +/** + * @brief Enable the clock + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + SENS.sar_tctrl2.tsens_reset = 1; + SENS.sar_tctrl2.tsens_reset = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_reset_module(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_reset_module(__VA_ARGS__);} while(0) + /** * @brief Choose the clock. No need to choose the clock source on ESP32-S2. ESP32-S2 * can use RTC clock. diff --git a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h index 98baca81bb..52fb122134 100644 --- a/components/hal/esp32s3/include/hal/temperature_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/temperature_sensor_ll.h @@ -48,11 +48,28 @@ static inline void temperature_sensor_ll_enable(bool enable) /** * @brief Enable the clock */ -static inline void temperature_sensor_ll_clk_enable(bool enable) +static inline void temperature_sensor_ll_bus_clk_enable(bool enable) { SENS.sar_peri_clk_gate_conf.tsens_clk_en = enable; } +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_bus_clk_enable(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_bus_clk_enable(__VA_ARGS__);} while(0) + +/** + * @brief Enable the clock + */ +static inline void temperature_sensor_ll_reset_module(void) +{ + SENS.sar_peri_reset_conf.tsens_reset = 1; + SENS.sar_peri_reset_conf.tsens_reset = 0; +} + +/// use a macro to wrap the function, force the caller to use it in a critical section +/// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance +#define temperature_sensor_ll_reset_module(...) do {(void)__DECLARE_RCC_ATOMIC_ENV; temperature_sensor_ll_reset_module(__VA_ARGS__);} while(0) + /** * @brief Choose the clock. No need to choose the clock source on ESP32-S3. ESP32-S3 * can use RTC clock. diff --git a/components/soc/esp32c6/include/soc/interrupts.h b/components/soc/esp32c6/include/soc/interrupts.h index 5448dfeece..8eb941cf83 100644 --- a/components/soc/esp32c6/include/soc/interrupts.h +++ b/components/soc/esp32c6/include/soc/interrupts.h @@ -79,7 +79,8 @@ typedef enum { ETS_SYSTIMER_TARGET1_EDGE_INTR_SOURCE = ETS_SYSTIMER_TARGET1_INTR_SOURCE, /**< use ETS_SYSTIMER_TARGET1_INTR_SOURCE */ ETS_SYSTIMER_TARGET2_EDGE_INTR_SOURCE = ETS_SYSTIMER_TARGET2_INTR_SOURCE, /**< use ETS_SYSTIMER_TARGET2_INTR_SOURCE */ ETS_APB_ADC_INTR_SOURCE = 60, /**< interrupt of APB ADC, LEVEL*/ - ETS_MCPWM0_INTR_SOURCE, /**< interrupt of MCPWM0, LEVEL*/ + ETS_TEMPERATURE_SENSOR_INTR_SOURCE = ETS_APB_ADC_INTR_SOURCE, + ETS_MCPWM0_INTR_SOURCE = 61, /**< interrupt of MCPWM0, LEVEL*/ ETS_PCNT_INTR_SOURCE, ETS_PARL_IO_INTR_SOURCE, ETS_SLC0_INTR_SOURCE, diff --git a/components/soc/esp32h2/include/soc/interrupts.h b/components/soc/esp32h2/include/soc/interrupts.h index b3ef71e772..cac8f02776 100644 --- a/components/soc/esp32h2/include/soc/interrupts.h +++ b/components/soc/esp32h2/include/soc/interrupts.h @@ -67,7 +67,8 @@ typedef enum { ETS_SYSTIMER_TARGET1_EDGE_INTR_SOURCE = ETS_SYSTIMER_TARGET1_INTR_SOURCE, /**< use ETS_SYSTIMER_TARGET1_INTR_SOURCE */ ETS_SYSTIMER_TARGET2_EDGE_INTR_SOURCE = ETS_SYSTIMER_TARGET2_INTR_SOURCE, /**< use ETS_SYSTIMER_TARGET2_INTR_SOURCE */ ETS_APB_ADC_INTR_SOURCE = 48, /**< interrupt of APB ADC, LEVEL*/ - ETS_MCPWM0_INTR_SOURCE, + ETS_TEMPERATURE_SENSOR_INTR_SOURCE = ETS_APB_ADC_INTR_SOURCE, + ETS_MCPWM0_INTR_SOURCE = 49, ETS_PCNT_INTR_SOURCE, ETS_PARL_IO_TX_INTR_SOURCE, ETS_PARL_IO_RX_INTR_SOURCE, diff --git a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in index d115e7a740..b95d78c91d 100644 --- a/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32p4/include/soc/Kconfig.soc_caps.in @@ -55,6 +55,10 @@ config SOC_ASYNC_MEMCPY_SUPPORTED bool default y +config SOC_TEMP_SENSOR_SUPPORTED + bool + default y + config SOC_SUPPORTS_SECURE_DL_MODE bool default y @@ -1327,11 +1331,11 @@ config SOC_PERIPH_CLK_CTRL_SHARED bool default y -config SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC +config SOC_TEMPERATURE_SENSOR_SUPPORT_LP_PLL bool default y -config SOC_TEMPERATURE_SENSOR_SUPPORT_XTAL +config SOC_TEMPERATURE_SENSOR_INTR_SUPPORT bool default y diff --git a/components/soc/esp32p4/include/soc/clk_tree_defs.h b/components/soc/esp32p4/include/soc/clk_tree_defs.h index a9366f0bb1..6c9bbe62b4 100644 --- a/components/soc/esp32p4/include/soc/clk_tree_defs.h +++ b/components/soc/esp32p4/include/soc/clk_tree_defs.h @@ -636,6 +636,21 @@ typedef enum { SDMMC_CLK_SRC_PLL200M = SOC_MOD_CLK_PLL_F200M, /*!< Select PLL_200M as the source clock */ } soc_periph_sdmmc_clk_src_t; +//////////////////////////////////////////////////Temp Sensor/////////////////////////////////////////////////////////// + +/** + * @brief Array initializer for all supported clock sources of Temperature Sensor + */ +#define SOC_TEMP_SENSOR_CLKS {SOC_MOD_CLK_LP_PLL} + +/** + * @brief Type of Temp Sensor clock source + */ +typedef enum { + TEMPERATURE_SENSOR_CLK_SRC_LP_PLL = SOC_MOD_CLK_LP_PLL, /*!< Select LP_PLL as the source clock */ + TEMPERATURE_SENSOR_CLK_SRC_DEFAULT = SOC_MOD_CLK_LP_PLL, /*!< Select LP_PLL as the default choice */ +} soc_periph_temperature_sensor_clk_src_t; + #ifdef __cplusplus } #endif diff --git a/components/soc/esp32p4/include/soc/interrupts.h b/components/soc/esp32p4/include/soc/interrupts.h index 685da9861d..ab4fb86f0a 100644 --- a/components/soc/esp32p4/include/soc/interrupts.h +++ b/components/soc/esp32p4/include/soc/interrupts.h @@ -31,7 +31,8 @@ typedef enum { ETS_LP_SPI_INTR_SOURCE, ETS_LP_TOUCH_INTR_SOURCE, ETS_LP_TSENS_INTR_SOURCE, - ETS_LP_UART_INTR_SOURCE, + ETS_TEMPERATURE_SENSOR_INTR_SOURCE = ETS_LP_TSENS_INTR_SOURCE, + ETS_LP_UART_INTR_SOURCE = 16, ETS_LP_EFUSE_INTR_SOURCE, ETS_LP_SW_INTR_SOURCE, ETS_LP_SYSREG_INTR_SOURCE, diff --git a/components/soc/esp32p4/include/soc/regi2c_defs.h b/components/soc/esp32p4/include/soc/regi2c_defs.h index 38db4f1595..922655c05a 100644 --- a/components/soc/esp32p4/include/soc/regi2c_defs.h +++ b/components/soc/esp32p4/include/soc/regi2c_defs.h @@ -15,7 +15,8 @@ #define I2C_MST_BBPLL_CAL_DONE (BIT(24)) -#define ANA_CONFIG_REG 0x600AF81C + +#define ANA_CONFIG_REG 0x5012401C #define ANA_CONFIG_S (8) #define ANA_CONFIG_M (0x3FF) @@ -23,7 +24,7 @@ #define ANA_I2C_BBPLL_M BIT(17) /* Clear to enable BBPLL */ -#define ANA_CONFIG2_REG 0x600AF820 +#define ANA_CONFIG2_REG 0x50124020 #define ANA_CONFIG2_M BIT(18) #define ANA_I2C_SAR_FORCE_PU BIT(16) diff --git a/components/soc/esp32p4/include/soc/regi2c_saradc.h b/components/soc/esp32p4/include/soc/regi2c_saradc.h index 7511dfa4e2..92e1b40634 100644 --- a/components/soc/esp32p4/include/soc/regi2c_saradc.h +++ b/components/soc/esp32p4/include/soc/regi2c_saradc.h @@ -14,3 +14,10 @@ * bus. These definitions are used via macros defined in regi2c_ctrl.h, by * function in adc_ll.h. */ + +#define I2C_SAR_ADC 0X69 +#define I2C_SAR_ADC_HOSTID 0 + +#define I2C_SARADC_TSENS_DAC 0x6 +#define I2C_SARADC_TSENS_DAC_MSB 3 +#define I2C_SARADC_TSENS_DAC_LSB 0 diff --git a/components/soc/esp32p4/include/soc/soc_caps.h b/components/soc/esp32p4/include/soc/soc_caps.h index 1356308ff8..943d16bf6f 100644 --- a/components/soc/esp32p4/include/soc/soc_caps.h +++ b/components/soc/esp32p4/include/soc/soc_caps.h @@ -36,7 +36,7 @@ #define SOC_ASYNC_MEMCPY_SUPPORTED 1 // disable usb serial jtag for esp32p4, current image does not support // #define SOC_USB_SERIAL_JTAG_SUPPORTED 1 //TODO: IDF-7496 -// #define SOC_TEMP_SENSOR_SUPPORTED 1 //TODO: IDF-7482 +#define SOC_TEMP_SENSOR_SUPPORTED 1 #define SOC_SUPPORTS_SECURE_DL_MODE 1 #define SOC_ULP_SUPPORTED 1 #define SOC_LP_CORE_SUPPORTED 1 @@ -571,8 +571,8 @@ #define SOC_PERIPH_CLK_CTRL_SHARED (1) /*!< Peripheral clock control (e.g. set clock source) is shared between various peripherals */ /*-------------------------- Temperature Sensor CAPS -------------------------------------*/ -#define SOC_TEMPERATURE_SENSOR_SUPPORT_FAST_RC (1) -#define SOC_TEMPERATURE_SENSOR_SUPPORT_XTAL (1) +#define SOC_TEMPERATURE_SENSOR_SUPPORT_LP_PLL (1) +#define SOC_TEMPERATURE_SENSOR_INTR_SUPPORT (1) /*-------------------------- Memory CAPS --------------------------*/ #define SOC_MEM_TCM_SUPPORTED (1) diff --git a/components/soc/esp32p4/include/soc/tsens_struct.h b/components/soc/esp32p4/include/soc/tsens_struct.h index 671e88faa8..99bf3429f6 100644 --- a/components/soc/esp32p4/include/soc/tsens_struct.h +++ b/components/soc/esp32p4/include/soc/tsens_struct.h @@ -222,6 +222,7 @@ typedef struct { volatile tsens_sample_rate_reg_t sample_rate; } tsens_dev_t; +extern tsens_dev_t LP_TSENS; #ifndef __cplusplus _Static_assert(sizeof(tsens_dev_t) == 0x2c, "Invalid size of tsens_dev_t structure"); diff --git a/components/soc/esp32p4/ld/esp32p4.peripherals.ld b/components/soc/esp32p4/ld/esp32p4.peripherals.ld index c9ade62618..93fa97ebd9 100644 --- a/components/soc/esp32p4/ld/esp32p4.peripherals.ld +++ b/components/soc/esp32p4/ld/esp32p4.peripherals.ld @@ -85,6 +85,7 @@ PROVIDE ( AHB_DMA = 0x50085000 ); PROVIDE ( AXI_DMA = 0x5008a000 ); PROVIDE ( LCD_CAM = 0x500dc000 ); PROVIDE ( LP_IOMUX = 0x5012B000 ); +PROVIDE ( LP_TSENS = 0x5012f000 ); PROVIDE ( MIPI_CSI_BRIDGE = 0x5009F800 ); PROVIDE ( MIPI_CSI_HOST = 0x5009F000 ); diff --git a/components/soc/esp32p4/temperature_sensor_periph.c b/components/soc/esp32p4/temperature_sensor_periph.c index a9812ecc72..a681a41e3b 100644 --- a/components/soc/esp32p4/temperature_sensor_periph.c +++ b/components/soc/esp32p4/temperature_sensor_periph.c @@ -7,5 +7,10 @@ #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/examples/peripherals/temperature_sensor/temp_sensor/README.md b/examples/peripherals/temperature_sensor/temp_sensor/README.md index 560f858925..e798628a4b 100644 --- a/examples/peripherals/temperature_sensor/temp_sensor/README.md +++ b/examples/peripherals/temperature_sensor/temp_sensor/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # Temperature Sensor Example diff --git a/examples/peripherals/temperature_sensor/temp_sensor_monitor/README.md b/examples/peripherals/temperature_sensor/temp_sensor_monitor/README.md index 865ec13c24..a1f921bd81 100644 --- a/examples/peripherals/temperature_sensor/temp_sensor_monitor/README.md +++ b/examples/peripherals/temperature_sensor/temp_sensor_monitor/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32-C6 | ESP32-H2 | -| ----------------- | -------- | -------- | +| Supported Targets | ESP32-C6 | ESP32-H2 | ESP32-P4 | +| ----------------- | -------- | -------- | -------- | # Temperature Sensor Interrupt Example