clock.c 3.2 KB

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