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/timestamp.h> |
---|
23 | #include <rtems/score/timecounterimpl.h> |
---|
24 | #include <rtems/score/watchdog.h> |
---|
25 | |
---|
26 | #include <sys/time.h> |
---|
27 | #include <time.h> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif |
---|
32 | |
---|
33 | /** |
---|
34 | * @defgroup ScoreTOD Time of Day Handler |
---|
35 | * |
---|
36 | * @ingroup Score |
---|
37 | * |
---|
38 | * The following constants are related to the time of day and are |
---|
39 | * independent of RTEMS. |
---|
40 | */ |
---|
41 | /**@{*/ |
---|
42 | |
---|
43 | /** |
---|
44 | * This constant represents the number of seconds in a minute. |
---|
45 | */ |
---|
46 | #define TOD_SECONDS_PER_MINUTE (uint32_t)60 |
---|
47 | |
---|
48 | /** |
---|
49 | * This constant represents the number of minutes per hour. |
---|
50 | */ |
---|
51 | #define TOD_MINUTES_PER_HOUR (uint32_t)60 |
---|
52 | |
---|
53 | /** |
---|
54 | * This constant represents the number of months in a year. |
---|
55 | */ |
---|
56 | #define TOD_MONTHS_PER_YEAR (uint32_t)12 |
---|
57 | |
---|
58 | /** |
---|
59 | * This constant represents the number of days in a non-leap year. |
---|
60 | */ |
---|
61 | #define TOD_DAYS_PER_YEAR (uint32_t)365 |
---|
62 | |
---|
63 | /** |
---|
64 | * This constant represents the number of hours per day. |
---|
65 | */ |
---|
66 | #define TOD_HOURS_PER_DAY (uint32_t)24 |
---|
67 | |
---|
68 | /** |
---|
69 | * This constant represents the number of seconds in a day which does |
---|
70 | * not include a leap second. |
---|
71 | */ |
---|
72 | #define TOD_SECONDS_PER_DAY (uint32_t) (TOD_SECONDS_PER_MINUTE * \ |
---|
73 | TOD_MINUTES_PER_HOUR * \ |
---|
74 | TOD_HOURS_PER_DAY) |
---|
75 | |
---|
76 | /** |
---|
77 | * This constant represents the number of seconds in a non-leap year. |
---|
78 | */ |
---|
79 | #define TOD_SECONDS_PER_NON_LEAP_YEAR (365 * TOD_SECONDS_PER_DAY) |
---|
80 | |
---|
81 | /** |
---|
82 | * This constant represents the number of millisecond in a second. |
---|
83 | */ |
---|
84 | #define TOD_MILLISECONDS_PER_SECOND (uint32_t)1000 |
---|
85 | |
---|
86 | /** |
---|
87 | * This constant represents the number of microseconds in a second. |
---|
88 | */ |
---|
89 | #define TOD_MICROSECONDS_PER_SECOND (uint32_t)1000000 |
---|
90 | |
---|
91 | /** |
---|
92 | * This constant represents the number of nanoseconds in a second. |
---|
93 | */ |
---|
94 | #define TOD_NANOSECONDS_PER_SECOND (uint32_t)1000000000 |
---|
95 | |
---|
96 | /** |
---|
97 | * This constant represents the number of nanoseconds in a mircosecond. |
---|
98 | */ |
---|
99 | #define TOD_NANOSECONDS_PER_MICROSECOND (uint32_t)1000 |
---|
100 | |
---|
101 | /**@}*/ |
---|
102 | |
---|
103 | /** |
---|
104 | * Seconds from January 1, 1970 to January 1, 1988. Used to account for |
---|
105 | * differences between POSIX API and RTEMS core. The timespec format time |
---|
106 | * is kept in POSIX compliant form. |
---|
107 | */ |
---|
108 | #define TOD_SECONDS_1970_THROUGH_1988 \ |
---|
109 | (((1987 - 1970 + 1) * TOD_SECONDS_PER_NON_LEAP_YEAR) + \ |
---|
110 | (4 * TOD_SECONDS_PER_DAY)) |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief Earliest year to which an time of day can be initialized. |
---|
114 | * |
---|
115 | * The following constant define the earliest year to which an |
---|
116 | * time of day can be initialized. This is considered the |
---|
117 | * epoch. |
---|
118 | */ |
---|
119 | #define TOD_BASE_YEAR 1988 |
---|
120 | |
---|
121 | /** |
---|
122 | * @defgroup ScoreTOD Time Of Day (TOD) Handler |
---|
123 | * |
---|
124 | * @ingroup Score |
---|
125 | * |
---|
126 | * This handler encapsulates functionality used to manage time of day. |
---|
127 | */ |
---|
128 | /**@{*/ |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief TOD control. |
---|
132 | */ |
---|
133 | typedef struct { |
---|
134 | /** |
---|
135 | * @brief Indicates if the time of day is set. |
---|
136 | * |
---|
137 | * This is true if the application has set the current |
---|
138 | * time of day, and false otherwise. |
---|
139 | */ |
---|
140 | bool is_set; |
---|
141 | } TOD_Control; |
---|
142 | |
---|
143 | extern TOD_Control _TOD; |
---|
144 | |
---|
145 | void _TOD_Lock( void ); |
---|
146 | |
---|
147 | void _TOD_Unlock( void ); |
---|
148 | |
---|
149 | #if defined(RTEMS_DEBUG) |
---|
150 | bool _TOD_Is_owner( void ); |
---|
151 | #endif |
---|
152 | |
---|
153 | static inline void _TOD_Acquire( ISR_lock_Context *lock_context ) |
---|
154 | { |
---|
155 | _Timecounter_Acquire( lock_context ); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief Sets the time of day. |
---|
160 | * |
---|
161 | * The caller must be the owner of the TOD lock. |
---|
162 | * |
---|
163 | * @param tod The new time of day in timespec format representing |
---|
164 | * the time since UNIX Epoch. |
---|
165 | * @param lock_context The ISR lock context used for the corresponding |
---|
166 | * _TOD_Acquire(). The caller must be the owner of the TOD lock. This |
---|
167 | * function will release the TOD lock. |
---|
168 | */ |
---|
169 | void _TOD_Set( |
---|
170 | const struct timespec *tod, |
---|
171 | ISR_lock_Context *lock_context |
---|
172 | ); |
---|
173 | |
---|
174 | /** |
---|
175 | * @brief Gets the current time in the timespec format. |
---|
176 | * |
---|
177 | * @param[out] time is the value gathered by the request |
---|
178 | */ |
---|
179 | static inline void _TOD_Get( |
---|
180 | struct timespec *tod |
---|
181 | ) |
---|
182 | { |
---|
183 | _Timecounter_Nanotime( tod ); |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
188 | * |
---|
189 | * This routine returns the system uptime with potential accuracy |
---|
190 | * to the nanosecond. |
---|
191 | * |
---|
192 | * The initial uptime value is undefined. |
---|
193 | * |
---|
194 | * @param[in] time is a pointer to the uptime to be returned |
---|
195 | */ |
---|
196 | static inline void _TOD_Get_uptime( |
---|
197 | Timestamp_Control *time |
---|
198 | ) |
---|
199 | { |
---|
200 | *time = _Timecounter_Sbinuptime(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
205 | * to the nanosecond. |
---|
206 | * |
---|
207 | * The initial uptime value is zero. |
---|
208 | * |
---|
209 | * @param[in] time is a pointer to the uptime to be returned |
---|
210 | */ |
---|
211 | static inline void _TOD_Get_zero_based_uptime( |
---|
212 | Timestamp_Control *time |
---|
213 | ) |
---|
214 | { |
---|
215 | *time = _Timecounter_Sbinuptime() - SBT_1S; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * @brief Gets the system uptime with potential accuracy to the nanosecond. |
---|
220 | * |
---|
221 | * The initial uptime value is zero. |
---|
222 | * |
---|
223 | * @param[in] time is a pointer to the uptime to be returned |
---|
224 | */ |
---|
225 | static inline void _TOD_Get_zero_based_uptime_as_timespec( |
---|
226 | struct timespec *time |
---|
227 | ) |
---|
228 | { |
---|
229 | _Timecounter_Nanouptime( time ); |
---|
230 | --time->tv_sec; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * @brief Number of seconds Since RTEMS epoch. |
---|
235 | * |
---|
236 | * The following contains the number of seconds from 00:00:00 |
---|
237 | * January 1, TOD_BASE_YEAR until the current time of day. |
---|
238 | */ |
---|
239 | static inline uint32_t _TOD_Seconds_since_epoch( void ) |
---|
240 | { |
---|
241 | return (uint32_t) _Timecounter_Time_second; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * @brief Gets number of ticks in a second. |
---|
246 | * |
---|
247 | * This method returns the number of ticks in a second. |
---|
248 | * |
---|
249 | * @note If the clock tick value does not multiply evenly into a second |
---|
250 | * then this number of ticks will be slightly shorter than a second. |
---|
251 | */ |
---|
252 | uint32_t TOD_TICKS_PER_SECOND_method(void); |
---|
253 | |
---|
254 | /** |
---|
255 | * @brief Gets number of ticks in a second. |
---|
256 | * |
---|
257 | * This method exists to hide the fact that TOD_TICKS_PER_SECOND can not |
---|
258 | * be implemented as a macro in a .h file due to visibility issues. |
---|
259 | * The Configuration Table is not available to SuperCore .h files but |
---|
260 | * is available to their .c files. |
---|
261 | */ |
---|
262 | #define TOD_TICKS_PER_SECOND TOD_TICKS_PER_SECOND_method() |
---|
263 | |
---|
264 | /** |
---|
265 | * This routine returns a timeval based upon the internal timespec format TOD. |
---|
266 | */ |
---|
267 | |
---|
268 | RTEMS_INLINE_ROUTINE void _TOD_Get_timeval( |
---|
269 | struct timeval *time |
---|
270 | ) |
---|
271 | { |
---|
272 | _Timecounter_Microtime( time ); |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * @brief Adjust the Time of Time |
---|
277 | * |
---|
278 | * This method is used to adjust the current time of day by the |
---|
279 | * specified amount. |
---|
280 | * |
---|
281 | * @param[in] delta is the amount to adjust |
---|
282 | */ |
---|
283 | void _TOD_Adjust( |
---|
284 | const struct timespec *delta |
---|
285 | ); |
---|
286 | |
---|
287 | /** |
---|
288 | * @brief Check if the TOD is Set |
---|
289 | * |
---|
290 | * @return TRUE is the time is set. FALSE otherwise. |
---|
291 | */ |
---|
292 | RTEMS_INLINE_ROUTINE bool _TOD_Is_set( void ) |
---|
293 | { |
---|
294 | return _TOD.is_set; |
---|
295 | } |
---|
296 | |
---|
297 | /**@}*/ |
---|
298 | |
---|
299 | #ifdef __cplusplus |
---|
300 | } |
---|
301 | #endif |
---|
302 | |
---|
303 | #endif |
---|
304 | /* end of include file */ |
---|