timer.c 23 KB

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