timer.c 23 KB

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