timer.c 12 KB

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