timer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * File : timer.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. */
  20. #include <rtthread.h>
  21. #include <rthw.h>
  22. #include "kservice.h"
  23. /* #define RT_TIMER_DEBUG */
  24. /* hard timer list */
  25. static rt_list_t rt_timer_list, rt_long_timer_list;
  26. #ifdef RT_USING_TIMER_SOFT
  27. /* soft timer list */
  28. static rt_list_t rt_soft_timer_list, rt_soft_long_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. /* init timer list */
  63. rt_list_init(&(timer->list));
  64. }
  65. /**
  66. * @addtogroup Clock
  67. */
  68. /*@{*/
  69. /**
  70. * This function will init 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 init */
  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. */
  144. rt_err_t rt_timer_delete(rt_timer_t timer)
  145. {
  146. register rt_base_t level;
  147. /* timer check */
  148. RT_ASSERT(timer != RT_NULL);
  149. /* disable interrupt */
  150. level = rt_hw_interrupt_disable();
  151. /* remove it from timer list */
  152. rt_list_remove(&(timer->list));
  153. /* enable interrupt */
  154. rt_hw_interrupt_enable(level);
  155. rt_object_delete((rt_object_t)timer);
  156. return -RT_EOK;
  157. }
  158. #endif
  159. /**
  160. * This function will start the timer
  161. *
  162. * @param timer the timer to be started
  163. *
  164. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  165. *
  166. */
  167. rt_err_t rt_timer_start(rt_timer_t timer)
  168. {
  169. struct rt_timer* t;
  170. register rt_base_t level;
  171. rt_list_t *n, *timer_list;
  172. /* timer check */
  173. RT_ASSERT(timer != RT_NULL);
  174. if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) return -RT_ERROR;
  175. #ifdef RT_USING_HOOK
  176. if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(timer->parent));
  177. #endif
  178. /* disable interrupt */
  179. level = rt_hw_interrupt_disable();
  180. /* get timeout tick, which will wrap around if it reaches max ticks */
  181. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  182. #ifdef RT_USING_TIMER_SOFT
  183. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  184. {
  185. if ((RT_TICK_MAX - timer->init_tick) < rt_tick_get())
  186. /* it's a long timer */
  187. timer_list = &rt_soft_long_timer_list;
  188. else
  189. /* it's a short timer */
  190. timer_list = &rt_soft_timer_list;
  191. /* insert timer to soft timer list */
  192. for (n = timer_list->next; n != timer_list; n = n->next)
  193. {
  194. t = rt_list_entry(n, struct rt_timer, list);
  195. if (t->timeout_tick > timer->timeout_tick)
  196. {
  197. rt_list_insert_before(n, &(timer->list));
  198. break;
  199. }
  200. }
  201. /* no found suitable position in timer list */
  202. if (n == timer_list)
  203. {
  204. rt_list_insert_before(n, &(timer->list));
  205. }
  206. }
  207. else
  208. #endif
  209. {
  210. if ((RT_TICK_MAX - timer->init_tick) < rt_tick_get())
  211. /* it's a long timer */
  212. timer_list = &rt_long_timer_list;
  213. else
  214. /* it's a short timer */
  215. timer_list = &rt_timer_list;
  216. /* insert timer to system timer list */
  217. for (n = timer_list->next; n != timer_list; n = n->next)
  218. {
  219. t = rt_list_entry(n, struct rt_timer, list);
  220. if (t->timeout_tick > timer->timeout_tick)
  221. {
  222. rt_list_insert_before(n, &(timer->list));
  223. break;
  224. }
  225. }
  226. /* not found suitable position in timer list */
  227. if (n == timer_list)
  228. {
  229. rt_list_insert_before(n, &(timer->list));
  230. }
  231. }
  232. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  233. /* enable interrupt */
  234. rt_hw_interrupt_enable(level);
  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. */
  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)) return -RT_ERROR;
  251. #ifdef RT_USING_HOOK
  252. if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(timer->parent));
  253. #endif
  254. /* disable interrupt */
  255. level = rt_hw_interrupt_disable();
  256. /* change stat */
  257. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  258. /* remove it from timer list */
  259. rt_list_remove(&(timer->list));
  260. /* enable interrupt */
  261. rt_hw_interrupt_enable(level);
  262. return RT_EOK;
  263. }
  264. /**
  265. * This function will get or set some options of the timer
  266. *
  267. * @param timer the timer to be get or set
  268. * @param cmd the control command
  269. * @param arg the argument
  270. *
  271. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  272. *
  273. */
  274. rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
  275. {
  276. /* timer check */
  277. RT_ASSERT(timer != RT_NULL);
  278. switch (cmd)
  279. {
  280. case RT_TIMER_CTRL_GET_TIME:
  281. *(rt_tick_t*)arg = timer->init_tick;
  282. break;
  283. case RT_TIMER_CTRL_SET_TIME:
  284. timer->init_tick = *(rt_tick_t*)arg;
  285. break;
  286. case RT_TIMER_CTRL_SET_ONESHOT:
  287. timer->parent.flag &= ~(1 << RT_TIMER_FLAG_PERIODIC);
  288. break;
  289. case RT_TIMER_CTRL_SET_PERIODIC:
  290. timer->parent.flag |= (1 << RT_TIMER_FLAG_PERIODIC);
  291. break;
  292. }
  293. return RT_EOK;
  294. }
  295. void rt_timer_switch(void)
  296. {
  297. rt_base_t level;
  298. rt_timer_t t;
  299. /* disable interrupt */
  300. level = rt_hw_interrupt_disable();
  301. /* switch system timer list to long timer list */
  302. /* remove all timer in the old timer list */
  303. while (!rt_list_isempty(&rt_timer_list))
  304. {
  305. t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
  306. #ifdef RT_USING_HOOK
  307. if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
  308. #endif
  309. /* remove timer from timer list firstly */
  310. rt_list_remove(&(t->list));
  311. /* call timeout function */
  312. t->timeout_func(t->parameter);
  313. }
  314. rt_timer_list.next = rt_long_timer_list.next;
  315. rt_timer_list.prev = rt_long_timer_list.prev;
  316. rt_long_timer_list.next->prev = &rt_timer_list;
  317. rt_long_timer_list.prev->next = &rt_timer_list;
  318. rt_list_init(&rt_long_timer_list);
  319. #ifdef RT_USING_TIMER_SOFT
  320. /* remove all timer in the old timer list */
  321. while (!rt_list_isempty(&rt_soft_timer_list))
  322. {
  323. t = rt_list_entry(rt_soft_timer_list.next, struct rt_timer, list);
  324. #ifdef RT_USING_HOOK
  325. if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
  326. #endif
  327. /* remove timer from timer list firstly */
  328. rt_list_remove(&(t->list));
  329. /* call timeout function */
  330. t->timeout_func(t->parameter);
  331. }
  332. rt_soft_timer_list.next = rt_soft_long_timer_list.next;
  333. rt_soft_timer_list.prev = rt_soft_long_timer_list.prev;
  334. rt_soft_long_timer_list.next->prev = &rt_soft_timer_list;
  335. rt_soft_long_timer_list.prev->next = &rt_soft_timer_list;
  336. rt_list_init(&rt_soft_long_timer_list);
  337. #endif
  338. /* enable interrupt */
  339. rt_hw_interrupt_enable(level);
  340. }
  341. /**
  342. * This function will check timer list, if a timeout event happens, the
  343. * corresponding timeout function will be invoked.
  344. *
  345. */
  346. #ifdef RT_USING_TIMER_SOFT
  347. void rt_soft_timer_tick_increase (void);
  348. #endif
  349. void rt_timer_check(void)
  350. {
  351. struct rt_timer *t;
  352. rt_tick_t current_tick;
  353. register rt_base_t level;
  354. #ifdef RT_TIMER_DEBUG
  355. rt_kprintf("timer check enter\n");
  356. #endif
  357. current_tick = rt_tick_get();
  358. /* disable interrupt */
  359. level = rt_hw_interrupt_disable();
  360. while (!rt_list_isempty(&rt_timer_list))
  361. {
  362. t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
  363. if (current_tick >= t->timeout_tick)
  364. {
  365. #ifdef RT_USING_HOOK
  366. if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
  367. #endif
  368. /* remove timer from timer list firstly */
  369. rt_list_remove(&(t->list));
  370. /* call timeout function */
  371. t->timeout_func(t->parameter);
  372. /* re-get tick */
  373. current_tick = rt_tick_get();
  374. #ifdef RT_TIMER_DEBUG
  375. rt_kprintf("current tick: %d\n", current_tick);
  376. #endif
  377. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  378. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  379. {
  380. /* start it */
  381. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  382. rt_timer_start(t);
  383. }
  384. else
  385. {
  386. /* stop timer */
  387. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  388. }
  389. }
  390. else break;
  391. }
  392. /* enable interrupt */
  393. rt_hw_interrupt_enable(level);
  394. /* increase soft timer tick */
  395. #ifdef RT_USING_TIMER_SOFT
  396. rt_soft_timer_tick_increase ( );
  397. #endif
  398. #ifdef RT_TIMER_DEBUG
  399. rt_kprintf("timer check leave\n");
  400. #endif
  401. }
  402. #ifdef RT_USING_TIMER_SOFT
  403. static struct rt_thread timer_thread;
  404. static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
  405. static struct rt_semaphore timer_sem;
  406. static rt_uint16_t timer_ex_cnt;
  407. void rt_soft_timer_tick_increase (void)
  408. {
  409. timer_ex_cnt++;
  410. if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
  411. {
  412. timer_ex_cnt = 0;
  413. rt_sem_release(&timer_sem);
  414. }
  415. }
  416. /**
  417. * This function will check timer list, if a timeout event happens, the
  418. * corresponding timeout function will be invoked.
  419. *
  420. */
  421. void rt_soft_timer_check()
  422. {
  423. rt_tick_t current_tick;
  424. rt_list_t *n;
  425. struct rt_timer *t;
  426. #ifdef RT_TIMER_DEBUG
  427. rt_kprintf("software timer check enter\n");
  428. #endif
  429. current_tick = rt_tick_get();
  430. for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); )
  431. {
  432. t = rt_list_entry(n, struct rt_timer, list);
  433. if (current_tick >= t->timeout_tick)
  434. {
  435. #ifdef RT_USING_HOOK
  436. if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
  437. #endif
  438. /* move node to the next */
  439. n = n->next;
  440. /* remove timer from timer list firstly */
  441. rt_list_remove(&(t->list));
  442. /* call timeout function */
  443. t->timeout_func(t->parameter);
  444. /* reget tick */
  445. current_tick = rt_tick_get();
  446. #ifdef RT_TIMER_DEBUG
  447. rt_kprintf("current tick: %d\n", current_tick);
  448. #endif
  449. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  450. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  451. {
  452. /* start it */
  453. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  454. rt_timer_start(t);
  455. }
  456. else
  457. {
  458. /* stop timer */
  459. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  460. }
  461. }
  462. else break;
  463. }
  464. #ifdef RT_TIMER_DEBUG
  465. rt_kprintf("software timer check leave\n");
  466. #endif
  467. }
  468. /* system timer thread entry */
  469. static void rt_thread_timer_entry(void* parameter)
  470. {
  471. while (1)
  472. {
  473. /* take software timer semaphore */
  474. rt_sem_take(&timer_sem,RT_WAITING_FOREVER);
  475. /* lock scheduler */
  476. rt_enter_critical();
  477. /* check software timer */
  478. rt_soft_timer_check();
  479. /* unlock scheduler */
  480. rt_exit_critical();
  481. }
  482. }
  483. #endif
  484. /**
  485. * @ingroup SystemInit
  486. *
  487. * This function will init system timer
  488. *
  489. */
  490. void rt_system_timer_init()
  491. {
  492. rt_list_init(&rt_timer_list);
  493. rt_list_init(&rt_long_timer_list);
  494. #ifdef RT_USING_TIMER_SOFT
  495. rt_list_init(&rt_soft_timer_list);
  496. rt_list_init(&rt_soft_long_timer_list);
  497. rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);
  498. #endif
  499. }
  500. /**
  501. * @ingroup SystemInit
  502. *
  503. * This function will init system timer thread
  504. *
  505. */
  506. void rt_system_timer_thread_init()
  507. {
  508. #ifdef RT_USING_TIMER_SOFT
  509. /* start software timer thread */
  510. rt_thread_init(&timer_thread,
  511. "timer",
  512. rt_thread_timer_entry, RT_NULL,
  513. &timer_thread_stack[0], sizeof(timer_thread_stack),
  514. RT_TIMER_THREAD_PRIO, 10);
  515. /* startup */
  516. rt_thread_startup(&timer_thread);
  517. #endif
  518. }
  519. /*@}*/