timer.c 24 KB

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