arch_alarm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /****************************************************************************
  2. * drivers/timers/arch_alarm.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_alarm.h>
  27. /****************************************************************************
  28. * Pre-processor Definitions
  29. ****************************************************************************/
  30. #define CONFIG_BOARD_LOOPSPER100USEC ((CONFIG_BOARD_LOOPSPERMSEC+5)/10)
  31. #define CONFIG_BOARD_LOOPSPER10USEC ((CONFIG_BOARD_LOOPSPERMSEC+50)/100)
  32. #define CONFIG_BOARD_LOOPSPERUSEC ((CONFIG_BOARD_LOOPSPERMSEC+500)/1000)
  33. #define timespec_to_usec(ts) \
  34. ((uint64_t)(ts)->tv_sec * USEC_PER_SEC + (ts)->tv_nsec / NSEC_PER_USEC)
  35. /****************************************************************************
  36. * Private Data
  37. ****************************************************************************/
  38. static FAR struct oneshot_lowerhalf_s *g_oneshot_lower;
  39. /****************************************************************************
  40. * Private Functions
  41. ****************************************************************************/
  42. static inline void timespec_from_usec(FAR struct timespec *ts,
  43. uint64_t microseconds)
  44. {
  45. ts->tv_sec = microseconds / USEC_PER_SEC;
  46. microseconds -= (uint64_t)ts->tv_sec * USEC_PER_SEC;
  47. ts->tv_nsec = microseconds * NSEC_PER_USEC;
  48. }
  49. static void udelay_accurate(useconds_t microseconds)
  50. {
  51. struct timespec now;
  52. struct timespec end;
  53. struct timespec delta;
  54. ONESHOT_CURRENT(g_oneshot_lower, &now);
  55. timespec_from_usec(&delta, microseconds);
  56. clock_timespec_add(&now, &delta, &end);
  57. while (clock_timespec_compare(&now, &end) < 0)
  58. {
  59. ONESHOT_CURRENT(g_oneshot_lower, &now);
  60. }
  61. }
  62. static void udelay_coarse(useconds_t microseconds)
  63. {
  64. volatile int i;
  65. /* We'll do this a little at a time because we expect that the
  66. * CONFIG_BOARD_LOOPSPERUSEC is very inaccurate during to truncation in
  67. * the divisions of its calculation. We'll use the largest values that
  68. * we can in order to prevent significant error buildup in the loops.
  69. */
  70. while (microseconds > 1000)
  71. {
  72. for (i = 0; i < CONFIG_BOARD_LOOPSPERMSEC; i++)
  73. {
  74. }
  75. microseconds -= 1000;
  76. }
  77. while (microseconds > 100)
  78. {
  79. for (i = 0; i < CONFIG_BOARD_LOOPSPER100USEC; i++)
  80. {
  81. }
  82. microseconds -= 100;
  83. }
  84. while (microseconds > 10)
  85. {
  86. for (i = 0; i < CONFIG_BOARD_LOOPSPER10USEC; i++)
  87. {
  88. }
  89. microseconds -= 10;
  90. }
  91. while (microseconds > 0)
  92. {
  93. for (i = 0; i < CONFIG_BOARD_LOOPSPERUSEC; i++)
  94. {
  95. }
  96. microseconds--;
  97. }
  98. }
  99. static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
  100. FAR void *arg)
  101. {
  102. struct timespec now;
  103. #ifdef CONFIG_SCHED_TICKLESS
  104. ONESHOT_CURRENT(g_oneshot_lower, &now);
  105. nxsched_alarm_expiration(&now);
  106. #else
  107. struct timespec delta;
  108. do
  109. {
  110. static uint64_t tick = 1;
  111. struct timespec next;
  112. nxsched_process_timer();
  113. timespec_from_usec(&next, ++tick * USEC_PER_TICK);
  114. ONESHOT_CURRENT(g_oneshot_lower, &now);
  115. clock_timespec_subtract(&next, &now, &delta);
  116. }
  117. while (delta.tv_sec == 0 && delta.tv_nsec == 0);
  118. ONESHOT_START(g_oneshot_lower, oneshot_callback, NULL, &delta);
  119. #endif
  120. }
  121. /****************************************************************************
  122. * Public Functions
  123. ****************************************************************************/
  124. void up_alarm_set_lowerhalf(FAR struct oneshot_lowerhalf_s *lower)
  125. {
  126. #ifdef CONFIG_SCHED_TICKLESS
  127. struct timespec maxts;
  128. uint64_t maxticks;
  129. g_oneshot_lower = lower;
  130. ONESHOT_MAX_DELAY(g_oneshot_lower, &maxts);
  131. maxticks = timespec_to_usec(&maxts) / USEC_PER_TICK;
  132. g_oneshot_maxticks = maxticks < UINT32_MAX ? maxticks : UINT32_MAX;
  133. #else
  134. struct timespec ts;
  135. g_oneshot_lower = lower;
  136. timespec_from_usec(&ts, USEC_PER_TICK);
  137. ONESHOT_START(g_oneshot_lower, oneshot_callback, NULL, &ts);
  138. #endif
  139. }
  140. /****************************************************************************
  141. * Name: up_timer_gettime
  142. *
  143. * Description:
  144. * Return the elapsed time since power-up (or, more correctly, since
  145. * the archtecture-specific timer was initialized). This function is
  146. * functionally equivalent to:
  147. *
  148. * int clock_gettime(clockid_t clockid, FAR struct timespec *ts);
  149. *
  150. * when clockid is CLOCK_MONOTONIC.
  151. *
  152. * This function provides the basis for reporting the current time and
  153. * also is used to eliminate error build-up from small errors in interval
  154. * time calculations.
  155. *
  156. * Provided by platform-specific code and called from the RTOS base code.
  157. *
  158. * Input Parameters:
  159. * ts - Provides the location in which to return the up-time.
  160. *
  161. * Returned Value:
  162. * Zero (OK) is returned on success; a negated errno value is returned on
  163. * any failure.
  164. *
  165. * Assumptions:
  166. * Called from the normal tasking context. The implementation must
  167. * provide whatever mutual exclusion is necessary for correct operation.
  168. * This can include disabling interrupts in order to assure atomic register
  169. * operations.
  170. *
  171. ****************************************************************************/
  172. #ifdef CONFIG_CLOCK_TIMEKEEPING
  173. int up_timer_getcounter(FAR uint64_t *cycles)
  174. {
  175. int ret = -EAGAIN;
  176. if (g_oneshot_lower != NULL)
  177. {
  178. struct timespec now;
  179. ret = ONESHOT_CURRENT(g_oneshot_lower, &now);
  180. if (ret == 0)
  181. {
  182. *cycles = timespec_to_usec(&now) / USEC_PER_TICK;
  183. }
  184. }
  185. return ret;
  186. }
  187. void up_timer_getmask(FAR uint64_t *mask)
  188. {
  189. *mask = 0;
  190. if (g_oneshot_lower != NULL)
  191. {
  192. struct timespec maxts;
  193. uint64_t maxticks;
  194. ONESHOT_MAX_DELAY(g_oneshot_lower, &maxts);
  195. maxticks = timespec_to_usec(&maxts) / USEC_PER_TICK;
  196. for (; ; )
  197. {
  198. uint64_t next = (*mask << 1) | 1;
  199. if (next > maxticks)
  200. {
  201. break;
  202. }
  203. *mask = next;
  204. }
  205. }
  206. }
  207. #endif
  208. #if defined(CONFIG_SCHED_TICKLESS)
  209. int up_timer_gettime(FAR struct timespec *ts)
  210. {
  211. int ret = -EAGAIN;
  212. if (g_oneshot_lower != NULL)
  213. {
  214. ret = ONESHOT_CURRENT(g_oneshot_lower, ts);
  215. }
  216. return ret;
  217. }
  218. #endif
  219. /****************************************************************************
  220. * Name: up_alarm_cancel
  221. *
  222. * Description:
  223. * Cancel the alarm and return the time of cancellation of the alarm.
  224. * These two steps need to be as nearly atomic as possible.
  225. * nxsched_alarm_expiration() will not be called unless the alarm is
  226. * restarted with up_alarm_start().
  227. *
  228. * If, as a race condition, the alarm has already expired when this
  229. * function is called, then time returned is the current time.
  230. *
  231. * NOTE: This function may execute at a high rate with no timer running (as
  232. * when pre-emption is enabled and disabled).
  233. *
  234. * Provided by platform-specific code and called from the RTOS base code.
  235. *
  236. * Input Parameters:
  237. * ts - Location to return the expiration time. The current time should
  238. * returned if the alarm is not active. ts may be NULL in which
  239. * case the time is not returned.
  240. *
  241. * Returned Value:
  242. * Zero (OK) is returned on success. A call to up_alarm_cancel() when
  243. * the timer is not active should also return success; a negated errno
  244. * value is returned on any failure.
  245. *
  246. * Assumptions:
  247. * May be called from interrupt level handling or from the normal tasking
  248. * level. Interrupts may need to be disabled internally to assure
  249. * non-reentrancy.
  250. *
  251. ****************************************************************************/
  252. #ifdef CONFIG_SCHED_TICKLESS
  253. int up_alarm_cancel(FAR struct timespec *ts)
  254. {
  255. int ret = -EAGAIN;
  256. if (g_oneshot_lower != NULL)
  257. {
  258. ret = ONESHOT_CANCEL(g_oneshot_lower, ts);
  259. ONESHOT_CURRENT(g_oneshot_lower, ts);
  260. }
  261. return ret;
  262. }
  263. #endif
  264. /****************************************************************************
  265. * Name: up_alarm_start
  266. *
  267. * Description:
  268. * Start the alarm. nxsched_alarm_expiration() will be called when the
  269. * alarm occurs (unless up_alaram_cancel is called to stop it).
  270. *
  271. * Provided by platform-specific code and called from the RTOS base code.
  272. *
  273. * Input Parameters:
  274. * ts - The time in the future at the alarm is expected to occur. When the
  275. * alarm occurs the timer logic will call nxsched_alarm_expiration().
  276. *
  277. * Returned Value:
  278. * Zero (OK) is returned on success; a negated errno value is returned on
  279. * any failure.
  280. *
  281. * Assumptions:
  282. * May be called from interrupt level handling or from the normal tasking
  283. * level. Interrupts may need to be disabled internally to assure
  284. * non-reentrancy.
  285. *
  286. ****************************************************************************/
  287. #ifdef CONFIG_SCHED_TICKLESS
  288. int up_alarm_start(FAR const struct timespec *ts)
  289. {
  290. int ret = -EAGAIN;
  291. if (g_oneshot_lower != NULL)
  292. {
  293. struct timespec now;
  294. struct timespec delta;
  295. ONESHOT_CURRENT(g_oneshot_lower, &now);
  296. clock_timespec_subtract(ts, &now, &delta);
  297. ret = ONESHOT_START(g_oneshot_lower, oneshot_callback, NULL, &delta);
  298. }
  299. return ret;
  300. }
  301. #endif
  302. /****************************************************************************
  303. * Name: up_critmon_*
  304. *
  305. * Description:
  306. * The first interface simply provides the current time value in unknown
  307. * units. NOTE: This function may be called early before the timer has
  308. * been initialized. In that event, the function should just return a
  309. * start time of zero.
  310. *
  311. * Nothing is assumed about the units of this time value. The following
  312. * are assumed, however: (1) The time is an unsigned integer value, (2)
  313. * the time is monotonically increasing, and (3) the elapsed time (also
  314. * in unknown units) can be obtained by subtracting a start time from
  315. * the current time.
  316. *
  317. * The second interface simple converts an elapsed time into well known
  318. * units.
  319. ****************************************************************************/
  320. #ifdef CONFIG_SCHED_CRITMONITOR
  321. uint32_t up_critmon_gettime(void)
  322. {
  323. uint32_t ret = 0;
  324. if (g_oneshot_lower != NULL)
  325. {
  326. struct timespec ts;
  327. ONESHOT_CURRENT(g_oneshot_lower, &ts);
  328. ret = timespec_to_usec(&ts);
  329. }
  330. return ret;
  331. }
  332. void up_critmon_convert(uint32_t elapsed, FAR struct timespec *ts)
  333. {
  334. timespec_from_usec(ts, elapsed);
  335. }
  336. #endif
  337. /****************************************************************************
  338. * Name: up_mdelay
  339. *
  340. * Description:
  341. * Delay inline for the requested number of milliseconds.
  342. * *** NOT multi-tasking friendly ***
  343. *
  344. ****************************************************************************/
  345. void up_mdelay(unsigned int milliseconds)
  346. {
  347. up_udelay(USEC_PER_MSEC * milliseconds);
  348. }
  349. /****************************************************************************
  350. * Name: up_udelay
  351. *
  352. * Description:
  353. * Delay inline for the requested number of microseconds.
  354. *
  355. * *** NOT multi-tasking friendly ***
  356. *
  357. ****************************************************************************/
  358. void up_udelay(useconds_t microseconds)
  359. {
  360. if (g_oneshot_lower != NULL)
  361. {
  362. udelay_accurate(microseconds);
  363. }
  364. else /* Oneshot timer hasn't been initialized yet */
  365. {
  366. udelay_coarse(microseconds);
  367. }
  368. }