timer.c 15 KB

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