clock.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * File : clock.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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://openlab.rt-thread.com/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. */
  16. #include <rtthread.h>
  17. static rt_tick_t rt_tick;
  18. static rt_time_t rt_passed_second;
  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. rt_passed_second = 0;
  29. }
  30. /**
  31. * @addtogroup Clock
  32. */
  33. /*@{*/
  34. /**
  35. * This function will return current tick from operating system startup
  36. *
  37. * @return current tick
  38. */
  39. rt_tick_t rt_tick_get()
  40. {
  41. /* return the global tick */
  42. return rt_tick;
  43. }
  44. /**
  45. * This function will notify kernel there is one tick passed. Normally,
  46. * this function is invoked by clock ISR.
  47. */
  48. void rt_tick_increase()
  49. {
  50. struct rt_thread* thread;
  51. /* increase the global tick */
  52. ++ rt_tick;
  53. if (rt_tick % RT_TICK_PER_SECOND == 0)
  54. {
  55. ++rt_passed_second;
  56. }
  57. /* check timer */
  58. rt_timer_check();
  59. /* check time slice */
  60. thread = rt_thread_self();
  61. -- thread->remaining_tick;
  62. if (thread->remaining_tick == 0)
  63. {
  64. /* change to initialized tick */
  65. thread->remaining_tick = thread->init_tick;
  66. /* yield */
  67. rt_thread_yield();
  68. }
  69. }
  70. /**
  71. * This function will calculate the tick from millisecond.
  72. *
  73. * @param ms the specified millisecond
  74. *
  75. * @return the calculated tick
  76. */
  77. rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
  78. {
  79. /* return the calculated tick */
  80. return RT_TICK_PER_SECOND * (ms / 1000);
  81. }
  82. /*@}*/