clock.c 1.8 KB

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