time.c 8.6 KB

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