clock.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. */
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #ifdef RT_USING_SMP
  22. #define rt_tick rt_cpu_index(0)->tick
  23. #else
  24. static volatile rt_tick_t rt_tick = 0;
  25. #endif /* RT_USING_SMP */
  26. /**
  27. * @addtogroup Clock
  28. */
  29. /**@{*/
  30. /**
  31. * @brief This function will return current tick from operating system startup
  32. *
  33. * @return Return current tick
  34. */
  35. rt_tick_t rt_tick_get(void)
  36. {
  37. /* return the global tick */
  38. return rt_tick;
  39. }
  40. RTM_EXPORT(rt_tick_get);
  41. /**
  42. * @brief This function will set current tick
  43. *
  44. * @param tick is the value that you will set.
  45. */
  46. void rt_tick_set(rt_tick_t tick)
  47. {
  48. rt_base_t level;
  49. level = rt_hw_interrupt_disable();
  50. rt_tick = tick;
  51. rt_hw_interrupt_enable(level);
  52. }
  53. /**
  54. * @brief This function will notify kernel there is one tick passed.
  55. * Normally, this function is invoked by clock ISR.
  56. */
  57. void rt_tick_increase(void)
  58. {
  59. struct rt_thread *thread;
  60. rt_base_t level;
  61. level = rt_hw_interrupt_disable();
  62. /* increase the global tick */
  63. #ifdef RT_USING_SMP
  64. rt_cpu_self()->tick ++;
  65. #else
  66. ++ rt_tick;
  67. #endif /* RT_USING_SMP */
  68. /* check time slice */
  69. thread = rt_thread_self();
  70. -- thread->remaining_tick;
  71. if (thread->remaining_tick == 0)
  72. {
  73. /* change to initialized tick */
  74. thread->remaining_tick = thread->init_tick;
  75. thread->stat |= RT_THREAD_STAT_YIELD;
  76. rt_hw_interrupt_enable(level);
  77. rt_schedule();
  78. }
  79. else
  80. {
  81. rt_hw_interrupt_enable(level);
  82. }
  83. /* check timer */
  84. rt_timer_check();
  85. }
  86. /**
  87. * @brief This function will calculate the tick from millisecond.
  88. *
  89. * @param ms is the specified millisecond
  90. * - Negative Number wait forever
  91. * - Zero not wait
  92. * - Max 0x7fffffff
  93. *
  94. * @return Return the calculated tick
  95. */
  96. rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
  97. {
  98. rt_tick_t tick;
  99. if (ms < 0)
  100. {
  101. tick = (rt_tick_t)RT_WAITING_FOREVER;
  102. }
  103. else
  104. {
  105. tick = RT_TICK_PER_SECOND * (ms / 1000);
  106. tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
  107. }
  108. /* return the calculated tick */
  109. return tick;
  110. }
  111. RTM_EXPORT(rt_tick_from_millisecond);
  112. /**
  113. * @brief This function will return the passed millisecond from boot.
  114. *
  115. * @note When the value of RT_TICK_PER_SECOND is lower than 1000 or
  116. * is not an integral multiple of 1000, this function will not
  117. * provide the correct 1ms-based tick.
  118. *
  119. * @return Return passed millisecond from boot
  120. */
  121. RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
  122. {
  123. #if 1000 % RT_TICK_PER_SECOND == 0u
  124. return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
  125. #else
  126. #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
  127. please redefine this function in another file by using a high-precision hard-timer."
  128. return 0;
  129. #endif /* 1000 % RT_TICK_PER_SECOND == 0u */
  130. }
  131. /**@}*/