system_time.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #endif
  31. __attribute__((unused)) static const char* TAG = "system_time";
  32. // Correction for underlying timer to keep definition
  33. // of system time consistent.
  34. static int64_t s_correction_us = 0;
  35. void esp_timer_impl_init_system_time(void)
  36. {
  37. s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time();
  38. #if defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  39. esp_err_t err = esp_register_shutdown_handler(esp_sync_timekeeping_timers);
  40. if (err != ESP_OK) {
  41. ESP_LOGW(TAG, "Register shutdown handler failed, err = 0x%x", err);
  42. }
  43. #endif
  44. }
  45. int64_t IRAM_ATTR esp_system_get_time(void)
  46. {
  47. return esp_timer_get_time() + s_correction_us;
  48. }
  49. uint32_t IRAM_ATTR esp_system_get_time_resolution(void)
  50. {
  51. return 1000;
  52. }
  53. #endif