From bc1624879cfe19278a5b2d7e64bee9d3aa05ddf8 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Fri, 16 May 2025 16:51:37 +0800 Subject: [PATCH] fix(esp_hw_support): update systimer step immediately when XTAL changes on esp32s2 --- components/esp_hw_support/port/esp32s2/rtc_clk.c | 7 +++++++ components/esp_pm/pm_impl.c | 9 --------- .../include/esp_private/esp_timer_private.h | 12 +----------- .../esp_timer/private_include/esp_timer_impl.h | 11 +---------- components/esp_timer/src/esp_timer_impl_systimer.c | 10 +--------- 5 files changed, 10 insertions(+), 39 deletions(-) diff --git a/components/esp_hw_support/port/esp32s2/rtc_clk.c b/components/esp_hw_support/port/esp32s2/rtc_clk.c index 50fa74c697..85e01d5591 100644 --- a/components/esp_hw_support/port/esp32s2/rtc_clk.c +++ b/components/esp_hw_support/port/esp32s2/rtc_clk.c @@ -20,6 +20,10 @@ #include "esp_hw_log.h" #include "sdkconfig.h" #include "hal/clk_tree_ll.h" +#ifndef BOOTLOADER_BUILD +#include "esp_private/systimer.h" +#include "hal/systimer_ll.h" +#endif static const char *TAG = "rtc_clk"; @@ -454,6 +458,9 @@ static void rtc_clk_cpu_freq_to_xtal(int cpu_freq, int div) /* no need to adjust the REF_TICK, default register value already set it to 1MHz with any cpu clock source */ /* switch clock source */ clk_ll_cpu_set_src(SOC_CPU_CLK_SRC_XTAL); +#ifndef BOOTLOADER_BUILD + systimer_ll_set_step_for_xtal(&SYSTIMER, systimer_us_to_ticks(1) / cpu_freq); +#endif rtc_clk_apb_freq_update(cpu_freq * MHZ); /* lower the voltage diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index 6f02d5a8f8..9d9cbd025a 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -567,15 +567,6 @@ void IRAM_ATTR esp_pm_impl_switch_mode(pm_mode_t mode, */ static void IRAM_ATTR on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us) { -#if !CONFIG_IDF_TARGET_ESP32 - uint32_t old_apb_ticks_per_us = MIN(old_ticks_per_us, 80); - uint32_t apb_ticks_per_us = MIN(ticks_per_us, 80); - /* Update APB frequency value used by the timer */ - if (old_apb_ticks_per_us != apb_ticks_per_us) { - esp_timer_private_update_apb_freq(apb_ticks_per_us); - } -#endif - #ifdef CONFIG_FREERTOS_SYSTICK_USES_CCOUNT #ifdef XT_RTOS_TIMER_INT /* Calculate new tick divisor */ diff --git a/components/esp_timer/include/esp_private/esp_timer_private.h b/components/esp_timer/include/esp_private/esp_timer_private.h index 37aaeac03f..c40d96cd40 100644 --- a/components/esp_timer/include/esp_private/esp_timer_private.h +++ b/components/esp_timer/include/esp_private/esp_timer_private.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -24,16 +24,6 @@ #ifdef __cplusplus extern "C" { #endif - -/** - * @brief Notify esp_timer implementation that APB frequency has changed - * - * Called by the frequency switching code. - * - * @param apb_ticks_per_us new number of APB clock ticks per microsecond - */ -void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us); - /** * @brief Set esp_timer time to a certain value * diff --git a/components/esp_timer/private_include/esp_timer_impl.h b/components/esp_timer/private_include/esp_timer_impl.h index d6484cbb95..6164cfe7d9 100644 --- a/components/esp_timer/private_include/esp_timer_impl.h +++ b/components/esp_timer/private_include/esp_timer_impl.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -69,15 +69,6 @@ void esp_timer_impl_set_alarm(uint64_t timestamp); */ void esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id); -/** - * @brief Notify esp_timer implementation that APB frequency has changed - * - * Called by the frequency switching code. - * - * @param apb_ticks_per_us new number of APB clock ticks per microsecond - */ -void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us); - /** * @brief Adjust current esp_timer time by a certain value * diff --git a/components/esp_timer/src/esp_timer_impl_systimer.c b/components/esp_timer/src/esp_timer_impl_systimer.c index 549316a6da..f66b3a047e 100644 --- a/components/esp_timer/src/esp_timer_impl_systimer.c +++ b/components/esp_timer/src/esp_timer_impl_systimer.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -129,13 +129,6 @@ static void IRAM_ATTR timer_alarm_isr(void *arg) #endif // ISR_HANDLERS != 1 } -void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us) -{ -#if !SOC_SYSTIMER_FIXED_DIVIDER - systimer_hal_on_apb_freq_update(&systimer_hal, apb_ticks_per_us); -#endif -} - void esp_timer_impl_set(uint64_t new_us) { portENTER_CRITICAL_SAFE(&s_time_update_lock); @@ -249,6 +242,5 @@ uint64_t esp_timer_impl_get_alarm_reg(void) return val; } -void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq"))); void esp_timer_private_set(uint64_t new_us) __attribute__((alias("esp_timer_impl_set"))); void esp_timer_private_advance(int64_t time_diff_us) __attribute__((alias("esp_timer_impl_advance")));