timer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 the ticks of timer
  216. *
  217. * @param flag is the flag of timer
  218. */
  219. void rt_timer_init(rt_timer_t timer,
  220. const char *name,
  221. void (*timeout)(void *parameter),
  222. void *parameter,
  223. rt_tick_t time,
  224. rt_uint8_t flag)
  225. {
  226. /* timer check */
  227. RT_ASSERT(timer != RT_NULL);
  228. /* timer object initialization */
  229. rt_object_init(&(timer->parent), RT_Object_Class_Timer, name);
  230. _timer_init(timer, timeout, parameter, time, flag);
  231. }
  232. RTM_EXPORT(rt_timer_init);
  233. /**
  234. * @brief This function will detach a timer from timer management.
  235. *
  236. * @param timer is the timer to be detached
  237. *
  238. * @return the status of detach
  239. */
  240. rt_err_t rt_timer_detach(rt_timer_t timer)
  241. {
  242. register rt_base_t level;
  243. /* timer check */
  244. RT_ASSERT(timer != RT_NULL);
  245. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  246. RT_ASSERT(rt_object_is_systemobject(&timer->parent));
  247. /* disable interrupt */
  248. level = rt_hw_interrupt_disable();
  249. _timer_remove(timer);
  250. /* stop timer */
  251. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  252. /* enable interrupt */
  253. rt_hw_interrupt_enable(level);
  254. rt_object_detach(&(timer->parent));
  255. return RT_EOK;
  256. }
  257. RTM_EXPORT(rt_timer_detach);
  258. #ifdef RT_USING_HEAP
  259. /**
  260. * @brief This function will create a timer
  261. *
  262. * @param name the name of timer
  263. *
  264. * @param timeout the timeout function
  265. *
  266. * @param parameter the parameter of timeout function
  267. *
  268. * @param time the tick of timer
  269. *
  270. * @param flag the flag of timer
  271. *
  272. * @return the created timer object
  273. */
  274. rt_timer_t rt_timer_create(const char *name,
  275. void (*timeout)(void *parameter),
  276. void *parameter,
  277. rt_tick_t time,
  278. rt_uint8_t flag)
  279. {
  280. struct rt_timer *timer;
  281. /* allocate a object */
  282. timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
  283. if (timer == RT_NULL)
  284. {
  285. return RT_NULL;
  286. }
  287. _timer_init(timer, timeout, parameter, time, flag);
  288. return timer;
  289. }
  290. RTM_EXPORT(rt_timer_create);
  291. /**
  292. * @brief This function will delete a timer and release timer memory
  293. *
  294. * @param timer the timer to be deleted
  295. *
  296. * @return the operation status, RT_EOK on OK; RT_ERROR on error
  297. */
  298. rt_err_t rt_timer_delete(rt_timer_t timer)
  299. {
  300. register rt_base_t level;
  301. /* timer check */
  302. RT_ASSERT(timer != RT_NULL);
  303. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  304. RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
  305. /* disable interrupt */
  306. level = rt_hw_interrupt_disable();
  307. _timer_remove(timer);
  308. /* stop timer */
  309. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  310. /* enable interrupt */
  311. rt_hw_interrupt_enable(level);
  312. rt_object_delete(&(timer->parent));
  313. return RT_EOK;
  314. }
  315. RTM_EXPORT(rt_timer_delete);
  316. #endif /* RT_USING_HEAP */
  317. /**
  318. * @brief This function will start the timer
  319. *
  320. * @param timer the timer to be started
  321. *
  322. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  323. */
  324. rt_err_t rt_timer_start(rt_timer_t timer)
  325. {
  326. unsigned int row_lvl;
  327. rt_list_t *timer_list;
  328. register rt_base_t level;
  329. register rt_bool_t need_schedule;
  330. rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
  331. unsigned int tst_nr;
  332. static unsigned int random_nr;
  333. /* timer check */
  334. RT_ASSERT(timer != RT_NULL);
  335. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  336. need_schedule = RT_FALSE;
  337. /* stop timer firstly */
  338. level = rt_hw_interrupt_disable();
  339. /* remove timer from list */
  340. _timer_remove(timer);
  341. /* change status of timer */
  342. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  343. RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
  344. /*
  345. * get timeout tick,
  346. * the max timeout tick shall not great than RT_TICK_MAX/2
  347. */
  348. RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
  349. timer->timeout_tick = rt_tick_get() + timer->init_tick;
  350. #ifdef RT_USING_TIMER_SOFT
  351. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  352. {
  353. /* insert timer to soft timer list */
  354. timer_list = _soft_timer_list;
  355. }
  356. else
  357. #endif /* RT_USING_TIMER_SOFT */
  358. {
  359. /* insert timer to system timer list */
  360. timer_list = _timer_list;
  361. }
  362. row_head[0] = &timer_list[0];
  363. for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  364. {
  365. for (; row_head[row_lvl] != timer_list[row_lvl].prev;
  366. row_head[row_lvl] = row_head[row_lvl]->next)
  367. {
  368. struct rt_timer *t;
  369. rt_list_t *p = row_head[row_lvl]->next;
  370. /* fix up the entry pointer */
  371. t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
  372. /* If we have two timers that timeout at the same time, it's
  373. * preferred that the timer inserted early get called early.
  374. * So insert the new timer to the end the the some-timeout timer
  375. * list.
  376. */
  377. if ((t->timeout_tick - timer->timeout_tick) == 0)
  378. {
  379. continue;
  380. }
  381. else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
  382. {
  383. break;
  384. }
  385. }
  386. if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
  387. row_head[row_lvl + 1] = row_head[row_lvl] + 1;
  388. }
  389. /* Interestingly, this super simple timer insert counter works very very
  390. * well on distributing the list height uniformly. By means of "very very
  391. * well", I mean it beats the randomness of timer->timeout_tick very easily
  392. * (actually, the timeout_tick is not random and easy to be attacked). */
  393. random_nr++;
  394. tst_nr = random_nr;
  395. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
  396. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  397. for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
  398. {
  399. if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
  400. rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
  401. &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
  402. else
  403. break;
  404. /* Shift over the bits we have tested. Works well with 1 bit and 2
  405. * bits. */
  406. tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
  407. }
  408. timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
  409. #ifdef RT_USING_TIMER_SOFT
  410. if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
  411. {
  412. /* check whether timer thread is ready */
  413. if ((_soft_timer_status == RT_SOFT_TIMER_IDLE) &&
  414. ((_timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
  415. {
  416. /* resume timer thread to check soft timer */
  417. rt_thread_resume(&_timer_thread);
  418. need_schedule = RT_TRUE;
  419. }
  420. }
  421. #endif /* RT_USING_TIMER_SOFT */
  422. /* enable interrupt */
  423. rt_hw_interrupt_enable(level);
  424. if (need_schedule)
  425. {
  426. rt_schedule();
  427. }
  428. return RT_EOK;
  429. }
  430. RTM_EXPORT(rt_timer_start);
  431. /**
  432. * @brief This function will stop the timer
  433. *
  434. * @param timer the timer to be stopped
  435. *
  436. * @return the operation status, RT_EOK on OK, -RT_ERROR on error
  437. */
  438. rt_err_t rt_timer_stop(rt_timer_t timer)
  439. {
  440. register rt_base_t level;
  441. /* timer check */
  442. RT_ASSERT(timer != RT_NULL);
  443. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  444. if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  445. return -RT_ERROR;
  446. RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
  447. /* disable interrupt */
  448. level = rt_hw_interrupt_disable();
  449. _timer_remove(timer);
  450. /* change status */
  451. timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  452. /* enable interrupt */
  453. rt_hw_interrupt_enable(level);
  454. return RT_EOK;
  455. }
  456. RTM_EXPORT(rt_timer_stop);
  457. /**
  458. * @brief This function will get or set some options of the timer
  459. *
  460. * @param timer the timer to be get or set
  461. * @param cmd the control command
  462. * @param arg the argument
  463. *
  464. * @return the statu of control
  465. */
  466. rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
  467. {
  468. register rt_base_t level;
  469. /* timer check */
  470. RT_ASSERT(timer != RT_NULL);
  471. RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
  472. level = rt_hw_interrupt_disable();
  473. switch (cmd)
  474. {
  475. case RT_TIMER_CTRL_GET_TIME:
  476. *(rt_tick_t *)arg = timer->init_tick;
  477. break;
  478. case RT_TIMER_CTRL_SET_TIME:
  479. timer->init_tick = *(rt_tick_t *)arg;
  480. break;
  481. case RT_TIMER_CTRL_SET_ONESHOT:
  482. timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
  483. break;
  484. case RT_TIMER_CTRL_SET_PERIODIC:
  485. timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
  486. break;
  487. case RT_TIMER_CTRL_GET_STATE:
  488. if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  489. {
  490. /*timer is start and run*/
  491. *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
  492. }
  493. else
  494. {
  495. /*timer is stop*/
  496. *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
  497. }
  498. break;
  499. default:
  500. break;
  501. }
  502. rt_hw_interrupt_enable(level);
  503. return RT_EOK;
  504. }
  505. RTM_EXPORT(rt_timer_control);
  506. /**
  507. * @brief This function will check timer list, if a timeout event happens,
  508. * the corresponding timeout function will be invoked.
  509. *
  510. * @note This function shall be invoked in operating system timer interrupt.
  511. */
  512. void rt_timer_check(void)
  513. {
  514. struct rt_timer *t;
  515. rt_tick_t current_tick;
  516. register rt_base_t level;
  517. rt_list_t list;
  518. rt_list_init(&list);
  519. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
  520. current_tick = rt_tick_get();
  521. /* disable interrupt */
  522. level = rt_hw_interrupt_disable();
  523. while (!rt_list_isempty(&_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  524. {
  525. t = rt_list_entry(_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  526. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  527. /*
  528. * It supposes that the new tick shall less than the half duration of
  529. * tick max.
  530. */
  531. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  532. {
  533. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  534. /* remove timer from timer list firstly */
  535. _timer_remove(t);
  536. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  537. {
  538. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  539. }
  540. /* add timer to temporary list */
  541. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  542. /* call timeout function */
  543. t->timeout_func(t->parameter);
  544. /* re-get tick */
  545. current_tick = rt_tick_get();
  546. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  547. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  548. /* Check whether the timer object is detached or started again */
  549. if (rt_list_isempty(&list))
  550. {
  551. continue;
  552. }
  553. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  554. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  555. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  556. {
  557. /* start it */
  558. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  559. rt_timer_start(t);
  560. }
  561. }
  562. else break;
  563. }
  564. /* enable interrupt */
  565. rt_hw_interrupt_enable(level);
  566. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
  567. }
  568. /**
  569. * @brief This function will return the next timeout tick in the system.
  570. *
  571. * @return the next timeout tick in the system
  572. */
  573. rt_tick_t rt_timer_next_timeout_tick(void)
  574. {
  575. return _timer_list_next_timeout(_timer_list);
  576. }
  577. #ifdef RT_USING_TIMER_SOFT
  578. /**
  579. * @brief This function will check software-timer list, if a timeout event happens, the
  580. * corresponding timeout function will be invoked.
  581. */
  582. void rt_soft_timer_check(void)
  583. {
  584. rt_tick_t current_tick;
  585. struct rt_timer *t;
  586. register rt_base_t level;
  587. rt_list_t list;
  588. rt_list_init(&list);
  589. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
  590. /* disable interrupt */
  591. level = rt_hw_interrupt_disable();
  592. while (!rt_list_isempty(&_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
  593. {
  594. t = rt_list_entry(_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
  595. struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
  596. current_tick = rt_tick_get();
  597. /*
  598. * It supposes that the new tick shall less than the half duration of
  599. * tick max.
  600. */
  601. if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
  602. {
  603. RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
  604. /* remove timer from timer list firstly */
  605. _timer_remove(t);
  606. if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
  607. {
  608. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  609. }
  610. /* add timer to temporary list */
  611. rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  612. _soft_timer_status = RT_SOFT_TIMER_BUSY;
  613. /* enable interrupt */
  614. rt_hw_interrupt_enable(level);
  615. /* call timeout function */
  616. t->timeout_func(t->parameter);
  617. RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
  618. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
  619. /* disable interrupt */
  620. level = rt_hw_interrupt_disable();
  621. _soft_timer_status = RT_SOFT_TIMER_IDLE;
  622. /* Check whether the timer object is detached or started again */
  623. if (rt_list_isempty(&list))
  624. {
  625. continue;
  626. }
  627. rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
  628. if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
  629. (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
  630. {
  631. /* start it */
  632. t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
  633. rt_timer_start(t);
  634. }
  635. }
  636. else break; /* not check anymore */
  637. }
  638. /* enable interrupt */
  639. rt_hw_interrupt_enable(level);
  640. RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
  641. }
  642. /**
  643. * @brief System timer thread entry
  644. *
  645. * @param parameter is the arg of the thread
  646. */
  647. static void _timer_thread_entry(void *parameter)
  648. {
  649. rt_tick_t next_timeout;
  650. while (1)
  651. {
  652. /* get the next timeout tick */
  653. next_timeout = _timer_list_next_timeout(_soft_timer_list);
  654. if (next_timeout == RT_TICK_MAX)
  655. {
  656. /* no software timer exist, suspend self. */
  657. rt_thread_suspend(rt_thread_self());
  658. rt_schedule();
  659. }
  660. else
  661. {
  662. rt_tick_t current_tick;
  663. /* get current tick */
  664. current_tick = rt_tick_get();
  665. if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
  666. {
  667. /* get the delta timeout tick */
  668. next_timeout = next_timeout - current_tick;
  669. rt_thread_delay(next_timeout);
  670. }
  671. }
  672. /* check software timer */
  673. rt_soft_timer_check();
  674. }
  675. }
  676. #endif /* RT_USING_TIMER_SOFT */
  677. /**
  678. * @ingroup SystemInit
  679. *
  680. * @brief This function will initialize system timer
  681. */
  682. void rt_system_timer_init(void)
  683. {
  684. int i;
  685. for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
  686. {
  687. rt_list_init(_timer_list + i);
  688. }
  689. }
  690. /**
  691. * @ingroup SystemInit
  692. *
  693. * @brief This function will initialize system timer thread
  694. */
  695. void rt_system_timer_thread_init(void)
  696. {
  697. #ifdef RT_USING_TIMER_SOFT
  698. int i;
  699. for (i = 0;
  700. i < sizeof(_soft_timer_list) / sizeof(_soft_timer_list[0]);
  701. i++)
  702. {
  703. rt_list_init(_soft_timer_list + i);
  704. }
  705. /* start software timer thread */
  706. rt_thread_init(&_timer_thread,
  707. "timer",
  708. _timer_thread_entry,
  709. RT_NULL,
  710. &_timer_thread_stack[0],
  711. sizeof(_timer_thread_stack),
  712. RT_TIMER_THREAD_PRIO,
  713. 10);
  714. /* startup */
  715. rt_thread_startup(&_timer_thread);
  716. #endif /* RT_USING_TIMER_SOFT */
  717. }
  718. /**@}*/