clock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. */
  7. /*
  8. * File : clock.c
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-12 Bernard first version
  13. * 2006-05-27 Bernard add support for same priority thread schedule
  14. * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
  15. * 2010-03-08 Bernard remove rt_passed_second
  16. * 2010-05-20 Bernard fix the tick exceeds the maximum limits
  17. * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
  18. * 2011-06-26 Bernard add rt_tick_set function.
  19. */
  20. #include <rthw.h>
  21. #include <rtthread.h>
  22. static rt_tick_t rt_tick = 0;
  23. extern void rt_timer_check(void);
  24. /**
  25. * This function will init system tick and set it to zero.
  26. * @ingroup SystemInit
  27. *
  28. * @deprecated since 1.1.0, this function does not need to be invoked
  29. * in the system initialization.
  30. */
  31. void rt_system_tick_init(void)
  32. {
  33. }
  34. /**
  35. * @addtogroup Clock
  36. */
  37. /**@{*/
  38. /**
  39. * This function will return current tick from operating system startup
  40. *
  41. * @return current tick
  42. */
  43. rt_tick_t rt_tick_get(void)
  44. {
  45. /* return the global tick */
  46. return rt_tick;
  47. }
  48. RTM_EXPORT(rt_tick_get);
  49. /**
  50. * This function will set current tick
  51. */
  52. void rt_tick_set(rt_tick_t tick)
  53. {
  54. rt_base_t level;
  55. level = rt_hw_interrupt_disable();
  56. rt_tick = tick;
  57. rt_hw_interrupt_enable(level);
  58. }
  59. /**
  60. * This function will notify kernel there is one tick passed. Normally,
  61. * this function is invoked by clock ISR.
  62. */
  63. void rt_tick_increase(void)
  64. {
  65. struct rt_thread *thread;
  66. /* increase the global tick */
  67. ++ rt_tick;
  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. /* yield */
  76. rt_thread_yield();
  77. }
  78. /* check timer */
  79. rt_timer_check();
  80. }
  81. /**
  82. * This function will calculate the tick from millisecond.
  83. *
  84. * @param ms the specified millisecond
  85. * - Negative Number wait forever
  86. * - Zero not wait
  87. * - Max 0x7fffffff
  88. *
  89. * @return the calculated tick
  90. */
  91. int rt_tick_from_millisecond(rt_int32_t ms)
  92. {
  93. int tick;
  94. if (ms < 0)
  95. tick = RT_WAITING_FOREVER;
  96. else
  97. tick = (RT_TICK_PER_SECOND * ms + 999) / 1000;
  98. /* return the calculated tick */
  99. return tick;
  100. }
  101. RTM_EXPORT(rt_tick_from_millisecond);
  102. /**@}*/