clock.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  17. #include <rthw.h>
  18. #include <rtthread.h>
  19. #ifdef RT_USING_SMP
  20. #define rt_tick rt_cpu_index(0)->tick
  21. #else
  22. static rt_tick_t rt_tick = 0;
  23. #endif
  24. extern void rt_timer_check(void);
  25. /**
  26. * This function will init system tick and set it to zero.
  27. * @ingroup SystemInit
  28. *
  29. * @deprecated since 1.1.0, this function does not need to be invoked
  30. * in the system initialization.
  31. */
  32. void rt_system_tick_init(void)
  33. {
  34. }
  35. /**
  36. * @addtogroup Clock
  37. */
  38. /**@{*/
  39. /**
  40. * This function will return current tick from operating system startup
  41. *
  42. * @return current tick
  43. */
  44. rt_tick_t rt_tick_get(void)
  45. {
  46. /* return the global tick */
  47. return rt_tick;
  48. }
  49. RTM_EXPORT(rt_tick_get);
  50. /**
  51. * This function will set current tick
  52. */
  53. void rt_tick_set(rt_tick_t tick)
  54. {
  55. rt_base_t level;
  56. level = rt_hw_interrupt_disable();
  57. rt_tick = tick;
  58. rt_hw_interrupt_enable(level);
  59. }
  60. /**
  61. * This function will notify kernel there is one tick passed. Normally,
  62. * this function is invoked by clock ISR.
  63. */
  64. void rt_tick_increase(void)
  65. {
  66. struct rt_thread *thread;
  67. /* increase the global tick */
  68. #ifdef RT_USING_SMP
  69. rt_cpu_self()->tick ++;
  70. #else
  71. ++ rt_tick;
  72. #endif
  73. /* check time slice */
  74. thread = rt_thread_self();
  75. -- thread->remaining_tick;
  76. if (thread->remaining_tick == 0)
  77. {
  78. /* change to initialized tick */
  79. thread->remaining_tick = thread->init_tick;
  80. /* yield */
  81. rt_thread_yield();
  82. }
  83. /* check timer */
  84. rt_timer_check();
  85. }
  86. /**
  87. * This function will calculate the tick from millisecond.
  88. *
  89. * @param ms the specified millisecond
  90. * - Negative Number wait forever
  91. * - Zero not wait
  92. * - Max 0x7fffffff
  93. *
  94. * @return the calculated tick
  95. */
  96. int rt_tick_from_millisecond(rt_int32_t ms)
  97. {
  98. int tick;
  99. if (ms < 0)
  100. tick = RT_WAITING_FOREVER;
  101. else
  102. tick = (RT_TICK_PER_SECOND * ms + 999) / 1000;
  103. /* return the calculated tick */
  104. return tick;
  105. }
  106. RTM_EXPORT(rt_tick_from_millisecond);
  107. /**@}*/