From be904e4ae9aac8285e336011bc254021ffdaa6ed Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Thu, 27 Oct 2022 21:45:38 +0800 Subject: [PATCH] esp_hw_support: Fix time jump after reboot Closes https://github.com/espressif/esp-idf/issues/9448 --- components/esp_hw_support/esp_clk.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/esp_hw_support/esp_clk.c b/components/esp_hw_support/esp_clk.c index 356b048fc8..3a0cef916e 100644 --- a/components/esp_hw_support/esp_clk.c +++ b/components/esp_hw_support/esp_clk.c @@ -46,7 +46,7 @@ extern uint32_t g_ticks_per_us_app; #endif static portMUX_TYPE s_esp_rtc_time_lock = portMUX_INITIALIZER_UNLOCKED; -static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0; +static RTC_NOINIT_ATTR uint64_t s_esp_rtc_time_us, s_rtc_last_ticks; inline static int IRAM_ATTR s_get_cpu_freq_mhz(void) { @@ -89,6 +89,10 @@ uint64_t esp_rtc_get_time_us(void) { portENTER_CRITICAL_SAFE(&s_esp_rtc_time_lock); const uint32_t cal = esp_clk_slowclk_cal_get(); + if (cal == 0) { + s_esp_rtc_time_us = 0; + s_rtc_last_ticks = 0; + } const uint64_t rtc_this_ticks = rtc_time_get(); const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks; /* RTC counter result is up to 2^48, calibration factor is up to 2^24,