clock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  19. * 2023-10-16 RiceChen fix: only the main core detection rt_timer_check(), in SMP mode
  20. */
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #include <rtatomic.h>
  24. #ifdef RT_USING_SMP
  25. #define rt_tick rt_cpu_index(0)->tick
  26. #else
  27. static volatile rt_atomic_t rt_tick = 0;
  28. #endif /* RT_USING_SMP */
  29. #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
  30. static void (*rt_tick_hook)(void);
  31. /**
  32. * @addtogroup Hook
  33. */
  34. /**@{*/
  35. /**
  36. * @brief This function will set a hook function, which will be invoked when tick increase
  37. *
  38. *
  39. * @param hook the hook function
  40. */
  41. void rt_tick_sethook(void (*hook)(void))
  42. {
  43. rt_tick_hook = hook;
  44. }
  45. /**@}*/
  46. #endif /* RT_USING_HOOK */
  47. /**
  48. * @addtogroup Clock
  49. */
  50. /**@{*/
  51. /**
  52. * @brief This function will return current tick from operating system startup.
  53. *
  54. * @return Return current tick.
  55. */
  56. rt_tick_t rt_tick_get(void)
  57. {
  58. /* return the global tick */
  59. return (rt_tick_t)rt_atomic_load(&(rt_tick));
  60. }
  61. RTM_EXPORT(rt_tick_get);
  62. /**
  63. * @brief This function will set current tick.
  64. *
  65. * @param tick is the value that you will set.
  66. */
  67. void rt_tick_set(rt_tick_t tick)
  68. {
  69. rt_atomic_store(&(rt_tick), tick);
  70. }
  71. /**
  72. * @brief This function will notify kernel there is one tick passed.
  73. * Normally, this function is invoked by clock ISR.
  74. */
  75. void rt_tick_increase(void)
  76. {
  77. struct rt_thread *thread;
  78. rt_base_t level;
  79. rt_atomic_t oldval = 0;
  80. RT_OBJECT_HOOK_CALL(rt_tick_hook, ());
  81. /* increase the global tick */
  82. #ifdef RT_USING_SMP
  83. rt_atomic_add(&(rt_cpu_self()->tick), 1);
  84. #else
  85. rt_atomic_add(&(rt_tick), 1);
  86. #endif /* RT_USING_SMP */
  87. /* check time slice */
  88. thread = rt_thread_self();
  89. rt_get_thread_struct(thread);
  90. level = rt_spin_lock_irqsave(&(thread->spinlock));
  91. rt_atomic_sub(&(thread->remaining_tick), 1);
  92. if (rt_atomic_compare_exchange_strong(&(thread->remaining_tick), &oldval, thread->init_tick))
  93. {
  94. thread->stat |= RT_THREAD_STAT_YIELD;
  95. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  96. rt_put_thread_struct(thread);
  97. rt_schedule();
  98. }
  99. else
  100. {
  101. rt_spin_unlock_irqrestore(&(thread->spinlock), level);
  102. rt_put_thread_struct(thread);
  103. }
  104. /* check timer */
  105. #ifdef RT_USING_SMP
  106. if (rt_hw_cpu_id() != 0)
  107. {
  108. return;
  109. }
  110. #endif
  111. rt_timer_check();
  112. }
  113. /**
  114. * @brief This function will calculate the tick from millisecond.
  115. *
  116. * @param ms is the specified millisecond.
  117. * - Negative Number wait forever
  118. * - Zero not wait
  119. * - Max 0x7fffffff
  120. *
  121. * @return Return the calculated tick.
  122. */
  123. rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
  124. {
  125. rt_tick_t tick;
  126. if (ms < 0)
  127. {
  128. tick = (rt_tick_t)RT_WAITING_FOREVER;
  129. }
  130. else
  131. {
  132. tick = RT_TICK_PER_SECOND * (ms / 1000);
  133. tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
  134. }
  135. /* return the calculated tick */
  136. return tick;
  137. }
  138. RTM_EXPORT(rt_tick_from_millisecond);
  139. /**
  140. * @brief This function will return the passed millisecond from boot.
  141. *
  142. * @note if the value of RT_TICK_PER_SECOND is lower than 1000 or
  143. * is not an integral multiple of 1000, this function will not
  144. * provide the correct 1ms-based tick.
  145. *
  146. * @return Return passed millisecond from boot.
  147. */
  148. rt_weak rt_tick_t rt_tick_get_millisecond(void)
  149. {
  150. #if RT_TICK_PER_SECOND == 0 // make cppcheck happy
  151. #error "RT_TICK_PER_SECOND must be greater than zero"
  152. #endif
  153. #if 1000 % RT_TICK_PER_SECOND == 0u
  154. return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
  155. #else
  156. #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
  157. please redefine this function in another file by using a high-precision hard-timer."
  158. return 0;
  159. #endif /* 1000 % RT_TICK_PER_SECOND == 0u */
  160. }
  161. /**@}*/