clock.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * This function will return current tick from operating system startup
  32. *
  33. * @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. * This function will set current tick
  43. */
  44. void rt_tick_set(rt_tick_t tick)
  45. {
  46. rt_base_t level;
  47. level = rt_hw_interrupt_disable();
  48. rt_tick = tick;
  49. rt_hw_interrupt_enable(level);
  50. }
  51. /**
  52. * This function will notify kernel there is one tick passed. Normally,
  53. * this function is invoked by clock ISR.
  54. */
  55. void rt_tick_increase(void)
  56. {
  57. struct rt_thread *thread;
  58. rt_base_t level;
  59. level = rt_hw_interrupt_disable();
  60. /* increase the global tick */
  61. #ifdef RT_USING_SMP
  62. rt_cpu_self()->tick ++;
  63. #else
  64. ++ rt_tick;
  65. #endif /* RT_USING_SMP */
  66. /* check time slice */
  67. thread = rt_thread_self();
  68. -- thread->remaining_tick;
  69. if (thread->remaining_tick == 0)
  70. {
  71. /* change to initialized tick */
  72. thread->remaining_tick = thread->init_tick;
  73. thread->stat |= RT_THREAD_STAT_YIELD;
  74. rt_hw_interrupt_enable(level);
  75. rt_schedule();
  76. }
  77. else
  78. {
  79. rt_hw_interrupt_enable(level);
  80. }
  81. /* check timer */
  82. rt_timer_check();
  83. }
  84. /**
  85. * This function will calculate the tick from millisecond.
  86. *
  87. * @param ms the specified millisecond
  88. * - Negative Number wait forever
  89. * - Zero not wait
  90. * - Max 0x7fffffff
  91. *
  92. * @return the calculated tick
  93. */
  94. rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
  95. {
  96. rt_tick_t tick;
  97. if (ms < 0)
  98. {
  99. tick = (rt_tick_t)RT_WAITING_FOREVER;
  100. }
  101. else
  102. {
  103. tick = RT_TICK_PER_SECOND * (ms / 1000);
  104. tick += (RT_TICK_PER_SECOND * (ms % 1000) + 999) / 1000;
  105. }
  106. /* return the calculated tick */
  107. return tick;
  108. }
  109. RTM_EXPORT(rt_tick_from_millisecond);
  110. /**
  111. * This function will provide the passed millisecond from boot.
  112. *
  113. * @return passed millisecond from boot
  114. */
  115. RT_WEAK rt_tick_t rt_tick_get_millisecond(void)
  116. {
  117. #if 1000 % RT_TICK_PER_SECOND == 0u
  118. return rt_tick_get() * (1000u / RT_TICK_PER_SECOND);
  119. #else
  120. #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\
  121. please redefine this function in another file by using a high-precision hard-timer."
  122. return 0;
  123. #endif /* 1000 % RT_TICK_PER_SECOND == 0u */
  124. }
  125. /**@}*/