timer.c 19 KB

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