timer.c 25 KB

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