time.c 7.9 KB

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