1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RTEMSScoreTOD |
---|
5 | * @ingroup RTEMSScoreTODHooks |
---|
6 | * |
---|
7 | * @brief This header file provides the interfaces of the |
---|
8 | * @ref RTEMSScoreTOD and the @ref RTEMSScoreTODHooks. |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * COPYRIGHT (c) 1989-2009. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.org/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef _RTEMS_SCORE_TODIMPL_H |
---|
21 | #define _RTEMS_SCORE_TODIMPL_H |
---|
22 | |
---|
23 | #include <rtems/score/status.h> |
---|
24 | #include <rtems/score/timestamp.h> |
---|
25 | #include <rtems/score/timecounterimpl.h> |
---|
26 | #include <rtems/score/watchdog.h> |
---|
27 | |
---|
28 | #include <sys/time.h> |
---|
29 | #include <time.h> |
---|
30 | |
---|
31 | #ifdef __cplusplus |
---|
32 | extern "C" { |
---|
33 | #endif |
---|
34 | |
---|
35 | /** |
---|
36 | * @defgroup RTEMSScoreTOD Time of Day Handler |
---|
37 | * |
---|
38 | * @ingroup RTEMSScore |
---|
39 | * |
---|
40 | * @brief This group contains the Time of Day Handler implementation. |
---|
41 | * |
---|
42 | * The following constants are related to the time of day and are |
---|
43 | * independent of RTEMS. |
---|
44 | * |
---|
45 | * @{ |
---|
46 | */ |
---|
47 | |
---|
48 | /** |
---|
49 | * This constant represents the number of seconds in a minute. |
---|
50 | */ |
---|
51 | #define TOD_SECONDS_PER_MINUTE (uint32_t)60 |
---|
52 | |
---|
53 | /** |
---|
54 | * This constant represents the number of minutes per hour. |
---|
55 | */ |
---|
56 | #define TOD_MINUTES_PER_HOUR (uint32_t)60 |
---|
57 | |
---|
58 | /** |
---|
59 | * This constant represents the number of months in a year. |
---|
60 | */ |
---|
61 | #define TOD_MONTHS_PER_YEAR (uint32_t)12 |
---|
62 | |
---|
63 | /** |
---|
64 | * This constant represents the number of days in a non-leap year. |
---|
65 | */ |
---|
66 | #define TOD_DAYS_PER_YEAR (uint32_t)365 |
---|
67 | |
---|
68 | /** |
---|
69 | * This constant represents the number of hours per day. |
---|
70 | */ |
---|
71 | #define TOD_HOURS_PER_DAY (uint32_t)24 |
---|
72 | |
---|
73 | /** |
---|
74 | * This constant represents the number of seconds in a day which does |
---|
75 | * not include a leap second. |
---|
76 | */ |
---|
77 | #define TOD_SECONDS_PER_DAY (uint32_t) (TOD_SECONDS_PER_MINUTE * \ |
---|
78 | TOD_MINUTES_PER_HOUR * \ |
---|
79 | TOD_HOURS_PER_DAY) |
---|
80 | |
---|
81 | /** |
---|
82 | * This constant represents the number of seconds in a non-leap year. |
---|
83 | */ |
---|
84 | #define TOD_SECONDS_PER_NON_LEAP_YEAR (365 * TOD_SECONDS_PER_DAY) |
---|
85 | |
---|
86 | /** |
---|
87 | * This constant represents the number of millisecond in a second. |
---|
88 | */ |
---|
89 | #define TOD_MILLISECONDS_PER_SECOND (uint32_t)1000 |
---|
90 | |
---|
91 | /** |
---|
92 | * This constant represents the number of microseconds in a second. |
---|
93 | */ |
---|
94 | #define TOD_MICROSECONDS_PER_SECOND (uint32_t)1000000 |
---|
95 | |
---|
96 | /** |
---|
97 | * This constant represents the number of nanoseconds in a second. |
---|
98 | */ |
---|
99 | #define TOD_NANOSECONDS_PER_SECOND (uint32_t)1000000000 |
---|
100 | |
---|
101 | /** |
---|
102 | * This constant represents the number of nanoseconds in a mircosecond. |
---|
103 | */ |
---|
104 | #define TOD_NANOSECONDS_PER_MICROSECOND (uint32_t)1000 |
---|
105 | |
---|
106 | /**@}*/ |
---|
107 | |
---|
108 | /** |
---|
109 | * Seconds from January 1, 1970 to January 1, 1988. Used to account for |
---|
110 | * differences between POSIX API and RTEMS core. The timespec format time |
---|
111 | * is kept in POSIX compliant form. |
---|
112 | */ |
---|
113 | #define TOD_SECONDS_1970_THROUGH_1988 \ |
---|
114 | (((1987 - 1970 + 1) * TOD_SECONDS_PER_NON_LEAP_YEAR) + \ |
---|
115 | (4 * TOD_SECONDS_PER_DAY)) |
---|
116 | |
---|
117 | /** |
---|
118 | * @brief Earliest year to which an time of day can be initialized. |
---|
119 | * |
---|
120 | * The following constant define the earliest year to which an |
---|
121 | * time of day can be initialized. This is considered the |
---|
122 | * epoch. |
---|
123 | */ |
---|
124 | #define TOD_BASE_YEAR 1988 |
---|
125 | |
---|
126 | /** |
---|
127 | * @addtogroup RTEMSScoreTOD |
---|
128 | * |
---|
129 | * This handler encapsulates functionality used to manage time of day. |
---|
130 | * |
---|
131 | * @{ |
---|
132 | */ |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief TOD control. |
---|
136 | */ |
---|
137 | typedef struct { |
---|
138 | /** |
---|
139 | * @brief Indicates if the time of day is set. |
---|
140 | * |
---|
141 | * This is true if the application has set the current |
---|
142 | * time of day, and false otherwise. |
---|
143 | */ |
---|
144 | bool is_set; |
---|
145 | } TOD_Control; |
---|
146 | |
---|
147 | /** |
---|
148 | * @brief TOD Management information |
---|
149 | */ |
---|
150 | extern TOD_Control _TOD; |
---|
151 | |
---|
152 | /** |
---|
153 | * @brief Locks the time of day mutex. |
---|
154 | */ |
---|
155 | void _TOD_Lock( void ); |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief Unlocks the time of day mutex. |
---|
159 | */ |
---|
160 | void _TOD_Unlock( void ); |
---|
161 | |
---|
162 | /** |
---|
163 | * @brief Checks if api mutex is owner of the time of day mutex. |
---|
164 | * |
---|
165 | * @retval true It is owner of the time of day mutex. |
---|
166 | * @retval false It is not owner of the time of day mutex. |
---|
167 | */ |
---|
168 | #if defined(RTEMS_DEBUG) |
---|
169 | bool _TOD_Is_owner( void ); |
---|
170 | #endif |
---|
171 | |
---|
172 | /** |
---|
173 | * @brief Acquires the lock context for the timecounter. |
---|
174 | * |
---|
175 | * @param lock_context The lock to acquire. |
---|
176 | */ |
---|
177 | static inline void _TOD_Acquire( ISR_lock_Context *lock_context ) |
---|
178 | { |
---|
179 | _Timecounter_Acquire( lock_context ); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * @brief Releases the lock context for the timecounter. |
---|
184 | * |
---|
185 | * @param lock_context The lock to release. |
---|
186 | */ |
---|
187 | static inline void _TOD_Release( ISR_lock_Context *lock_context ) |
---|
188 | { |
---|
189 | _Timecounter_Release( lock_context ); |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief Sets the time of day. |
---|
194 | * |
---|
195 | * The caller must be the owner of the TOD lock. |
---|
196 | * |
---|
197 | * @param tod The new time of day in timespec format representing |
---|
198 | * the time since UNIX Epoch. |
---|
199 | * @param lock_context The ISR lock context used for the corresponding |
---|
200 | * _TOD_Acquire(). The caller must be the owner of the TOD lock. This |
---|
201 | * function will release the TOD lock. |
---|
202 | * |
---|
203 | * @retval STATUS_SUCCESSFUL Successful operation. |
---|
204 | * @retval other Some error occurred. |
---|
205 | */ |
---|
206 | Status_Control _TOD_Set( |
---|
207 | const struct timespec *tod, |
---|
208 | ISR_lock_Context *lock_context |
---|
209 | ); |
---|
210 | |
---|
211 | /** |
---|
212 | * @brief Gets the current time in the timespec format. |
---|
213 | * |
---|
214 | * @param[out] time The value gathered by the request. |
---|
215 | */ |
---|
216 | static inline void _TOD_Get( |
---|
217 | struct timespec *tod |
---|
218 | ) |
---|
219 | { |
---|
220 | _Timecounter_Nanotime( tod ); |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
225 | * |
---|
226 | * This routine returns the system uptime with potential accuracy |
---|
227 | * to the nanosecond. |
---|
228 | * |
---|
229 | * The initial uptime value is undefined. |
---|
230 | * |
---|
231 | * @param[out] time Is a pointer to the uptime after the method call. |
---|
232 | */ |
---|
233 | static inline void _TOD_Get_uptime( |
---|
234 | Timestamp_Control *time |
---|
235 | ) |
---|
236 | { |
---|
237 | *time = _Timecounter_Sbinuptime(); |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
242 | * |
---|
243 | * The initial uptime value is zero. |
---|
244 | * |
---|
245 | * @param[out] time Is a pointer to the uptime after the method call. |
---|
246 | */ |
---|
247 | static inline void _TOD_Get_zero_based_uptime( |
---|
248 | Timestamp_Control *time |
---|
249 | ) |
---|
250 | { |
---|
251 | *time = _Timecounter_Sbinuptime() - SBT_1S; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
256 | * |
---|
257 | * The initial uptime value is zero. |
---|
258 | * |
---|
259 | * @param[out] time Is a pointer to the uptime after the method call. |
---|
260 | */ |
---|
261 | static inline void _TOD_Get_zero_based_uptime_as_timespec( |
---|
262 | struct timespec *time |
---|
263 | ) |
---|
264 | { |
---|
265 | _Timecounter_Nanouptime( time ); |
---|
266 | --time->tv_sec; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * @brief Returns Number of seconds Since RTEMS epoch. |
---|
271 | * |
---|
272 | * The following contains the number of seconds from 00:00:00 |
---|
273 | * January 1, TOD_BASE_YEAR until the current time of day. |
---|
274 | * |
---|
275 | * @return The number of seconds since RTEMS epoch. |
---|
276 | */ |
---|
277 | static inline uint32_t _TOD_Seconds_since_epoch( void ) |
---|
278 | { |
---|
279 | return (uint32_t) _Timecounter_Time_second; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * @brief Gets number of ticks in a second. |
---|
284 | * |
---|
285 | * This method returns the number of ticks in a second. |
---|
286 | * |
---|
287 | * @note If the clock tick value does not multiply evenly into a second |
---|
288 | * then this number of ticks will be slightly shorter than a second. |
---|
289 | * |
---|
290 | * @return The number of ticks in a second. |
---|
291 | */ |
---|
292 | uint32_t TOD_TICKS_PER_SECOND_method(void); |
---|
293 | |
---|
294 | /** |
---|
295 | * @brief Gets number of ticks in a second. |
---|
296 | * |
---|
297 | * This method exists to hide the fact that TOD_TICKS_PER_SECOND can not |
---|
298 | * be implemented as a macro in a .h file due to visibility issues. |
---|
299 | * The Configuration Table is not available to SuperCore .h files but |
---|
300 | * is available to their .c files. |
---|
301 | */ |
---|
302 | #define TOD_TICKS_PER_SECOND TOD_TICKS_PER_SECOND_method() |
---|
303 | |
---|
304 | /** |
---|
305 | * @brief This routine returns a timeval based upon the internal timespec |
---|
306 | * format TOD. |
---|
307 | * |
---|
308 | * @param[out] time The timeval to be filled in by the method. |
---|
309 | */ |
---|
310 | RTEMS_INLINE_ROUTINE void _TOD_Get_timeval( |
---|
311 | struct timeval *time |
---|
312 | ) |
---|
313 | { |
---|
314 | _Timecounter_Microtime( time ); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * @brief Adjusts the Time of Time. |
---|
319 | * |
---|
320 | * This method is used to adjust the current time of day by the |
---|
321 | * specified amount. |
---|
322 | * |
---|
323 | * @param delta is the amount to adjust. |
---|
324 | */ |
---|
325 | void _TOD_Adjust( |
---|
326 | const struct timespec *delta |
---|
327 | ); |
---|
328 | |
---|
329 | /** |
---|
330 | * @brief Check if the TOD is Set |
---|
331 | * |
---|
332 | * @retval true The time is set. |
---|
333 | * @retval false The time is not set. |
---|
334 | */ |
---|
335 | RTEMS_INLINE_ROUTINE bool _TOD_Is_set( void ) |
---|
336 | { |
---|
337 | return _TOD.is_set; |
---|
338 | } |
---|
339 | |
---|
340 | /** @} */ |
---|
341 | |
---|
342 | /** |
---|
343 | * @defgroup RTEMSScoreTODHooks Time of Day Handler Action Hooks |
---|
344 | * |
---|
345 | * @ingroup RTEMSScoreTOD |
---|
346 | * |
---|
347 | * @brief This group contains the implementation to support Time of Day Handler |
---|
348 | * action hooks. |
---|
349 | * |
---|
350 | * The following support registering a hook which is invoked |
---|
351 | * when the TOD is set. These can be used by a paravirtualized |
---|
352 | * BSP to mirror time changes to the hosting environment or a |
---|
353 | * regular BSP to program a real-time clock when the RTEMS TOD |
---|
354 | * is set. |
---|
355 | * |
---|
356 | * @{ |
---|
357 | */ |
---|
358 | |
---|
359 | /** |
---|
360 | * @brief Possible actions where a registered hook could be invoked |
---|
361 | */ |
---|
362 | typedef enum { |
---|
363 | /** |
---|
364 | * @brief Constant to indicate the TOD is being set. |
---|
365 | */ |
---|
366 | TOD_ACTION_SET_CLOCK |
---|
367 | } TOD_Action; |
---|
368 | |
---|
369 | /** |
---|
370 | * @brief Structure to manage each TOD action hook |
---|
371 | */ |
---|
372 | typedef struct TOD_Hook { |
---|
373 | /** This is the chain node portion of an object. */ |
---|
374 | Chain_Node Node; |
---|
375 | |
---|
376 | /** This is the TOD action hook that is invoked. */ |
---|
377 | Status_Control ( *handler )( TOD_Action, const struct timespec * ); |
---|
378 | } TOD_Hook; |
---|
379 | |
---|
380 | /** |
---|
381 | * @brief Set of registered methods for TOD Actions |
---|
382 | */ |
---|
383 | extern Chain_Control _TOD_Hooks; |
---|
384 | |
---|
385 | /** |
---|
386 | * @brief Add a TOD Action Hook |
---|
387 | * |
---|
388 | * This method is used to add a hook to the TOD action set. |
---|
389 | * |
---|
390 | * @brief hook is the action hook to register. |
---|
391 | */ |
---|
392 | void _TOD_Hook_Register( |
---|
393 | TOD_Hook *hook |
---|
394 | ); |
---|
395 | |
---|
396 | /** |
---|
397 | * @brief Remove a TOD Action Hook |
---|
398 | * |
---|
399 | * This method is used to remove a hook from the TOD action set. |
---|
400 | * |
---|
401 | * @brief hook is the action hook to unregister. |
---|
402 | */ |
---|
403 | void _TOD_Hook_Unregister( |
---|
404 | TOD_Hook *hook |
---|
405 | ); |
---|
406 | |
---|
407 | /** |
---|
408 | * @brief Run the TOD Action Hooks |
---|
409 | * |
---|
410 | * This method is used to invoke the set of TOD action hooks. |
---|
411 | * |
---|
412 | * @brief action The action which triggered this run. |
---|
413 | * @brief tod The current time of day. |
---|
414 | * |
---|
415 | * @retval STATUS_SUCCESSFUL Successful operation. |
---|
416 | * @retval other Some error occurred. |
---|
417 | */ |
---|
418 | Status_Control _TOD_Hook_Run( |
---|
419 | TOD_Action action, |
---|
420 | const struct timespec *tod |
---|
421 | ); |
---|
422 | |
---|
423 | |
---|
424 | /** @} */ |
---|
425 | |
---|
426 | #ifdef __cplusplus |
---|
427 | } |
---|
428 | #endif |
---|
429 | |
---|
430 | #endif |
---|
431 | /* end of include file */ |
---|