time.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2015-2017 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 <errno.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <reent.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/reent.h>
  21. #include <sys/time.h>
  22. #include <sys/times.h>
  23. #include <sys/lock.h>
  24. #include <rom/rtc.h>
  25. #include "esp_attr.h"
  26. #include "esp_intr_alloc.h"
  27. #include "esp_clk.h"
  28. #include "esp_timer.h"
  29. #include "soc/soc.h"
  30. #include "soc/rtc.h"
  31. #include "soc/rtc_cntl_reg.h"
  32. #include "soc/frc_timer_reg.h"
  33. #include "rom/ets_sys.h"
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/xtensa_api.h"
  36. #include "freertos/task.h"
  37. #include "sdkconfig.h"
  38. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  39. #define WITH_RTC 1
  40. #endif
  41. #if defined( CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 ) || defined( CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 )
  42. #define WITH_FRC 1
  43. #endif
  44. #ifdef WITH_RTC
  45. static uint64_t get_rtc_time_us()
  46. {
  47. const uint64_t ticks = rtc_time_get();
  48. const uint32_t cal = esp_clk_slowclk_cal_get();
  49. /* RTC counter result is up to 2^48, calibration factor is up to 2^24,
  50. * for a 32kHz clock. We need to calculate (assuming no overflow):
  51. * (ticks * cal) >> RTC_CLK_CAL_FRACT
  52. *
  53. * An overflow in the (ticks * cal) multiplication would cause time to
  54. * wrap around after approximately 13 days, which is probably not enough
  55. * for some applications.
  56. * Therefore multiplication is split into two terms, for the lower 32-bit
  57. * and the upper 16-bit parts of "ticks", i.e.:
  58. * ((ticks_low + 2^32 * ticks_high) * cal) >> RTC_CLK_CAL_FRACT
  59. */
  60. const uint64_t ticks_low = ticks & UINT32_MAX;
  61. const uint64_t ticks_high = ticks >> 32;
  62. return ((ticks_low * cal) >> RTC_CLK_CAL_FRACT) +
  63. ((ticks_high * cal) << (32 - RTC_CLK_CAL_FRACT));
  64. }
  65. #endif // WITH_RTC
  66. // s_boot_time: time from Epoch to the first boot time
  67. #ifdef WITH_RTC
  68. // when RTC is used to persist time, two RTC_STORE registers are used to store boot time
  69. #elif defined(WITH_FRC)
  70. static uint64_t s_boot_time;
  71. #endif // WITH_RTC
  72. #if defined(WITH_RTC) || defined(WITH_FRC)
  73. static _lock_t s_boot_time_lock;
  74. #endif
  75. // Offset between FRC timer and the RTC.
  76. // Initialized after reset or light sleep.
  77. #if defined(WITH_RTC) && defined(WITH_FRC)
  78. uint64_t s_microseconds_offset;
  79. #endif
  80. #if defined(WITH_RTC) || defined(WITH_FRC)
  81. static void set_boot_time(uint64_t time_us)
  82. {
  83. _lock_acquire(&s_boot_time_lock);
  84. #ifdef WITH_RTC
  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. static uint64_t get_boot_time()
  93. {
  94. uint64_t result;
  95. _lock_acquire(&s_boot_time_lock);
  96. #ifdef WITH_RTC
  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. #endif //defined(WITH_RTC) || defined(WITH_FRC)
  105. void esp_clk_slowclk_cal_set(uint32_t new_cal)
  106. {
  107. #if defined(WITH_RTC)
  108. /* To force monotonic time values even when clock calibration value changes,
  109. * we adjust boot time, given current time and the new calibration value:
  110. * T = boot_time_old + cur_cal * ticks / 2^19
  111. * T = boot_time_adj + new_cal * ticks / 2^19
  112. * which results in:
  113. * boot_time_adj = boot_time_old + ticks * (cur_cal - new_cal) / 2^19
  114. */
  115. const int64_t ticks = (int64_t) rtc_time_get();
  116. const uint32_t cur_cal = REG_READ(RTC_SLOW_CLK_CAL_REG);
  117. int32_t cal_diff = (int32_t) (cur_cal - new_cal);
  118. int64_t boot_time_diff = ticks * cal_diff / (1LL << RTC_CLK_CAL_FRACT);
  119. uint64_t boot_time_adj = get_boot_time() + boot_time_diff;
  120. set_boot_time(boot_time_adj);
  121. #endif // WITH_RTC
  122. REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
  123. }
  124. uint32_t esp_clk_slowclk_cal_get()
  125. {
  126. return REG_READ(RTC_SLOW_CLK_CAL_REG);
  127. }
  128. void esp_set_time_from_rtc()
  129. {
  130. #if defined( WITH_FRC ) && defined( WITH_RTC )
  131. // initialize time from RTC clock
  132. s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time();
  133. #endif // WITH_FRC && WITH_RTC
  134. }
  135. uint64_t esp_clk_rtc_time(void)
  136. {
  137. #ifdef WITH_RTC
  138. return get_rtc_time_us();
  139. #else
  140. return 0;
  141. #endif
  142. }
  143. clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
  144. {
  145. clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
  146. ptms->tms_cstime = 0;
  147. ptms->tms_cutime = 0;
  148. ptms->tms_stime = t;
  149. ptms->tms_utime = 0;
  150. struct timeval tv = {0, 0};
  151. _gettimeofday_r(r, &tv, NULL);
  152. return (clock_t) tv.tv_sec;
  153. }
  154. #if defined( WITH_FRC ) || defined( WITH_RTC )
  155. static uint64_t get_time_since_boot()
  156. {
  157. uint64_t microseconds = 0;
  158. #ifdef WITH_FRC
  159. #ifdef WITH_RTC
  160. microseconds = s_microseconds_offset + esp_timer_get_time();
  161. #else
  162. microseconds = esp_timer_get_time();
  163. #endif // WITH_RTC
  164. #elif defined(WITH_RTC)
  165. microseconds = get_rtc_time_us();
  166. #endif // WITH_FRC
  167. return microseconds;
  168. }
  169. #endif // defined( WITH_FRC ) || defined( WITH_RTC )
  170. int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
  171. {
  172. (void) tz;
  173. #if defined( WITH_FRC ) || defined( WITH_RTC )
  174. if (tv) {
  175. uint64_t microseconds = get_boot_time() + get_time_since_boot();
  176. tv->tv_sec = microseconds / 1000000;
  177. tv->tv_usec = microseconds % 1000000;
  178. }
  179. return 0;
  180. #else
  181. __errno_r(r) = ENOSYS;
  182. return -1;
  183. #endif // defined( WITH_FRC ) || defined( WITH_RTC )
  184. }
  185. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  186. {
  187. (void) tz;
  188. #if defined( WITH_FRC ) || defined( WITH_RTC )
  189. if (tv) {
  190. uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
  191. uint64_t since_boot = get_time_since_boot();
  192. set_boot_time(now - since_boot);
  193. }
  194. return 0;
  195. #else
  196. errno = ENOSYS;
  197. return -1;
  198. #endif
  199. }
  200. int usleep(useconds_t us)
  201. {
  202. const int us_per_tick = portTICK_PERIOD_MS * 1000;
  203. if (us < us_per_tick) {
  204. ets_delay_us((uint32_t) us);
  205. } else {
  206. /* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
  207. * round up to compensate.
  208. */
  209. vTaskDelay((us + us_per_tick - 1) / us_per_tick);
  210. }
  211. return 0;
  212. }
  213. unsigned int sleep(unsigned int seconds)
  214. {
  215. usleep(seconds*1000000UL);
  216. return 0;
  217. }
  218. uint32_t system_get_time(void)
  219. {
  220. #if defined( WITH_FRC ) || defined( WITH_RTC )
  221. return get_time_since_boot();
  222. #else
  223. return 0;
  224. #endif
  225. }
  226. uint32_t system_get_current_time(void) __attribute__((alias("system_get_time")));
  227. uint32_t system_relative_time(uint32_t current_time)
  228. {
  229. #if defined( WITH_FRC ) || defined( WITH_RTC )
  230. return get_time_since_boot() - current_time;
  231. #else
  232. return 0;
  233. #endif
  234. }
  235. uint64_t system_get_rtc_time(void)
  236. {
  237. #ifdef WITH_RTC
  238. return get_rtc_time_us();
  239. #else
  240. return 0;
  241. #endif
  242. }