system_time.c 1.7 KB

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