source: rtems/cpukit/score/include/rtems/score/todimpl.h @ 7cd2484

4.115
Last change on this file since 7cd2484 was 7cd2484, checked in by Alexander Krutwig <alexander.krutwig@…>, on 05/12/15 at 12:32:47

timecounter: Use in RTEMS

Replace timestamp implementation with FreeBSD bintime and timecounters.

New test sptests/sptimecounter02.

Update #2271.

  • Property mode set to 100644
File size: 8.0 KB
Line 
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
25#include <sys/time.h>
26#include <time.h>
27
28#ifdef __cplusplus
29extern "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 */
132typedef struct {
133  /**
134   * @brief Time of day seconds trigger.
135   *
136   * This value specifies the nanoseconds since the last time of day second.
137   * It is updated and evaluated in _TOD_Tickle_ticks().  It is set in
138   * _TOD_Set_with_timestamp().
139   */
140  uint32_t seconds_trigger;
141
142  /**
143   *  @brief Indicates if the time of day is set.
144   *
145   *  This is true if the application has set the current
146   *  time of day, and false otherwise.
147   */
148  bool is_set;
149} TOD_Control;
150
151SCORE_EXTERN TOD_Control _TOD;
152
153/**
154 *  @brief Initializes the time of day handler.
155 *
156 *  Performs the initialization necessary for the Time Of Day handler.
157 */
158void _TOD_Handler_initialization(void);
159
160/**
161 *  @brief Sets the time of day from timestamp.
162 *
163 *  The @a tod_as_timestamp timestamp represents the time since UNIX epoch.
164 *  The watchdog seconds chain will be adjusted.
165 *
166 *  @param[in] tod_as_timestamp is the constant of the time of day as a timestamp
167 */
168void _TOD_Set_with_timestamp(
169  const Timestamp_Control *tod_as_timestamp
170);
171
172/**
173 *  @brief Sets the time of day from timespec.
174 *
175 *  The @a tod_as_timestamp timestamp represents the time since UNIX epoch.
176 *  The watchdog seconds chain will be adjusted.
177 *
178 *  In the process the input given as timespec will be transformed to FreeBSD
179 *  bintime format to guarantee the right format for later setting it with a
180 *  timestamp.
181 *
182 *  @param[in] tod_as_timespec is the constant of the time of day as a timespec
183 */
184static inline void _TOD_Set(
185  const struct timespec *tod_as_timespec
186)
187{
188  Timestamp_Control tod_as_timestamp;
189
190  _Timestamp_Set(
191    &tod_as_timestamp,
192    tod_as_timespec->tv_sec,
193    tod_as_timespec->tv_nsec
194  );
195  _TOD_Set_with_timestamp( &tod_as_timestamp );
196}
197
198/**
199 *  @brief Gets the current time in the bintime format.
200 *
201 *  @param[out] time is the value gathered by the bintime request
202 */
203static inline void _TOD_Get(
204  Timestamp_Control *time
205)
206{
207  _Timecounter_Bintime(time);
208}
209
210/**
211 *  @brief Gets the current time in the timespec format.
212 *
213 *  @param[out] time is the value gathered by the nanotime request
214 */
215static inline void _TOD_Get_as_timespec(
216  struct timespec *time
217)
218{
219  _Timecounter_Nanotime(time);
220}
221
222/**
223 *  @brief Gets the system uptime with potential accuracy to the nanosecond.
224 *
225 *  This routine returns the system uptime with potential accuracy
226 *  to the nanosecond.
227 *
228 *  The initial uptime value is undefined.
229 *
230 *  @param[in] time is a pointer to the uptime to be returned
231 */
232static inline void _TOD_Get_uptime(
233  Timestamp_Control *time
234)
235{
236  _Timecounter_Binuptime( time );
237}
238
239/**
240 *  @brief Gets the system uptime with potential accuracy to the nanosecond.
241 *  to the nanosecond.
242 *
243 *  The initial uptime value is zero.
244 *
245 *  @param[in] time is a pointer to the uptime to be returned
246 */
247static inline void _TOD_Get_zero_based_uptime(
248  Timestamp_Control *time
249)
250{
251  _Timecounter_Binuptime( time );
252  --time->sec;
253}
254
255/**
256 *  @brief Gets the system uptime with potential accuracy to the nanosecond.
257 *
258 *  The initial uptime value is zero.
259 *
260 *  @param[in] time is a pointer to the uptime to be returned
261 */
262static inline void _TOD_Get_zero_based_uptime_as_timespec(
263  struct timespec *time
264)
265{
266  _Timecounter_Nanouptime( time );
267  --time->tv_sec;
268}
269
270/**
271 *  @brief Number of seconds Since RTEMS epoch.
272 *
273 *  The following contains the number of seconds from 00:00:00
274 *  January 1, TOD_BASE_YEAR until the current time of day.
275 */
276static inline uint32_t _TOD_Seconds_since_epoch( void )
277{
278  return (uint32_t) _Timecounter_Time_second;
279}
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 */
287void _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 */
297uint32_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
313RTEMS_INLINE_ROUTINE void _TOD_Get_timeval(
314  struct timeval *time
315)
316{
317  _Timecounter_Microtime( time );
318}
319
320/**
321 * @brief Adjust the Time of Time
322 *
323 * This method is used to adjust the current time of day by the
324 * specified amount.
325 *
326 * @param[in] delta is the amount to adjust
327 */
328void _TOD_Adjust(
329  const Timestamp_Control timestamp
330);
331
332/**
333 * @brief Check if the TOD is Set
334 *
335 * @return TRUE is the time is set. FALSE otherwise.
336 */
337RTEMS_INLINE_ROUTINE bool _TOD_Is_set( void )
338{
339  return _TOD.is_set;
340}
341
342/**@}*/
343
344#ifdef __cplusplus
345}
346#endif
347
348#endif
349/* end of include file */
Note: See TracBrowser for help on using the repository browser.