clock_time.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <rtthread.h>
  2. #include <pthread.h>
  3. struct timeval _timevalue;
  4. void clock_time_system_init()
  5. {
  6. time_t time;
  7. rt_tick_t tick;
  8. rt_device_t device;
  9. time = 0;
  10. device = rt_device_find("rtc");
  11. if (device != RT_NULL)
  12. {
  13. /* get realtime seconds */
  14. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  15. }
  16. /* get tick */
  17. tick = rt_tick_get();
  18. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  19. _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
  20. }
  21. int clock_time_to_tick(const struct timespec *time)
  22. {
  23. int tick;
  24. int nsecond, second;
  25. struct timespec tp;
  26. RT_ASSERT(time != RT_NULL);
  27. tick = RT_WAITING_FOREVER;
  28. if (time != NULL)
  29. {
  30. /* get current tp */
  31. clock_gettime(CLOCK_REALTIME, &tp);
  32. if ((time->tv_nsec - tp.tv_nsec) < 0)
  33. {
  34. nsecond = NANOSECOND_PER_SECOND - (tp.tv_nsec - time->tv_nsec);
  35. second = time->tv_sec - tp.tv_sec - 1;
  36. }
  37. else
  38. {
  39. nsecond = time->tv_nsec - tp.tv_nsec;
  40. second = time->tv_sec - tp.tv_sec;
  41. }
  42. tick = second * RT_TICK_PER_SECOND + nsecond * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  43. if (tick < 0) tick = 0;
  44. }
  45. return tick;
  46. }
  47. RTM_EXPORT(clock_time_to_tick);
  48. int clock_getres (clockid_t clockid, struct timespec *res)
  49. {
  50. if ((clockid != CLOCK_REALTIME) || (res == RT_NULL))
  51. {
  52. rt_set_errno(EINVAL);
  53. return -1;
  54. }
  55. res->tv_sec = 0;
  56. res->tv_nsec = NANOSECOND_PER_SECOND/RT_TICK_PER_SECOND;
  57. return 0;
  58. }
  59. RTM_EXPORT(clock_getres);
  60. int clock_gettime (clockid_t clockid, struct timespec *tp)
  61. {
  62. rt_tick_t tick;
  63. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  64. {
  65. rt_set_errno(EINVAL);
  66. return -1;
  67. }
  68. /* get tick */
  69. tick = rt_tick_get();
  70. tp->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
  71. tp->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * NANOSECOND_PER_TICK) * 1000;
  72. return 0;
  73. }
  74. RTM_EXPORT(clock_gettime);
  75. int clock_settime (clockid_t clockid, const struct timespec *tp)
  76. {
  77. int second;
  78. rt_tick_t tick;
  79. rt_device_t device;
  80. if ((clockid != CLOCK_REALTIME) || (tp == RT_NULL))
  81. {
  82. rt_set_errno(EINVAL);
  83. return -1;
  84. }
  85. /* get second */
  86. second = tp->tv_sec;
  87. /* get tick */
  88. tick = rt_tick_get();
  89. /* update timevalue */
  90. _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
  91. _timevalue.tv_sec = second - tick/RT_TICK_PER_SECOND - 1;
  92. /* update for RTC device */
  93. device = rt_device_find("rtc");
  94. if (device != RT_NULL)
  95. {
  96. /* set realtime seconds */
  97. rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &second);
  98. }
  99. else return -1;
  100. return 0;
  101. }
  102. RTM_EXPORT(clock_settime);