timer.c 19 KB

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