clock_initialize.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /****************************************************************************
  2. * sched/clock/clock_initialize.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011-2012, 2017-2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <nuttx/compiler.h>
  41. #include <stdint.h>
  42. #include <time.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #ifdef CONFIG_RTC
  46. # include <nuttx/irq.h>
  47. #endif
  48. #include <nuttx/arch.h>
  49. #include <nuttx/clock.h>
  50. #include <nuttx/time.h>
  51. #include "clock/clock.h"
  52. #ifdef CONFIG_CLOCK_TIMEKEEPING
  53. # include "clock/clock_timekeeping.h"
  54. #endif
  55. /****************************************************************************
  56. * Public Data
  57. ****************************************************************************/
  58. #ifndef CONFIG_SCHED_TICKLESS
  59. #ifdef CONFIG_SYSTEM_TIME64
  60. volatile uint64_t g_system_timer = INITIAL_SYSTEM_TIMER_TICKS;
  61. #else
  62. volatile uint32_t g_system_timer = INITIAL_SYSTEM_TIMER_TICKS;
  63. #endif
  64. #endif
  65. #ifndef CONFIG_CLOCK_TIMEKEEPING
  66. struct timespec g_basetime;
  67. #endif
  68. /****************************************************************************
  69. * Private Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: clock_basetime
  73. *
  74. * Description:
  75. * Get the initial time value from the best source available.
  76. *
  77. ****************************************************************************/
  78. #ifdef CONFIG_RTC
  79. #if defined(CONFIG_RTC_DATETIME)
  80. /* Initialize the system time using a broken out date/time structure */
  81. int clock_basetime(FAR struct timespec *tp)
  82. {
  83. struct tm rtctime;
  84. long nsecs = 0;
  85. int ret;
  86. /* Get the broken-out time from the date/time RTC. */
  87. #ifdef CONFIG_ARCH_HAVE_RTC_SUBSECONDS
  88. ret = up_rtc_getdatetime_with_subseconds(&rtctime, &nsecs);
  89. #else
  90. ret = up_rtc_getdatetime(&rtctime);
  91. #endif
  92. if (ret >= 0)
  93. {
  94. /* And use the broken-out time to initialize the system time */
  95. tp->tv_sec = mktime(&rtctime);
  96. tp->tv_nsec = nsecs;
  97. }
  98. return ret;
  99. }
  100. #elif defined(CONFIG_RTC_HIRES)
  101. /* Initialize the system time using a high-resolution structure */
  102. int clock_basetime(FAR struct timespec *tp)
  103. {
  104. /* Get the complete time from the hi-res RTC. */
  105. return up_rtc_gettime(tp);
  106. }
  107. #else
  108. /* Initialize the system time using seconds only */
  109. int clock_basetime(FAR struct timespec *tp)
  110. {
  111. /* Get the seconds (only) from the lo-resolution RTC */
  112. tp->tv_sec = up_rtc_time();
  113. tp->tv_nsec = 0;
  114. return OK;
  115. }
  116. #endif /* CONFIG_RTC_HIRES */
  117. #else /* CONFIG_RTC */
  118. int clock_basetime(FAR struct timespec *tp)
  119. {
  120. time_t jdn = 0;
  121. /* Get the EPOCH-relative julian date from the calendar year,
  122. * month, and date
  123. */
  124. jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH - 1,
  125. CONFIG_START_DAY);
  126. /* Set the base time as seconds into this julian day. */
  127. tp->tv_sec = jdn * SEC_PER_DAY;
  128. tp->tv_nsec = 0;
  129. return OK;
  130. }
  131. #endif /* CONFIG_RTC */
  132. /****************************************************************************
  133. * Name: clock_inittime
  134. *
  135. * Description:
  136. * Get the initial time value from the best source available.
  137. *
  138. ****************************************************************************/
  139. #ifdef CONFIG_RTC
  140. static void clock_inittime(void)
  141. {
  142. /* (Re-)initialize the time value to match the RTC */
  143. #ifndef CONFIG_CLOCK_TIMEKEEPING
  144. struct timespec ts;
  145. clock_basetime(&g_basetime);
  146. clock_systimespec(&ts);
  147. /* Adjust base time to hide initial timer ticks. */
  148. g_basetime.tv_sec -= ts.tv_sec;
  149. g_basetime.tv_nsec -= ts.tv_nsec;
  150. while (g_basetime.tv_nsec < 0)
  151. {
  152. g_basetime.tv_nsec += NSEC_PER_SEC;
  153. g_basetime.tv_sec--;
  154. }
  155. #else
  156. clock_inittimekeeping();
  157. #endif
  158. }
  159. #endif
  160. /****************************************************************************
  161. * Public Functions
  162. ****************************************************************************/
  163. /****************************************************************************
  164. * Name: clock_initialize
  165. *
  166. * Description:
  167. * Perform one-time initialization of the timing facilities.
  168. *
  169. ****************************************************************************/
  170. void clock_initialize(void)
  171. {
  172. #if defined(CONFIG_RTC) && !defined(CONFIG_RTC_EXTERNAL)
  173. /* Initialize the internal RTC hardware. Initialization of external RTC
  174. * must be deferred until the system has booted.
  175. */
  176. up_rtc_initialize();
  177. /* Initialize the time value to match the RTC */
  178. clock_inittime();
  179. #endif
  180. }
  181. /****************************************************************************
  182. * Name: clock_synchronize
  183. *
  184. * Description:
  185. * Synchronize the system timer to a hardware RTC. This operation is
  186. * normally performed automatically by the system during clock
  187. * initialization. However, the user may also need to explicitly re-
  188. * synchronize the system timer to the RTC under certain conditions where
  189. * the system timer is known to be in error. For example, in certain low-
  190. * power states, the system timer may be stopped but the RTC will continue
  191. * keep correct time. After recovering from such low-power state, this
  192. * function should be called to restore the correct system time.
  193. *
  194. * Calling this function could result in system time going "backward" in
  195. * time, especially with certain lower resolution RTC implementations.
  196. * Time going backward could have bad consequences if there are ongoing
  197. * timers and delays. So use this interface with care.
  198. *
  199. * Input Parameters:
  200. * None
  201. *
  202. * Returned Value:
  203. * None
  204. *
  205. * Assumptions:
  206. *
  207. ****************************************************************************/
  208. #ifdef CONFIG_RTC
  209. void clock_synchronize(void)
  210. {
  211. irqstate_t flags;
  212. /* Re-initialize the time value to match the RTC */
  213. flags = enter_critical_section();
  214. clock_inittime();
  215. leave_critical_section(flags);
  216. }
  217. #endif
  218. /****************************************************************************
  219. * Name: clock_resynchronize
  220. *
  221. * Description:
  222. * Resynchronize the system timer to a hardware RTC. The user can
  223. * explicitly re-synchronize the system timer to the RTC under certain
  224. * conditions where the system timer is known to be in error. For example,
  225. * in certain low-power states, the system timer may be stopped but the
  226. * RTC will continue keep correct time. After recovering from such
  227. * low-power state, this function should be called to restore the correct
  228. * system time. Function also keeps monotonic clock at rate of RTC.
  229. *
  230. * Calling this function will not result in system time going "backward" in
  231. * time. If setting system time with RTC would result time going "backward"
  232. * then resynchronization is not performed.
  233. *
  234. * Input Parameters:
  235. * rtc_diff: amount of time system-time is adjusted forward with RTC
  236. *
  237. * Returned Value:
  238. * None
  239. *
  240. * Assumptions:
  241. *
  242. ****************************************************************************/
  243. #if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS)
  244. void clock_resynchronize(FAR struct timespec *rtc_diff)
  245. {
  246. struct timespec rtc_time, bias, curr_ts;
  247. struct timespec rtc_diff_tmp;
  248. irqstate_t flags;
  249. int32_t carry;
  250. int ret;
  251. if (rtc_diff == NULL)
  252. {
  253. rtc_diff = &rtc_diff_tmp;
  254. }
  255. /* Set the time value to match the RTC */
  256. flags = enter_critical_section();
  257. /* Get RTC time */
  258. ret = clock_basetime(&rtc_time);
  259. if (ret < 0)
  260. {
  261. /* Error reading RTC, skip resynchronization. */
  262. sinfo("rtc error %d, skip resync\n", ret);
  263. rtc_diff->tv_sec = 0;
  264. rtc_diff->tv_nsec = 0;
  265. goto skip;
  266. }
  267. /* Get the elapsed time since power up (in milliseconds). This is a
  268. * bias value that we need to use to correct the base time.
  269. */
  270. (void)clock_systimespec(&bias);
  271. /* Add the base time to this. The base time is the time-of-day
  272. * setting. When added to the elapsed time since the time-of-day
  273. * was last set, this gives us the current time.
  274. */
  275. curr_ts.tv_sec = bias.tv_sec + g_basetime.tv_sec;
  276. curr_ts.tv_nsec = bias.tv_nsec + g_basetime.tv_nsec;
  277. /* Handle carry to seconds. */
  278. if (curr_ts.tv_nsec >= NSEC_PER_SEC)
  279. {
  280. carry = curr_ts.tv_nsec / NSEC_PER_SEC;
  281. curr_ts.tv_sec += carry;
  282. curr_ts.tv_nsec -= (carry * NSEC_PER_SEC);
  283. }
  284. /* Check if RTC has advanced past system time. */
  285. if (curr_ts.tv_sec > rtc_time.tv_sec ||
  286. (curr_ts.tv_sec == rtc_time.tv_sec && curr_ts.tv_nsec >= rtc_time.tv_nsec))
  287. {
  288. /* Setting system time with RTC now would result time going
  289. * backwards. Skip resynchronization.
  290. */
  291. sinfo("skip resync\n");
  292. rtc_diff->tv_sec = 0;
  293. rtc_diff->tv_nsec = 0;
  294. }
  295. else
  296. {
  297. /* Output difference between time at entry and new current time. */
  298. rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
  299. rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;
  300. /* Handle carry to seconds. */
  301. if (rtc_diff->tv_nsec < 0)
  302. {
  303. carry = -((-(rtc_diff->tv_nsec + 1)) / NSEC_PER_SEC + 1);
  304. }
  305. else if (rtc_diff->tv_nsec >= NSEC_PER_SEC)
  306. {
  307. carry = rtc_diff->tv_nsec / NSEC_PER_SEC;
  308. }
  309. else
  310. {
  311. carry = 0;
  312. }
  313. if (carry != 0)
  314. {
  315. rtc_diff->tv_sec += carry;
  316. rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
  317. }
  318. /* Add the sleep time to correct system timer */
  319. g_system_timer += SEC2TICK(rtc_diff->tv_sec);
  320. g_system_timer += NSEC2TICK(rtc_diff->tv_nsec);
  321. }
  322. skip:
  323. leave_critical_section(flags);
  324. }
  325. #endif
  326. /****************************************************************************
  327. * Name: clock_timer
  328. *
  329. * Description:
  330. * This function must be called once every time the real time clock
  331. * interrupt occurs. The interval of this clock interrupt must be
  332. * USEC_PER_TICK
  333. *
  334. ****************************************************************************/
  335. #ifndef CONFIG_SCHED_TICKLESS
  336. void clock_timer(void)
  337. {
  338. /* Increment the per-tick system counter */
  339. g_system_timer++;
  340. }
  341. #endif