time.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <sys/time.h>
  2. #include <rtthread.h>
  3. #ifdef RT_USING_DEVICE
  4. int gettimeofday(struct timeval *tp, void *ignore)
  5. {
  6. time_t time;
  7. rt_device_t device;
  8. device = rt_device_find("rtc");
  9. if (device != RT_NULL)
  10. {
  11. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  12. if (tp != RT_NULL)
  13. {
  14. tp->tv_sec = time;
  15. tp->tv_usec = 0;
  16. }
  17. return time;
  18. }
  19. return 0;
  20. }
  21. #endif
  22. /**
  23. * Returns the current time.
  24. *
  25. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  26. *
  27. * @return time_t return timestamp current.
  28. *
  29. */
  30. /* for IAR 6.2 later Compiler */
  31. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  32. #pragma module_name = "?time"
  33. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  34. #else
  35. time_t time(time_t *t)
  36. #endif
  37. {
  38. time_t time_now = 0;
  39. #ifdef RT_USING_RTC
  40. static rt_device_t device = RT_NULL;
  41. /* optimization: find rtc device only first. */
  42. if (device == RT_NULL)
  43. {
  44. device = rt_device_find("rtc");
  45. }
  46. /* read timestamp from RTC device. */
  47. if (device != RT_NULL)
  48. {
  49. if (rt_device_open(device, 0) == RT_EOK)
  50. {
  51. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  52. rt_device_close(device);
  53. }
  54. }
  55. #endif /* RT_USING_RTC */
  56. /* if t is not NULL, write timestamp to *t */
  57. if (t != RT_NULL)
  58. {
  59. *t = time_now;
  60. }
  61. return time_now;
  62. }
  63. RT_WEAK clock_t clock(void)
  64. {
  65. return rt_tick_get();
  66. }