clock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2006-2021, 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-05-27 Bernard add support for same priority thread schedule
  10. * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
  11. * 2010-03-08 Bernard remove rt_passed_second
  12. * 2010-05-20 Bernard fix the tick exceeds the maximum limits
  13. * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
  14. * 2011-06-26 Bernard add rt_tick_set function.
  15. * 2018-11-22 Jesven add per cpu tick
  16. * 2020-12-29 Meco Man implement rt_tick_get_millisecond()
  17. * 2021-06-01 Meco Man add critical section projection for rt_tick_increase()
  18. * 2023-10-16 RiceChen fix: only the main core detection rt_timer_check(), in SMP mode
  19. */
  20. #include <rthw.h>
  21. #include <rtthread.h>
  22. #ifdef RT_USING_SMP
  23. #define rt_tick rt_cpu_index(0)->tick
  24. #else
  25. static volatile rt_tick_t rt_tick = 0;
  26. #endif /* RT_USING_SMP */
  27. #ifndef __on_rt_tick_hook
  28. #define __on_rt_tick_hook() __ON_HOOK_ARGS(rt_tick_hook, ())
  29. #endif
  30. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  31. static void (*rt_tick_hook)(void);
  32. /**
  33. * @addtogroup Hook
  34. */
  35. /**@{*/
  36. /**
  37. * @brief This function will set a hook function, which will be invoked when tick increase
  38. *
  39. *
  40. * @param hook the hook function
  41. */
  42. void rt_tick_sethook(void (*hook)(void))
  43. {
  44. rt_tick_hook = hook;
  45. }
  46. /**@}*/
  47. #endif /* RT_USING_HOOK */
  48. /**
  49. * @addtogroup Clock
  50. */
  51. /**@{*/
  52. /**
  53. * @brief This function will return current tick from operating system startup.
  54. *
  55. * @return Return current tick.
  56. */
  57. rt_tick_t rt_tick_get(void)
  58. {
  59. /* return the global tick */
  60. return rt_tick;
  61. }
  62. RTM_EXPORT(rt_tick_get);
  63. /**
  64. * @brief This function will set current tick.
  65. *
  66. * @param tick is the value that you will set.
  67. */
  68. void rt_tick_set(rt_tick_t tick)
  69. {
  70. rt_base_t level;
  71. level = rt_hw_interrupt_disable();
  72. rt_tick = tick;
  73. rt_hw_interrupt_enable(level);
  74. }
  75. /**
  76. * @brief This function will notify kernel there is one tick passed.
  77. * Normally, this function is invoked by clock ISR.
  78. */
  79. void rt_tick_increase(void)
  80. {
  81. struct rt_thread *thread;
  82. rt_base_t level;
  83. RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
  84. level = rt_hw_interrupt_disable();
  85. /* increase the global tick */
  86. #ifdef RT_USING_SMP
  87. rt_cpu_self()->tick ++;
  88. #else
  89. ++ rt_tick;
  90. #endif /* RT_USING_SMP */
  91. /* check time slice */
  92. thread = rt_thread_self();
  93. -- thread->remaining_tick;
  94. if (thread->remaining_tick == 0)
  95. {
  96. /* change to initialized tick */
  97. thread->remaining_tick = thread->init_tick;
  98. thread->stat |= RT_THREAD_STAT_YIELD;
  99. rt_hw_interrupt_enable(level);
  100. rt_schedule();
  101. }
  102. else
  103. {
  104. rt_hw_interrupt_enable(level);
  105. }
  106. /* check timer */
  107. #ifdef RT_USING_SMP
  108. if (rt_hw_cpu_id() != 0)
  109. {
  110. return;
  111. }
  112. #endif
  113. rt_timer_check();
  114. }
  115. /**
  116. * @brief This function will calculate the tick from millisecond.
  117. *
  118. * @param ms is the specified millisecond.
  119. * - Negative Number wait forever
  120. * - Zero not wait
  121. * - Max 0x7fffffff
  122. *
  123. * @return Return the calculated tick.
  124. */
  125. rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
  126. {
  127. rt_tick_t tick;
  128. if (ms < 0)
  129. {
  130. tick = (rt_tick_t)RT_WAITING_FOREVER;
  131. }
  132. else
  133. {
  134. tick = RT_TICK_PER_SECOND * (ms / 1000);
  135. tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
  136. }
  137. /* return the calculated tick */
  138. return tick;
  139. }
  140. RTM_EXPORT(rt_tick_from_millisecond);
  141. /**
  142. * @brief This function will return the passed millisecond from boot.
  143. *
  144. * @note if the value of RT_TICK_PER_SECOND is lower than 1000 or
  145. * is not an integral multiple of 1000, this function will not
  146. * provide the correct 1ms-based tick.
  147. *
  148. * @return Return passed millisecond from boot.
  149. */
  150. rt_weak rt_tick_t rt_tick_get_millisecond(void)
  151. {
  152. #if 1000 % RT_TICK_PER_SECOND == 0u
  153. return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
  154. #else
  155. #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
  156. please redefine this function in another file by using a high-precision hard-timer."
  157. return 0;
  158. #endif /* 1000 % RT_TICK_PER_SECOND == 0u */
  159. }
  160. /**@}*/