clock_time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "clock_time.h"
  28. static struct timeval _timevalue;
  29. int clock_time_system_init()
  30. {
  31. time_t time;
  32. rt_tick_t tick;
  33. rt_device_t device;
  34. time = 0;
  35. device = rt_device_find("rtc");
  36. if (device != RT_NULL)
  37. {
  38. /* get realtime seconds */
  39. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  40. }
  41. /* get tick */
  42. tick = rt_tick_get();
  43. _timevalue.tv_usec = (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  44. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  45. return 0;
  46. }
  47. INIT_COMPONENT_EXPORT(clock_time_system_init);
  48. int clock_time_to_tick(const struct timespec *time)
  49. {
  50. int tick;
  51. int nsecond, second;
  52. struct timespec tp;
  53. RT_ASSERT(time != RT_NULL);
  54. tick = RT_WAITING_FOREVER;
  55. if (time != NULL)
  56. {
  57. /* get current tp */
  58. clock_gettime(CLOCK_REALTIME, &tp);
  59. if ((time->tv_nsec - tp.tv_nsec) < 0)
  60. {
  61. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  62. second = time->tv_sec - tp.tv_sec - 1;
  63. }
  64. else
  65. {
  66. nsecond = time->tv_nsec - tp.tv_nsec;
  67. second = time->tv_sec - tp.tv_sec;
  68. }
  69. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  70. if (tick < 0) tick = 0;
  71. }
  72. return tick;
  73. }
  74. RTM_EXPORT(clock_time_to_tick);
  75. int clock_getres(clockid_t clockid, struct timespec *res)
  76. {
  77. int ret = 0;
  78. if (res == RT_NULL)
  79. {
  80. rt_set_errno(EINVAL);
  81. return -1;
  82. }
  83. switch (clockid)
  84. {
  85. case CLOCK_REALTIME:
  86. res->tv_sec = 0;
  87. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  88. break;
  89. #ifdef RT_USING_CPUTIME
  90. case CLOCK_CPUTIME_ID:
  91. res->tv_sec = 0;
  92. res->tv_nsec = clock_cpu_getres();
  93. break;
  94. #endif
  95. default:
  96. ret = -1;
  97. rt_set_errno(EINVAL);
  98. break;
  99. }
  100. return ret;
  101. }
  102. RTM_EXPORT(clock_getres);
  103. int clock_gettime(clockid_t clockid, struct timespec *tp)
  104. {
  105. int ret = 0;
  106. if (tp == RT_NULL)
  107. {
  108. rt_set_errno(EINVAL);
  109. return -1;
  110. }
  111. switch (clockid)
  112. {
  113. case CLOCK_REALTIME:
  114. {
  115. /* get tick */
  116. int tick = rt_tick_get();
  117. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  118. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
  119. }
  120. break;
  121. #ifdef RT_USING_CPUTIME
  122. case CLOCK_CPUTIME_ID:
  123. {
  124. float unit = 0;
  125. long long cpu_tick;
  126. unit = clock_cpu_getres();
  127. cpu_tick = clock_cpu_gettime();
  128. tp->tv_sec = ((int)(cpu_tick * unit)) / NANOSECOND_PER_SECOND;
  129. tp->tv_nsec = ((int)(cpu_tick * unit)) % NANOSECOND_PER_SECOND;
  130. }
  131. break;
  132. #endif
  133. default:
  134. rt_set_errno(EINVAL);
  135. ret = -1;
  136. }
  137. return ret;
  138. }
  139. RTM_EXPORT(clock_gettime);
  140. int clock_settime(clockid_t clockid, const struct timespec *tp)
  141. {
  142. int second;
  143. rt_tick_t tick;
  144. rt_device_t device;
  145. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  146. {
  147. rt_set_errno(EINVAL);
  148. return -1;
  149. }
  150. /* get second */
  151. second = tp->tv_sec;
  152. /* get tick */
  153. tick = rt_tick_get();
  154. /* update timevalue */
  155. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  156. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  157. /* update for RTC device */
  158. device = rt_device_find("rtc");
  159. if (device != RT_NULL)
  160. {
  161. /* set realtime seconds */
  162. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
  163. }
  164. else
  165. return -1;
  166. return 0;
  167. }
  168. RTM_EXPORT(clock_settime);