timer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. }
  411. return RT_EOK;
  412. }
  413. RTM_EXPORT(rt_timer_control);
  414. /**
  415. * This function will check timer list, if a timeout event happens, the
  416. * corresponding timeout function will be invoked.
  417. *
  418. * @note this function shall be invoked in operating system timer interrupt.
  419. */
  420. void rt_timer_check(void)
  421. {
  422. struct rt_timer *t;
  423. rt_tick_t current_tick;
  424. register rt_base_t level;
  425. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  426. current_tick = rt_tick_get();
  427. /* disable interrupt */
  428. level = rt_hw_interrupt_disable();
  429. while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  430. {
  431. t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  432. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  433. /*
  434. * It supposes that the new tick shall less than the half duration of
  435. * tick max.
  436. */
  437. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  438. {
  439. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  440. /* remove timer from timer list firstly */
  441. _rt_timer_remove(t);
  442. /* call timeout function */
  443. t->timeout_func(t->parameter);
  444. /* re-get tick */
  445. current_tick = rt_tick_get();
  446. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  447. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  448. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  449. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  450. {
  451. /* start it */
  452. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  453. rt_timer_start(t);
  454. }
  455. else
  456. {
  457. /* stop timer */
  458. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  459. }
  460. }
  461. else
  462. break;
  463. }
  464. /* enable interrupt */
  465. rt_hw_interrupt_enable(level);
  466. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  467. }
  468. /**
  469. * This function will return the next timeout tick in the system.
  470. *
  471. * @return the next timeout tick in the system
  472. */
  473. rt_tick_t rt_timer_next_timeout_tick(void)
  474. {
  475. return rt_timer_list_next_timeout(rt_timer_list);
  476. }
  477. #ifdef RT_USING_TIMER_SOFT
  478. /**
  479. * This function will check timer list, if a timeout event happens, the
  480. * corresponding timeout function will be invoked.
  481. */
  482. void rt_soft_timer_check(void)
  483. {
  484. rt_tick_t current_tick;
  485. rt_list_t *n;
  486. struct rt_timer *t;
  487. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  488. current_tick = rt_tick_get();
  489. /* lock scheduler */
  490. rt_enter_critical();
  491. for (n = rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
  492. n != &(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]);)
  493. {
  494. t = rt_list_entry(n, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  495. /*
  496. * It supposes that the new tick shall less than the half duration of
  497. * tick max.
  498. */
  499. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  500. {
  501. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  502. /* move node to the next */
  503. n = n->next;
  504. /* remove timer from timer list firstly */
  505. _rt_timer_remove(t);
  506. /* not lock scheduler when performing timeout function */
  507. rt_exit_critical();
  508. /* call timeout function */
  509. t->timeout_func(t->parameter);
  510. /* re-get tick */
  511. current_tick = rt_tick_get();
  512. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  513. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  514. /* lock scheduler */
  515. rt_enter_critical();
  516. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  517. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  518. {
  519. /* start it */
  520. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  521. rt_timer_start(t);
  522. }
  523. else
  524. {
  525. /* stop timer */
  526. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  527. }
  528. }
  529. else break; /* not check anymore */
  530. }
  531. /* unlock scheduler */
  532. rt_exit_critical();
  533. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  534. }
  535. /* system timer thread entry */
  536. static void rt_thread_timer_entry(void *parameter)
  537. {
  538. rt_tick_t next_timeout;
  539. while (1)
  540. {
  541. /* get the next timeout tick */
  542. next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
  543. if (next_timeout == RT_TICK_MAX)
  544. {
  545. /* no software timer exist, suspend self. */
  546. rt_thread_suspend(rt_thread_self());
  547. rt_schedule();
  548. }
  549. else
  550. {
  551. rt_tick_t current_tick;
  552. /* get current tick */
  553. current_tick = rt_tick_get();
  554. if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
  555. {
  556. /* get the delta timeout tick */
  557. next_timeout = next_timeout - current_tick;
  558. rt_thread_delay(next_timeout);
  559. }
  560. }
  561. /* check software timer */
  562. rt_soft_timer_check();
  563. }
  564. }
  565. #endif
  566. /**
  567. * @ingroup SystemInit
  568. *
  569. * This function will initialize system timer
  570. */
  571. void rt_system_timer_init(void)
  572. {
  573. int i;
  574. for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
  575. {
  576. rt_list_init(rt_timer_list + i);
  577. }
  578. }
  579. /**
  580. * @ingroup SystemInit
  581. *
  582. * This function will initialize system timer thread
  583. */
  584. void rt_system_timer_thread_init(void)
  585. {
  586. #ifdef RT_USING_TIMER_SOFT
  587. int i;
  588. for (i = 0;
  589. i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
  590. i++)
  591. {
  592. rt_list_init(rt_soft_timer_list + i);
  593. }
  594. /* start software timer thread */
  595. rt_thread_init(&timer_thread,
  596. "timer",
  597. rt_thread_timer_entry,
  598. RT_NULL,
  599. &timer_thread_stack[0],
  600. sizeof(timer_thread_stack),
  601. RT_TIMER_THREAD_PRIO,
  602. 10);
  603. /* startup */
  604. rt_thread_startup(&timer_thread);
  605. #endif
  606. }
  607. /**@}*/