esp_time_impl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <time.h>
  16. #include <sys/time.h>
  17. #include <sys/lock.h>
  18. #include "esp_attr.h"
  19. #include "esp_system.h"
  20. #include "soc/rtc.h"
  21. #include "esp_rom_sys.h"
  22. #include "esp_private/system_internal.h"
  23. #include "esp_time_impl.h"
  24. #include "sdkconfig.h"
  25. #if CONFIG_IDF_TARGET_ESP32
  26. #include "esp32/rom/rtc.h"
  27. #include "esp32/clk.h"
  28. #include "esp32/rtc.h"
  29. #elif CONFIG_IDF_TARGET_ESP32S2
  30. #include "esp32s2/rom/rtc.h"
  31. #include "esp32s2/clk.h"
  32. #include "esp32s2/rtc.h"
  33. #elif CONFIG_IDF_TARGET_ESP32S3
  34. #include "esp32s3/rom/rtc.h"
  35. #include "esp32s3/clk.h"
  36. #include "esp32s3/rtc.h"
  37. #elif CONFIG_IDF_TARGET_ESP32C3
  38. #include "esp32c3/rom/rtc.h"
  39. #include "esp32c3/clk.h"
  40. #include "esp32c3/rtc.h"
  41. #elif CONFIG_IDF_TARGET_ESP32H2
  42. #include "esp32h2/rom/rtc.h"
  43. #include "esp32h2/clk.h"
  44. #include "esp32h2/rtc.h"
  45. #endif
  46. // Offset between FRC timer and the RTC.
  47. // Initialized after reset or light sleep.
  48. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
  49. int64_t s_microseconds_offset = 0;
  50. #endif
  51. #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  52. static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
  53. #endif
  54. static _lock_t s_boot_time_lock;
  55. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  56. uint64_t esp_time_impl_get_time_since_boot(void)
  57. {
  58. uint64_t microseconds = 0;
  59. #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  60. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  61. microseconds = s_microseconds_offset + esp_system_get_time();
  62. #else
  63. microseconds = esp_system_get_time();
  64. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  65. #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  66. microseconds = esp_rtc_get_time_us();
  67. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  68. return microseconds;
  69. }
  70. uint64_t esp_time_impl_get_time(void)
  71. {
  72. uint64_t microseconds = 0;
  73. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  74. microseconds = esp_system_get_time();
  75. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  76. microseconds = esp_rtc_get_time_us();
  77. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  78. return microseconds;
  79. }
  80. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  81. void esp_time_impl_set_boot_time(uint64_t time_us)
  82. {
  83. _lock_acquire(&s_boot_time_lock);
  84. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  85. REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
  86. REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
  87. #else
  88. s_boot_time = time_us;
  89. #endif
  90. _lock_release(&s_boot_time_lock);
  91. }
  92. uint64_t esp_time_impl_get_boot_time(void)
  93. {
  94. uint64_t result;
  95. _lock_acquire(&s_boot_time_lock);
  96. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  97. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  98. #else
  99. result = s_boot_time;
  100. #endif
  101. _lock_release(&s_boot_time_lock);
  102. return result;
  103. }
  104. void esp_set_time_from_rtc(void)
  105. {
  106. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  107. // initialize time from RTC clock
  108. s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
  109. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  110. }
  111. void esp_sync_counters_rtc_and_frc(void)
  112. {
  113. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  114. struct timeval tv;
  115. gettimeofday(&tv, NULL);
  116. settimeofday(&tv, NULL);
  117. int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
  118. esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
  119. #endif
  120. }
  121. void esp_time_impl_init(void)
  122. {
  123. esp_set_time_from_rtc();
  124. }