From c6731c0d53ddd1572afccf57aeabfda580f819a4 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Mon, 31 Mar 2025 14:58:00 +0800 Subject: [PATCH 1/2] 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/esp32c6/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/esp32c6/clk.c | 8 +++++--- components/esp_system/port/soc/esp32h2/clk.c | 8 +++++--- 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/esp32c6/include/soc/rtc.h | 7 ++++++- components/soc/esp32h2/include/soc/rtc.h | 7 ++++++- components/soc/esp32s2/include/soc/rtc.h | 7 ++++++- components/soc/esp32s3/include/soc/rtc.h | 9 +++++++-- 21 files changed, 142 insertions(+), 31 deletions(-) diff --git a/components/esp_hw_support/port/esp32/rtc_clk.c b/components/esp_hw_support/port/esp32/rtc_clk.c index bab0bdb3ce..16c2e7bc39 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 a494daba47..16b0d89e92 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 b505f7bf17..d1c2684901 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 */ @@ -55,6 +55,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/esp32c6/rtc_clk.c b/components/esp_hw_support/port/esp32c6/rtc_clk.c index 735ca71103..f55d403803 100644 --- a/components/esp_hw_support/port/esp32c6/rtc_clk.c +++ b/components/esp_hw_support/port/esp32c6/rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-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_GPIO0_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-C6, '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 8b94a7aeab..bee509acb2 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: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -62,6 +62,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 369ce96355..6d89130622 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 0c95355d2f..fc5a13dbfc 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 */ @@ -71,6 +71,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 3d52e3ab56..1c7e88582a 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 65889101b4..3b88deca25 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 */ @@ -174,7 +174,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/esp32c6/clk.c b/components/esp_system/port/soc/esp32c6/clk.c index a52ab2d7a9..aab6a0e11e 100644 --- a/components/esp_system/port/soc/esp32c6/clk.c +++ b/components/esp_system/port/soc/esp32c6/clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -163,12 +163,14 @@ static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src) 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_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { + 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 (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_RC32K) { rtc_clk_rc32k_enable(false); } diff --git a/components/esp_system/port/soc/esp32h2/clk.c b/components/esp_system/port/soc/esp32h2/clk.c index f233302eb0..d4d9b54ffb 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: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -164,12 +164,14 @@ static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src) 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_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { + 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 (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_RC32K) { rtc_clk_rc32k_enable(false); } 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 6835b664d2..2b0194813b 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 93cea4dbd6..d8b3c1897c 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 */ @@ -263,6 +263,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 848bb9979a..3e91b64bbf 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/esp32c6/include/soc/rtc.h b/components/soc/esp32c6/include/soc/rtc.h index 2dbabe6f47..f73a508bc8 100644 --- a/components/soc/esp32c6/include/soc/rtc.h +++ b/components/soc/esp32c6/include/soc/rtc.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -234,6 +234,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/esp32h2/include/soc/rtc.h b/components/soc/esp32h2/include/soc/rtc.h index 44a2dd8e0f..3c6010db02 100644 --- a/components/soc/esp32h2/include/soc/rtc.h +++ b/components/soc/esp32h2/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 */ @@ -237,6 +237,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 41ecd2faf0..3a333fa379 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 21f3154708..e1b34178ba 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 */ @@ -283,7 +283,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); From 774548e0fe7587688be7f05d9785765b6531ec77 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Thu, 10 Apr 2025 20:29:07 +0800 Subject: [PATCH 2/2] fix(esp_hw_support): fix unused OSC source deinit breaks XTAL32K configuration --- components/esp_system/port/soc/esp32/clk.c | 7 ++----- components/esp_system/port/soc/esp32c3/clk.c | 7 ++----- components/esp_system/port/soc/esp32c6/clk.c | 4 +--- components/esp_system/port/soc/esp32h2/clk.c | 4 +--- components/esp_system/port/soc/esp32s2/clk.c | 7 ++----- components/esp_system/port/soc/esp32s3/clk.c | 7 ++----- 6 files changed, 10 insertions(+), 26 deletions(-) diff --git a/components/esp_system/port/soc/esp32/clk.c b/components/esp_system/port/soc/esp32/clk.c index 9dccb46dbe..2133cc8b25 100644 --- a/components/esp_system/port/soc/esp32/clk.c +++ b/components/esp_system/port/soc/esp32/clk.c @@ -92,11 +92,8 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) } 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(); - } + rtc_clk_32k_enable(false); + 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/esp32c3/clk.c b/components/esp_system/port/soc/esp32c3/clk.c index 3b88deca25..4eaef281d6 100644 --- a/components/esp_system/port/soc/esp32c3/clk.c +++ b/components/esp_system/port/soc/esp32c3/clk.c @@ -175,11 +175,8 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) } 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(); - } + rtc_clk_32k_enable(false); + 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/esp32c6/clk.c b/components/esp_system/port/soc/esp32c6/clk.c index aab6a0e11e..049c6ada9a 100644 --- a/components/esp_system/port/soc/esp32c6/clk.c +++ b/components/esp_system/port/soc/esp32c6/clk.c @@ -165,10 +165,8 @@ static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src) 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) { + if ((rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) && (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW)) { rtc_clk_32k_enable(false); - } - if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { rtc_clk_32k_disable_external(); } if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_RC32K) { diff --git a/components/esp_system/port/soc/esp32h2/clk.c b/components/esp_system/port/soc/esp32h2/clk.c index d4d9b54ffb..3a81d9c0d6 100644 --- a/components/esp_system/port/soc/esp32h2/clk.c +++ b/components/esp_system/port/soc/esp32h2/clk.c @@ -166,10 +166,8 @@ static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src) 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) { + if ((rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_XTAL32K) && (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW)) { rtc_clk_32k_enable(false); - } - if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) { rtc_clk_32k_disable_external(); } if (rtc_slow_clk_src != SOC_RTC_SLOW_CLK_SRC_RC32K) { diff --git a/components/esp_system/port/soc/esp32s2/clk.c b/components/esp_system/port/soc/esp32s2/clk.c index b818747346..bb43f3c8e6 100644 --- a/components/esp_system/port/soc/esp32s2/clk.c +++ b/components/esp_system/port/soc/esp32s2/clk.c @@ -181,11 +181,8 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) } 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(); - } + rtc_clk_32k_enable(false); + 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/esp32s3/clk.c b/components/esp_system/port/soc/esp32s3/clk.c index 808c07f6c4..cd33cc210a 100644 --- a/components/esp_system/port/soc/esp32s3/clk.c +++ b/components/esp_system/port/soc/esp32s3/clk.c @@ -177,11 +177,8 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) } 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(); - } + rtc_clk_32k_enable(false); + rtc_clk_32k_disable_external(); } if (SLOW_CLK_CAL_CYCLES > 0) { /* TODO: 32k XTAL oscillator has some frequency drift at startup.