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