esp_time_impl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #endif
  42. // Offset between FRC timer and the RTC.
  43. // Initialized after reset or light sleep.
  44. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
  45. uint64_t s_microseconds_offset;
  46. #endif
  47. #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  48. static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
  49. #endif
  50. static _lock_t s_boot_time_lock;
  51. static _lock_t s_esp_rtc_time_lock;
  52. static RTC_DATA_ATTR uint64_t s_esp_rtc_time_us = 0, s_rtc_last_ticks = 0;
  53. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  54. uint64_t esp_time_impl_get_time_since_boot(void)
  55. {
  56. uint64_t microseconds = 0;
  57. #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  58. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  59. microseconds = s_microseconds_offset + esp_system_get_time();
  60. #else
  61. microseconds = esp_system_get_time();
  62. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  63. #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  64. microseconds = esp_rtc_get_time_us();
  65. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  66. return microseconds;
  67. }
  68. uint64_t esp_time_impl_get_time(void)
  69. {
  70. uint64_t microseconds = 0;
  71. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  72. microseconds = esp_system_get_time();
  73. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  74. microseconds = esp_rtc_get_time_us();
  75. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  76. return microseconds;
  77. }
  78. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  79. void esp_time_impl_set_boot_time(uint64_t time_us)
  80. {
  81. _lock_acquire(&s_boot_time_lock);
  82. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  83. REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
  84. REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
  85. #else
  86. s_boot_time = time_us;
  87. #endif
  88. _lock_release(&s_boot_time_lock);
  89. }
  90. uint64_t esp_clk_rtc_time(void)
  91. {
  92. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  93. return esp_rtc_get_time_us();
  94. #else
  95. return 0;
  96. #endif
  97. }
  98. uint64_t esp_time_impl_get_boot_time(void)
  99. {
  100. uint64_t result;
  101. _lock_acquire(&s_boot_time_lock);
  102. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  103. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  104. #else
  105. result = s_boot_time;
  106. #endif
  107. _lock_release(&s_boot_time_lock);
  108. return result;
  109. }
  110. uint32_t esp_clk_slowclk_cal_get(void)
  111. {
  112. return REG_READ(RTC_SLOW_CLK_CAL_REG);
  113. }
  114. uint64_t esp_rtc_get_time_us(void)
  115. {
  116. _lock_acquire(&s_esp_rtc_time_lock);
  117. const uint32_t cal = esp_clk_slowclk_cal_get();
  118. const uint64_t rtc_this_ticks = rtc_time_get();
  119. const uint64_t ticks = rtc_this_ticks - s_rtc_last_ticks;
  120. /* RTC counter result is up to 2^48, calibration factor is up to 2^24,
  121. * for a 32kHz clock. We need to calculate (assuming no overflow):
  122. * (ticks * cal) >> RTC_CLK_CAL_FRACT
  123. *
  124. * An overflow in the (ticks * cal) multiplication would cause time to
  125. * wrap around after approximately 13 days, which is probably not enough
  126. * for some applications.
  127. * Therefore multiplication is split into two terms, for the lower 32-bit
  128. * and the upper 16-bit parts of "ticks", i.e.:
  129. * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT
  130. */
  131. const uint64_t ticks_low = ticks & UINT32_MAX;
  132. const uint64_t ticks_high = ticks >> 32;
  133. const uint64_t delta_time_us = ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
  134. ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
  135. s_esp_rtc_time_us += delta_time_us;
  136. s_rtc_last_ticks = rtc_this_ticks;
  137. _lock_release(&s_esp_rtc_time_lock);
  138. return s_esp_rtc_time_us;
  139. }
  140. void esp_clk_slowclk_cal_set(uint32_t new_cal)
  141. {
  142. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  143. /* To force monotonic time values even when clock calibration value changes,
  144. * we adjust esp_rtc_time
  145. */
  146. esp_rtc_get_time_us();
  147. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  148. REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
  149. }
  150. void esp_set_time_from_rtc(void)
  151. {
  152. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  153. // initialize time from RTC clock
  154. s_microseconds_offset = esp_rtc_get_time_us() - esp_system_get_time();
  155. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER && CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  156. }
  157. void esp_sync_counters_rtc_and_frc(void)
  158. {
  159. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) && defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  160. struct timeval tv;
  161. gettimeofday(&tv, NULL);
  162. settimeofday(&tv, NULL);
  163. int64_t s_microseconds_offset_cur = esp_rtc_get_time_us() - esp_system_get_time();
  164. esp_time_impl_set_boot_time(esp_time_impl_get_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
  165. #endif
  166. }
  167. void esp_time_impl_init(void)
  168. {
  169. esp_set_time_from_rtc();
  170. }