clock.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. */
  19. #include <rtthread.h>
  20. static rt_tick_t rt_tick;
  21. extern void rt_timer_check(void);
  22. extern void rt_timer_switch(void);
  23. /**
  24. * This function will init system tick and set it to zero.
  25. * @ingroup SystemInit
  26. *
  27. */
  28. void rt_system_tick_init()
  29. {
  30. rt_tick = 0;
  31. }
  32. /**
  33. * @addtogroup Clock
  34. */
  35. /*@{*/
  36. /**
  37. * This function will return current tick from operating system startup
  38. *
  39. * @return current tick
  40. */
  41. rt_tick_t rt_tick_get()
  42. {
  43. /* return the global tick */
  44. return rt_tick;
  45. }
  46. /**
  47. * This function will notify kernel there is one tick passed. Normally,
  48. * this function is invoked by clock ISR.
  49. */
  50. void rt_tick_increase()
  51. {
  52. struct rt_thread* thread;
  53. /* increase the global tick */
  54. if (rt_tick == RT_TICK_MAX)
  55. {
  56. /* switch to long timer list */
  57. rt_timer_switch();
  58. rt_tick = 0;
  59. }
  60. else ++ rt_tick;
  61. /* check time slice */
  62. thread = rt_thread_self();
  63. -- thread->remaining_tick;
  64. if (thread->remaining_tick == 0)
  65. {
  66. /* change to initialized tick */
  67. thread->remaining_tick = thread->init_tick;
  68. /* yield */
  69. rt_thread_yield();
  70. }
  71. /* check timer */
  72. rt_timer_check();
  73. }
  74. /**
  75. * This function will calculate the tick from millisecond.
  76. *
  77. * @param ms the specified millisecond
  78. *
  79. * @return the calculated tick
  80. */
  81. rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
  82. {
  83. /* return the calculated tick */
  84. return (RT_TICK_PER_SECOND * ms+999) / 1000;
  85. }
  86. /*@}*/