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