timer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-12 Bernard first version
  9. * 2006-04-29 Bernard implement thread timer
  10. * 2006-06-04 Bernard implement rt_timer_control
  11. * 2006-08-10 Bernard fix the periodic timer bug
  12. * 2006-09-03 Bernard implement rt_timer_detach
  13. * 2009-11-11 LiJin add soft timer
  14. * 2010-05-12 Bernard fix the timer check bug.
  15. * 2010-11-02 Charlie re-implement tick overflow issue
  16. * 2012-12-15 Bernard fix the next timeout issue in soft timer
  17. * 2014-07-12 Bernard does not lock scheduler when invoking soft-timer
  18. * timeout function.
  19. */
  20. #include <rtthread.h>
  21. #include <rthw.h>
  22. /* hard timer list */
  23. static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
  24. #ifdef RT_USING_TIMER_SOFT
  25. #define RT_SOFT_TIMER_IDLE 1
  26. #define RT_SOFT_TIMER_BUSY 0
  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 status */
  34. static rt_uint8_t soft_timer_status = RT_SOFT_TIMER_IDLE;
  35. /* soft timer list */
  36. static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
  37. static struct rt_thread timer_thread;
  38. ALIGN(RT_ALIGN_SIZE)
  39. static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
  40. #endif
  41. #ifdef RT_USING_HOOK
  42. extern void (*rt_object_take_hook)(struct rt_object *object);
  43. extern void (*rt_object_put_hook)(struct rt_object *object);
  44. static void (*rt_timer_enter_hook)(struct rt_timer *timer);
  45. static void (*rt_timer_exit_hook)(struct rt_timer *timer);
  46. /**
  47. * @addtogroup Hook
  48. */
  49. /**@{*/
  50. /**
  51. * This function will set a hook function, which will be invoked when enter
  52. * timer timeout callback function.
  53. *
  54. * @param hook the hook function
  55. */
  56. void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
  57. {
  58. rt_timer_enter_hook = hook;
  59. }
  60. /**
  61. * This function will set a hook function, which will be invoked when exit
  62. * timer timeout callback function.
  63. *
  64. * @param hook the hook function
  65. */
  66. void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
  67. {
  68. rt_timer_exit_hook = hook;
  69. }
  70. /**@}*/
  71. #endif
  72. static void _rt_timer_init(rt_timer_t timer,
  73. void (*timeout)(void *parameter),
  74. void *parameter,
  75. rt_tick_t time,
  76. rt_uint8_t flag)
  77. {
  78. int i;
  79. /* set flag */
  80. timer->parent.flag = flag;
  81. /* set deactivated */
  82. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  83. timer->timeout_func = timeout;
  84. timer->parameter = parameter;
  85. timer->timeout_tick = 0;
  86. timer->init_tick = time;
  87. /* initialize timer list */
  88. for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
  89. {
  90. rt_list_init(&(timer->row[i]));
  91. }
  92. }
  93. /* the fist timer always in the last row */
  94. static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
  95. {
  96. struct rt_timer *timer;
  97. register rt_base_t level;
  98. rt_tick_t timeout_tick = RT_TICK_MAX;
  99. /* disable interrupt */
  100. level = rt_hw_interrupt_disable();
  101. if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  102. {
  103. timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  104. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  105. timeout_tick = timer->timeout_tick;
  106. }
  107. /* enable interrupt */
  108. rt_hw_interrupt_enable(level);
  109. return timeout_tick;
  110. }
  111. rt_inline void _rt_timer_remove(rt_timer_t timer)
  112. {
  113. int i;
  114. for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
  115. {
  116. rt_list_remove(&timer->row[i]);
  117. }
  118. }
  119. #if RT_DEBUG_TIMER
  120. static int rt_timer_count_height(struct rt_timer *timer)
  121. {
  122. int i, cnt = 0;
  123. for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
  124. {
  125. if (!rt_list_isempty(&timer->row[i]))
  126. cnt++;
  127. }
  128. return cnt;
  129. }
  130. void rt_timer_dump(rt_list_t timer_heads[])
  131. {
  132. rt_list_t *list;
  133. for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
  134. list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
  135. list = list->next)
  136. {
  137. struct rt_timer *timer = rt_list_entry(list,
  138. struct rt_timer,
  139. row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  140. rt_kprintf("%d", rt_timer_count_height(timer));
  141. }
  142. rt_kprintf("\n");
  143. }
  144. #endif
  145. /**
  146. * @addtogroup Clock
  147. */
  148. /**@{*/
  149. /**
  150. * This function will initialize a timer, normally this function is used to
  151. * initialize a static timer object.
  152. *
  153. * @param timer the static timer object
  154. * @param name the name of timer
  155. * @param timeout the timeout function
  156. * @param parameter the parameter of timeout function
  157. * @param time the tick of timer
  158. * @param flag the flag of timer
  159. */
  160. void rt_timer_init(rt_timer_t timer,
  161. const char *name,
  162. void (*timeout)(void *parameter),
  163. void *parameter,
  164. rt_tick_t time,
  165. rt_uint8_t flag)
  166. {
  167. /* timer check */
  168. RT_ASSERT(timer != RT_NULL);
  169. /* timer object initialization */
  170. rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
  171. _rt_timer_init(timer, timeout, parameter, time, flag);
  172. }
  173. /**
  174. * This function will detach a timer from timer management.
  175. *
  176. * @param timer the static timer object
  177. *
  178. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  179. */
  180. rt_err_t rt_timer_detach(rt_timer_t timer)
  181. {
  182. register rt_base_t level;
  183. /* timer check */
  184. RT_ASSERT(timer != RT_NULL);
  185. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  186. RT_ASSERT(rt_object_is_systemobject(&timer->parent));
  187. /* disable interrupt */
  188. level = rt_hw_interrupt_disable();
  189. _rt_timer_remove(timer);
  190. /* stop timer */
  191. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  192. /* enable interrupt */
  193. rt_hw_interrupt_enable(level);
  194. rt_object_detach((rt_object_t)timer);
  195. return RT_EOK;
  196. }
  197. #ifdef RT_USING_HEAP
  198. /**
  199. * This function will create a timer
  200. *
  201. * @param name the name of timer
  202. * @param timeout the timeout function
  203. * @param parameter the parameter of timeout function
  204. * @param time the tick of timer
  205. * @param flag the flag of timer
  206. *
  207. * @return the created timer object
  208. */
  209. rt_timer_t rt_timer_create(const char *name,
  210. void (*timeout)(void *parameter),
  211. void *parameter,
  212. rt_tick_t time,
  213. rt_uint8_t flag)
  214. {
  215. struct rt_timer *timer;
  216. /* allocate a object */
  217. timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
  218. if (timer == RT_NULL)
  219. {
  220. return RT_NULL;
  221. }
  222. _rt_timer_init(timer, timeout, parameter, time, flag);
  223. return timer;
  224. }
  225. /**
  226. * This function will delete a timer and release timer memory
  227. *
  228. * @param timer the timer to be deleted
  229. *
  230. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  231. */
  232. rt_err_t rt_timer_delete(rt_timer_t timer)
  233. {
  234. register rt_base_t level;
  235. /* timer check */
  236. RT_ASSERT(timer != RT_NULL);
  237. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  238. RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
  239. /* disable interrupt */
  240. level = rt_hw_interrupt_disable();
  241. _rt_timer_remove(timer);
  242. /* stop timer */
  243. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  244. /* enable interrupt */
  245. rt_hw_interrupt_enable(level);
  246. rt_object_delete((rt_object_t)timer);
  247. return RT_EOK;
  248. }
  249. #endif
  250. /**
  251. * This function will start the timer
  252. *
  253. * @param timer the timer to be started
  254. *
  255. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  256. */
  257. rt_err_t rt_timer_start(rt_timer_t timer)
  258. {
  259. unsigned int row_lvl;
  260. rt_list_t *timer_list;
  261. register rt_base_t level;
  262. rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
  263. unsigned int tst_nr;
  264. static unsigned int random_nr;
  265. /* timer check */
  266. RT_ASSERT(timer != RT_NULL);
  267. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  268. /* stop timer firstly */
  269. level = rt_hw_interrupt_disable();
  270. /* remove timer from list */
  271. _rt_timer_remove(timer);
  272. /* change status of timer */
  273. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  274. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  275. /*
  276. * get timeout tick,
  277. * the max timeout tick shall not great than RT_TICK_MAX/2
  278. */
  279. RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
  280. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  281. #ifdef RT_USING_TIMER_SOFT
  282. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  283. {
  284. /* insert timer to soft timer list */
  285. timer_list = rt_soft_timer_list;
  286. }
  287. else
  288. #endif
  289. {
  290. /* insert timer to system timer list */
  291. timer_list = rt_timer_list;
  292. }
  293. row_head[0] = &timer_list[0];
  294. for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  295. {
  296. for (; row_head[row_lvl] != timer_list[row_lvl].prev;
  297. row_head[row_lvl] = row_head[row_lvl]->next)
  298. {
  299. struct rt_timer *t;
  300. rt_list_t *p = row_head[row_lvl]->next;
  301. /* fix up the entry pointer */
  302. t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
  303. /* If we have two timers that timeout at the same time, it's
  304. * preferred that the timer inserted early get called early.
  305. * So insert the new timer to the end the the some-timeout timer
  306. * list.
  307. */
  308. if ((t->timeout_tick - timer->timeout_tick) == 0)
  309. {
  310. continue;
  311. }
  312. else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
  313. {
  314. break;
  315. }
  316. }
  317. if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
  318. row_head[row_lvl + 1] = row_head[row_lvl] + 1;
  319. }
  320. /* Interestingly, this super simple timer insert counter works very very
  321. * well on distributing the list height uniformly. By means of "very very
  322. * well", I mean it beats the randomness of timer->timeout_tick very easily
  323. * (actually, the timeout_tick is not random and easy to be attacked). */
  324. random_nr++;
  325. tst_nr = random_nr;
  326. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
  327. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  328. for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  329. {
  330. if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
  331. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
  332. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
  333. else
  334. break;
  335. /* Shift over the bits we have tested. Works well with 1 bit and 2
  336. * bits. */
  337. tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
  338. }
  339. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  340. /* enable interrupt */
  341. rt_hw_interrupt_enable(level);
  342. #ifdef RT_USING_TIMER_SOFT
  343. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  344. {
  345. /* check whether timer thread is ready */
  346. if ((soft_timer_status == RT_SOFT_TIMER_IDLE) &&
  347. ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  348. {
  349. /* resume timer thread to check soft timer */
  350. rt_thread_resume(&timer_thread);
  351. rt_schedule();
  352. }
  353. }
  354. #endif
  355. return RT_EOK;
  356. }
  357. /**
  358. * This function will stop the timer
  359. *
  360. * @param timer the timer to be stopped
  361. *
  362. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  363. */
  364. rt_err_t rt_timer_stop(rt_timer_t timer)
  365. {
  366. register rt_base_t level;
  367. /* timer check */
  368. RT_ASSERT(timer != RT_NULL);
  369. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  370. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  371. return -RT_ERROR;
  372. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  373. /* disable interrupt */
  374. level = rt_hw_interrupt_disable();
  375. _rt_timer_remove(timer);
  376. /* change status */
  377. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  378. /* enable interrupt */
  379. rt_hw_interrupt_enable(level);
  380. return RT_EOK;
  381. }
  382. /**
  383. * This function will get or set some options of the timer
  384. *
  385. * @param timer the timer to be get or set
  386. * @param cmd the control command
  387. * @param arg the argument
  388. *
  389. * @return RT_EOK
  390. */
  391. rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
  392. {
  393. register rt_base_t level;
  394. /* timer check */
  395. RT_ASSERT(timer != RT_NULL);
  396. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  397. level = rt_hw_interrupt_disable();
  398. switch (cmd)
  399. {
  400. case RT_TIMER_CTRL_GET_TIME:
  401. *(rt_tick_t *)arg = timer->init_tick;
  402. break;
  403. case RT_TIMER_CTRL_SET_TIME:
  404. timer->init_tick = *(rt_tick_t *)arg;
  405. break;
  406. case RT_TIMER_CTRL_SET_ONESHOT:
  407. timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
  408. break;
  409. case RT_TIMER_CTRL_SET_PERIODIC:
  410. timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
  411. break;
  412. case RT_TIMER_CTRL_GET_STATE:
  413. if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  414. {
  415. /*timer is start and run*/
  416. *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;
  417. }
  418. else
  419. {
  420. /*timer is stop*/
  421. *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
  422. }
  423. break;
  424. default:
  425. break;
  426. }
  427. rt_hw_interrupt_enable(level);
  428. return RT_EOK;
  429. }
  430. /**
  431. * This function will check timer list, if a timeout event happens, the
  432. * corresponding timeout function will be invoked.
  433. *
  434. * @note this function shall be invoked in operating system timer interrupt.
  435. */
  436. void rt_timer_check(void)
  437. {
  438. struct rt_timer *t;
  439. rt_tick_t current_tick;
  440. register rt_base_t level;
  441. rt_list_t list;
  442. rt_list_init(&list);
  443. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  444. current_tick = rt_tick_get();
  445. /* disable interrupt */
  446. level = rt_hw_interrupt_disable();
  447. while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  448. {
  449. t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  450. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  451. /*
  452. * It supposes that the new tick shall less than the half duration of
  453. * tick max.
  454. */
  455. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  456. {
  457. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  458. /* remove timer from timer list firstly */
  459. _rt_timer_remove(t);
  460. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  461. {
  462. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  463. }
  464. /* add timer to temporary list */
  465. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  466. /* call timeout function */
  467. t->timeout_func(t->parameter);
  468. /* re-get tick */
  469. current_tick = rt_tick_get();
  470. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  471. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  472. /* Check whether the timer object is detached or started again */
  473. if (rt_list_isempty(&list))
  474. {
  475. continue;
  476. }
  477. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  478. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  479. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  480. {
  481. /* start it */
  482. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  483. rt_timer_start(t);
  484. }
  485. }
  486. else break;
  487. }
  488. /* enable interrupt */
  489. rt_hw_interrupt_enable(level);
  490. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  491. }
  492. /**
  493. * This function will return the next timeout tick in the system.
  494. *
  495. * @return the next timeout tick in the system
  496. */
  497. rt_tick_t rt_timer_next_timeout_tick(void)
  498. {
  499. return rt_timer_list_next_timeout(rt_timer_list);
  500. }
  501. #ifdef RT_USING_TIMER_SOFT
  502. /**
  503. * This function will check software-timer list, if a timeout event happens, the
  504. * corresponding timeout function will be invoked.
  505. */
  506. void rt_soft_timer_check(void)
  507. {
  508. rt_tick_t current_tick;
  509. struct rt_timer *t;
  510. register rt_base_t level;
  511. rt_list_t list;
  512. rt_list_init(&list);
  513. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  514. /* disable interrupt */
  515. level = rt_hw_interrupt_disable();
  516. while (!rt_list_isempty(&rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  517. {
  518. t = rt_list_entry(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  519. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  520. current_tick = rt_tick_get();
  521. /*
  522. * It supposes that the new tick shall less than the half duration of
  523. * tick max.
  524. */
  525. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  526. {
  527. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  528. /* remove timer from timer list firstly */
  529. _rt_timer_remove(t);
  530. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  531. {
  532. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  533. }
  534. /* add timer to temporary list */
  535. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  536. soft_timer_status = RT_SOFT_TIMER_BUSY;
  537. /* enable interrupt */
  538. rt_hw_interrupt_enable(level);
  539. /* call timeout function */
  540. t->timeout_func(t->parameter);
  541. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  542. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  543. /* disable interrupt */
  544. level = rt_hw_interrupt_disable();
  545. soft_timer_status = RT_SOFT_TIMER_IDLE;
  546. /* Check whether the timer object is detached or started again */
  547. if (rt_list_isempty(&list))
  548. {
  549. continue;
  550. }
  551. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  552. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  553. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  554. {
  555. /* start it */
  556. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  557. rt_timer_start(t);
  558. }
  559. }
  560. else break; /* not check anymore */
  561. }
  562. /* enable interrupt */
  563. rt_hw_interrupt_enable(level);
  564. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  565. }
  566. /* system timer thread entry */
  567. static void rt_thread_timer_entry(void *parameter)
  568. {
  569. rt_tick_t next_timeout;
  570. while (1)
  571. {
  572. /* get the next timeout tick */
  573. next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
  574. if (next_timeout == RT_TICK_MAX)
  575. {
  576. /* no software timer exist, suspend self. */
  577. rt_thread_suspend(rt_thread_self());
  578. rt_schedule();
  579. }
  580. else
  581. {
  582. rt_tick_t current_tick;
  583. /* get current tick */
  584. current_tick = rt_tick_get();
  585. if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
  586. {
  587. /* get the delta timeout tick */
  588. next_timeout = next_timeout - current_tick;
  589. rt_thread_delay(next_timeout);
  590. }
  591. }
  592. /* check software timer */
  593. rt_soft_timer_check();
  594. }
  595. }
  596. #endif
  597. /**
  598. * @ingroup SystemInit
  599. *
  600. * This function will initialize system timer
  601. */
  602. void rt_system_timer_init(void)
  603. {
  604. int i;
  605. for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
  606. {
  607. rt_list_init(rt_timer_list + i);
  608. }
  609. }
  610. /**
  611. * @ingroup SystemInit
  612. *
  613. * This function will initialize system timer thread
  614. */
  615. void rt_system_timer_thread_init(void)
  616. {
  617. #ifdef RT_USING_TIMER_SOFT
  618. int i;
  619. for (i = 0;
  620. i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
  621. i++)
  622. {
  623. rt_list_init(rt_soft_timer_list + i);
  624. }
  625. /* start software timer thread */
  626. rt_thread_init(&timer_thread,
  627. "timer",
  628. rt_thread_timer_entry,
  629. RT_NULL,
  630. &timer_thread_stack[0],
  631. sizeof(timer_thread_stack),
  632. RT_TIMER_THREAD_PRIO,
  633. 10);
  634. /* startup */
  635. rt_thread_startup(&timer_thread);
  636. #endif
  637. }
  638. /**@}*/