timer.c 25 KB

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