time.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_FRC1 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_FRC1)
  70. static uint64_t s_boot_time;
  71. #endif
  72. #if defined(WITH_RTC) || defined(WITH_FRC1)
  73. static _lock_t s_boot_time_lock;
  74. #endif
  75. #ifdef WITH_RTC
  76. uint64_t s_microseconds_offset;
  77. #endif
  78. #if defined(WITH_RTC) || defined(WITH_FRC1)
  79. static void set_boot_time(uint64_t time_us)
  80. {
  81. _lock_acquire(&s_boot_time_lock);
  82. #ifdef WITH_RTC
  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. static uint64_t get_boot_time()
  91. {
  92. uint64_t result;
  93. _lock_acquire(&s_boot_time_lock);
  94. #ifdef WITH_RTC
  95. result = ((uint64_t) REG_READ(RTC_BOOT_TIME_LOW_REG)) + (((uint64_t) REG_READ(RTC_BOOT_TIME_HIGH_REG)) << 32);
  96. #else
  97. result = s_boot_time;
  98. #endif
  99. _lock_release(&s_boot_time_lock);
  100. return result;
  101. }
  102. #endif //defined(WITH_RTC) || defined(WITH_FRC1)
  103. void esp_clk_slowclk_cal_set(uint32_t new_cal)
  104. {
  105. #if defined(WITH_RTC)
  106. /* To force monotonic time values even when clock calibration value changes,
  107. * we adjust boot time, given current time and the new calibration value:
  108. * T = boot_time_old + cur_cal * ticks / 2^19
  109. * T = boot_time_adj + new_cal * ticks / 2^19
  110. * which results in:
  111. * boot_time_adj = boot_time_old + ticks * (cur_cal - new_cal) / 2^19
  112. */
  113. const int64_t ticks = (int64_t) rtc_time_get();
  114. const uint32_t cur_cal = REG_READ(RTC_SLOW_CLK_CAL_REG);
  115. int32_t cal_diff = (int32_t) (cur_cal - new_cal);
  116. int64_t boot_time_diff = ticks * cal_diff / (1LL << RTC_CLK_CAL_FRACT);
  117. uint64_t boot_time_adj = get_boot_time() + boot_time_diff;
  118. set_boot_time(boot_time_adj);
  119. #endif // WITH_RTC
  120. REG_WRITE(RTC_SLOW_CLK_CAL_REG, new_cal);
  121. }
  122. uint32_t esp_clk_slowclk_cal_get()
  123. {
  124. return REG_READ(RTC_SLOW_CLK_CAL_REG);
  125. }
  126. void esp_set_time_from_rtc()
  127. {
  128. #if defined( WITH_FRC1 ) && defined( WITH_RTC )
  129. // initialize time from RTC clock
  130. s_microseconds_offset = get_rtc_time_us() - esp_timer_get_time();
  131. #endif // WITH_FRC1 && WITH_RTC
  132. }
  133. uint64_t esp_clk_rtc_time(void)
  134. {
  135. #ifdef WITH_RTC
  136. return get_rtc_time_us();
  137. #else
  138. return 0;
  139. #endif
  140. }
  141. clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
  142. {
  143. clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
  144. ptms->tms_cstime = 0;
  145. ptms->tms_cutime = 0;
  146. ptms->tms_stime = t;
  147. ptms->tms_utime = 0;
  148. struct timeval tv = {0, 0};
  149. _gettimeofday_r(r, &tv, NULL);
  150. return (clock_t) tv.tv_sec;
  151. }
  152. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  153. static uint64_t get_time_since_boot()
  154. {
  155. uint64_t microseconds = 0;
  156. #ifdef WITH_FRC1
  157. microseconds = s_microseconds_offset + esp_timer_get_time();
  158. #elif defined(WITH_RTC)
  159. microseconds = get_rtc_time_us();
  160. #endif
  161. return microseconds;
  162. }
  163. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  164. int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
  165. {
  166. (void) tz;
  167. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  168. if (tv) {
  169. uint64_t microseconds = get_boot_time() + get_time_since_boot();
  170. tv->tv_sec = microseconds / 1000000;
  171. tv->tv_usec = microseconds % 1000000;
  172. }
  173. return 0;
  174. #else
  175. __errno_r(r) = ENOSYS;
  176. return -1;
  177. #endif // defined( WITH_FRC1 ) || defined( WITH_RTC )
  178. }
  179. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  180. {
  181. (void) tz;
  182. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  183. if (tv) {
  184. uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
  185. uint64_t since_boot = get_time_since_boot();
  186. set_boot_time(now - since_boot);
  187. }
  188. return 0;
  189. #else
  190. errno = ENOSYS;
  191. return -1;
  192. #endif
  193. }
  194. int usleep(useconds_t us)
  195. {
  196. const int us_per_tick = portTICK_PERIOD_MS * 1000;
  197. if (us < us_per_tick) {
  198. ets_delay_us((uint32_t) us);
  199. } else {
  200. /* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
  201. * round up to compensate.
  202. */
  203. vTaskDelay((us + us_per_tick - 1) / us_per_tick);
  204. }
  205. return 0;
  206. }
  207. unsigned int sleep(unsigned int seconds)
  208. {
  209. usleep(seconds*1000000UL);
  210. return 0;
  211. }
  212. uint32_t system_get_time(void)
  213. {
  214. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  215. return get_time_since_boot();
  216. #else
  217. return 0;
  218. #endif
  219. }
  220. uint32_t system_get_current_time(void) __attribute__((alias("system_get_time")));
  221. uint32_t system_relative_time(uint32_t current_time)
  222. {
  223. #if defined( WITH_FRC1 ) || defined( WITH_RTC )
  224. return get_time_since_boot() - current_time;
  225. #else
  226. return 0;
  227. #endif
  228. }
  229. uint64_t system_get_rtc_time(void)
  230. {
  231. #ifdef WITH_RTC
  232. return get_rtc_time_us();
  233. #else
  234. return 0;
  235. #endif
  236. }