From 2d1501f25daec7feff759499198e4748e28593cc Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Mon, 31 Mar 2025 14:58:00 +0800 Subject: [PATCH] fix(esp_hw_support): fix current leakage if ext32k slow clock source not exists --- components/esp_hw_support/port/esp32/rtc_clk.c | 7 ++++++- components/esp_hw_support/port/esp32c2/rtc_clk.c | 8 +++++++- components/esp_hw_support/port/esp32c3/rtc_clk.c | 9 ++++++++- components/esp_hw_support/port/esp32h2/rtc_clk.c | 9 ++++++++- components/esp_hw_support/port/esp32s2/rtc_clk.c | 9 ++++++++- components/esp_hw_support/port/esp32s3/rtc_clk.c | 9 ++++++++- components/esp_system/port/soc/esp32/clk.c | 10 ++++++++-- components/esp_system/port/soc/esp32c2/clk.c | 6 ++++-- components/esp_system/port/soc/esp32c3/clk.c | 10 ++++++++-- components/esp_system/port/soc/esp32h2/clk.c | 10 +++++++++- components/esp_system/port/soc/esp32s2/clk.c | 10 ++++++++-- components/esp_system/port/soc/esp32s3/clk.c | 10 ++++++++-- components/soc/esp32/include/soc/rtc.h | 7 ++++++- components/soc/esp32c2/include/soc/rtc.h | 7 ++++++- components/soc/esp32c3/include/soc/rtc.h | 7 ++++++- components/soc/esp32s2/include/soc/rtc.h | 7 ++++++- components/soc/esp32s3/include/soc/rtc.h | 9 +++++++-- 17 files changed, 121 insertions(+), 23 deletions(-) diff --git a/components/esp_hw_support/port/esp32/rtc_clk.c b/components/esp_hw_support/port/esp32/rtc_clk.c index f7a4155c0c..7efb56ead9 100644 --- a/components/esp_hw_support/port/esp32/rtc_clk.c +++ b/components/esp_hw_support/port/esp32/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -114,6 +114,11 @@ void rtc_clk_32k_enable_external(void) rtc_clk_32k_enable_common(CLK_LL_XTAL32K_ENABLE_MODE_EXTERNAL); } +void rtc_clk_32k_disable_external(void) +{ + clk_ll_xtal32k_disable(); +} + /* Helping external 32kHz crystal to start up. * External crystal connected to outputs GPIO32 GPIO33. * Forms N pulses with a frequency of about 32KHz on the outputs of the crystal. diff --git a/components/esp_hw_support/port/esp32c2/rtc_clk.c b/components/esp_hw_support/port/esp32c2/rtc_clk.c index d8f08fe1e7..2d32887f0c 100644 --- a/components/esp_hw_support/port/esp32c2/rtc_clk.c +++ b/components/esp_hw_support/port/esp32c2/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -38,6 +38,12 @@ void rtc_clk_32k_enable_external(void) REG_SET_BIT(RTC_CNTL_PAD_HOLD_REG, BIT(EXT_OSC_SLOW_GPIO_NUM)); } +void rtc_clk_32k_disable_external(void) +{ + PIN_INPUT_DISABLE(IO_MUX_GPIO0_REG); + REG_CLR_BIT(RTC_CNTL_PAD_HOLD_REG, BIT(EXT_OSC_SLOW_GPIO_NUM)); +} + void rtc_clk_8m_enable(bool clk_8m_en, bool d256_en) { if (clk_8m_en) { diff --git a/components/esp_hw_support/port/esp32c3/rtc_clk.c b/components/esp_hw_support/port/esp32c3/rtc_clk.c index 312489c805..408a6e6642 100644 --- a/components/esp_hw_support/port/esp32c3/rtc_clk.c +++ b/components/esp_hw_support/port/esp32c3/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -45,6 +45,13 @@ void rtc_clk_32k_enable_external(void) clk_ll_xtal32k_enable(CLK_LL_XTAL32K_ENABLE_MODE_EXTERNAL); } +void rtc_clk_32k_disable_external(void) +{ + PIN_INPUT_DISABLE(IO_MUX_GPIO0_REG); + CLEAR_PERI_REG_MASK(RTC_CNTL_PAD_HOLD_REG, RTC_CNTL_GPIO_PIN0_HOLD); + clk_ll_xtal32k_disable(); +} + void rtc_clk_32k_bootstrap(uint32_t cycle) { /* No special bootstrapping needed for ESP32-C3, 'cycle' argument is to keep the signature diff --git a/components/esp_hw_support/port/esp32h2/rtc_clk.c b/components/esp_hw_support/port/esp32h2/rtc_clk.c index d0dd14a779..f1bb647133 100644 --- a/components/esp_hw_support/port/esp32h2/rtc_clk.c +++ b/components/esp_hw_support/port/esp32h2/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -59,6 +59,13 @@ void rtc_clk_32k_enable_external(void) clk_ll_xtal32k_enable(CLK_LL_XTAL32K_ENABLE_MODE_EXTERNAL); } +void rtc_clk_32k_disable_external(void) +{ + PIN_INPUT_DISABLE(IO_MUX_GPIO13_REG); + REG_CLR_BIT(LP_AON_GPIO_HOLD0_REG, BIT(EXT_OSC_SLOW_GPIO_NUM)); + clk_ll_xtal32k_disable(); +} + void rtc_clk_32k_bootstrap(uint32_t cycle) { /* No special bootstrapping needed for ESP32-H2, 'cycle' argument is to keep the signature diff --git a/components/esp_hw_support/port/esp32s2/rtc_clk.c b/components/esp_hw_support/port/esp32s2/rtc_clk.c index 5c5c28b64e..6b8edbc603 100644 --- a/components/esp_hw_support/port/esp32s2/rtc_clk.c +++ b/components/esp_hw_support/port/esp32s2/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -48,6 +48,13 @@ void rtc_clk_32k_enable_external(void) clk_ll_xtal32k_enable(CLK_LL_XTAL32K_ENABLE_MODE_EXTERNAL); } +void rtc_clk_32k_disable_external(void) +{ + CLEAR_PERI_REG_MASK(RTC_IO_XTAL_32P_PAD_REG, RTC_IO_X32P_MUX_SEL); + CLEAR_PERI_REG_MASK(RTC_IO_XTAL_32N_PAD_REG, RTC_IO_X32N_MUX_SEL); + clk_ll_xtal32k_disable(); +} + void rtc_clk_32k_bootstrap(uint32_t cycle) { /* No special bootstrapping needed for ESP32-S2, 'cycle' argument is to keep the signature diff --git a/components/esp_hw_support/port/esp32s3/rtc_clk.c b/components/esp_hw_support/port/esp32s3/rtc_clk.c index 54a7129b58..b38b708eb6 100644 --- a/components/esp_hw_support/port/esp32s3/rtc_clk.c +++ b/components/esp_hw_support/port/esp32s3/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -61,6 +61,13 @@ void rtc_clk_32k_enable_external(void) clk_ll_xtal32k_enable(CLK_LL_XTAL32K_ENABLE_MODE_EXTERNAL); } +void rtc_clk_32k_disable_external(void) +{ + PIN_INPUT_DISABLE(IO_MUX_GPIO15_REG); + CLEAR_PERI_REG_MASK(RTC_CNTL_PAD_HOLD_REG, RTC_CNTL_X32P_HOLD); + clk_ll_xtal32k_disable(); +} + void rtc_clk_32k_bootstrap(uint32_t cycle) { /* No special bootstrapping needed for ESP32-S3, 'cycle' argument is to keep the signature diff --git a/components/esp_system/port/soc/esp32/clk.c b/components/esp_system/port/soc/esp32/clk.c index f8f1edf332..9dccb46dbe 100644 --- a/components/esp_system/port/soc/esp32/clk.c +++ b/components/esp_system/port/soc/esp32/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -91,7 +91,13 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_8m_enable(true, true); } rtc_clk_slow_src_set(rtc_slow_clk_src); - + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) { + if (slow_clk == SLOW_CLK_32K_XTAL) { + rtc_clk_32k_enable(false); + } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) { + rtc_clk_32k_disable_external(); + } + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. * Improve calibration routine to wait until the frequency is stable. diff --git a/components/esp_system/port/soc/esp32c2/clk.c b/components/esp_system/port/soc/esp32c2/clk.c index 45ba5dc531..db98d97975 100644 --- a/components/esp_system/port/soc/esp32c2/clk.c +++ b/components/esp_system/port/soc/esp32c2/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -179,7 +179,9 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_8m_enable(true, true); } rtc_clk_slow_src_set(rtc_slow_clk_src); - + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { + rtc_clk_32k_disable_external(); + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. * Improve calibration routine to wait until the frequency is stable. diff --git a/components/esp_system/port/soc/esp32c3/clk.c b/components/esp_system/port/soc/esp32c3/clk.c index 97457fc457..3021f6ffc3 100644 --- a/components/esp_system/port/soc/esp32c3/clk.c +++ b/components/esp_system/port/soc/esp32c3/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -175,7 +175,13 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_8m_enable(true, true); } rtc_clk_slow_src_set(rtc_slow_clk_src); - + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) { + if (slow_clk == SLOW_CLK_32K_XTAL) { + rtc_clk_32k_enable(false); + } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) { + rtc_clk_32k_disable_external(); + } + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. * Improve calibration routine to wait until the frequency is stable. diff --git a/components/esp_system/port/soc/esp32h2/clk.c b/components/esp_system/port/soc/esp32h2/clk.c index 839a3f3f6c..92725410bb 100644 --- a/components/esp_system/port/soc/esp32h2/clk.c +++ b/components/esp_system/port/soc/esp32h2/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -164,6 +164,14 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_rc32k_enable(true); } rtc_clk_slow_src_set(rtc_slow_clk_src); + // Disable unused clock sources after clock source switching is complete. + // Regardless of the clock source selection, the internal 136K clock source will always keep on. + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) { + rtc_clk_32k_enable(false); + } + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { + rtc_clk_32k_disable_external(); + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. diff --git a/components/esp_system/port/soc/esp32s2/clk.c b/components/esp_system/port/soc/esp32s2/clk.c index 7e3d2ab239..b818747346 100644 --- a/components/esp_system/port/soc/esp32s2/clk.c +++ b/components/esp_system/port/soc/esp32s2/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -180,7 +180,13 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_8m_enable(true, true); } rtc_clk_slow_src_set(rtc_slow_clk_src); - + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) { + if (slow_clk == SLOW_CLK_32K_XTAL) { + rtc_clk_32k_enable(false); + } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) { + rtc_clk_32k_disable_external(); + } + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. * Improve calibration routine to wait until the frequency is stable. diff --git a/components/esp_system/port/soc/esp32s3/clk.c b/components/esp_system/port/soc/esp32s3/clk.c index 19f8b7dff0..808c07f6c4 100644 --- a/components/esp_system/port/soc/esp32s3/clk.c +++ b/components/esp_system/port/soc/esp32s3/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -176,7 +176,13 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) rtc_clk_8m_enable(true, true); } rtc_clk_slow_src_set(rtc_slow_clk_src); - + if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) { + if (slow_clk == SLOW_CLK_32K_XTAL) { + rtc_clk_32k_enable(false); + } else if (slow_clk == SLOW_CLK_32K_EXT_OSC) { + rtc_clk_32k_disable_external(); + } + } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup. * Improve calibration routine to wait until the frequency is stable. diff --git a/components/soc/esp32/include/soc/rtc.h b/components/soc/esp32/include/soc/rtc.h index 783a10db3c..d57f7e56f3 100644 --- a/components/soc/esp32/include/soc/rtc.h +++ b/components/soc/esp32/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -179,6 +179,11 @@ void rtc_clk_32k_enable(bool en); */ void rtc_clk_32k_enable_external(void); +/** + * @brief Disable 32 kHz XTAL oscillator input. + */ +void rtc_clk_32k_disable_external(void); + /** * @brief Get the state of 32k XTAL oscillator * @return true if 32k XTAL oscillator has been enabled diff --git a/components/soc/esp32c2/include/soc/rtc.h b/components/soc/esp32c2/include/soc/rtc.h index 163aff6645..001654b680 100644 --- a/components/soc/esp32c2/include/soc/rtc.h +++ b/components/soc/esp32c2/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -260,6 +260,11 @@ void rtc_clk_xtal_freq_update(rtc_xtal_freq_t xtal_freq); */ void rtc_clk_32k_enable_external(void); +/** + * @brief Disable 32KHz external oscillator + */ +void rtc_clk_32k_disable_external(void); + /** * @brief Enable or disable 8 MHz internal oscillator * diff --git a/components/soc/esp32c3/include/soc/rtc.h b/components/soc/esp32c3/include/soc/rtc.h index cc8c4c22df..ef3544c227 100644 --- a/components/soc/esp32c3/include/soc/rtc.h +++ b/components/soc/esp32c3/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -268,6 +268,11 @@ void rtc_clk_32k_enable(bool en); */ void rtc_clk_32k_enable_external(void); +/** + * @brief Disable 32 kHz XTAL oscillator input. + */ +void rtc_clk_32k_disable_external(void); + /** * @brief Get the state of 32k XTAL oscillator * @return true if 32k XTAL oscillator has been enabled diff --git a/components/soc/esp32s2/include/soc/rtc.h b/components/soc/esp32s2/include/soc/rtc.h index dcc689e2eb..0a69d01574 100644 --- a/components/soc/esp32s2/include/soc/rtc.h +++ b/components/soc/esp32s2/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -263,6 +263,11 @@ void rtc_clk_32k_enable(bool en); */ void rtc_clk_32k_enable_external(void); +/** + * @brief Disable 32 kHz XTAL oscillator input. + */ +void rtc_clk_32k_disable_external(void); + /** * @brief Get the state of 32k XTAL oscillator * @return true if 32k XTAL oscillator has been enabled diff --git a/components/soc/esp32s3/include/soc/rtc.h b/components/soc/esp32s3/include/soc/rtc.h index ab730ea516..5c5542b015 100644 --- a/components/soc/esp32s3/include/soc/rtc.h +++ b/components/soc/esp32s3/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -284,7 +284,12 @@ void rtc_clk_32k_enable(bool en); void rtc_clk_32k_enable_external(void); /** - * @brief Get the state of 32k XTAL oscillator + * @brief Disable 32 kHz XTAL oscillator input. + */ +void rtc_clk_32k_disable_external(void); + +/** + * @brief Get the state of 32k XTAL oscillators * @return true if 32k XTAL oscillator has been enabled */ bool rtc_clk_32k_enabled(void);