timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * File : timer.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-12 Bernard first version
  13. * 2006-04-29 Bernard implement thread timer
  14. * 2006-06-04 Bernard implement rt_timer_control
  15. * 2006-08-10 Bernard fix the periodic timer bug
  16. * 2006-09-03 Bernard implement rt_timer_detach
  17. * 2009-11-11 LiJin add soft timer
  18. * 2010-05-12 Bernard fix the timer check bug.
  19. * 2010-11-02 Charlie re-implement tick overflow issue
  20. */
  21. #include <rtthread.h>
  22. #include <rthw.h>
  23. /* hard timer list */
  24. static rt_list_t rt_timer_list = RT_LIST_OBJECT_INIT(rt_timer_list);
  25. #ifdef RT_USING_TIMER_SOFT
  26. /* soft timer list */
  27. static rt_list_t rt_soft_timer_list;
  28. static struct rt_thread timer_thread;
  29. ALIGN(RT_ALIGN_SIZE)
  30. static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
  31. #endif
  32. #ifdef RT_USING_HOOK
  33. extern void (*rt_object_take_hook)(struct rt_object *object);
  34. extern void (*rt_object_put_hook)(struct rt_object *object);
  35. static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
  36. /**
  37. * @addtogroup Hook
  38. */
  39. /*@{*/
  40. /**
  41. * This function will set a hook function, which will be invoked when timer
  42. * is timeout.
  43. *
  44. * @param hook the hook function
  45. */
  46. void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
  47. {
  48. rt_timer_timeout_hook = hook;
  49. }
  50. /*@}*/
  51. #endif
  52. static void _rt_timer_init(rt_timer_t timer,
  53. void (*timeout)(void *parameter), void *parameter,
  54. rt_tick_t time, rt_uint8_t flag)
  55. {
  56. /* set flag */
  57. timer->parent.flag = flag;
  58. /* set deactivated */
  59. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  60. timer->timeout_func = timeout;
  61. timer->parameter = parameter;
  62. timer->timeout_tick = 0;
  63. timer->init_tick = time;
  64. /* initialize timer list */
  65. rt_list_init(&(timer->list));
  66. }
  67. static rt_tick_t rt_timer_list_next_timeout(rt_list_t *timer_list)
  68. {
  69. struct rt_timer *timer;
  70. if (rt_list_isempty(timer_list))
  71. return RT_TICK_MAX;
  72. timer = rt_list_entry(timer_list->next, struct rt_timer, list);
  73. return timer->timeout_tick;
  74. }
  75. /**
  76. * @addtogroup Clock
  77. */
  78. /*@{*/
  79. /**
  80. * This function will initialize a timer, normally this function is used to
  81. * initialize a static timer object.
  82. *
  83. * @param timer the static timer object
  84. * @param name the name of timer
  85. * @param timeout the timeout function
  86. * @param parameter the parameter of timeout function
  87. * @param time the tick of timer
  88. * @param flag the flag of timer
  89. */
  90. void rt_timer_init(rt_timer_t timer,
  91. const char *name,
  92. void (*timeout)(void *parameter), void *parameter,
  93. rt_tick_t time, rt_uint8_t flag)
  94. {
  95. /* timer check */
  96. RT_ASSERT(timer != RT_NULL);
  97. /* timer object initialization */
  98. rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
  99. _rt_timer_init(timer, timeout, parameter, time, flag);
  100. }
  101. RTM_EXPORT(rt_timer_init);
  102. /**
  103. * This function will detach a timer from timer management.
  104. *
  105. * @param timer the static timer object
  106. *
  107. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  108. */
  109. rt_err_t rt_timer_detach(rt_timer_t timer)
  110. {
  111. register rt_base_t level;
  112. /* timer check */
  113. RT_ASSERT(timer != RT_NULL);
  114. /* disable interrupt */
  115. level = rt_hw_interrupt_disable();
  116. /* remove it from timer list */
  117. rt_list_remove(&(timer->list));
  118. /* enable interrupt */
  119. rt_hw_interrupt_enable(level);
  120. rt_object_detach((rt_object_t)timer);
  121. return -RT_EOK;
  122. }
  123. RTM_EXPORT(rt_timer_detach);
  124. #ifdef RT_USING_HEAP
  125. /**
  126. * This function will create a timer
  127. *
  128. * @param name the name of timer
  129. * @param timeout the timeout function
  130. * @param parameter the parameter of timeout function
  131. * @param time the tick of timer
  132. * @param flag the flag of timer
  133. *
  134. * @return the created timer object
  135. */
  136. rt_timer_t rt_timer_create(const char *name, void (*timeout)(void *parameter),
  137. void *parameter, rt_tick_t time, rt_uint8_t flag)
  138. {
  139. struct rt_timer *timer;
  140. /* allocate a object */
  141. timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
  142. if (timer == RT_NULL)
  143. {
  144. return RT_NULL;
  145. }
  146. _rt_timer_init(timer, timeout, parameter, time, flag);
  147. return timer;
  148. }
  149. RTM_EXPORT(rt_timer_create);
  150. /**
  151. * This function will delete a timer and release timer memory
  152. *
  153. * @param timer the timer to be deleted
  154. *
  155. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  156. */
  157. rt_err_t rt_timer_delete(rt_timer_t timer)
  158. {
  159. register rt_base_t level;
  160. /* timer check */
  161. RT_ASSERT(timer != RT_NULL);
  162. /* disable interrupt */
  163. level = rt_hw_interrupt_disable();
  164. /* remove it from timer list */
  165. rt_list_remove(&(timer->list));
  166. /* enable interrupt */
  167. rt_hw_interrupt_enable(level);
  168. rt_object_delete((rt_object_t)timer);
  169. return -RT_EOK;
  170. }
  171. RTM_EXPORT(rt_timer_delete);
  172. #endif
  173. /**
  174. * This function will start the timer
  175. *
  176. * @param timer the timer to be started
  177. *
  178. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  179. */
  180. rt_err_t rt_timer_start(rt_timer_t timer)
  181. {
  182. struct rt_timer *t;
  183. register rt_base_t level;
  184. rt_list_t *n, *timer_list;
  185. /* timer check */
  186. RT_ASSERT(timer != RT_NULL);
  187. if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  188. return -RT_ERROR;
  189. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  190. /* get timeout tick, the max timeout tick shall not great than RT_TICK_MAX/2 */
  191. RT_ASSERT(timer->init_tick < RT_TICK_MAX/2);
  192. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  193. /* disable interrupt */
  194. level = rt_hw_interrupt_disable();
  195. #ifdef RT_USING_TIMER_SOFT
  196. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  197. {
  198. /* insert timer to soft timer list */
  199. timer_list = &rt_soft_timer_list;
  200. }
  201. else
  202. #endif
  203. {
  204. /* insert timer to system timer list */
  205. timer_list = &rt_timer_list;
  206. }
  207. for (n = timer_list->next; n != timer_list; n = n->next)
  208. {
  209. t = rt_list_entry(n, struct rt_timer, list);
  210. /*
  211. * It supposes that the new tick shall less than the half duration of
  212. * tick max.
  213. */
  214. if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX/2)
  215. {
  216. rt_list_insert_before(n, &(timer->list));
  217. break;
  218. }
  219. }
  220. /* no found suitable position in timer list */
  221. if (n == timer_list)
  222. {
  223. rt_list_insert_before(n, &(timer->list));
  224. }
  225. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  226. /* enable interrupt */
  227. rt_hw_interrupt_enable(level);
  228. #ifdef RT_USING_TIMER_SOFT
  229. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  230. {
  231. /* check whether timer thread is ready */
  232. if (timer_thread.stat != RT_THREAD_READY)
  233. {
  234. /* resume timer thread to check soft timer */
  235. rt_thread_resume(&timer_thread);
  236. rt_schedule();
  237. }
  238. }
  239. #endif
  240. return -RT_EOK;
  241. }
  242. RTM_EXPORT(rt_timer_start);
  243. /**
  244. * This function will stop the timer
  245. *
  246. * @param timer the timer to be stopped
  247. *
  248. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  249. */
  250. rt_err_t rt_timer_stop(rt_timer_t timer)
  251. {
  252. register rt_base_t level;
  253. /* timer check */
  254. RT_ASSERT(timer != RT_NULL);
  255. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  256. return -RT_ERROR;
  257. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  258. /* disable interrupt */
  259. level = rt_hw_interrupt_disable();
  260. /* remove it from timer list */
  261. rt_list_remove(&(timer->list));
  262. /* enable interrupt */
  263. rt_hw_interrupt_enable(level);
  264. /* change stat */
  265. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  266. return RT_EOK;
  267. }
  268. RTM_EXPORT(rt_timer_stop);
  269. /**
  270. * This function will get or set some options of the timer
  271. *
  272. * @param timer the timer to be get or set
  273. * @param cmd the control command
  274. * @param arg the argument
  275. *
  276. * @return RT_EOK
  277. */
  278. rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
  279. {
  280. /* timer check */
  281. RT_ASSERT(timer != RT_NULL);
  282. switch (cmd)
  283. {
  284. case RT_TIMER_CTRL_GET_TIME:
  285. *(rt_tick_t *)arg = timer->init_tick;
  286. break;
  287. case RT_TIMER_CTRL_SET_TIME:
  288. timer->init_tick = *(rt_tick_t *)arg;
  289. break;
  290. case RT_TIMER_CTRL_SET_ONESHOT:
  291. timer->parent.flag &= ~(1 << RT_TIMER_FLAG_PERIODIC);
  292. break;
  293. case RT_TIMER_CTRL_SET_PERIODIC:
  294. timer->parent.flag |= (1 << RT_TIMER_FLAG_PERIODIC);
  295. break;
  296. }
  297. return RT_EOK;
  298. }
  299. RTM_EXPORT(rt_timer_control);
  300. /**
  301. * This function will check timer list, if a timeout event happens, the
  302. * corresponding timeout function will be invoked.
  303. *
  304. * @note this function shall be invoked in operating system timer interrupt.
  305. */
  306. void rt_timer_check(void)
  307. {
  308. struct rt_timer *t;
  309. rt_tick_t current_tick;
  310. register rt_base_t level;
  311. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  312. current_tick = rt_tick_get();
  313. /* disable interrupt */
  314. level = rt_hw_interrupt_disable();
  315. while (!rt_list_isempty(&rt_timer_list))
  316. {
  317. t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
  318. /*
  319. * It supposes that the new tick shall less than the half duration of
  320. * tick max.
  321. */
  322. if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
  323. {
  324. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  325. /* remove timer from timer list firstly */
  326. rt_list_remove(&(t->list));
  327. /* call timeout function */
  328. t->timeout_func(t->parameter);
  329. /* re-get tick */
  330. current_tick = rt_tick_get();
  331. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  332. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  333. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  334. {
  335. /* start it */
  336. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  337. rt_timer_start(t);
  338. }
  339. else
  340. {
  341. /* stop timer */
  342. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  343. }
  344. }
  345. else
  346. break;
  347. }
  348. /* enable interrupt */
  349. rt_hw_interrupt_enable(level);
  350. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  351. }
  352. /**
  353. * This function will return the next timeout tick in the system.
  354. *
  355. * @return the next timeout tick in the system
  356. */
  357. rt_tick_t rt_timer_next_timeout_tick(void)
  358. {
  359. return rt_timer_list_next_timeout(&rt_timer_list);
  360. }
  361. #ifdef RT_USING_TIMER_SOFT
  362. /**
  363. * This function will check timer list, if a timeout event happens, the
  364. * corresponding timeout function will be invoked.
  365. */
  366. void rt_soft_timer_check(void)
  367. {
  368. rt_tick_t current_tick;
  369. rt_list_t *n;
  370. struct rt_timer *t;
  371. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  372. current_tick = rt_tick_get();
  373. for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
  374. {
  375. t = rt_list_entry(n, struct rt_timer, list);
  376. /*
  377. * It supposes that the new tick shall less than the half duration of
  378. * tick max.
  379. */
  380. if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
  381. {
  382. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  383. /* move node to the next */
  384. n = n->next;
  385. /* remove timer from timer list firstly */
  386. rt_list_remove(&(t->list));
  387. /* call timeout function */
  388. t->timeout_func(t->parameter);
  389. /* re-get tick */
  390. current_tick = rt_tick_get();
  391. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  392. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  393. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  394. {
  395. /* start it */
  396. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  397. rt_timer_start(t);
  398. }
  399. else
  400. {
  401. /* stop timer */
  402. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  403. }
  404. }
  405. else break; /* not check anymore */
  406. }
  407. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  408. }
  409. /* system timer thread entry */
  410. static void rt_thread_timer_entry(void *parameter)
  411. {
  412. rt_tick_t next_timeout;
  413. while (1)
  414. {
  415. next_timeout = rt_timer_list_next_timeout(&rt_soft_timer_list);
  416. if (next_timeout == RT_TICK_MAX)
  417. {
  418. rt_thread_suspend(rt_thread_self());
  419. rt_schedule();
  420. }
  421. else
  422. {
  423. rt_thread_delay(next_timeout);
  424. }
  425. /* lock scheduler */
  426. rt_enter_critical();
  427. /* check software timer */
  428. rt_soft_timer_check();
  429. /* unlock scheduler */
  430. rt_exit_critical();
  431. }
  432. }
  433. #endif
  434. /**
  435. * @ingroup SystemInit
  436. *
  437. * This function will initialize system timer
  438. *
  439. * @deprecated since 1.1.0, this function does not need to be invoked
  440. * in the system initialization.
  441. */
  442. void rt_system_timer_init(void)
  443. {
  444. }
  445. /**
  446. * @ingroup SystemInit
  447. *
  448. * This function will initialize system timer thread
  449. */
  450. void rt_system_timer_thread_init(void)
  451. {
  452. #ifdef RT_USING_TIMER_SOFT
  453. rt_list_init(&rt_soft_timer_list);
  454. /* start software timer thread */
  455. rt_thread_init(&timer_thread,
  456. "timer",
  457. rt_thread_timer_entry, RT_NULL,
  458. &timer_thread_stack[0], sizeof(timer_thread_stack),
  459. RT_TIMER_THREAD_PRIO, 10);
  460. /* startup */
  461. rt_thread_startup(&timer_thread);
  462. #endif
  463. }
  464. /*@}*/