clock_timekeeping.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /****************************************************************************
  2. * sched/clock/clock_timekeeping.c
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Max Neklyudov <macscomp@gmail.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #ifdef CONFIG_CLOCK_TIMEKEEPING
  40. #include <sys/time.h>
  41. #include <stdint.h>
  42. #include <time.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/irq.h>
  46. #include <nuttx/arch.h>
  47. #include "clock/clock.h"
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. #define NTP_MAX_ADJUST 500
  52. /**********************************************************************
  53. * Private Data
  54. **********************************************************************/
  55. static struct timespec g_clock_wall_time;
  56. static uint64_t g_clock_last_counter;
  57. static uint64_t g_clock_mask;
  58. static long g_clock_adjust;
  59. /****************************************************************************
  60. * Private Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: clock_get_current_time
  64. ****************************************************************************/
  65. static int clock_get_current_time(FAR struct timespec *ts,
  66. FAR struct timespec *base)
  67. {
  68. irqstate_t flags;
  69. uint64_t counter;
  70. uint64_t offset;
  71. uint64_t nsec;
  72. time_t sec;
  73. int ret;
  74. flags = enter_critical_section();
  75. ret = up_timer_getcounter(&counter);
  76. if (ret < 0)
  77. {
  78. goto errout_in_critical_section;
  79. }
  80. offset = (counter - g_clock_last_counter) & g_clock_mask;
  81. nsec = offset * NSEC_PER_TICK;
  82. sec = nsec / NSEC_PER_SEC;
  83. nsec -= sec * NSEC_PER_SEC;
  84. nsec += base->tv_nsec;
  85. if (nsec > NSEC_PER_SEC)
  86. {
  87. nsec -= NSEC_PER_SEC;
  88. sec += 1;
  89. }
  90. ts->tv_nsec = nsec;
  91. ts->tv_sec = base->tv_sec + sec;
  92. errout_in_critical_section:
  93. leave_critical_section(flags);
  94. return ret;
  95. }
  96. /****************************************************************************
  97. * Public Functions
  98. ****************************************************************************/
  99. /****************************************************************************
  100. * Name: clock_timekeeping_get_wall_time
  101. ****************************************************************************/
  102. int clock_timekeeping_get_wall_time(FAR struct timespec *ts)
  103. {
  104. return clock_get_current_time(ts, &g_clock_wall_time);
  105. }
  106. /****************************************************************************
  107. * Name: clock_timekeeping_set_wall_time
  108. ****************************************************************************/
  109. int clock_timekeeping_set_wall_time(FAR struct timespec *ts)
  110. {
  111. irqstate_t flags;
  112. uint64_t counter;
  113. int ret;
  114. flags = enter_critical_section();
  115. ret = up_timer_getcounter(&counter);
  116. if (ret < 0)
  117. {
  118. goto errout_in_critical_section;
  119. }
  120. g_clock_wall_time = *ts;
  121. g_clock_adjust = 0;
  122. g_clock_last_counter = counter;
  123. errout_in_critical_section:
  124. leave_critical_section(flags);
  125. return ret;
  126. }
  127. /********************************************************************************
  128. * Name: adjtime
  129. *
  130. * Description:
  131. * The adjtime() function gradually adjusts the system clock (as returned
  132. * by gettimeofday(2)). The amount of time by which the clock is to be
  133. * adjusted is specified in the structure pointed to by delta.
  134. *
  135. * This structure has the following form:
  136. *
  137. * struct timeval
  138. * {
  139. * time_t tv_sec; (seconds)
  140. * suseconds_t tv_usec; (microseconds)
  141. * };
  142. *
  143. * If the adjustment in delta is positive, then the system clock is
  144. * speeded up by some small percentage (i.e., by adding a small amount of
  145. * time to the clock value in each second) until the adjustment has been
  146. * completed. If the adjustment in delta is negative, then the clock is
  147. * slowed down in a similar fashion.
  148. *
  149. * If a clock adjustment from an earlier adjtime() call is already in
  150. * progress at the time of a later adjtime() call, and delta is not NULL
  151. * for the later call, then the earlier adjustment is stopped, but any
  152. * already completed part of that adjustment is not undone.
  153. *
  154. * If olddelta is not NULL, then the buffer that it points to is used to
  155. * return the amount of time remaining from any previous adjustment that
  156. * has not yet been completed.
  157. *
  158. * NOTE: This is not a POSIX interface but derives from 4.3BSD, System V.
  159. * It is also supported for Linux compatibility.
  160. *
  161. ****************************************************************************/
  162. int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta)
  163. {
  164. irqstate_t flags;
  165. long adjust_usec;
  166. if (!delta)
  167. {
  168. set_errno(EINVAL);
  169. return -1;
  170. }
  171. flags = enter_critical_section();
  172. adjust_usec = delta->tv_sec * USEC_PER_SEC + delta->tv_usec;
  173. if (olddelta)
  174. {
  175. olddelta->tv_usec = g_clock_adjust;
  176. }
  177. g_clock_adjust = adjust_usec;
  178. leave_critical_section(flags);
  179. return OK;
  180. }
  181. /****************************************************************************
  182. * Name: clock_update_wall_time
  183. ****************************************************************************/
  184. void clock_update_wall_time(void)
  185. {
  186. irqstate_t flags;
  187. uint64_t counter;
  188. uint64_t offset;
  189. int64_t nsec;
  190. time_t sec;
  191. int ret;
  192. flags = enter_critical_section();
  193. ret = up_timer_getcounter(&counter);
  194. if (ret < 0)
  195. {
  196. goto errout_in_critical_section;
  197. }
  198. offset = (counter - g_clock_last_counter) & g_clock_mask;
  199. if (offset == 0)
  200. {
  201. goto errout_in_critical_section;
  202. }
  203. nsec = offset * NSEC_PER_TICK;
  204. sec = nsec / NSEC_PER_SEC;
  205. nsec -= sec * NSEC_PER_SEC;
  206. nsec += g_clock_wall_time.tv_nsec;
  207. if (nsec > NSEC_PER_SEC)
  208. {
  209. nsec -= NSEC_PER_SEC;
  210. sec += 1;
  211. }
  212. if (g_clock_adjust != 0 && sec > 0)
  213. {
  214. long adjust = NTP_MAX_ADJUST * (long)sec;
  215. if (g_clock_adjust < adjust && g_clock_adjust > -adjust)
  216. {
  217. adjust = g_clock_adjust;
  218. }
  219. nsec += adjust * NSEC_PER_USEC;
  220. while (nsec < 0)
  221. {
  222. nsec += NSEC_PER_SEC;
  223. sec -= 1;
  224. }
  225. while (nsec > NSEC_PER_SEC)
  226. {
  227. nsec -= NSEC_PER_SEC;
  228. sec += 1;
  229. }
  230. }
  231. g_clock_wall_time.tv_sec += sec;
  232. g_clock_wall_time.tv_nsec = (long)nsec;
  233. g_clock_last_counter = counter;
  234. errout_in_critical_section:
  235. leave_critical_section(flags);
  236. }
  237. /****************************************************************************
  238. * Name: clock_inittimekeeping
  239. ****************************************************************************/
  240. void clock_inittimekeeping(void)
  241. {
  242. up_timer_getmask(&g_clock_mask);
  243. clock_basetime(&g_clock_wall_time);
  244. up_timer_getcounter(&g_clock_last_counter);
  245. }
  246. #endif /* CONFIG_CLOCK_TIMEKEEPING */