clock.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ++ rt_tick;
  55. /* check time slice */
  56. thread = rt_thread_self();
  57. -- thread->remaining_tick;
  58. if (thread->remaining_tick == 0)
  59. {
  60. /* change to initialized tick */
  61. thread->remaining_tick = thread->init_tick;
  62. /* yield */
  63. rt_thread_yield();
  64. }
  65. /* check timer */
  66. rt_timer_check();
  67. }
  68. /**
  69. * This function will calculate the tick from millisecond.
  70. *
  71. * @param ms the specified millisecond
  72. *
  73. * @return the calculated tick
  74. */
  75. rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
  76. {
  77. /* return the calculated tick */
  78. return (RT_TICK_PER_SECOND * ms+999) / 1000;
  79. }
  80. /*@}*/