timer.c 21 KB

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