1 /* 2 * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 // Provides strong definition for system time functions relied upon 8 // by core components. 9 #include "sdkconfig.h" 10 11 #if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER 12 #include "esp_timer.h" 13 #include "esp_timer_impl.h" 14 #include "esp_system.h" 15 #include "esp_log.h" 16 17 #include "esp_private/startup_internal.h" 18 19 #if CONFIG_IDF_TARGET_ESP32 20 #include "esp32/rtc.h" 21 #elif CONFIG_IDF_TARGET_ESP32S2 22 #include "esp32s2/rtc.h" 23 #elif CONFIG_IDF_TARGET_ESP32S3 24 #include "esp32s3/rtc.h" 25 #elif CONFIG_IDF_TARGET_ESP32C3 26 #include "esp32c3/rtc.h" 27 #elif CONFIG_IDF_TARGET_ESP32C2 28 #include "esp32c2/rtc.h" 29 #elif CONFIG_IDF_TARGET_ESP32C6 30 #include "esp32c6/rtc.h" 31 #elif CONFIG_IDF_TARGET_ESP32H2 32 #include "esp32h2/rtc.h" 33 #endif 34 35 __attribute__((unused)) static const char* TAG = "system_time"; 36 37 // Correction for underlying timer to keep definition 38 // of system time consistent. 39 static int64_t s_correction_us = 0; 40 esp_timer_impl_init_system_time(void)41void esp_timer_impl_init_system_time(void) 42 { 43 s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time(); 44 #if defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) 45 esp_err_t err = esp_register_shutdown_handler(esp_sync_timekeeping_timers); 46 if (err != ESP_OK) { 47 ESP_LOGW(TAG, "Register shutdown handler failed, err = 0x%x", err); 48 } 49 #endif 50 } 51 esp_system_get_time(void)52int64_t IRAM_ATTR esp_system_get_time(void) 53 { 54 return esp_timer_get_time() + s_correction_us; 55 } 56 esp_system_get_time_resolution(void)57uint32_t IRAM_ATTR esp_system_get_time_resolution(void) 58 { 59 return 1000; 60 } 61 #endif 62