1 |
8 |
alfik |
/******************************************************************************
|
2 |
|
|
* *
|
3 |
|
|
* License Agreement *
|
4 |
|
|
* *
|
5 |
|
|
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
|
6 |
|
|
* All rights reserved. *
|
7 |
|
|
* *
|
8 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
9 |
|
|
* copy of this software and associated documentation files (the "Software"), *
|
10 |
|
|
* to deal in the Software without restriction, including without limitation *
|
11 |
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
12 |
|
|
* and/or sell copies of the Software, and to permit persons to whom the *
|
13 |
|
|
* Software is furnished to do so, subject to the following conditions: *
|
14 |
|
|
* *
|
15 |
|
|
* The above copyright notice and this permission notice shall be included in *
|
16 |
|
|
* all copies or substantial portions of the Software. *
|
17 |
|
|
* *
|
18 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
19 |
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
21 |
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
22 |
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
23 |
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
24 |
|
|
* DEALINGS IN THE SOFTWARE. *
|
25 |
|
|
* *
|
26 |
|
|
* This agreement shall be governed in all respects by the laws of the State *
|
27 |
|
|
* of California and by the laws of the United States of America. *
|
28 |
|
|
* *
|
29 |
|
|
* Altera does not recommend, suggest or require that this reference design *
|
30 |
|
|
* file be used in conjunction or combination with any other product. *
|
31 |
|
|
******************************************************************************/
|
32 |
|
|
|
33 |
|
|
#include <sys/time.h>
|
34 |
|
|
#include <sys/times.h>
|
35 |
|
|
#include <errno.h>
|
36 |
|
|
|
37 |
|
|
#include "sys/alt_alarm.h"
|
38 |
|
|
#include "alt_types.h"
|
39 |
|
|
#include "os/alt_syscall.h"
|
40 |
|
|
|
41 |
|
|
/*
|
42 |
|
|
* Macro defining the number of micoseconds in a second.
|
43 |
|
|
*/
|
44 |
|
|
|
45 |
|
|
#define ALT_US (1000000)
|
46 |
|
|
|
47 |
|
|
/*
|
48 |
|
|
* "alt_timezone" and "alt_resettime" are the values of the the reset time and
|
49 |
|
|
* time zone set through the last call to settimeofday(). By default they are
|
50 |
|
|
* zero initialised.
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
struct timezone alt_timezone = {0, 0};
|
54 |
|
|
struct timeval alt_resettime = {0, 0};
|
55 |
|
|
|
56 |
|
|
/*
|
57 |
|
|
* gettimeofday() can be called to obtain a time structure which indicates the
|
58 |
|
|
* current "wall clock" time. This is calculated using the elapsed number of
|
59 |
|
|
* system clock ticks, and the value of "alt_resettime" and "alt_timezone" set
|
60 |
|
|
* through the last call to settimeofday().
|
61 |
|
|
*
|
62 |
|
|
* Warning: if this function is called concurrently with a call to
|
63 |
|
|
* settimeofday(), the value returned by gettimeofday() will be unreliable.
|
64 |
|
|
*
|
65 |
|
|
* ALT_GETTIMEOFDAY is mapped onto the gettimeofday() system call in
|
66 |
|
|
* alt_syscall.h
|
67 |
|
|
*/
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
#if defined (__GNUC__) && (__GNUC__ >= 4)
|
71 |
|
|
int ALT_GETTIMEOFDAY (struct timeval *ptimeval, void *ptimezone_vptr)
|
72 |
|
|
{
|
73 |
|
|
struct timezone *ptimezone = (struct timezone*)ptimezone_vptr;
|
74 |
|
|
#else
|
75 |
|
|
int ALT_GETTIMEOFDAY (struct timeval *ptimeval, struct timezone *ptimezone)
|
76 |
|
|
{
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
alt_u32 nticks = alt_nticks ();
|
80 |
|
|
alt_u32 tick_rate = alt_ticks_per_second ();
|
81 |
|
|
|
82 |
|
|
/*
|
83 |
|
|
* Check to see if the system clock is running. This is indicated by a
|
84 |
|
|
* non-zero system clock rate. If the system clock is not running, an error
|
85 |
|
|
* is generated and the contents of "ptimeval" and "ptimezone" are not
|
86 |
|
|
* updated.
|
87 |
|
|
*/
|
88 |
|
|
|
89 |
|
|
if (tick_rate)
|
90 |
|
|
{
|
91 |
|
|
ptimeval->tv_sec = alt_resettime.tv_sec + nticks/tick_rate;
|
92 |
|
|
ptimeval->tv_usec = alt_resettime.tv_usec +
|
93 |
|
|
(alt_u32)(((alt_u64)nticks*(ALT_US/tick_rate))%ALT_US);
|
94 |
|
|
|
95 |
|
|
while(ptimeval->tv_usec < 0) {
|
96 |
|
|
if (ptimeval->tv_sec <= 0)
|
97 |
|
|
{
|
98 |
|
|
ptimeval->tv_sec = 0;
|
99 |
|
|
ptimeval->tv_usec = 0;
|
100 |
|
|
break;
|
101 |
|
|
}
|
102 |
|
|
else
|
103 |
|
|
{
|
104 |
|
|
ptimeval->tv_sec--;
|
105 |
|
|
ptimeval->tv_usec += ALT_US;
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
while(ptimeval->tv_usec >= ALT_US) {
|
110 |
|
|
ptimeval->tv_sec++;
|
111 |
|
|
ptimeval->tv_usec -= ALT_US;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
if (ptimezone)
|
115 |
|
|
{
|
116 |
|
|
ptimezone->tz_minuteswest = alt_timezone.tz_minuteswest;
|
117 |
|
|
ptimezone->tz_dsttime = alt_timezone.tz_dsttime;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
return 0;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
return -ENOTSUP;
|
124 |
|
|
}
|
125 |
|
|
|