timer.c 19 KB

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