timer.c 12 KB

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