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.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | #ifndef _RTEMS_SCORE_TODIMPL_H |
---|
19 | #define _RTEMS_SCORE_TODIMPL_H |
---|
20 | |
---|
21 | #include <rtems/score/tod.h> |
---|
22 | #include <rtems/score/apimutex.h> |
---|
23 | #include <rtems/score/timestamp.h> |
---|
24 | #include <rtems/score/timecounterimpl.h> |
---|
25 | #include <rtems/score/watchdog.h> |
---|
26 | |
---|
27 | #include <sys/time.h> |
---|
28 | #include <time.h> |
---|
29 | |
---|
30 | #ifdef __cplusplus |
---|
31 | extern "C" { |
---|
32 | #endif |
---|
33 | |
---|
34 | /** |
---|
35 | * @defgroup ScoreTOD Time of Day Handler |
---|
36 | * |
---|
37 | * @ingroup Score |
---|
38 | * |
---|
39 | * The following constants are related to the time of day and are |
---|
40 | * independent of RTEMS. |
---|
41 | */ |
---|
42 | /**@{*/ |
---|
43 | |
---|
44 | /** |
---|
45 | * This constant represents the number of seconds in a minute. |
---|
46 | */ |
---|
47 | #define TOD_SECONDS_PER_MINUTE (uint32_t)60 |
---|
48 | |
---|
49 | /** |
---|
50 | * This constant represents the number of minutes per hour. |
---|
51 | */ |
---|
52 | #define TOD_MINUTES_PER_HOUR (uint32_t)60 |
---|
53 | |
---|
54 | /** |
---|
55 | * This constant represents the number of months in a year. |
---|
56 | */ |
---|
57 | #define TOD_MONTHS_PER_YEAR (uint32_t)12 |
---|
58 | |
---|
59 | /** |
---|
60 | * This constant represents the number of days in a non-leap year. |
---|
61 | */ |
---|
62 | #define TOD_DAYS_PER_YEAR (uint32_t)365 |
---|
63 | |
---|
64 | /** |
---|
65 | * This constant represents the number of hours per day. |
---|
66 | */ |
---|
67 | #define TOD_HOURS_PER_DAY (uint32_t)24 |
---|
68 | |
---|
69 | /** |
---|
70 | * This constant represents the number of seconds in a day which does |
---|
71 | * not include a leap second. |
---|
72 | */ |
---|
73 | #define TOD_SECONDS_PER_DAY (uint32_t) (TOD_SECONDS_PER_MINUTE * \ |
---|
74 | TOD_MINUTES_PER_HOUR * \ |
---|
75 | TOD_HOURS_PER_DAY) |
---|
76 | |
---|
77 | /** |
---|
78 | * This constant represents the number of seconds in a non-leap year. |
---|
79 | */ |
---|
80 | #define TOD_SECONDS_PER_NON_LEAP_YEAR (365 * TOD_SECONDS_PER_DAY) |
---|
81 | |
---|
82 | /** |
---|
83 | * This constant represents the number of millisecond in a second. |
---|
84 | */ |
---|
85 | #define TOD_MILLISECONDS_PER_SECOND (uint32_t)1000 |
---|
86 | |
---|
87 | /** |
---|
88 | * This constant represents the number of microseconds in a second. |
---|
89 | */ |
---|
90 | #define TOD_MICROSECONDS_PER_SECOND (uint32_t)1000000 |
---|
91 | |
---|
92 | /** |
---|
93 | * This constant represents the number of nanoseconds in a second. |
---|
94 | */ |
---|
95 | #define TOD_NANOSECONDS_PER_SECOND (uint32_t)1000000000 |
---|
96 | |
---|
97 | /** |
---|
98 | * This constant represents the number of nanoseconds in a mircosecond. |
---|
99 | */ |
---|
100 | #define TOD_NANOSECONDS_PER_MICROSECOND (uint32_t)1000 |
---|
101 | |
---|
102 | /**@}*/ |
---|
103 | |
---|
104 | /** |
---|
105 | * Seconds from January 1, 1970 to January 1, 1988. Used to account for |
---|
106 | * differences between POSIX API and RTEMS core. The timespec format time |
---|
107 | * is kept in POSIX compliant form. |
---|
108 | */ |
---|
109 | #define TOD_SECONDS_1970_THROUGH_1988 \ |
---|
110 | (((1987 - 1970 + 1) * TOD_SECONDS_PER_NON_LEAP_YEAR) + \ |
---|
111 | (4 * TOD_SECONDS_PER_DAY)) |
---|
112 | |
---|
113 | /** |
---|
114 | * @brief Earliest year to which an time of day can be initialized. |
---|
115 | * |
---|
116 | * The following constant define the earliest year to which an |
---|
117 | * time of day can be initialized. This is considered the |
---|
118 | * epoch. |
---|
119 | */ |
---|
120 | #define TOD_BASE_YEAR 1988 |
---|
121 | |
---|
122 | /** |
---|
123 | * @defgroup ScoreTOD Time Of Day (TOD) Handler |
---|
124 | * |
---|
125 | * @ingroup Score |
---|
126 | * |
---|
127 | * This handler encapsulates functionality used to manage time of day. |
---|
128 | */ |
---|
129 | /**@{*/ |
---|
130 | |
---|
131 | /** |
---|
132 | * @brief TOD control. |
---|
133 | */ |
---|
134 | typedef struct { |
---|
135 | /** |
---|
136 | * @brief Indicates if the time of day is set. |
---|
137 | * |
---|
138 | * This is true if the application has set the current |
---|
139 | * time of day, and false otherwise. |
---|
140 | */ |
---|
141 | bool is_set; |
---|
142 | } TOD_Control; |
---|
143 | |
---|
144 | extern TOD_Control _TOD; |
---|
145 | |
---|
146 | static inline void _TOD_Lock( void ) |
---|
147 | { |
---|
148 | /* FIXME: https://devel.rtems.org/ticket/2630 */ |
---|
149 | _API_Mutex_Lock( _Once_Mutex ); |
---|
150 | } |
---|
151 | |
---|
152 | static inline void _TOD_Unlock( void ) |
---|
153 | { |
---|
154 | _API_Mutex_Unlock( _Once_Mutex ); |
---|
155 | } |
---|
156 | |
---|
157 | static inline void _TOD_Acquire( ISR_lock_Context *lock_context ) |
---|
158 | { |
---|
159 | _Timecounter_Acquire( lock_context ); |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | * @brief Sets the time of day. |
---|
164 | * |
---|
165 | * The caller must be the owner of the TOD lock. |
---|
166 | * |
---|
167 | * @param tod_as_timestamp The new time of day in timestamp format representing |
---|
168 | * the time since UNIX Epoch. |
---|
169 | * @param lock_context The ISR lock context used for the corresponding |
---|
170 | * _TOD_Acquire(). The caller must be the owner of the TOD lock. This |
---|
171 | * function will release the TOD lock. |
---|
172 | */ |
---|
173 | void _TOD_Set( |
---|
174 | const Timestamp_Control *tod_as_timestamp, |
---|
175 | ISR_lock_Context *lock_context |
---|
176 | ); |
---|
177 | |
---|
178 | /** |
---|
179 | * @brief Sets the time of day with timespec format. |
---|
180 | * |
---|
181 | * @param tod_as_timespec The new time of day in timespec format. |
---|
182 | * |
---|
183 | * @see _TOD_Set(). |
---|
184 | */ |
---|
185 | static inline void _TOD_Set_with_timespec( |
---|
186 | const struct timespec *tod_as_timespec |
---|
187 | ) |
---|
188 | { |
---|
189 | Timestamp_Control tod_as_timestamp; |
---|
190 | ISR_lock_Context lock_context; |
---|
191 | |
---|
192 | _Timestamp_Set( |
---|
193 | &tod_as_timestamp, |
---|
194 | tod_as_timespec->tv_sec, |
---|
195 | tod_as_timespec->tv_nsec |
---|
196 | ); |
---|
197 | |
---|
198 | _TOD_Lock(); |
---|
199 | _TOD_Acquire( &lock_context ); |
---|
200 | _TOD_Set( &tod_as_timestamp, &lock_context ); |
---|
201 | _TOD_Unlock(); |
---|
202 | } |
---|
203 | |
---|
204 | /** |
---|
205 | * @brief Gets the current time in the bintime format. |
---|
206 | * |
---|
207 | * @param[out] time is the value gathered by the bintime request |
---|
208 | */ |
---|
209 | static inline void _TOD_Get( |
---|
210 | Timestamp_Control *time |
---|
211 | ) |
---|
212 | { |
---|
213 | _Timecounter_Bintime(time); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * @brief Gets the current time in the timespec format. |
---|
218 | * |
---|
219 | * @param[out] time is the value gathered by the nanotime request |
---|
220 | */ |
---|
221 | static inline void _TOD_Get_as_timespec( |
---|
222 | struct timespec *time |
---|
223 | ) |
---|
224 | { |
---|
225 | _Timecounter_Nanotime(time); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
230 | * |
---|
231 | * This routine returns the system uptime with potential accuracy |
---|
232 | * to the nanosecond. |
---|
233 | * |
---|
234 | * The initial uptime value is undefined. |
---|
235 | * |
---|
236 | * @param[in] time is a pointer to the uptime to be returned |
---|
237 | */ |
---|
238 | static inline void _TOD_Get_uptime( |
---|
239 | Timestamp_Control *time |
---|
240 | ) |
---|
241 | { |
---|
242 | _Timecounter_Binuptime( time ); |
---|
243 | } |
---|
244 | |
---|
245 | /** |
---|
246 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
247 | * to the nanosecond. |
---|
248 | * |
---|
249 | * The initial uptime value is zero. |
---|
250 | * |
---|
251 | * @param[in] time is a pointer to the uptime to be returned |
---|
252 | */ |
---|
253 | static inline void _TOD_Get_zero_based_uptime( |
---|
254 | Timestamp_Control *time |
---|
255 | ) |
---|
256 | { |
---|
257 | _Timecounter_Binuptime( time ); |
---|
258 | --time->sec; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
263 | * |
---|
264 | * The initial uptime value is zero. |
---|
265 | * |
---|
266 | * @param[in] time is a pointer to the uptime to be returned |
---|
267 | */ |
---|
268 | static inline void _TOD_Get_zero_based_uptime_as_timespec( |
---|
269 | struct timespec *time |
---|
270 | ) |
---|
271 | { |
---|
272 | _Timecounter_Nanouptime( time ); |
---|
273 | --time->tv_sec; |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * @brief Number of seconds Since RTEMS epoch. |
---|
278 | * |
---|
279 | * The following contains the number of seconds from 00:00:00 |
---|
280 | * January 1, TOD_BASE_YEAR until the current time of day. |
---|
281 | */ |
---|
282 | static inline uint32_t _TOD_Seconds_since_epoch( void ) |
---|
283 | { |
---|
284 | return (uint32_t) _Timecounter_Time_second; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * @brief Gets number of ticks in a second. |
---|
289 | * |
---|
290 | * This method returns the number of ticks in a second. |
---|
291 | * |
---|
292 | * @note If the clock tick value does not multiply evenly into a second |
---|
293 | * then this number of ticks will be slightly shorter than a second. |
---|
294 | */ |
---|
295 | uint32_t TOD_TICKS_PER_SECOND_method(void); |
---|
296 | |
---|
297 | /** |
---|
298 | * @brief Gets number of ticks in a second. |
---|
299 | * |
---|
300 | * This method exists to hide the fact that TOD_TICKS_PER_SECOND can not |
---|
301 | * be implemented as a macro in a .h file due to visibility issues. |
---|
302 | * The Configuration Table is not available to SuperCore .h files but |
---|
303 | * is available to their .c files. |
---|
304 | */ |
---|
305 | #define TOD_TICKS_PER_SECOND TOD_TICKS_PER_SECOND_method() |
---|
306 | |
---|
307 | /** |
---|
308 | * This routine returns a timeval based upon the internal timespec format TOD. |
---|
309 | */ |
---|
310 | |
---|
311 | RTEMS_INLINE_ROUTINE void _TOD_Get_timeval( |
---|
312 | struct timeval *time |
---|
313 | ) |
---|
314 | { |
---|
315 | _Timecounter_Microtime( time ); |
---|
316 | } |
---|
317 | |
---|
318 | /** |
---|
319 | * @brief Adjust the Time of Time |
---|
320 | * |
---|
321 | * This method is used to adjust the current time of day by the |
---|
322 | * specified amount. |
---|
323 | * |
---|
324 | * @param[in] delta is the amount to adjust |
---|
325 | */ |
---|
326 | void _TOD_Adjust( |
---|
327 | const Timestamp_Control *delta |
---|
328 | ); |
---|
329 | |
---|
330 | /** |
---|
331 | * @brief Check if the TOD is Set |
---|
332 | * |
---|
333 | * @return TRUE is the time is set. FALSE otherwise. |
---|
334 | */ |
---|
335 | RTEMS_INLINE_ROUTINE bool _TOD_Is_set( void ) |
---|
336 | { |
---|
337 | return _TOD.is_set; |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * @brief Absolute timeout conversion results. |
---|
342 | * |
---|
343 | * This enumeration defines the possible results of converting |
---|
344 | * an absolute time used for timeouts to POSIX blocking calls to |
---|
345 | * a number of ticks for example. |
---|
346 | */ |
---|
347 | typedef enum { |
---|
348 | /** The timeout is invalid. */ |
---|
349 | TOD_ABSOLUTE_TIMEOUT_INVALID, |
---|
350 | /** The timeout represents a time that is in the past. */ |
---|
351 | TOD_ABSOLUTE_TIMEOUT_IS_IN_PAST, |
---|
352 | /** The timeout represents a time that is equal to the current time. */ |
---|
353 | TOD_ABSOLUTE_TIMEOUT_IS_NOW, |
---|
354 | /** The timeout represents a time that is in the future. */ |
---|
355 | TOD_ABSOLUTE_TIMEOUT_IS_IN_FUTURE, |
---|
356 | } TOD_Absolute_timeout_conversion_results; |
---|
357 | |
---|
358 | /** |
---|
359 | * @brief Convert absolute timeout to ticks. |
---|
360 | * |
---|
361 | * This method takes an absolute time being used as a timeout |
---|
362 | * to a blocking directive, validates it and returns the number |
---|
363 | * of corresponding clock ticks for use by the SuperCore. |
---|
364 | * |
---|
365 | * @param[in] abstime is a pointer to the timeout |
---|
366 | * @param[in] clock is the time source to use for the timeout |
---|
367 | * @param[out] ticks_out will contain the number of ticks |
---|
368 | * |
---|
369 | * @return This method returns the number of ticks in @a ticks_out |
---|
370 | * and a status value indicating whether the absolute time |
---|
371 | * is valid, in the past, equal to the current time or in |
---|
372 | * the future as it should be. |
---|
373 | */ |
---|
374 | TOD_Absolute_timeout_conversion_results _TOD_Absolute_timeout_to_ticks( |
---|
375 | const struct timespec *abstime, |
---|
376 | clockid_t clock, |
---|
377 | Watchdog_Interval *ticks_out |
---|
378 | ); |
---|
379 | |
---|
380 | /**@}*/ |
---|
381 | |
---|
382 | #ifdef __cplusplus |
---|
383 | } |
---|
384 | #endif |
---|
385 | |
---|
386 | #endif |
---|
387 | /* end of include file */ |
---|