clock_time.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * File : clock_time.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-12-08 Bernard fix the issue of _timevalue.tv_usec initialization,
  23. * which found by Rob <rdent@iinet.net.au>
  24. */
  25. #include <rtthread.h>
  26. #include <pthread.h>
  27. struct timeval _timevalue;
  28. void clock_time_system_init()
  29. {
  30. time_t time;
  31. rt_tick_t tick;
  32. rt_device_t device;
  33. time = 0;
  34. device = rt_device_find("rtc");
  35. if (device != RT_NULL)
  36. {
  37. /* get realtime seconds */
  38. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  39. }
  40. /* get tick */
  41. tick = rt_tick_get();
  42. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  43. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  44. }
  45. int clock_time_to_tick(const struct timespec *time)
  46. {
  47. int tick;
  48. int nsecond, second;
  49. struct timespec tp;
  50. RT_ASSERT(time != RT_NULL);
  51. tick = RT_WAITING_FOREVER;
  52. if (time != NULL)
  53. {
  54. /* get current tp */
  55. clock_gettime(CLOCK_REALTIME, &tp);
  56. if ((time->tv_nsec - tp.tv_nsec) < 0)
  57. {
  58. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  59. second = time->tv_sec - tp.tv_sec - 1;
  60. }
  61. else
  62. {
  63. nsecond = time->tv_nsec - tp.tv_nsec;
  64. second = time->tv_sec - tp.tv_sec;
  65. }
  66. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  67. if (tick < 0) tick = 0;
  68. }
  69. return tick;
  70. }
  71. RTM_EXPORT(clock_time_to_tick);
  72. int clock_getres(clockid_t clockid, struct timespec *res)
  73. {
  74. if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
  75. {
  76. rt_set_errno(EINVAL);
  77. return -1;
  78. }
  79. res->tv_sec = 0;
  80. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  81. return 0;
  82. }
  83. RTM_EXPORT(clock_getres);
  84. int clock_gettime(clockid_t clockid, struct timespec *tp)
  85. {
  86. rt_tick_t tick;
  87. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  88. {
  89. rt_set_errno(EINVAL);
  90. return -1;
  91. }
  92. /* get tick */
  93. tick = rt_tick_get();
  94. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  95. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * NANOSECOND_PER_TICK) * 1000;
  96. return 0;
  97. }
  98. RTM_EXPORT(clock_gettime);
  99. int clock_settime(clockid_t clockid, const struct timespec *tp)
  100. {
  101. int second;
  102. rt_tick_t tick;
  103. rt_device_t device;
  104. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  105. {
  106. rt_set_errno(EINVAL);
  107. return -1;
  108. }
  109. /* get second */
  110. second = tp->tv_sec;
  111. /* get tick */
  112. tick = rt_tick_get();
  113. /* update timevalue */
  114. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  115. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  116. /* update for RTC device */
  117. device = rt_device_find("rtc");
  118. if (device != RT_NULL)
  119. {
  120. /* set realtime seconds */
  121. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
  122. }
  123. else
  124. return -1;
  125. return 0;
  126. }
  127. RTM_EXPORT(clock_settime);