1
0

time.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. ********************************************************************************
  3. **
  4. ** \file ./boot/startup/src/time.c
  5. **
  6. ** \version $Id: time.c 6426 2016-03-22 05:49:59Z liuwei $
  7. **
  8. ** \brief ARM1176 RealView libc function retargeting.
  9. **
  10. ** This files general (compiler independent) time specific retargets.
  11. **
  12. ** \attention THIS SAMPLE CODE IS PROVIDED AS IS. FUJITSU MICROELECTRONICS
  13. ** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
  14. ** OMMISSIONS.
  15. **
  16. ** (C) Copyright 2006-2010 by Fujitsu Microelectronics Europe GmbH
  17. ** (C) Copyright 2010 by Fujitsu Semiconductor Europe GmbH
  18. **
  19. ********************************************************************************
  20. **
  21. ** \note The following LINT statements have been suppressed:
  22. **
  23. **
  24. *****************************************************************************
  25. */
  26. #include <rtthread.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include "internal.h"
  30. /* Checks if a type is signed or unsigned */
  31. /*lint -emacro((506), IS_SIGNED) */
  32. /*lint -e(961) */
  33. #define IS_SIGNED(type) (((type) -1) < 0)
  34. /* Calculates maximum positive value for given scalar type with respect */
  35. /*lint -e(961) */
  36. #define POSITIVE_MAX_OF_TYPE(type) \
  37. ((type) (IS_SIGNED(type) ? \
  38. ((1LL << ((sizeof(type) * 8) - 1)) - 1) : \
  39. (-1)))
  40. /* Precalculate maximum type positive type values */
  41. #define Int64TMax (9223372036854775807LL)
  42. static const clock_t ClockTMax = (POSITIVE_MAX_OF_TYPE(clock_t));
  43. #if defined(__arm__) && !defined(__GNUC__) /* RVCT v3- */
  44. static const time_t TimeTMax = (POSITIVE_MAX_OF_TYPE(time_t) - 1);
  45. #else
  46. static const time_t TimeTMax = (POSITIVE_MAX_OF_TYPE(time_t));
  47. #endif
  48. static int64_t systemTimeBase = 0LL;
  49. static int32_t get_system_time( time_t *time )
  50. {
  51. int32_t rc = 0;
  52. uint64_t ts = rt_tick_get()/1000; //seconed
  53. /* check input parameter */
  54. if (time == NULL) {
  55. rc = -1;
  56. }
  57. /* check for time overflow */
  58. else if ((ts > Int64TMax) || /* pre-check downcast precision loss */
  59. ((int64_t) ts > ((int64_t) TimeTMax - systemTimeBase))) {
  60. rc = -1;
  61. }
  62. else {
  63. *time = (time_t) (systemTimeBase + (int64_t) ts);
  64. }
  65. return (rc);
  66. }
  67. /*
  68. ********************************************************************************
  69. ** \brief Retarget implementation of the stdc library function.
  70. **
  71. ** This is the standard C library clock() function from time.h.
  72. **
  73. ** This function returns the implementation's best approximation to the
  74. ** processor time used by the program since program invocation.
  75. **
  76. ** \return Returns elapsed processor time used by program. The time in seconds
  77. ** is the value returned divided by the value of the macro CLOCKS_PER_SEC.
  78. ** The value ((clock_t) -1) is returned if the processor time used is
  79. ** not available or invalid.
  80. ********************************************************************************
  81. */
  82. clock_t clock(void)
  83. {
  84. clock_t c;
  85. if (get_system_time((time_t*)&c) != 0) {
  86. c = (clock_t) -1;
  87. }
  88. return (c);
  89. }
  90. /*
  91. ********************************************************************************
  92. ** \brief Retarget implementation of the stdc library function.
  93. **
  94. ** This is the standard C library time() function from time.h.
  95. **
  96. ** This function returns an approximation of the current calendar time.
  97. **
  98. ** \param timer Return value resource for time_t. If timer is not a
  99. ** NULL pointer, the return value is also assigned to the time_t*.
  100. **
  101. ** \return Returns current calendar time, which represents the number of seconds
  102. ** elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time.
  103. ** (This date is sometimes referred to as the epoch.)
  104. ** The value ((time_t) -1) is returned if the calendar time is not available.
  105. ********************************************************************************
  106. */
  107. time_t time(time_t * timer)
  108. {
  109. time_t tv;
  110. if (get_system_time(&tv) != 0) {
  111. tv = (time_t) -1;
  112. }
  113. if (timer) {
  114. *timer = tv;
  115. }
  116. return (tv);
  117. }
  118. int gettimeofday(struct timeval *tp, void *tzvp)
  119. {
  120. time_t tvl;
  121. get_system_time(&tvl);
  122. tp->tv_sec = tvl;
  123. tp->tv_usec = (rt_tick_get()%1000) * 1000;
  124. return (0);
  125. }
  126. int settimeofday(const struct timeval *tv, const struct timezone *tz)
  127. {
  128. if(tv )
  129. systemTimeBase = tv->tv_sec + tv->tv_usec/1000000 - rt_tick_get()/1000;
  130. return 0;
  131. }