timer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 /* RT_TIMER_THREAD_STACK_SIZE */
  30. #ifndef RT_TIMER_THREAD_PRIO
  31. #define RT_TIMER_THREAD_PRIO 0
  32. #endif /* RT_TIMER_THREAD_PRIO */
  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 /* RT_USING_TIMER_SOFT */
  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 /* RT_USING_HOOK */
  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 /* RT_DEBUG_TIMER */
  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(&(timer->parent), RT_Object_Class_Timer, name);
  171. _rt_timer_init(timer, timeout, parameter, time, flag);
  172. }
  173. RTM_EXPORT(rt_timer_init);
  174. /**
  175. * This function will detach a timer from timer management.
  176. *
  177. * @param timer the static timer object
  178. *
  179. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  180. */
  181. rt_err_t rt_timer_detach(rt_timer_t timer)
  182. {
  183. register rt_base_t level;
  184. /* timer check */
  185. RT_ASSERT(timer != RT_NULL);
  186. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  187. RT_ASSERT(rt_object_is_systemobject(&timer->parent));
  188. /* disable interrupt */
  189. level = rt_hw_interrupt_disable();
  190. _rt_timer_remove(timer);
  191. /* stop timer */
  192. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  193. /* enable interrupt */
  194. rt_hw_interrupt_enable(level);
  195. rt_object_detach(&(timer->parent));
  196. return RT_EOK;
  197. }
  198. RTM_EXPORT(rt_timer_detach);
  199. #ifdef RT_USING_HEAP
  200. /**
  201. * This function will create a timer
  202. *
  203. * @param name the name of timer
  204. * @param timeout the timeout function
  205. * @param parameter the parameter of timeout function
  206. * @param time the tick of timer
  207. * @param flag the flag of timer
  208. *
  209. * @return the created timer object
  210. */
  211. rt_timer_t rt_timer_create(const char *name,
  212. void (*timeout)(void *parameter),
  213. void *parameter,
  214. rt_tick_t time,
  215. rt_uint8_t flag)
  216. {
  217. struct rt_timer *timer;
  218. /* allocate a object */
  219. timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
  220. if (timer == RT_NULL)
  221. {
  222. return RT_NULL;
  223. }
  224. _rt_timer_init(timer, timeout, parameter, time, flag);
  225. return timer;
  226. }
  227. RTM_EXPORT(rt_timer_create);
  228. /**
  229. * This function will delete a timer and release timer memory
  230. *
  231. * @param timer the timer to be deleted
  232. *
  233. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  234. */
  235. rt_err_t rt_timer_delete(rt_timer_t timer)
  236. {
  237. register rt_base_t level;
  238. /* timer check */
  239. RT_ASSERT(timer != RT_NULL);
  240. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  241. RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
  242. /* disable interrupt */
  243. level = rt_hw_interrupt_disable();
  244. _rt_timer_remove(timer);
  245. /* stop timer */
  246. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  247. /* enable interrupt */
  248. rt_hw_interrupt_enable(level);
  249. rt_object_delete(&(timer->parent));
  250. return RT_EOK;
  251. }
  252. RTM_EXPORT(rt_timer_delete);
  253. #endif /* RT_USING_HEAP */
  254. /**
  255. * This function will start the timer
  256. *
  257. * @param timer the timer to be started
  258. *
  259. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  260. */
  261. rt_err_t rt_timer_start(rt_timer_t timer)
  262. {
  263. unsigned int row_lvl;
  264. rt_list_t *timer_list;
  265. register rt_base_t level;
  266. rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
  267. unsigned int tst_nr;
  268. static unsigned int random_nr;
  269. /* timer check */
  270. RT_ASSERT(timer != RT_NULL);
  271. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  272. /* stop timer firstly */
  273. level = rt_hw_interrupt_disable();
  274. /* remove timer from list */
  275. _rt_timer_remove(timer);
  276. /* change status of timer */
  277. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  278. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  279. /*
  280. * get timeout tick,
  281. * the max timeout tick shall not great than RT_TICK_MAX/2
  282. */
  283. RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
  284. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  285. #ifdef RT_USING_TIMER_SOFT
  286. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  287. {
  288. /* insert timer to soft timer list */
  289. timer_list = rt_soft_timer_list;
  290. }
  291. else
  292. #endif /* RT_USING_TIMER_SOFT */
  293. {
  294. /* insert timer to system timer list */
  295. timer_list = rt_timer_list;
  296. }
  297. row_head[0] = &timer_list[0];
  298. for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  299. {
  300. for (; row_head[row_lvl] != timer_list[row_lvl].prev;
  301. row_head[row_lvl] = row_head[row_lvl]->next)
  302. {
  303. struct rt_timer *t;
  304. rt_list_t *p = row_head[row_lvl]->next;
  305. /* fix up the entry pointer */
  306. t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
  307. /* If we have two timers that timeout at the same time, it's
  308. * preferred that the timer inserted early get called early.
  309. * So insert the new timer to the end the the some-timeout timer
  310. * list.
  311. */
  312. if ((t->timeout_tick - timer->timeout_tick) == 0)
  313. {
  314. continue;
  315. }
  316. else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
  317. {
  318. break;
  319. }
  320. }
  321. if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
  322. row_head[row_lvl + 1] = row_head[row_lvl] + 1;
  323. }
  324. /* Interestingly, this super simple timer insert counter works very very
  325. * well on distributing the list height uniformly. By means of "very very
  326. * well", I mean it beats the randomness of timer->timeout_tick very easily
  327. * (actually, the timeout_tick is not random and easy to be attacked). */
  328. random_nr++;
  329. tst_nr = random_nr;
  330. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
  331. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  332. for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  333. {
  334. if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
  335. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
  336. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
  337. else
  338. break;
  339. /* Shift over the bits we have tested. Works well with 1 bit and 2
  340. * bits. */
  341. tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
  342. }
  343. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  344. /* enable interrupt */
  345. rt_hw_interrupt_enable(level);
  346. #ifdef RT_USING_TIMER_SOFT
  347. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  348. {
  349. /* check whether timer thread is ready */
  350. if ((soft_timer_status == RT_SOFT_TIMER_IDLE) &&
  351. ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  352. {
  353. /* resume timer thread to check soft timer */
  354. rt_thread_resume(&timer_thread);
  355. rt_schedule();
  356. }
  357. }
  358. #endif /* RT_USING_TIMER_SOFT */
  359. return RT_EOK;
  360. }
  361. RTM_EXPORT(rt_timer_start);
  362. /**
  363. * This function will stop the timer
  364. *
  365. * @param timer the timer to be stopped
  366. *
  367. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  368. */
  369. rt_err_t rt_timer_stop(rt_timer_t timer)
  370. {
  371. register rt_base_t level;
  372. /* timer check */
  373. RT_ASSERT(timer != RT_NULL);
  374. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  375. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  376. return -RT_ERROR;
  377. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  378. /* disable interrupt */
  379. level = rt_hw_interrupt_disable();
  380. _rt_timer_remove(timer);
  381. /* change status */
  382. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  383. /* enable interrupt */
  384. rt_hw_interrupt_enable(level);
  385. return RT_EOK;
  386. }
  387. RTM_EXPORT(rt_timer_stop);
  388. /**
  389. * This function will get or set some options of the timer
  390. *
  391. * @param timer the timer to be get or set
  392. * @param cmd the control command
  393. * @param arg the argument
  394. *
  395. * @return RT_EOK
  396. */
  397. rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
  398. {
  399. register rt_base_t level;
  400. /* timer check */
  401. RT_ASSERT(timer != RT_NULL);
  402. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  403. level = rt_hw_interrupt_disable();
  404. switch (cmd)
  405. {
  406. case RT_TIMER_CTRL_GET_TIME:
  407. *(rt_tick_t *)arg = timer->init_tick;
  408. break;
  409. case RT_TIMER_CTRL_SET_TIME:
  410. timer->init_tick = *(rt_tick_t *)arg;
  411. break;
  412. case RT_TIMER_CTRL_SET_ONESHOT:
  413. timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
  414. break;
  415. case RT_TIMER_CTRL_SET_PERIODIC:
  416. timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
  417. break;
  418. case RT_TIMER_CTRL_GET_STATE:
  419. if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  420. {
  421. /*timer is start and run*/
  422. *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;
  423. }
  424. else
  425. {
  426. /*timer is stop*/
  427. *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
  428. }
  429. break;
  430. default:
  431. break;
  432. }
  433. rt_hw_interrupt_enable(level);
  434. return RT_EOK;
  435. }
  436. RTM_EXPORT(rt_timer_control);
  437. /**
  438. * This function will check timer list, if a timeout event happens, the
  439. * corresponding timeout function will be invoked.
  440. *
  441. * @note this function shall be invoked in operating system timer interrupt.
  442. */
  443. void rt_timer_check(void)
  444. {
  445. struct rt_timer *t;
  446. rt_tick_t current_tick;
  447. register rt_base_t level;
  448. rt_list_t list;
  449. rt_list_init(&list);
  450. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  451. current_tick = rt_tick_get();
  452. /* disable interrupt */
  453. level = rt_hw_interrupt_disable();
  454. while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  455. {
  456. t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  457. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  458. /*
  459. * It supposes that the new tick shall less than the half duration of
  460. * tick max.
  461. */
  462. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  463. {
  464. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  465. /* remove timer from timer list firstly */
  466. _rt_timer_remove(t);
  467. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  468. {
  469. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  470. }
  471. /* add timer to temporary list */
  472. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  473. /* call timeout function */
  474. t->timeout_func(t->parameter);
  475. /* re-get tick */
  476. current_tick = rt_tick_get();
  477. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  478. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  479. /* Check whether the timer object is detached or started again */
  480. if (rt_list_isempty(&list))
  481. {
  482. continue;
  483. }
  484. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  485. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  486. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  487. {
  488. /* start it */
  489. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  490. rt_timer_start(t);
  491. }
  492. }
  493. else break;
  494. }
  495. /* enable interrupt */
  496. rt_hw_interrupt_enable(level);
  497. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  498. }
  499. /**
  500. * This function will return the next timeout tick in the system.
  501. *
  502. * @return the next timeout tick in the system
  503. */
  504. rt_tick_t rt_timer_next_timeout_tick(void)
  505. {
  506. return rt_timer_list_next_timeout(rt_timer_list);
  507. }
  508. #ifdef RT_USING_TIMER_SOFT
  509. /**
  510. * This function will check software-timer list, if a timeout event happens, the
  511. * corresponding timeout function will be invoked.
  512. */
  513. void rt_soft_timer_check(void)
  514. {
  515. rt_tick_t current_tick;
  516. struct rt_timer *t;
  517. register rt_base_t level;
  518. rt_list_t list;
  519. rt_list_init(&list);
  520. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  521. /* disable interrupt */
  522. level = rt_hw_interrupt_disable();
  523. while (!rt_list_isempty(&rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  524. {
  525. t = rt_list_entry(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  526. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  527. current_tick = rt_tick_get();
  528. /*
  529. * It supposes that the new tick shall less than the half duration of
  530. * tick max.
  531. */
  532. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  533. {
  534. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  535. /* remove timer from timer list firstly */
  536. _rt_timer_remove(t);
  537. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  538. {
  539. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  540. }
  541. /* add timer to temporary list */
  542. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  543. soft_timer_status = RT_SOFT_TIMER_BUSY;
  544. /* enable interrupt */
  545. rt_hw_interrupt_enable(level);
  546. /* call timeout function */
  547. t->timeout_func(t->parameter);
  548. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  549. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  550. /* disable interrupt */
  551. level = rt_hw_interrupt_disable();
  552. soft_timer_status = RT_SOFT_TIMER_IDLE;
  553. /* Check whether the timer object is detached or started again */
  554. if (rt_list_isempty(&list))
  555. {
  556. continue;
  557. }
  558. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  559. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  560. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  561. {
  562. /* start it */
  563. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  564. rt_timer_start(t);
  565. }
  566. }
  567. else break; /* not check anymore */
  568. }
  569. /* enable interrupt */
  570. rt_hw_interrupt_enable(level);
  571. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  572. }
  573. /* system timer thread entry */
  574. static void rt_thread_timer_entry(void *parameter)
  575. {
  576. rt_tick_t next_timeout;
  577. while (1)
  578. {
  579. /* get the next timeout tick */
  580. next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
  581. if (next_timeout == RT_TICK_MAX)
  582. {
  583. /* no software timer exist, suspend self. */
  584. rt_thread_suspend(rt_thread_self());
  585. rt_schedule();
  586. }
  587. else
  588. {
  589. rt_tick_t current_tick;
  590. /* get current tick */
  591. current_tick = rt_tick_get();
  592. if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
  593. {
  594. /* get the delta timeout tick */
  595. next_timeout = next_timeout - current_tick;
  596. rt_thread_delay(next_timeout);
  597. }
  598. }
  599. /* check software timer */
  600. rt_soft_timer_check();
  601. }
  602. }
  603. #endif /* RT_USING_TIMER_SOFT */
  604. /**
  605. * @ingroup SystemInit
  606. *
  607. * This function will initialize system timer
  608. */
  609. void rt_system_timer_init(void)
  610. {
  611. int i;
  612. for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
  613. {
  614. rt_list_init(rt_timer_list + i);
  615. }
  616. }
  617. /**
  618. * @ingroup SystemInit
  619. *
  620. * This function will initialize system timer thread
  621. */
  622. void rt_system_timer_thread_init(void)
  623. {
  624. #ifdef RT_USING_TIMER_SOFT
  625. int i;
  626. for (i = 0;
  627. i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
  628. i++)
  629. {
  630. rt_list_init(rt_soft_timer_list + i);
  631. }
  632. /* start software timer thread */
  633. rt_thread_init(&timer_thread,
  634. "timer",
  635. rt_thread_timer_entry,
  636. RT_NULL,
  637. &timer_thread_stack[0],
  638. sizeof(timer_thread_stack),
  639. RT_TIMER_THREAD_PRIO,
  640. 10);
  641. /* startup */
  642. rt_thread_startup(&timer_thread);
  643. #endif /* RT_USING_TIMER_SOFT */
  644. }
  645. /**@}*/