timer.c 12 KB

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