clock.c 2.5 KB

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