clock_initialize.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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;
  61. #else
  62. volatile uint32_t g_system_timer;
  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. static void clock_inittime(void)
  140. {
  141. /* (Re-)initialize the time value to match the RTC */
  142. #ifndef CONFIG_CLOCK_TIMEKEEPING
  143. #ifndef CONFIG_RTC_HIRES
  144. clock_basetime(&g_basetime);
  145. #endif
  146. #ifndef CONFIG_SCHED_TICKLESS
  147. g_system_timer = INITIAL_SYSTEM_TIMER_TICKS;
  148. if (g_system_timer > 0)
  149. {
  150. struct timespec ts;
  151. (void)clock_ticks2time((sclock_t)g_system_timer, &ts);
  152. /* Adjust base time to hide initial timer ticks. */
  153. g_basetime.tv_sec -= ts.tv_sec;
  154. g_basetime.tv_nsec -= ts.tv_nsec;
  155. while (g_basetime.tv_nsec < 0)
  156. {
  157. g_basetime.tv_nsec += NSEC_PER_SEC;
  158. g_basetime.tv_sec--;
  159. }
  160. }
  161. #endif /* !CONFIG_SCHED_TICKLESS */
  162. #else
  163. clock_inittimekeeping();
  164. #endif
  165. }
  166. /****************************************************************************
  167. * Public Functions
  168. ****************************************************************************/
  169. /****************************************************************************
  170. * Name: clock_initialize
  171. *
  172. * Description:
  173. * Perform one-time initialization of the timing facilities.
  174. *
  175. ****************************************************************************/
  176. void clock_initialize(void)
  177. {
  178. #if defined(CONFIG_RTC) && !defined(CONFIG_RTC_EXTERNAL)
  179. /* Initialize the internal RTC hardware. Initialization of external RTC
  180. * must be deferred until the system has booted.
  181. */
  182. up_rtc_initialize();
  183. #endif
  184. /* Initialize the time value to match the RTC */
  185. clock_inittime();
  186. }
  187. /****************************************************************************
  188. * Name: clock_synchronize
  189. *
  190. * Description:
  191. * Synchronize the system timer to a hardware RTC. This operation is
  192. * normally performed automatically by the system during clock
  193. * initialization. However, the user may also need to explicitly re-
  194. * synchronize the system timer to the RTC under certain conditions where
  195. * the system timer is known to be in error. For example, in certain low-
  196. * power states, the system timer may be stopped but the RTC will continue
  197. * keep correct time. After recovering from such low-power state, this
  198. * function should be called to restore the correct system time.
  199. *
  200. * Calling this function could result in system time going "backward" in
  201. * time, especially with certain lower resolution RTC implementations.
  202. * Time going backward could have bad consequences if there are ongoing
  203. * timers and delays. So use this interface with care.
  204. *
  205. * Input Parameters:
  206. * None
  207. *
  208. * Returned Value:
  209. * None
  210. *
  211. * Assumptions:
  212. *
  213. ****************************************************************************/
  214. #ifdef CONFIG_RTC
  215. void clock_synchronize(void)
  216. {
  217. irqstate_t flags;
  218. /* Re-initialize the time value to match the RTC */
  219. flags = enter_critical_section();
  220. clock_inittime();
  221. leave_critical_section(flags);
  222. }
  223. #endif
  224. /****************************************************************************
  225. * Name: clock_resynchronize
  226. *
  227. * Description:
  228. * Resynchronize the system timer to a hardware RTC. The user can
  229. * explicitly re-synchronize the system timer to the RTC under certain
  230. * conditions where the system timer is known to be in error. For example,
  231. * in certain low-power states, the system timer may be stopped but the
  232. * RTC will continue keep correct time. After recovering from such
  233. * low-power state, this function should be called to restore the correct
  234. * system time. Function also keeps monotonic clock at rate of RTC.
  235. *
  236. * Calling this function will not result in system time going "backward" in
  237. * time. If setting system time with RTC would result time going "backward"
  238. * then resynchronization is not performed.
  239. *
  240. * Input Parameters:
  241. * rtc_diff: amount of time system-time is adjusted forward with RTC
  242. *
  243. * Returned Value:
  244. * None
  245. *
  246. * Assumptions:
  247. *
  248. ****************************************************************************/
  249. #if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS)
  250. void clock_resynchronize(FAR struct timespec *rtc_diff)
  251. {
  252. struct timespec rtc_time;
  253. struct timespec bias;
  254. struct timespec curr_ts;
  255. struct timespec rtc_diff_tmp;
  256. irqstate_t flags;
  257. int32_t carry;
  258. int ret;
  259. if (rtc_diff == NULL)
  260. {
  261. rtc_diff = &rtc_diff_tmp;
  262. }
  263. /* Set the time value to match the RTC */
  264. flags = enter_critical_section();
  265. /* Get RTC time */
  266. ret = clock_basetime(&rtc_time);
  267. if (ret < 0)
  268. {
  269. /* Error reading RTC, skip resynchronization. */
  270. sinfo("rtc error %d, skip resync\n", ret);
  271. rtc_diff->tv_sec = 0;
  272. rtc_diff->tv_nsec = 0;
  273. goto skip;
  274. }
  275. /* Get the elapsed time since power up (in milliseconds). This is a
  276. * bias value that we need to use to correct the base time.
  277. */
  278. (void)clock_systimespec(&bias);
  279. /* Add the base time to this. The base time is the time-of-day
  280. * setting. When added to the elapsed time since the time-of-day
  281. * was last set, this gives us the current time.
  282. */
  283. curr_ts.tv_sec = bias.tv_sec + g_basetime.tv_sec;
  284. curr_ts.tv_nsec = bias.tv_nsec + g_basetime.tv_nsec;
  285. /* Handle carry to seconds. */
  286. if (curr_ts.tv_nsec >= NSEC_PER_SEC)
  287. {
  288. carry = curr_ts.tv_nsec / NSEC_PER_SEC;
  289. curr_ts.tv_sec += carry;
  290. curr_ts.tv_nsec -= (carry * NSEC_PER_SEC);
  291. }
  292. /* Check if RTC has advanced past system time. */
  293. if (curr_ts.tv_sec > rtc_time.tv_sec ||
  294. (curr_ts.tv_sec == rtc_time.tv_sec && curr_ts.tv_nsec >= rtc_time.tv_nsec))
  295. {
  296. /* Setting system time with RTC now would result time going
  297. * backwards. Skip resynchronization.
  298. */
  299. sinfo("skip resync\n");
  300. rtc_diff->tv_sec = 0;
  301. rtc_diff->tv_nsec = 0;
  302. }
  303. else
  304. {
  305. /* Output difference between time at entry and new current time. */
  306. rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
  307. rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;
  308. /* Handle carry to seconds. */
  309. if (rtc_diff->tv_nsec < 0)
  310. {
  311. carry = -((-(rtc_diff->tv_nsec + 1)) / NSEC_PER_SEC + 1);
  312. }
  313. else if (rtc_diff->tv_nsec >= NSEC_PER_SEC)
  314. {
  315. carry = rtc_diff->tv_nsec / NSEC_PER_SEC;
  316. }
  317. else
  318. {
  319. carry = 0;
  320. }
  321. if (carry != 0)
  322. {
  323. rtc_diff->tv_sec += carry;
  324. rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
  325. }
  326. /* Add the sleep time to correct system timer */
  327. g_system_timer += SEC2TICK(rtc_diff->tv_sec);
  328. g_system_timer += NSEC2TICK(rtc_diff->tv_nsec);
  329. }
  330. skip:
  331. leave_critical_section(flags);
  332. }
  333. #endif
  334. /****************************************************************************
  335. * Name: clock_timer
  336. *
  337. * Description:
  338. * This function must be called once every time the real time clock
  339. * interrupt occurs. The interval of this clock interrupt must be
  340. * USEC_PER_TICK
  341. *
  342. ****************************************************************************/
  343. #ifndef CONFIG_SCHED_TICKLESS
  344. void clock_timer(void)
  345. {
  346. /* Increment the per-tick system counter */
  347. g_system_timer++;
  348. }
  349. #endif