clock.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  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;
  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. */
  29. void rt_system_tick_init(void)
  30. {
  31. rt_tick = 0;
  32. }
  33. /**
  34. * @addtogroup Clock
  35. */
  36. /*@{*/
  37. /**
  38. * This function will return current tick from operating system startup
  39. *
  40. * @return current tick
  41. */
  42. rt_tick_t rt_tick_get(void)
  43. {
  44. /* return the global tick */
  45. return rt_tick;
  46. }
  47. /**
  48. * This function will set current tick
  49. */
  50. void rt_tick_set(rt_tick_t tick)
  51. {
  52. rt_base_t level;
  53. level = rt_hw_interrupt_disable();
  54. rt_tick = tick;
  55. rt_hw_interrupt_enable(level);
  56. }
  57. /**
  58. * This function will notify kernel there is one tick passed. Normally,
  59. * this function is invoked by clock ISR.
  60. */
  61. void rt_tick_increase(void)
  62. {
  63. struct rt_thread *thread;
  64. /* increase the global tick */
  65. ++ rt_tick;
  66. /* check time slice */
  67. thread = rt_thread_self();
  68. -- thread->remaining_tick;
  69. if (thread->remaining_tick == 0)
  70. {
  71. /* change to initialized tick */
  72. thread->remaining_tick = thread->init_tick;
  73. /* yield */
  74. rt_thread_yield();
  75. }
  76. /* check timer */
  77. rt_timer_check();
  78. }
  79. /**
  80. * This function will calculate the tick from millisecond.
  81. *
  82. * @param ms the specified millisecond
  83. *
  84. * @return the calculated tick
  85. */
  86. rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
  87. {
  88. /* return the calculated tick */
  89. return (RT_TICK_PER_SECOND * ms + 999) / 1000;
  90. }
  91. /*@}*/