esp_time_impl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <time.h>
  8. #include <sys/time.h>
  9. #include <sys/lock.h>
  10. #include "esp_attr.h"
  11. #include "esp_system.h"
  12. #include "soc/rtc.h"
  13. #include "esp_rom_sys.h"
  14. #include "esp_private/system_internal.h"
  15. #include "esp_private/esp_clk.h"
  16. #include "esp_time_impl.h"
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/rtc.h"
  20. #include "esp32/rtc.h"
  21. #elif CONFIG_IDF_TARGET_ESP32S2
  22. #include "esp32s2/rom/rtc.h"
  23. #include "esp32s2/rtc.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S3
  25. #include "esp32s3/rom/rtc.h"
  26. #include "esp32s3/rtc.h"
  27. #elif CONFIG_IDF_TARGET_ESP32C3
  28. #include "esp32c3/rom/rtc.h"
  29. #include "esp32c3/rtc.h"
  30. #elif CONFIG_IDF_TARGET_ESP32H2
  31. #include "esp32h2/rom/rtc.h"
  32. #include "esp32h2/rtc.h"
  33. #elif CONFIG_IDF_TARGET_ESP32C2
  34. #include "esp32c2/rom/rtc.h"
  35. #include "esp32c2/rtc.h"
  36. #endif
  37. // Offset between High resolution timer and the RTC.
  38. // Initialized after reset or light sleep.
  39. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
  40. int64_t s_microseconds_offset = 0;
  41. #endif
  42. #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  43. static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
  44. #endif
  45. static _lock_t s_boot_time_lock;
  46. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  47. uint64_t esp_time_impl_get_time_since_boot(void)
  48. {
  49. uint64_t microseconds = 0;
  50. #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  51. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  52. microseconds = s_microseconds_offset + esp_system_get_time();
  53. #else
  54. microseconds = esp_system_get_time();
  55. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  56. #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  57. microseconds = esp_rtc_get_time_us();
  58. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  59. return microseconds;
  60. }
  61. uint64_t esp_time_impl_get_time(void)
  62. {
  63. uint64_t microseconds = 0;
  64. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  65. microseconds = esp_system_get_time();
  66. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  67. microseconds = esp_rtc_get_time_us();
  68. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  69. return microseconds;
  70. }
  71. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  72. void esp_time_impl_set_boot_time(uint64_t time_us)
  73. {
  74. _lock_acquire(&s_boot_time_lock);
  75. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  76. REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
  77. REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
  78. #else
  79. s_boot_time = time_us;
  80. #endif
  81. _lock_release(&s_boot_time_lock);
  82. }
  83. uint64_t esp_time_impl_get_boot_time(void)
  84. {
  85. uint64_t result;
  86. _lock_acquire(&s_boot_time_lock);
  87. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  88. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  89. #else
  90. result = s_boot_time;
  91. #endif
  92. _lock_release(&s_boot_time_lock);
  93. return result;
  94. }
  95. void esp_set_time_from_rtc(void)
  96. {
  97. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  98. // initialize time from RTC clock
  99. s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
  100. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  101. }
  102. void esp_sync_timekeeping_timers(void)
  103. {
  104. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  105. struct timeval tv;
  106. gettimeofday(&tv, NULL);
  107. settimeofday(&tv, NULL);
  108. int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
  109. esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
  110. #endif
  111. }
  112. void esp_time_impl_init(void)
  113. {
  114. esp_set_time_from_rtc();
  115. }