clock.c 2.0 KB

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