clock_time.c 4.8 KB

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