timer.c 12 KB

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