timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. /* 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. */
  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. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  176. /* disable interrupt */
  177. level = rt_hw_interrupt_disable();
  178. /* get timeout tick, the max timeout tick shall not great than RT_TICK_MAX/2 */
  179. RT_ASSERT(timer->init_tick < RT_TICK_MAX/2);
  180. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  181. #ifdef RT_USING_TIMER_SOFT
  182. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  183. {
  184. /* insert timer to soft timer list */
  185. timer_list = &rt_soft_timer_list;
  186. }
  187. else
  188. #endif
  189. {
  190. /* insert timer to system timer list */
  191. timer_list = &rt_timer_list;
  192. }
  193. for (n = timer_list->next; n != timer_list; n = n->next)
  194. {
  195. t = rt_list_entry(n, struct rt_timer, list);
  196. /*
  197. * It supposes that the new tick shall less than the half duration of tick max.
  198. */
  199. if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX/2)
  200. {
  201. rt_list_insert_before(n, &(timer->list));
  202. break;
  203. }
  204. }
  205. /* no found suitable position in timer list */
  206. if (n == timer_list)
  207. {
  208. rt_list_insert_before(n, &(timer->list));
  209. }
  210. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  211. /* enable interrupt */
  212. rt_hw_interrupt_enable(level);
  213. return -RT_EOK;
  214. }
  215. /**
  216. * This function will stop the timer
  217. *
  218. * @param timer the timer to be stopped
  219. *
  220. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  221. *
  222. */
  223. rt_err_t rt_timer_stop(rt_timer_t timer)
  224. {
  225. register rt_base_t level;
  226. /* timer check */
  227. RT_ASSERT(timer != RT_NULL);
  228. if(!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR;
  229. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  230. /* disable interrupt */
  231. level = rt_hw_interrupt_disable();
  232. /* change stat */
  233. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  234. /* remove it from timer list */
  235. rt_list_remove(&(timer->list));
  236. /* enable interrupt */
  237. rt_hw_interrupt_enable(level);
  238. return RT_EOK;
  239. }
  240. /**
  241. * This function will get or set some options of the timer
  242. *
  243. * @param timer the timer to be get or set
  244. * @param cmd the control command
  245. * @param arg the argument
  246. *
  247. * @return RT_EOK
  248. *
  249. */
  250. rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
  251. {
  252. /* timer check */
  253. RT_ASSERT(timer != RT_NULL);
  254. switch (cmd)
  255. {
  256. case RT_TIMER_CTRL_GET_TIME:
  257. *(rt_tick_t*)arg = timer->init_tick;
  258. break;
  259. case RT_TIMER_CTRL_SET_TIME:
  260. timer->init_tick = *(rt_tick_t*)arg;
  261. break;
  262. case RT_TIMER_CTRL_SET_ONESHOT:
  263. timer->parent.flag &= ~(1 << RT_TIMER_FLAG_PERIODIC);
  264. break;
  265. case RT_TIMER_CTRL_SET_PERIODIC:
  266. timer->parent.flag |= (1 << RT_TIMER_FLAG_PERIODIC);
  267. break;
  268. }
  269. return RT_EOK;
  270. }
  271. /**
  272. * This function will check timer list, if a timeout event happens, the
  273. * corresponding timeout function will be invoked.
  274. *
  275. * @note this function shall be invoked in operating system timer interrupt.
  276. */
  277. #ifdef RT_USING_TIMER_SOFT
  278. void rt_soft_timer_tick_increase (void);
  279. #endif
  280. void rt_timer_check(void)
  281. {
  282. struct rt_timer *t;
  283. rt_tick_t current_tick;
  284. register rt_base_t level;
  285. RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check enter\n"));
  286. current_tick = rt_tick_get();
  287. /* disable interrupt */
  288. level = rt_hw_interrupt_disable();
  289. while (!rt_list_isempty(&rt_timer_list))
  290. {
  291. t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
  292. /*
  293. * It supposes that the new tick shall less than the half duration of tick max.
  294. */
  295. if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
  296. {
  297. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  298. /* remove timer from timer list firstly */
  299. rt_list_remove(&(t->list));
  300. /* call timeout function */
  301. t->timeout_func(t->parameter);
  302. /* re-get tick */
  303. current_tick = rt_tick_get();
  304. RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick));
  305. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  306. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  307. {
  308. /* start it */
  309. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  310. rt_timer_start(t);
  311. }
  312. else
  313. {
  314. /* stop timer */
  315. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  316. }
  317. }
  318. else break;
  319. }
  320. /* enable interrupt */
  321. rt_hw_interrupt_enable(level);
  322. /* increase soft timer tick */
  323. #ifdef RT_USING_TIMER_SOFT
  324. rt_soft_timer_tick_increase ( );
  325. #endif
  326. RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check leave\n"));
  327. }
  328. #ifdef RT_USING_TIMER_SOFT
  329. static struct rt_thread timer_thread;
  330. ALIGN(RT_ALIGN_SIZE)
  331. static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
  332. static struct rt_semaphore timer_sem;
  333. static rt_uint16_t timer_ex_cnt;
  334. void rt_soft_timer_tick_increase (void)
  335. {
  336. timer_ex_cnt++;
  337. if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
  338. {
  339. timer_ex_cnt = 0;
  340. rt_sem_release(&timer_sem);
  341. }
  342. }
  343. /**
  344. * This function will check timer list, if a timeout event happens, the
  345. * corresponding timeout function will be invoked.
  346. *
  347. */
  348. void rt_soft_timer_check()
  349. {
  350. rt_tick_t current_tick;
  351. rt_list_t *n;
  352. struct rt_timer *t;
  353. RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check enter\n"));
  354. current_tick = rt_tick_get();
  355. for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); )
  356. {
  357. t = rt_list_entry(n, struct rt_timer, list);
  358. /*
  359. * It supposes that the new tick shall less than the half duration of tick max.
  360. */
  361. if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
  362. {
  363. RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
  364. /* move node to the next */
  365. n = n->next;
  366. /* remove timer from timer list firstly */
  367. rt_list_remove(&(t->list));
  368. /* call timeout function */
  369. t->timeout_func(t->parameter);
  370. /* re-get tick */
  371. current_tick = rt_tick_get();
  372. RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick));
  373. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  374. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  375. {
  376. /* start it */
  377. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  378. rt_timer_start(t);
  379. }
  380. else
  381. {
  382. /* stop timer */
  383. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  384. }
  385. }
  386. else break; /* not check anymore */
  387. }
  388. RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check leave\n"));
  389. }
  390. /* system timer thread entry */
  391. static void rt_thread_timer_entry(void* parameter)
  392. {
  393. while (1)
  394. {
  395. /* take software timer semaphore */
  396. rt_sem_take(&timer_sem,RT_WAITING_FOREVER);
  397. /* lock scheduler */
  398. rt_enter_critical();
  399. /* check software timer */
  400. rt_soft_timer_check();
  401. /* unlock scheduler */
  402. rt_exit_critical();
  403. }
  404. }
  405. #endif
  406. /**
  407. * @ingroup SystemInit
  408. *
  409. * This function will initialize system timer
  410. *
  411. */
  412. void rt_system_timer_init()
  413. {
  414. rt_list_init(&rt_timer_list);
  415. #ifdef RT_USING_TIMER_SOFT
  416. rt_list_init(&rt_soft_timer_list);
  417. rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);
  418. #endif
  419. }
  420. /**
  421. * @ingroup SystemInit
  422. *
  423. * This function will initialize system timer thread
  424. *
  425. */
  426. void rt_system_timer_thread_init()
  427. {
  428. #ifdef RT_USING_TIMER_SOFT
  429. /* start software timer thread */
  430. rt_thread_init(&timer_thread,
  431. "timer",
  432. rt_thread_timer_entry, RT_NULL,
  433. &timer_thread_stack[0], sizeof(timer_thread_stack),
  434. RT_TIMER_THREAD_PRIO, 10);
  435. /* startup */
  436. rt_thread_startup(&timer_thread);
  437. #endif
  438. }
  439. /*@}*/