arch_timer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /****************************************************************************
  2. * drivers/timers/arch_timer.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/arch.h>
  25. #include <nuttx/clock.h>
  26. #include <nuttx/timers/arch_timer.h>
  27. /****************************************************************************
  28. * Pre-processor Definitions
  29. ****************************************************************************/
  30. #if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)
  31. # error CONFIG_SCHED_TICKLESS_ALARM must be unset to use the arch timer
  32. #endif
  33. #define CONFIG_BOARD_LOOPSPER100USEC ((CONFIG_BOARD_LOOPSPERMSEC+5)/10)
  34. #define CONFIG_BOARD_LOOPSPER10USEC ((CONFIG_BOARD_LOOPSPERMSEC+50)/100)
  35. #define CONFIG_BOARD_LOOPSPERUSEC ((CONFIG_BOARD_LOOPSPERMSEC+500)/1000)
  36. #define TIMER_START(l) ((l)->ops->start(l))
  37. #define TIMER_GETSTATUS(l,s) ((l)->ops->getstatus(l,s))
  38. #define TIMER_SETTIMEOUT(l,t) ((l)->ops->settimeout(l,t))
  39. #define TIMER_SETCALLBACK(l,c,a) ((l)->ops->setcallback(l,c,a))
  40. #define TIMER_MAXTIMEOUT(l,t) ((l)->ops->maxtimeout(l,t))
  41. /****************************************************************************
  42. * Private Types
  43. ****************************************************************************/
  44. struct arch_timer_s
  45. {
  46. FAR struct timer_lowerhalf_s *lower;
  47. uint32_t *next_interval;
  48. uint32_t maxtimeout;
  49. uint64_t timebase;
  50. };
  51. /****************************************************************************
  52. * Private Data
  53. ****************************************************************************/
  54. static struct arch_timer_s g_timer;
  55. /****************************************************************************
  56. * Private Functions
  57. ****************************************************************************/
  58. #if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_SCHED_CRITMONITOR) \
  59. || defined(CONFIG_SCHED_IRQMONITOR_GETTIME)
  60. static inline void timespec_from_usec(FAR struct timespec *ts,
  61. uint64_t microseconds)
  62. {
  63. ts->tv_sec = microseconds / USEC_PER_SEC;
  64. microseconds -= (uint64_t)ts->tv_sec * USEC_PER_SEC;
  65. ts->tv_nsec = microseconds * NSEC_PER_USEC;
  66. }
  67. #endif
  68. #ifdef CONFIG_SCHED_TICKLESS
  69. static inline uint64_t timespec_to_usec(const FAR struct timespec *ts)
  70. {
  71. return (uint64_t)ts->tv_sec * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC;
  72. }
  73. static inline bool timeout_diff(uint32_t new, uint32_t old)
  74. {
  75. return new < old ? old - new >= USEC_PER_TICK : new - old >= USEC_PER_TICK;
  76. }
  77. static uint32_t update_timeout(uint32_t timeout)
  78. {
  79. struct timer_status_s status;
  80. /* Don't need critical section here
  81. * since caller already do it for us
  82. */
  83. TIMER_GETSTATUS(g_timer.lower, &status);
  84. if (g_timer.next_interval)
  85. {
  86. /* If the timer interrupt is in the process,
  87. * let the callback return the right interval.
  88. */
  89. *g_timer.next_interval = timeout;
  90. }
  91. else if (timeout_diff(timeout, status.timeleft))
  92. {
  93. /* Otherwise, update the timeout directly. */
  94. TIMER_SETTIMEOUT(g_timer.lower, timeout);
  95. g_timer.timebase += status.timeout - status.timeleft;
  96. }
  97. return status.timeleft;
  98. }
  99. #endif
  100. static uint64_t current_usec(void)
  101. {
  102. struct timer_status_s status;
  103. uint64_t timebase;
  104. do
  105. {
  106. timebase = g_timer.timebase;
  107. TIMER_GETSTATUS(g_timer.lower, &status);
  108. }
  109. while (timebase != g_timer.timebase);
  110. return timebase + (status.timeout - status.timeleft);
  111. }
  112. static void udelay_accurate(useconds_t microseconds)
  113. {
  114. uint64_t start = current_usec();
  115. while (current_usec() - start < microseconds)
  116. {
  117. ; /* Wait until the timeout reach */
  118. }
  119. }
  120. static void udelay_coarse(useconds_t microseconds)
  121. {
  122. volatile int i;
  123. /* We'll do this a little at a time because we expect that the
  124. * CONFIG_BOARD_LOOPSPERUSEC is very inaccurate during to truncation in
  125. * the divisions of its calculation. We'll use the largest values that
  126. * we can in order to prevent significant error buildup in the loops.
  127. */
  128. while (microseconds > 1000)
  129. {
  130. for (i = 0; i < CONFIG_BOARD_LOOPSPERMSEC; i++)
  131. {
  132. }
  133. microseconds -= 1000;
  134. }
  135. while (microseconds > 100)
  136. {
  137. for (i = 0; i < CONFIG_BOARD_LOOPSPER100USEC; i++)
  138. {
  139. }
  140. microseconds -= 100;
  141. }
  142. while (microseconds > 10)
  143. {
  144. for (i = 0; i < CONFIG_BOARD_LOOPSPER10USEC; i++)
  145. {
  146. }
  147. microseconds -= 10;
  148. }
  149. while (microseconds > 0)
  150. {
  151. for (i = 0; i < CONFIG_BOARD_LOOPSPERUSEC; i++)
  152. {
  153. }
  154. microseconds--;
  155. }
  156. }
  157. static bool timer_callback(FAR uint32_t *next_interval_us, FAR void *arg)
  158. {
  159. #ifdef CONFIG_SCHED_TICKLESS
  160. struct timer_status_s status;
  161. uint32_t next_interval;
  162. g_timer.timebase += *next_interval_us;
  163. next_interval = g_timer.maxtimeout;
  164. g_timer.next_interval = &next_interval;
  165. nxsched_timer_expiration();
  166. g_timer.next_interval = NULL;
  167. TIMER_GETSTATUS(g_timer.lower, &status);
  168. if (timeout_diff(next_interval, status.timeleft))
  169. {
  170. g_timer.timebase += status.timeout - status.timeleft;
  171. *next_interval_us = next_interval;
  172. }
  173. #else
  174. g_timer.timebase += USEC_PER_TICK;
  175. nxsched_process_timer();
  176. #endif
  177. return true;
  178. }
  179. /****************************************************************************
  180. * Public Functions
  181. ****************************************************************************/
  182. void up_timer_set_lowerhalf(FAR struct timer_lowerhalf_s *lower)
  183. {
  184. g_timer.lower = lower;
  185. TIMER_MAXTIMEOUT(g_timer.lower, &g_timer.maxtimeout);
  186. #ifdef CONFIG_SCHED_TICKLESS
  187. g_oneshot_maxticks = g_timer.maxtimeout / USEC_PER_TICK;
  188. TIMER_SETTIMEOUT(g_timer.lower, g_timer.maxtimeout);
  189. #else
  190. TIMER_SETTIMEOUT(g_timer.lower, USEC_PER_TICK);
  191. #endif
  192. TIMER_SETCALLBACK(g_timer.lower, timer_callback, NULL);
  193. TIMER_START(g_timer.lower);
  194. }
  195. /****************************************************************************
  196. * Name: up_timer_gettime
  197. *
  198. * Description:
  199. * Return the elapsed time since power-up (or, more correctly, since
  200. * the architecture-specific timer was initialized). This function is
  201. * functionally equivalent to:
  202. *
  203. * int clock_gettime(clockid_t clockid, FAR struct timespec *ts);
  204. *
  205. * when clockid is CLOCK_MONOTONIC.
  206. *
  207. * This function provides the basis for reporting the current time and
  208. * also is used to eliminate error build-up from small errors in interval
  209. * time calculations.
  210. *
  211. * Provided by platform-specific code and called from the RTOS base code.
  212. *
  213. * Input Parameters:
  214. * ts - Provides the location in which to return the up-time.
  215. *
  216. * Returned Value:
  217. * Zero (OK) is returned on success; a negated errno value is returned on
  218. * any failure.
  219. *
  220. * Assumptions:
  221. * Called from the normal tasking context. The implementation must
  222. * provide whatever mutual exclusion is necessary for correct operation.
  223. * This can include disabling interrupts in order to assure atomic register
  224. * operations.
  225. *
  226. ****************************************************************************/
  227. #ifdef CONFIG_CLOCK_TIMEKEEPING
  228. int up_timer_getcounter(FAR uint64_t *cycles)
  229. {
  230. int ret = -EAGAIN;
  231. if (g_timer.lower != NULL)
  232. {
  233. *cycles = current_usec() / USEC_PER_TICK;
  234. ret = 0;
  235. }
  236. return ret;
  237. }
  238. void up_timer_getmask(FAR uint64_t *mask)
  239. {
  240. uint32_t maxticks = g_timer.maxtimeout / USEC_PER_TICK;
  241. *mask = 0;
  242. while (1)
  243. {
  244. uint64_t next = (*mask << 1) | 1;
  245. if (next > maxticks)
  246. {
  247. break;
  248. }
  249. *mask = next;
  250. }
  251. }
  252. #endif
  253. #if defined(CONFIG_SCHED_TICKLESS)
  254. int up_timer_gettime(FAR struct timespec *ts)
  255. {
  256. int ret = -EAGAIN;
  257. if (g_timer.lower != NULL)
  258. {
  259. timespec_from_usec(ts, current_usec());
  260. ret = 0;
  261. }
  262. return ret;
  263. }
  264. #endif
  265. /****************************************************************************
  266. * Name: up_timer_cancel
  267. *
  268. * Description:
  269. * Cancel the interval timer and return the time remaining on the timer.
  270. * These two steps need to be as nearly atomic as possible.
  271. * nxsched_timer_expiration() will not be called unless the timer is
  272. * restarted with up_timer_start().
  273. *
  274. * If, as a race condition, the timer has already expired when this
  275. * function is called, then that pending interrupt must be cleared so
  276. * that up_timer_start() and the remaining time of zero should be
  277. * returned.
  278. *
  279. * NOTE: This function may execute at a high rate with no timer running (as
  280. * when pre-emption is enabled and disabled).
  281. *
  282. * Provided by platform-specific code and called from the RTOS base code.
  283. *
  284. * Input Parameters:
  285. * ts - Location to return the remaining time. Zero should be returned
  286. * if the timer is not active. ts may be zero in which case the
  287. * time remaining is not returned.
  288. *
  289. * Returned Value:
  290. * Zero (OK) is returned on success. A call to up_timer_cancel() when
  291. * the timer is not active should also return success; a negated errno
  292. * value is returned on any failure.
  293. *
  294. * Assumptions:
  295. * May be called from interrupt level handling or from the normal tasking
  296. * level. Interrupts may need to be disabled internally to assure
  297. * non-reentrancy.
  298. *
  299. ****************************************************************************/
  300. #ifdef CONFIG_SCHED_TICKLESS
  301. int up_timer_cancel(FAR struct timespec *ts)
  302. {
  303. int ret = -EAGAIN;
  304. if (g_timer.lower != NULL)
  305. {
  306. timespec_from_usec(ts, update_timeout(g_timer.maxtimeout));
  307. ret = 0;
  308. }
  309. return ret;
  310. }
  311. #endif
  312. /****************************************************************************
  313. * Name: up_timer_start
  314. *
  315. * Description:
  316. * Start the interval timer. nxsched_timer_expiration() will be called at
  317. * the completion of the timeout (unless up_timer_cancel is called to stop
  318. * the timing.
  319. *
  320. * Provided by platform-specific code and called from the RTOS base code.
  321. *
  322. * Input Parameters:
  323. * ts - Provides the time interval until nxsched_timer_expiration() is
  324. * called.
  325. *
  326. * Returned Value:
  327. * Zero (OK) is returned on success; a negated errno value is returned on
  328. * any failure.
  329. *
  330. * Assumptions:
  331. * May be called from interrupt level handling or from the normal tasking
  332. * level. Interrupts may need to be disabled internally to assure
  333. * non-reentrancy.
  334. *
  335. ****************************************************************************/
  336. #ifdef CONFIG_SCHED_TICKLESS
  337. int up_timer_start(FAR const struct timespec *ts)
  338. {
  339. int ret = -EAGAIN;
  340. if (g_timer.lower != NULL)
  341. {
  342. update_timeout(timespec_to_usec(ts));
  343. ret = 0;
  344. }
  345. return ret;
  346. }
  347. #endif
  348. /****************************************************************************
  349. * Name: up_critmon_*
  350. *
  351. * Description:
  352. * The first interface simply provides the current time value in unknown
  353. * units. NOTE: This function may be called early before the timer has
  354. * been initialized. In that event, the function should just return a
  355. * start time of zero.
  356. *
  357. * Nothing is assumed about the units of this time value. The following
  358. * are assumed, however: (1) The time is an unsigned integer value, (2)
  359. * the time is monotonically increasing, and (3) the elapsed time (also
  360. * in unknown units) can be obtained by subtracting a start time from
  361. * the current time.
  362. *
  363. * The second interface simple converts an elapsed time into well known
  364. * units.
  365. ****************************************************************************/
  366. #ifdef CONFIG_SCHED_CRITMONITOR
  367. uint32_t up_critmon_gettime(void)
  368. {
  369. uint32_t ret = 0;
  370. if (g_timer.lower != NULL)
  371. {
  372. ret = current_usec();
  373. }
  374. return ret;
  375. }
  376. void up_critmon_convert(uint32_t elapsed, FAR struct timespec *ts)
  377. {
  378. timespec_from_usec(ts, elapsed);
  379. }
  380. #endif
  381. /****************************************************************************
  382. * Name: up_mdelay
  383. *
  384. * Description:
  385. * Delay inline for the requested number of milliseconds.
  386. * *** NOT multi-tasking friendly ***
  387. *
  388. ****************************************************************************/
  389. void up_mdelay(unsigned int milliseconds)
  390. {
  391. up_udelay(USEC_PER_MSEC * milliseconds);
  392. }
  393. /****************************************************************************
  394. * Name: up_udelay
  395. *
  396. * Description:
  397. * Delay inline for the requested number of microseconds.
  398. *
  399. * *** NOT multi-tasking friendly ***
  400. *
  401. ****************************************************************************/
  402. void up_udelay(useconds_t microseconds)
  403. {
  404. if (g_timer.lower != NULL)
  405. {
  406. udelay_accurate(microseconds);
  407. }
  408. else /* Period timer hasn't been initialized yet */
  409. {
  410. udelay_coarse(microseconds);
  411. }
  412. }