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