source: rtems/cpukit/score/include/rtems/score/tod.h @ 3246b0f8

4.115
Last change on this file since 3246b0f8 was 3246b0f8, checked in by Sebastian Huber <sebastian.huber@…>, on 06/13/12 at 09:29:53

score: New structure TOD_Control

Group the global TOD variables (_TOD_Now, _TOD_Uptime, and _TOD_Is_set)
in a structure to reduce address loads in _TOD_Tickle_ticks().

  • Property mode set to 100644
File size: 7.0 KB
Line 
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
25extern "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 */
128typedef 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 Indicates if the time of day is set.
141   *
142   *  This is true if the application has set the current
143   *  time of day, and false otherwise.
144   */
145  bool is_set;
146} TOD_Control;
147
148SCORE_EXTERN TOD_Control _TOD;
149
150/**
151 *  @brief Seconds Since RTEMS Epoch
152 *
153 *  The following contains the number of seconds from 00:00:00
154 *  January 1, TOD_BASE_YEAR until the current time of day.
155 */
156#define _TOD_Seconds_since_epoch() \
157  _Timestamp_Get_seconds(&_TOD.now)
158
159/**
160 *  @brief _TOD_Handler_initialization
161 *
162 *  This routine performs the initialization necessary for this handler.
163 */
164void _TOD_Handler_initialization(void);
165
166/**
167 *  @brief Sets the time of day according to @a tod_as_timestamp.
168 *
169 *  The @a tod_as_timestamp timestamp represents the time since UNIX epoch.  The watchdog
170 *  seconds chain will be adjusted.
171 */
172void _TOD_Set_with_timestamp(
173  const Timestamp_Control *tod_as_timestamp
174);
175
176static inline void _TOD_Set(
177  const struct timespec *tod_as_timespec
178)
179{
180  Timestamp_Control tod_as_timestamp;
181
182  _Timestamp_Set(
183    &tod_as_timestamp,
184    tod_as_timespec->tv_sec,
185    tod_as_timespec->tv_nsec
186  );
187  _TOD_Set_with_timestamp( &tod_as_timestamp );
188}
189
190/**
191 *  @brief Returns the time of day in @a tod_as_timestamp.
192 *
193 *  The @a tod_as_timestamp timestamp represents the time since UNIX epoch.
194 */
195void _TOD_Get_as_timestamp(
196  Timestamp_Control *tod_as_timestamp
197);
198
199static inline void _TOD_Get(
200  struct timespec *tod_as_timespec
201)
202{
203  Timestamp_Control tod_as_timestamp;
204
205  _TOD_Get_as_timestamp( &tod_as_timestamp );
206  _Timestamp_To_timespec( &tod_as_timestamp, tod_as_timespec );
207}
208
209/**
210 *  @brief _TOD_Get_uptime
211 *
212 *  This routine returns the system uptime with potential accuracy
213 *  to the nanosecond.
214 *
215 *  @param[in] time is a pointer to the uptime to be returned
216 */
217void _TOD_Get_uptime(
218  Timestamp_Control *time
219);
220
221/**
222 *  @brief _TOD_Get_uptime_as_timespec
223 *
224 *  This routine returns the system uptime with potential accuracy
225 *  to the nanosecond.
226 *
227 *  @param[in] time is a pointer to the uptime to be returned
228 */
229void _TOD_Get_uptime_as_timespec(
230  struct timespec *time
231);
232
233/**
234 *  This routine increments the ticks field of the current time of
235 *  day at each clock tick.
236 */
237void _TOD_Tickle_ticks( void );
238
239/**
240 *  @brief TOD_MILLISECONDS_TO_MICROSECONDS
241 *
242 *  This routine converts an interval expressed in milliseconds to microseconds.
243 *
244 *  @note This must be a macro so it can be used in "static" tables.
245 */
246#define TOD_MILLISECONDS_TO_MICROSECONDS(_ms) ((uint32_t)(_ms) * 1000L)
247
248/**
249 *  @brief TOD_MICROSECONDS_TO_TICKS
250 *
251 *  This routine converts an interval expressed in microseconds to ticks.
252 *
253 *  @note This must be a macro so it can be used in "static" tables.
254 */
255uint32_t TOD_MICROSECONDS_TO_TICKS(
256  uint32_t microseconds
257);
258
259/**
260 *  @brief TOD_MILLISECONDS_TO_TICKS
261 *
262 *  This routine converts an interval expressed in milliseconds to ticks.
263 *
264 *  @note This must be a macro so it can be used in "static" tables.
265 */
266uint32_t TOD_MILLISECONDS_TO_TICKS(
267  uint32_t milliseconds
268);
269
270/**
271 *  @brief How many ticks in a second?
272 *
273 *  This method returns the number of ticks in a second.
274 *
275 *  @note If the clock tick value does not multiply evenly into a second
276 *        then this number of ticks will be slightly shorter than a second.
277 */
278uint32_t TOD_TICKS_PER_SECOND_method(void);
279
280/**
281 *  @brief Method to return number of ticks in a second
282 *
283 *  This method exists to hide the fact that TOD_TICKS_PER_SECOND can not
284 *  be implemented as a macro in a .h file due to visibility issues.
285 *  The Configuration Table is not available to SuperCore .h files but
286 *  is available to their .c files.
287 */
288#define TOD_TICKS_PER_SECOND TOD_TICKS_PER_SECOND_method()
289
290#ifndef __RTEMS_APPLICATION__
291#include <rtems/score/tod.inl>
292#endif
293
294#ifdef __cplusplus
295}
296#endif
297
298/**@}*/
299
300#endif
301/* end of include file */
Note: See TracBrowser for help on using the repository browser.