timer.c 18 KB

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