1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ScoreTOD |
---|
5 | * |
---|
6 | * @brief Time of Day Handler API |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * COPYRIGHT (c) 1989-2009. |
---|
11 | * On-Line Applications Research Corporation (OAR). |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_TOD_H |
---|
19 | #define _RTEMS_SCORE_TOD_H |
---|
20 | |
---|
21 | #include <rtems/score/basedefs.h> |
---|
22 | #include <rtems/score/timestamp.h> |
---|
23 | |
---|
24 | #include <sys/time.h> |
---|
25 | #include <time.h> |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" { |
---|
29 | #endif |
---|
30 | |
---|
31 | /** |
---|
32 | * @defgroup ScoreTOD Time of Day Handler |
---|
33 | * |
---|
34 | * @ingroup Score |
---|
35 | * |
---|
36 | * The following constants are related to the time of day and are |
---|
37 | * independent of RTEMS. |
---|
38 | */ |
---|
39 | /**@{*/ |
---|
40 | |
---|
41 | /** |
---|
42 | * This constant represents the number of seconds in a minute. |
---|
43 | */ |
---|
44 | #define TOD_SECONDS_PER_MINUTE (uint32_t)60 |
---|
45 | |
---|
46 | /** |
---|
47 | * This constant represents the number of minutes per hour. |
---|
48 | */ |
---|
49 | #define TOD_MINUTES_PER_HOUR (uint32_t)60 |
---|
50 | |
---|
51 | /** |
---|
52 | * This constant represents the number of months in a year. |
---|
53 | */ |
---|
54 | #define TOD_MONTHS_PER_YEAR (uint32_t)12 |
---|
55 | |
---|
56 | /** |
---|
57 | * This constant represents the number of days in a non-leap year. |
---|
58 | */ |
---|
59 | #define TOD_DAYS_PER_YEAR (uint32_t)365 |
---|
60 | |
---|
61 | /** |
---|
62 | * This constant represents the number of hours per day. |
---|
63 | */ |
---|
64 | #define TOD_HOURS_PER_DAY (uint32_t)24 |
---|
65 | |
---|
66 | /** |
---|
67 | * This constant represents the number of seconds in a day which does |
---|
68 | * not include a leap second. |
---|
69 | */ |
---|
70 | #define TOD_SECONDS_PER_DAY (uint32_t) (TOD_SECONDS_PER_MINUTE * \ |
---|
71 | TOD_MINUTES_PER_HOUR * \ |
---|
72 | TOD_HOURS_PER_DAY) |
---|
73 | |
---|
74 | /** |
---|
75 | * This constant represents the number of seconds in a non-leap year. |
---|
76 | */ |
---|
77 | #define TOD_SECONDS_PER_NON_LEAP_YEAR (365 * TOD_SECONDS_PER_DAY) |
---|
78 | |
---|
79 | /** |
---|
80 | * This constant represents the number of millisecond in a second. |
---|
81 | */ |
---|
82 | #define TOD_MILLISECONDS_PER_SECOND (uint32_t)1000 |
---|
83 | |
---|
84 | /** |
---|
85 | * This constant represents the number of microseconds in a second. |
---|
86 | */ |
---|
87 | #define TOD_MICROSECONDS_PER_SECOND (uint32_t)1000000 |
---|
88 | |
---|
89 | /** |
---|
90 | * This constant represents the number of nanoseconds in a second. |
---|
91 | */ |
---|
92 | #define TOD_NANOSECONDS_PER_SECOND (uint32_t)1000000000 |
---|
93 | |
---|
94 | /** |
---|
95 | * This constant represents the number of nanoseconds in a mircosecond. |
---|
96 | */ |
---|
97 | #define TOD_NANOSECONDS_PER_MICROSECOND (uint32_t)1000 |
---|
98 | |
---|
99 | /**@}*/ |
---|
100 | |
---|
101 | /** |
---|
102 | * Seconds from January 1, 1970 to January 1, 1988. Used to account for |
---|
103 | * differences between POSIX API and RTEMS core. The timespec format time |
---|
104 | * is kept in POSIX compliant form. |
---|
105 | */ |
---|
106 | #define TOD_SECONDS_1970_THROUGH_1988 \ |
---|
107 | (((1987 - 1970 + 1) * TOD_SECONDS_PER_NON_LEAP_YEAR) + \ |
---|
108 | (4 * TOD_SECONDS_PER_DAY)) |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief Earliest year to which an time of day can be initialized. |
---|
112 | * |
---|
113 | * The following constant define the earliest year to which an |
---|
114 | * time of day can be initialized. This is considered the |
---|
115 | * epoch. |
---|
116 | */ |
---|
117 | #define TOD_BASE_YEAR 1988 |
---|
118 | |
---|
119 | /** |
---|
120 | * @defgroup ScoreTOD Time Of Day (TOD) Handler |
---|
121 | * |
---|
122 | * @ingroup Score |
---|
123 | * |
---|
124 | * This handler encapsulates functionality used to manage time of day. |
---|
125 | */ |
---|
126 | /**@{*/ |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief TOD control. |
---|
130 | */ |
---|
131 | typedef struct { |
---|
132 | /** |
---|
133 | * @brief Current time of day value. |
---|
134 | */ |
---|
135 | Timestamp_Control now; |
---|
136 | |
---|
137 | /** |
---|
138 | * @brief System uptime. |
---|
139 | */ |
---|
140 | Timestamp_Control uptime; |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Time of day seconds trigger. |
---|
144 | * |
---|
145 | * This value specifies the nanoseconds since the last time of day second. |
---|
146 | * It is updated and evaluated in _TOD_Tickle_ticks(). It is set in |
---|
147 | * _TOD_Set_with_timestamp(). |
---|
148 | */ |
---|
149 | uint32_t seconds_trigger; |
---|
150 | |
---|
151 | /** |
---|
152 | * @brief Indicates if the time of day is set. |
---|
153 | * |
---|
154 | * This is true if the application has set the current |
---|
155 | * time of day, and false otherwise. |
---|
156 | */ |
---|
157 | bool is_set; |
---|
158 | } TOD_Control; |
---|
159 | |
---|
160 | SCORE_EXTERN TOD_Control _TOD; |
---|
161 | |
---|
162 | /** |
---|
163 | * @brief Number of seconds Since RTEMS epoch. |
---|
164 | * |
---|
165 | * The following contains the number of seconds from 00:00:00 |
---|
166 | * January 1, TOD_BASE_YEAR until the current time of day. |
---|
167 | */ |
---|
168 | #define _TOD_Seconds_since_epoch() \ |
---|
169 | _Timestamp_Get_seconds(&_TOD.now) |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief Initializes the time of day handler. |
---|
173 | * |
---|
174 | * Performs the initialization necessary for the Time Of Day handler. |
---|
175 | */ |
---|
176 | void _TOD_Handler_initialization(void); |
---|
177 | |
---|
178 | /** |
---|
179 | * @brief Sets the time of day from timestamp. |
---|
180 | * |
---|
181 | * The @a tod_as_timestamp timestamp represents the time since UNIX epoch. |
---|
182 | * The watchdog seconds chain will be adjusted. |
---|
183 | * |
---|
184 | * @param[in] tod_as_timestamp is the constant of the time of day as a timestamp |
---|
185 | */ |
---|
186 | void _TOD_Set_with_timestamp( |
---|
187 | const Timestamp_Control *tod_as_timestamp |
---|
188 | ); |
---|
189 | |
---|
190 | static inline void _TOD_Set( |
---|
191 | const struct timespec *tod_as_timespec |
---|
192 | ) |
---|
193 | { |
---|
194 | Timestamp_Control tod_as_timestamp; |
---|
195 | |
---|
196 | _Timestamp_Set( |
---|
197 | &tod_as_timestamp, |
---|
198 | tod_as_timespec->tv_sec, |
---|
199 | tod_as_timespec->tv_nsec |
---|
200 | ); |
---|
201 | _TOD_Set_with_timestamp( &tod_as_timestamp ); |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * @brief Returns a snapshot of a clock. |
---|
206 | * |
---|
207 | * This function invokes the nanoseconds extension. |
---|
208 | * |
---|
209 | * @param[out] snapshot points to an area that will contain the current |
---|
210 | * TOD plus the BSP nanoseconds since last tick adjustment |
---|
211 | * @param[in] clock contains the current TOD |
---|
212 | * |
---|
213 | * @retval @a snapshot |
---|
214 | */ |
---|
215 | Timestamp_Control *_TOD_Get_with_nanoseconds( |
---|
216 | Timestamp_Control *snapshot, |
---|
217 | const Timestamp_Control *clock |
---|
218 | ); |
---|
219 | |
---|
220 | static inline void _TOD_Get( |
---|
221 | struct timespec *tod_as_timespec |
---|
222 | ) |
---|
223 | { |
---|
224 | Timestamp_Control tod_as_timestamp; |
---|
225 | Timestamp_Control *tod_as_timestamp_ptr; |
---|
226 | |
---|
227 | tod_as_timestamp_ptr = |
---|
228 | _TOD_Get_with_nanoseconds( &tod_as_timestamp, &_TOD.now ); |
---|
229 | _Timestamp_To_timespec( tod_as_timestamp_ptr, tod_as_timespec ); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
234 | * |
---|
235 | * This routine returns the system uptime with potential accuracy |
---|
236 | * to the nanosecond. |
---|
237 | * |
---|
238 | * @param[in] time is a pointer to the uptime to be returned |
---|
239 | */ |
---|
240 | static inline void _TOD_Get_uptime( |
---|
241 | Timestamp_Control *time |
---|
242 | ) |
---|
243 | { |
---|
244 | _TOD_Get_with_nanoseconds( time, &_TOD.uptime ); |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
249 | * |
---|
250 | * This routine returns the system uptime with potential accuracy |
---|
251 | * to the nanosecond. |
---|
252 | * |
---|
253 | * @param[in] time is a pointer to the uptime to be returned |
---|
254 | */ |
---|
255 | void _TOD_Get_uptime_as_timespec( |
---|
256 | struct timespec *time |
---|
257 | ); |
---|
258 | |
---|
259 | /** |
---|
260 | * @brief Increments time of day at each clock tick. |
---|
261 | * |
---|
262 | * This routine increments the ticks field of the current time of |
---|
263 | * day at each clock tick. |
---|
264 | */ |
---|
265 | void _TOD_Tickle_ticks( void ); |
---|
266 | |
---|
267 | /** |
---|
268 | * @brief Gets number of ticks in a second. |
---|
269 | * |
---|
270 | * This method returns the number of ticks in a second. |
---|
271 | * |
---|
272 | * @note If the clock tick value does not multiply evenly into a second |
---|
273 | * then this number of ticks will be slightly shorter than a second. |
---|
274 | */ |
---|
275 | uint32_t TOD_TICKS_PER_SECOND_method(void); |
---|
276 | |
---|
277 | /** |
---|
278 | * @brief Gets number of ticks in a second. |
---|
279 | * |
---|
280 | * This method exists to hide the fact that TOD_TICKS_PER_SECOND can not |
---|
281 | * be implemented as a macro in a .h file due to visibility issues. |
---|
282 | * The Configuration Table is not available to SuperCore .h files but |
---|
283 | * is available to their .c files. |
---|
284 | */ |
---|
285 | #define TOD_TICKS_PER_SECOND TOD_TICKS_PER_SECOND_method() |
---|
286 | |
---|
287 | /** |
---|
288 | * This routine deactivates updating of the current time of day. |
---|
289 | */ |
---|
290 | |
---|
291 | RTEMS_INLINE_ROUTINE void _TOD_Deactivate( void ) |
---|
292 | { |
---|
293 | /* XXX do we need something now that we are using timespec for TOD */ |
---|
294 | } |
---|
295 | |
---|
296 | /** |
---|
297 | * This routine activates updating of the current time of day. |
---|
298 | */ |
---|
299 | |
---|
300 | RTEMS_INLINE_ROUTINE void _TOD_Activate( void ) |
---|
301 | { |
---|
302 | /* XXX do we need something now that we are using timespec for TOD */ |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * This routine returns a timeval based upon the internal timespec format TOD. |
---|
307 | */ |
---|
308 | |
---|
309 | RTEMS_INLINE_ROUTINE void _TOD_Get_timeval( |
---|
310 | struct timeval *time |
---|
311 | ) |
---|
312 | { |
---|
313 | Timestamp_Control snapshot_as_timestamp; |
---|
314 | Timestamp_Control *snapshot_as_timestamp_ptr; |
---|
315 | |
---|
316 | snapshot_as_timestamp_ptr = |
---|
317 | _TOD_Get_with_nanoseconds( &snapshot_as_timestamp, &_TOD.now ); |
---|
318 | _Timestamp_To_timeval( snapshot_as_timestamp_ptr, time ); |
---|
319 | } |
---|
320 | |
---|
321 | /**@}*/ |
---|
322 | |
---|
323 | #ifdef __cplusplus |
---|
324 | } |
---|
325 | #endif |
---|
326 | |
---|
327 | #endif |
---|
328 | /* end of include file */ |
---|