diff options
| author | bernard.xiong <[email protected]> | 2011-03-10 10:30:51 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2011-03-10 10:30:51 +0000 |
| commit | 79696e86492f3091c5a4d874bd6d8bf72c828a4c (patch) | |
| tree | 416c42e269fc321b4afa9e148e2d2b35cefdb3ec /components | |
| parent | 107dc2378aac8d4a56b2c1c7356124dee768f9af (diff) | |
fix _gettimeofday_r issue.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1325 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components')
| -rw-r--r-- | components/libc/newlib/syscalls.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/components/libc/newlib/syscalls.c b/components/libc/newlib/syscalls.c index 9254cc7cea..f3c7b09e43 100644 --- a/components/libc/newlib/syscalls.c +++ b/components/libc/newlib/syscalls.c @@ -170,6 +170,70 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes) return rc;
}
+#ifndef RT_USING_PTHREADS
+
+#ifndef MILLISECOND_PER_SECOND
+#define MILLISECOND_PER_SECOND 1000UL
+#endif
+
+#ifndef MICROSECOND_PER_SECOND
+#define MICROSECOND_PER_SECOND 1000000UL
+#endif
+
+#ifndef NANOSECOND_PER_SECOND
+#define NANOSECOND_PER_SECOND 1000000000UL
+#endif
+
+#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
+#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
+#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
+
+
+struct timeval _timevalue = {0};
+static void libc_system_time_init()
+{
+ time_t time;
+ rt_tick_t tick;
+ rt_device_t device;
+
+ time = 0;
+ device = rt_device_find("rtc");
+ if (device != RT_NULL)
+ {
+ /* get realtime seconds */
+ rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
+ }
+
+ /* get tick */
+ tick = rt_tick_get();
+
+ _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
+ _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
+}
+
+int libc_get_time(struct timespec *time)
+{
+ rt_tick_t tick;
+ static rt_bool_t inited = 0;
+
+ RT_ASSERT(time != RT_NULL);
+
+ /* initialize system time */
+ if (inited == 0)
+ {
+ libc_system_time_init();
+ inited = 1;
+ }
+
+ /* get tick */
+ tick = rt_tick_get();
+
+ time->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
+ time->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * NANOSECOND_PER_TICK) * 1000;
+
+ return 0;
+}
+
int
_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
{
@@ -190,6 +254,30 @@ _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp) ptr->_errno = ENOTSUP;
return -1;
}
+#else
+/* POSIX thread provides clock_gettime function */
+#include <time.h>
+int
+_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
+{
+ struct timespec tp;
+
+ if (clock_gettime(CLOCK_REALTIME, &tp) == 0)
+ {
+ if (__tp != RT_NULL)
+ {
+ __tp->tv_sec = tp.tv_sec;
+ __tp->tv_usec = tp.tv_nsec * 1000UL;
+ }
+
+ return tp.tv_sec;
+ }
+
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
+}
+#endif
/* Memory routine */
void *
|
