timer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (c) 2006-2018, 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. 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((rt_object_t)timer);
  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((rt_object_t)timer);
  250. return RT_EOK;
  251. }
  252. RTM_EXPORT(rt_timer_delete);
  253. #endif
  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_hw_interrupt_enable(level);
  279. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  280. /*
  281. * get timeout tick,
  282. * the max timeout tick shall not great than RT_TICK_MAX/2
  283. */
  284. RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
  285. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  286. /* disable interrupt */
  287. level = rt_hw_interrupt_disable();
  288. #ifdef RT_USING_TIMER_SOFT
  289. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  290. {
  291. /* insert timer to soft timer list */
  292. timer_list = rt_soft_timer_list;
  293. }
  294. else
  295. #endif
  296. {
  297. /* insert timer to system timer list */
  298. timer_list = rt_timer_list;
  299. }
  300. row_head[0] = &timer_list[0];
  301. for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  302. {
  303. for (; row_head[row_lvl] != timer_list[row_lvl].prev;
  304. row_head[row_lvl] = row_head[row_lvl]->next)
  305. {
  306. struct rt_timer *t;
  307. rt_list_t *p = row_head[row_lvl]->next;
  308. /* fix up the entry pointer */
  309. t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
  310. /* If we have two timers that timeout at the same time, it's
  311. * preferred that the timer inserted early get called early.
  312. * So insert the new timer to the end the the some-timeout timer
  313. * list.
  314. */
  315. if ((t->timeout_tick - timer->timeout_tick) == 0)
  316. {
  317. continue;
  318. }
  319. else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
  320. {
  321. break;
  322. }
  323. }
  324. if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
  325. row_head[row_lvl + 1] = row_head[row_lvl] + 1;
  326. }
  327. /* Interestingly, this super simple timer insert counter works very very
  328. * well on distributing the list height uniformly. By means of "very very
  329. * well", I mean it beats the randomness of timer->timeout_tick very easily
  330. * (actually, the timeout_tick is not random and easy to be attacked). */
  331. random_nr++;
  332. tst_nr = random_nr;
  333. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
  334. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  335. for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  336. {
  337. if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
  338. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
  339. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
  340. else
  341. break;
  342. /* Shift over the bits we have tested. Works well with 1 bit and 2
  343. * bits. */
  344. tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
  345. }
  346. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  347. /* enable interrupt */
  348. rt_hw_interrupt_enable(level);
  349. #ifdef RT_USING_TIMER_SOFT
  350. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  351. {
  352. /* check whether timer thread is ready */
  353. if ((soft_timer_status == RT_SOFT_TIMER_IDLE) &&
  354. ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  355. {
  356. /* resume timer thread to check soft timer */
  357. rt_thread_resume(&timer_thread);
  358. rt_schedule();
  359. }
  360. }
  361. #endif
  362. return RT_EOK;
  363. }
  364. RTM_EXPORT(rt_timer_start);
  365. /**
  366. * This function will stop the timer
  367. *
  368. * @param timer the timer to be stopped
  369. *
  370. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  371. */
  372. rt_err_t rt_timer_stop(rt_timer_t timer)
  373. {
  374. register rt_base_t level;
  375. /* timer check */
  376. RT_ASSERT(timer != RT_NULL);
  377. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  378. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  379. return -RT_ERROR;
  380. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  381. /* disable interrupt */
  382. level = rt_hw_interrupt_disable();
  383. _rt_timer_remove(timer);
  384. /* change status */
  385. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  386. /* enable interrupt */
  387. rt_hw_interrupt_enable(level);
  388. return RT_EOK;
  389. }
  390. RTM_EXPORT(rt_timer_stop);
  391. /**
  392. * This function will get or set some options of the timer
  393. *
  394. * @param timer the timer to be get or set
  395. * @param cmd the control command
  396. * @param arg the argument
  397. *
  398. * @return RT_EOK
  399. */
  400. rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
  401. {
  402. register rt_base_t level;
  403. /* timer check */
  404. RT_ASSERT(timer != RT_NULL);
  405. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  406. level = rt_hw_interrupt_disable();
  407. switch (cmd)
  408. {
  409. case RT_TIMER_CTRL_GET_TIME:
  410. *(rt_tick_t *)arg = timer->init_tick;
  411. break;
  412. case RT_TIMER_CTRL_SET_TIME:
  413. timer->init_tick = *(rt_tick_t *)arg;
  414. break;
  415. case RT_TIMER_CTRL_SET_ONESHOT:
  416. timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
  417. break;
  418. case RT_TIMER_CTRL_SET_PERIODIC:
  419. timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
  420. break;
  421. case RT_TIMER_CTRL_GET_STATE:
  422. if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  423. {
  424. /*timer is start and run*/
  425. *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;
  426. }
  427. else
  428. {
  429. /*timer is stop*/
  430. *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
  431. }
  432. break;
  433. default:
  434. break;
  435. }
  436. rt_hw_interrupt_enable(level);
  437. return RT_EOK;
  438. }
  439. RTM_EXPORT(rt_timer_control);
  440. /**
  441. * This function will check timer list, if a timeout event happens, the
  442. * corresponding timeout function will be invoked.
  443. *
  444. * @note this function shall be invoked in operating system timer interrupt.
  445. */
  446. void rt_timer_check(void)
  447. {
  448. struct rt_timer *t;
  449. rt_tick_t current_tick;
  450. register rt_base_t level;
  451. rt_list_t list = RT_LIST_OBJECT_INIT(list);
  452. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  453. current_tick = rt_tick_get();
  454. /* disable interrupt */
  455. level = rt_hw_interrupt_disable();
  456. while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  457. {
  458. t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  459. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  460. /*
  461. * It supposes that the new tick shall less than the half duration of
  462. * tick max.
  463. */
  464. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  465. {
  466. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  467. /* remove timer from timer list firstly */
  468. _rt_timer_remove(t);
  469. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  470. {
  471. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  472. }
  473. /* add timer to temporary list */
  474. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  475. /* call timeout function */
  476. t->timeout_func(t->parameter);
  477. /* re-get tick */
  478. current_tick = rt_tick_get();
  479. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  480. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  481. /* Check whether the timer object is detached or started again */
  482. if (rt_list_isempty(&list))
  483. {
  484. continue;
  485. }
  486. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  487. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  488. {
  489. /* start it */
  490. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  491. rt_timer_start(t);
  492. }
  493. }
  494. else break;
  495. }
  496. /* enable interrupt */
  497. rt_hw_interrupt_enable(level);
  498. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  499. }
  500. /**
  501. * This function will return the next timeout tick in the system.
  502. *
  503. * @return the next timeout tick in the system
  504. */
  505. rt_tick_t rt_timer_next_timeout_tick(void)
  506. {
  507. return rt_timer_list_next_timeout(rt_timer_list);
  508. }
  509. #ifdef RT_USING_TIMER_SOFT
  510. /**
  511. * This function will check software-timer list, if a timeout event happens, the
  512. * corresponding timeout function will be invoked.
  513. */
  514. void rt_soft_timer_check(void)
  515. {
  516. rt_tick_t current_tick;
  517. struct rt_timer *t;
  518. register rt_base_t level;
  519. rt_list_t list = RT_LIST_OBJECT_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. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  559. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  560. {
  561. /* start it */
  562. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  563. rt_timer_start(t);
  564. }
  565. }
  566. else break; /* not check anymore */
  567. }
  568. /* enable interrupt */
  569. rt_hw_interrupt_enable(level);
  570. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  571. }
  572. /* system timer thread entry */
  573. static void rt_thread_timer_entry(void *parameter)
  574. {
  575. rt_tick_t next_timeout;
  576. while (1)
  577. {
  578. /* get the next timeout tick */
  579. next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
  580. if (next_timeout == RT_TICK_MAX)
  581. {
  582. /* no software timer exist, suspend self. */
  583. rt_thread_suspend(rt_thread_self());
  584. rt_schedule();
  585. }
  586. else
  587. {
  588. rt_tick_t current_tick;
  589. /* get current tick */
  590. current_tick = rt_tick_get();
  591. if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
  592. {
  593. /* get the delta timeout tick */
  594. next_timeout = next_timeout - current_tick;
  595. rt_thread_delay(next_timeout);
  596. }
  597. }
  598. /* check software timer */
  599. rt_soft_timer_check();
  600. }
  601. }
  602. #endif
  603. /**
  604. * @ingroup SystemInit
  605. *
  606. * This function will initialize system timer
  607. */
  608. void rt_system_timer_init(void)
  609. {
  610. int i;
  611. for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
  612. {
  613. rt_list_init(rt_timer_list + i);
  614. }
  615. }
  616. /**
  617. * @ingroup SystemInit
  618. *
  619. * This function will initialize system timer thread
  620. */
  621. void rt_system_timer_thread_init(void)
  622. {
  623. #ifdef RT_USING_TIMER_SOFT
  624. int i;
  625. for (i = 0;
  626. i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
  627. i++)
  628. {
  629. rt_list_init(rt_soft_timer_list + i);
  630. }
  631. /* start software timer thread */
  632. rt_thread_init(&timer_thread,
  633. "timer",
  634. rt_thread_timer_entry,
  635. RT_NULL,
  636. &timer_thread_stack[0],
  637. sizeof(timer_thread_stack),
  638. RT_TIMER_THREAD_PRIO,
  639. 10);
  640. /* startup */
  641. rt_thread_startup(&timer_thread);
  642. #endif
  643. }
  644. /**@}*/