esp_time_impl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_system.h"
  19. #include "soc/rtc.h"
  20. #include "esp_rom_sys.h"
  21. #include "esp_private/system_internal.h"
  22. #include "esp_time_impl.h"
  23. #include "sdkconfig.h"
  24. #if CONFIG_IDF_TARGET_ESP32
  25. #include "esp32/rom/rtc.h"
  26. #include "esp32/clk.h"
  27. #include "esp32/rtc.h"
  28. #elif CONFIG_IDF_TARGET_ESP32S2
  29. #include "esp32s2/rom/rtc.h"
  30. #include "esp32s2/clk.h"
  31. #include "esp32s2/rtc.h"
  32. #elif CONFIG_IDF_TARGET_ESP32S3
  33. #include "esp32s3/rom/rtc.h"
  34. #include "esp32s3/clk.h"
  35. #include "esp32s3/rtc.h"
  36. #elif CONFIG_IDF_TARGET_ESP32C3
  37. #include "esp32c3/rom/rtc.h"
  38. #include "esp32c3/clk.h"
  39. #include "esp32c3/rtc.h"
  40. #endif
  41. // Offset between FRC timer and the RTC.
  42. // Initialized after reset or light sleep.
  43. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER)
  44. uint64_t s_microseconds_offset;
  45. #endif
  46. #ifndef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  47. static uint64_t s_boot_time; // when RTC is used to persist time, two RTC_STORE registers are used to store boot time instead
  48. #endif
  49. static _lock_t s_boot_time_lock;
  50. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  51. uint64_t esp_time_impl_get_time_since_boot(void)
  52. {
  53. uint64_t microseconds = 0;
  54. #ifdef CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  55. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  56. microseconds = s_microseconds_offset + esp_system_get_time();
  57. #else
  58. microseconds = esp_system_get_time();
  59. #endif // CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  60. #elif defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  61. microseconds = esp_rtc_get_time_us();
  62. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  63. return microseconds;
  64. }
  65. uint64_t esp_time_impl_get_time(void)
  66. {
  67. uint64_t microseconds = 0;
  68. #if defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER )
  69. microseconds = esp_system_get_time();
  70. #elif defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  71. microseconds = esp_rtc_get_time_us();
  72. #endif // CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
  73. return microseconds;
  74. }
  75. #endif // defined( CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER ) || defined( CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER )
  76. void esp_time_impl_set_boot_time(uint64_t time_us)
  77. {
  78. _lock_acquire(&s_boot_time_lock);
  79. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  80. REG_WRITE(RTC_BOOT_TIME_LOW_REG, (uint32_t) (time_us & 0xffffffff));
  81. REG_WRITE(RTC_BOOT_TIME_HIGH_REG, (uint32_t) (time_us >> 32));
  82. #else
  83. s_boot_time = time_us;
  84. #endif
  85. _lock_release(&s_boot_time_lock);
  86. }
  87. uint64_t esp_clk_rtc_time(void)
  88. {
  89. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  90. return esp_rtc_get_time_us();
  91. #else
  92. return 0;
  93. #endif
  94. }
  95. uint64_t esp_time_impl_get_boot_time(void)
  96. {
  97. uint64_t result;
  98. _lock_acquire(&s_boot_time_lock);
  99. #ifdef CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER
  100. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  101. #else
  102. result = s_boot_time;
  103. #endif
  104. _lock_release(&s_boot_time_lock);
  105. return result;
  106. }
  107. uint32_t esp_clk_slowclk_cal_get(void)
  108. {
  109. return REG_READ(RTC_SLOW_CLK_CAL_REG);
  110. }
  111. uint64_t esp_rtc_get_time_us(void)
  112. {
  113. const uint64_t ticks = rtc_time_get();
  114. const uint32_t cal = esp_clk_slowclk_cal_get();
  115. /* RTC counter result is up to 2^48, calibration factor is up to 2^24,
  116. * for a 32kHz clock. We need to calculate (assuming no overflow):
  117. * (ticks * cal) >> RTC_CLK_CAL_FRACT
  118. *
  119. * An overflow in the (ticks * cal) multiplication would cause time to
  120. * wrap around after approximately 13 days, which is probably not enough
  121. * for some applications.
  122. * Therefore multiplication is split into two terms, for the lower 32-bit
  123. * and the upper 16-bit parts of "ticks", i.e.:
  124. * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT
  125. */
  126. const uint64_t ticks_low = ticks & UINT32_MAX;
  127. const uint64_t ticks_high = ticks >> 32;
  128. return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
  129. ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
  130. }
  131. void esp_clk_slowclk_cal_set(uint32_t new_cal)
  132. {
  133. #if defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
  134. /* To force monotonic time values even when clock calibration value changes,
  135. * we adjust boot time, given current time and the new calibration value:
  136. * T = boot_time_old + cur_cal * ticks / 2^19
  137. * T = boot_time_adj + new_cal * ticks / 2^19
  138. * which results in:
  139. * boot_time_adj = boot_time_old + ticks * (cur_cal - new_cal) / 2^19
  140. */
  141. const int64_t ticks = (int64_t) rtc_time_get();
  142. const uint32_t cur_cal = REG_READ(RTC_SLOW_CLK_CAL_REG);
  143. int32_t cal_diff = (int32_t) (cur_cal - new_cal);
  144. int64_t boot_time_diff = ticks * cal_diff / (1LL << RTC_CLK_CAL_FRACT);
  145. uint64_t boot_time_adj = esp_time_impl_get_boot_time() + boot_time_diff;
  146. esp_time_impl_set_boot_time(boot_time_adj);
  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. }