timer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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.
  230. */
  231. if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
  232. {
  233. rt_list_insert_before(n, &(timer->list));
  234. break;
  235. }
  236. }
  237. /* no found suitable position in timer list */
  238. if (n == timer_list)
  239. {
  240. rt_list_insert_before(n, &(timer->list));
  241. }
  242. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  243. /* enable interrupt */
  244. rt_hw_interrupt_enable(level);
  245. #ifdef RT_USING_TIMER_SOFT
  246. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  247. {
  248. /* check whether timer thread is ready */
  249. if (timer_thread.stat != RT_THREAD_READY)
  250. {
  251. /* resume timer thread to check soft timer */
  252. rt_thread_resume(&timer_thread);
  253. rt_schedule();
  254. }
  255. }
  256. #endif
  257. return -RT_EOK;
  258. }
  259. RTM_EXPORT(rt_timer_start);
  260. /**
  261. * This function will stop the timer
  262. *
  263. * @param timer the timer to be stopped
  264. *
  265. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  266. */
  267. rt_err_t rt_timer_stop(rt_timer_t timer)
  268. {
  269. register rt_base_t level;
  270. /* timer check */
  271. RT_ASSERT(timer != RT_NULL);
  272. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  273. return -RT_ERROR;
  274. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  275. /* disable interrupt */
  276. level = rt_hw_interrupt_disable();
  277. /* remove it from timer list */
  278. rt_list_remove(&(timer->list));
  279. /* enable interrupt */
  280. rt_hw_interrupt_enable(level);
  281. /* change stat */
  282. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  283. return RT_EOK;
  284. }
  285. RTM_EXPORT(rt_timer_stop);
  286. /**
  287. * This function will get or set some options of the timer
  288. *
  289. * @param timer the timer to be get or set
  290. * @param cmd the control command
  291. * @param arg the argument
  292. *
  293. * @return RT_EOK
  294. */
  295. rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
  296. {
  297. /* timer check */
  298. RT_ASSERT(timer != RT_NULL);
  299. switch (cmd)
  300. {
  301. case RT_TIMER_CTRL_GET_TIME:
  302. *(rt_tick_t *)arg = timer->init_tick;
  303. break;
  304. case RT_TIMER_CTRL_SET_TIME:
  305. timer->init_tick = *(rt_tick_t *)arg;
  306. break;
  307. case RT_TIMER_CTRL_SET_ONESHOT:
  308. timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
  309. break;
  310. case RT_TIMER_CTRL_SET_PERIODIC:
  311. timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
  312. break;
  313. }
  314. return RT_EOK;
  315. }
  316. RTM_EXPORT(rt_timer_control);
  317. /**
  318. * This function will check timer list, if a timeout event happens, the
  319. * corresponding timeout function will be invoked.
  320. *
  321. * @note this function shall be invoked in operating system timer interrupt.
  322. */
  323. void rt_timer_check(void)
  324. {
  325. struct rt_timer *t;
  326. rt_tick_t current_tick;
  327. register rt_base_t level;
  328. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  329. current_tick = rt_tick_get();
  330. /* disable interrupt */
  331. level = rt_hw_interrupt_disable();
  332. while (!rt_list_isempty(&rt_timer_list))
  333. {
  334. t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
  335. /*
  336. * It supposes that the new tick shall less than the half duration of
  337. * tick max.
  338. */
  339. if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
  340. {
  341. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  342. /* remove timer from timer list firstly */
  343. rt_list_remove(&(t->list));
  344. /* call timeout function */
  345. t->timeout_func(t->parameter);
  346. /* re-get tick */
  347. current_tick = rt_tick_get();
  348. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  349. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  350. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  351. {
  352. /* start it */
  353. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  354. rt_timer_start(t);
  355. }
  356. else
  357. {
  358. /* stop timer */
  359. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  360. }
  361. }
  362. else
  363. break;
  364. }
  365. /* enable interrupt */
  366. rt_hw_interrupt_enable(level);
  367. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  368. }
  369. /**
  370. * This function will return the next timeout tick in the system.
  371. *
  372. * @return the next timeout tick in the system
  373. */
  374. rt_tick_t rt_timer_next_timeout_tick(void)
  375. {
  376. return rt_timer_list_next_timeout(&rt_timer_list);
  377. }
  378. #ifdef RT_USING_TIMER_SOFT
  379. /**
  380. * This function will check timer list, if a timeout event happens, the
  381. * corresponding timeout function will be invoked.
  382. */
  383. void rt_soft_timer_check(void)
  384. {
  385. rt_tick_t current_tick;
  386. rt_list_t *n;
  387. struct rt_timer *t;
  388. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  389. current_tick = rt_tick_get();
  390. for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
  391. {
  392. t = rt_list_entry(n, struct rt_timer, list);
  393. /*
  394. * It supposes that the new tick shall less than the half duration of
  395. * tick max.
  396. */
  397. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  398. {
  399. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  400. /* move node to the next */
  401. n = n->next;
  402. /* remove timer from timer list firstly */
  403. rt_list_remove(&(t->list));
  404. /* call timeout function */
  405. t->timeout_func(t->parameter);
  406. /* re-get tick */
  407. current_tick = rt_tick_get();
  408. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  409. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  410. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  411. {
  412. /* start it */
  413. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  414. rt_timer_start(t);
  415. }
  416. else
  417. {
  418. /* stop timer */
  419. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  420. }
  421. }
  422. else break; /* not check anymore */
  423. }
  424. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  425. }
  426. /* system timer thread entry */
  427. static void rt_thread_timer_entry(void *parameter)
  428. {
  429. rt_tick_t next_timeout;
  430. while (1)
  431. {
  432. /* get the next timeout tick */
  433. next_timeout = rt_timer_list_next_timeout(&rt_soft_timer_list);
  434. if (next_timeout == RT_TICK_MAX)
  435. {
  436. /* no software timer exist, suspend self. */
  437. rt_thread_suspend(rt_thread_self());
  438. rt_schedule();
  439. }
  440. else
  441. {
  442. rt_tick_t current_tick;
  443. /* get current tick */
  444. current_tick = rt_tick_get();
  445. if ((next_timeout - current_tick) < RT_TICK_MAX/2)
  446. {
  447. /* get the delta timeout tick */
  448. next_timeout = next_timeout - current_tick;
  449. rt_thread_delay(next_timeout);
  450. }
  451. }
  452. /* lock scheduler */
  453. rt_enter_critical();
  454. /* check software timer */
  455. rt_soft_timer_check();
  456. /* unlock scheduler */
  457. rt_exit_critical();
  458. }
  459. }
  460. #endif
  461. /**
  462. * @ingroup SystemInit
  463. *
  464. * This function will initialize system timer
  465. *
  466. * @deprecated since 1.1.0, this function does not need to be invoked
  467. * in the system initialization.
  468. */
  469. void rt_system_timer_init(void)
  470. {
  471. }
  472. /**
  473. * @ingroup SystemInit
  474. *
  475. * This function will initialize system timer thread
  476. */
  477. void rt_system_timer_thread_init(void)
  478. {
  479. #ifdef RT_USING_TIMER_SOFT
  480. rt_list_init(&rt_soft_timer_list);
  481. /* start software timer thread */
  482. rt_thread_init(&timer_thread,
  483. "timer",
  484. rt_thread_timer_entry,
  485. RT_NULL,
  486. &timer_thread_stack[0],
  487. sizeof(timer_thread_stack),
  488. RT_TIMER_THREAD_PRIO,
  489. 10);
  490. /* startup */
  491. rt_thread_startup(&timer_thread);
  492. #endif
  493. }
  494. /*@}*/