timer.c 23 KB

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