time.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <reent.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/reent.h>
  13. #include <sys/time.h>
  14. #include <sys/times.h>
  15. #include <sys/lock.h>
  16. #include "esp_system.h"
  17. #include "esp_attr.h"
  18. #include "esp_rom_sys.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "esp_private/system_internal.h"
  22. #include "soc/rtc.h"
  23. #include "esp_time_impl.h"
  24. #include "sdkconfig.h"
  25. #if !CONFIG_ESP_TIME_FUNCS_USE_NONE
  26. #define IMPL_NEWLIB_TIME_FUNCS 1
  27. #endif
  28. #if IMPL_NEWLIB_TIME_FUNCS
  29. // stores the start time of the slew
  30. static uint64_t s_adjtime_start_us;
  31. // is how many microseconds total to slew
  32. static int64_t s_adjtime_total_correction_us;
  33. static _lock_t s_time_lock;
  34. // This function gradually changes boot_time to the correction value and immediately updates it.
  35. static uint64_t adjust_boot_time(void)
  36. {
  37. #define ADJTIME_CORRECTION_FACTOR 6
  38. uint64_t boot_time = esp_time_impl_get_boot_time();
  39. if ((boot_time == 0) || (esp_time_impl_get_time_since_boot() < s_adjtime_start_us)) {
  40. s_adjtime_start_us = 0;
  41. }
  42. if (s_adjtime_start_us > 0) {
  43. uint64_t since_boot = esp_time_impl_get_time_since_boot();
  44. // If to call this function once per second, then (since_boot - s_adjtime_start_us) will be 1_000_000 (1 second),
  45. // and the correction will be equal to (1_000_000us >> 6) = 15_625 us.
  46. // The minimum possible correction step can be (64us >> 6) = 1us.
  47. // Example: if the time error is 1 second, then it will be compensate for 1 sec / 0,015625 = 64 seconds.
  48. int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (s_adjtime_start_us >> ADJTIME_CORRECTION_FACTOR);
  49. if (correction > 0) {
  50. s_adjtime_start_us = since_boot;
  51. if (s_adjtime_total_correction_us < 0) {
  52. if ((s_adjtime_total_correction_us + correction) >= 0) {
  53. boot_time = boot_time + s_adjtime_total_correction_us;
  54. s_adjtime_start_us = 0;
  55. } else {
  56. s_adjtime_total_correction_us += correction;
  57. boot_time -= correction;
  58. }
  59. } else {
  60. if ((s_adjtime_total_correction_us - correction) <= 0) {
  61. boot_time = boot_time + s_adjtime_total_correction_us;
  62. s_adjtime_start_us = 0;
  63. } else {
  64. s_adjtime_total_correction_us -= correction;
  65. boot_time += correction;
  66. }
  67. }
  68. esp_time_impl_set_boot_time(boot_time);
  69. }
  70. }
  71. return boot_time;
  72. }
  73. // Get the adjusted boot time.
  74. static uint64_t get_adjusted_boot_time(void)
  75. {
  76. _lock_acquire(&s_time_lock);
  77. uint64_t adjust_time = adjust_boot_time();
  78. _lock_release(&s_time_lock);
  79. return adjust_time;
  80. }
  81. // Applying the accumulated correction to base_time and stopping the smooth time adjustment.
  82. static void adjtime_corr_stop (void)
  83. {
  84. _lock_acquire(&s_time_lock);
  85. if (s_adjtime_start_us != 0){
  86. adjust_boot_time();
  87. s_adjtime_start_us = 0;
  88. }
  89. _lock_release(&s_time_lock);
  90. }
  91. #endif
  92. int adjtime(const struct timeval *delta, struct timeval *outdelta)
  93. {
  94. #if IMPL_NEWLIB_TIME_FUNCS
  95. if(outdelta != NULL){
  96. _lock_acquire(&s_time_lock);
  97. adjust_boot_time();
  98. if (s_adjtime_start_us != 0) {
  99. outdelta->tv_sec = s_adjtime_total_correction_us / 1000000L;
  100. outdelta->tv_usec = s_adjtime_total_correction_us % 1000000L;
  101. } else {
  102. outdelta->tv_sec = 0;
  103. outdelta->tv_usec = 0;
  104. }
  105. _lock_release(&s_time_lock);
  106. }
  107. if(delta != NULL){
  108. int64_t sec = delta->tv_sec;
  109. int64_t usec = delta->tv_usec;
  110. if(llabs(sec) > ((INT_MAX / 1000000L) - 1L)) {
  111. return -1;
  112. }
  113. /*
  114. * If adjusting the system clock by adjtime () is already done during the second call adjtime (),
  115. * and the delta of the second call is not NULL, the earlier tuning is stopped,
  116. * but the already completed part of the adjustment is not canceled.
  117. */
  118. _lock_acquire(&s_time_lock);
  119. // If correction is already in progress (s_adjtime_start_time_us != 0), then apply accumulated corrections.
  120. adjust_boot_time();
  121. s_adjtime_start_us = esp_time_impl_get_time_since_boot();
  122. s_adjtime_total_correction_us = sec * 1000000L + usec;
  123. _lock_release(&s_time_lock);
  124. }
  125. return 0;
  126. #else
  127. return -1;
  128. #endif
  129. }
  130. clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
  131. {
  132. clock_t t = xTaskGetTickCount() * (portTICK_PERIOD_MS * CLK_TCK / 1000);
  133. ptms->tms_cstime = 0;
  134. ptms->tms_cutime = 0;
  135. ptms->tms_stime = t;
  136. ptms->tms_utime = 0;
  137. struct timeval tv = {0, 0};
  138. _gettimeofday_r(r, &tv, NULL);
  139. return (clock_t) tv.tv_sec;
  140. }
  141. int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
  142. {
  143. (void) tz;
  144. #if IMPL_NEWLIB_TIME_FUNCS
  145. if (tv) {
  146. uint64_t microseconds = get_adjusted_boot_time() + esp_time_impl_get_time_since_boot();
  147. tv->tv_sec = microseconds / 1000000;
  148. tv->tv_usec = microseconds % 1000000;
  149. }
  150. return 0;
  151. #else
  152. __errno_r(r) = ENOSYS;
  153. return -1;
  154. #endif
  155. }
  156. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  157. {
  158. (void) tz;
  159. #if IMPL_NEWLIB_TIME_FUNCS
  160. if (tv) {
  161. adjtime_corr_stop();
  162. uint64_t now = ((uint64_t) tv->tv_sec) * 1000000LL + tv->tv_usec;
  163. uint64_t since_boot = esp_time_impl_get_time_since_boot();
  164. esp_time_impl_set_boot_time(now - since_boot);
  165. }
  166. return 0;
  167. #else
  168. errno = ENOSYS;
  169. return -1;
  170. #endif
  171. }
  172. int usleep(useconds_t us)
  173. {
  174. const int us_per_tick = portTICK_PERIOD_MS * 1000;
  175. if (us < us_per_tick) {
  176. esp_rom_delay_us((uint32_t) us);
  177. } else {
  178. /* since vTaskDelay(1) blocks for anywhere between 0 and portTICK_PERIOD_MS,
  179. * round up to compensate.
  180. */
  181. vTaskDelay((us + us_per_tick - 1) / us_per_tick);
  182. }
  183. return 0;
  184. }
  185. unsigned int sleep(unsigned int seconds)
  186. {
  187. usleep(seconds*1000000UL);
  188. return 0;
  189. }
  190. int clock_settime(clockid_t clock_id, const struct timespec *tp)
  191. {
  192. #if IMPL_NEWLIB_TIME_FUNCS
  193. if (tp == NULL) {
  194. errno = EINVAL;
  195. return -1;
  196. }
  197. struct timeval tv;
  198. switch (clock_id) {
  199. case CLOCK_REALTIME:
  200. tv.tv_sec = tp->tv_sec;
  201. tv.tv_usec = tp->tv_nsec / 1000L;
  202. settimeofday(&tv, NULL);
  203. break;
  204. default:
  205. errno = EINVAL;
  206. return -1;
  207. }
  208. return 0;
  209. #else
  210. errno = ENOSYS;
  211. return -1;
  212. #endif
  213. }
  214. int clock_gettime (clockid_t clock_id, struct timespec *tp)
  215. {
  216. #if IMPL_NEWLIB_TIME_FUNCS
  217. if (tp == NULL) {
  218. errno = EINVAL;
  219. return -1;
  220. }
  221. struct timeval tv;
  222. uint64_t monotonic_time_us = 0;
  223. switch (clock_id) {
  224. case CLOCK_REALTIME:
  225. _gettimeofday_r(NULL, &tv, NULL);
  226. tp->tv_sec = tv.tv_sec;
  227. tp->tv_nsec = tv.tv_usec * 1000L;
  228. break;
  229. case CLOCK_MONOTONIC:
  230. monotonic_time_us = esp_time_impl_get_time();
  231. tp->tv_sec = monotonic_time_us / 1000000LL;
  232. tp->tv_nsec = (monotonic_time_us % 1000000LL) * 1000L;
  233. break;
  234. default:
  235. errno = EINVAL;
  236. return -1;
  237. }
  238. return 0;
  239. #else
  240. errno = ENOSYS;
  241. return -1;
  242. #endif
  243. }
  244. int clock_getres (clockid_t clock_id, struct timespec *res)
  245. {
  246. #if IMPL_NEWLIB_TIME_FUNCS
  247. if (res == NULL) {
  248. errno = EINVAL;
  249. return -1;
  250. }
  251. res->tv_sec = 0;
  252. res->tv_nsec = esp_system_get_time_resolution();
  253. return 0;
  254. #else
  255. errno = ENOSYS;
  256. return -1;
  257. #endif
  258. }
  259. void esp_newlib_time_init(void)
  260. {
  261. esp_time_impl_init();
  262. }