clock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-12 Bernard first version
  23. * 2006-05-27 Bernard add support for same priority thread schedule
  24. * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase
  25. * 2010-03-08 Bernard remove rt_passed_second
  26. * 2010-05-20 Bernard fix the tick exceeds the maximum limits
  27. * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
  28. * 2011-06-26 Bernard add rt_tick_set function.
  29. */
  30. #include <rthw.h>
  31. #include <rtthread.h>
  32. static rt_tick_t rt_tick = 0;
  33. extern void rt_timer_check(void);
  34. /**
  35. * This function will init system tick and set it to zero.
  36. * @ingroup SystemInit
  37. *
  38. * @deprecated since 1.1.0, this function does not need to be invoked
  39. * in the system initialization.
  40. */
  41. void rt_system_tick_init(void)
  42. {
  43. }
  44. /**
  45. * @addtogroup Clock
  46. */
  47. /*@{*/
  48. /**
  49. * This function will return current tick from operating system startup
  50. *
  51. * @return current tick
  52. */
  53. rt_tick_t rt_tick_get(void)
  54. {
  55. /* return the global tick */
  56. return rt_tick;
  57. }
  58. RTM_EXPORT(rt_tick_get);
  59. /**
  60. * This function will set current tick
  61. */
  62. void rt_tick_set(rt_tick_t tick)
  63. {
  64. rt_base_t level;
  65. level = rt_hw_interrupt_disable();
  66. rt_tick = tick;
  67. rt_hw_interrupt_enable(level);
  68. }
  69. /**
  70. * This function will notify kernel there is one tick passed. Normally,
  71. * this function is invoked by clock ISR.
  72. */
  73. void rt_tick_increase(void)
  74. {
  75. struct rt_thread *thread;
  76. /* increase the global tick */
  77. ++ rt_tick;
  78. /* check time slice */
  79. thread = rt_thread_self();
  80. -- thread->remaining_tick;
  81. if (thread->remaining_tick == 0)
  82. {
  83. /* change to initialized tick */
  84. thread->remaining_tick = thread->init_tick;
  85. /* yield */
  86. rt_thread_yield();
  87. }
  88. /* check timer */
  89. rt_timer_check();
  90. }
  91. /**
  92. * This function will calculate the tick from millisecond.
  93. *
  94. * @param ms the specified millisecond
  95. *
  96. * @return the calculated tick
  97. */
  98. rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
  99. {
  100. /* return the calculated tick */
  101. return (RT_TICK_PER_SECOND * ms + 999) / 1000;
  102. }
  103. RTM_EXPORT(rt_tick_from_millisecond);
  104. /*@}*/