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