[4b487363] | 1 | /** |
---|
| 2 | * @file rtems/rtems/clock.h |
---|
[067a96a] | 3 | * |
---|
[d106ab3] | 4 | * @defgroup ClassicClock Clocks |
---|
[f1eb368d] | 5 | * |
---|
[d106ab3] | 6 | * @ingroup ClassicRTEMS |
---|
| 7 | * @brief Clock Manager API |
---|
[ac7d5ef0] | 8 | * |
---|
[d106ab3] | 9 | * This include file contains all the constants and structures associated |
---|
| 10 | * with the Clock Manager. This manager provides facilities to set, obtain, |
---|
| 11 | * and continually update the current date and time. |
---|
[ac7d5ef0] | 12 | * |
---|
[d106ab3] | 13 | * This manager provides directives to: |
---|
| 14 | * |
---|
| 15 | * - set the current date and time |
---|
| 16 | * - obtain the current date and time |
---|
| 17 | * - announce a clock tick |
---|
| 18 | * - obtain the system uptime |
---|
[067a96a] | 19 | */ |
---|
| 20 | |
---|
[651e3aa] | 21 | /* COPYRIGHT (c) 1989-2013. |
---|
[d106ab3] | 22 | * On-Line Applications Research Corporation (OAR). |
---|
[ac7d5ef0] | 23 | * |
---|
[d106ab3] | 24 | * The license and distribution terms for this file may be |
---|
| 25 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 26 | * http://www.rtems.org/license/LICENSE. |
---|
[ac7d5ef0] | 27 | */ |
---|
| 28 | |
---|
[092f142a] | 29 | #ifndef _RTEMS_RTEMS_CLOCK_H |
---|
| 30 | #define _RTEMS_RTEMS_CLOCK_H |
---|
[ac7d5ef0] | 31 | |
---|
[812da54] | 32 | #include <rtems/score/watchdog.h> |
---|
| 33 | #include <rtems/rtems/status.h> |
---|
[3a4ae6c] | 34 | #include <rtems/rtems/types.h> |
---|
[96ec8ee8] | 35 | #include <rtems/config.h> |
---|
[6fcf3732] | 36 | |
---|
[067a96a] | 37 | /** |
---|
[c85ab23] | 38 | * @defgroup ClassicClock Clocks |
---|
| 39 | * |
---|
| 40 | * @ingroup ClassicRTEMS |
---|
[067a96a] | 41 | * |
---|
[651e3aa] | 42 | * This encapsulates functionality related to the Classic API Clock |
---|
| 43 | * Manager. |
---|
[067a96a] | 44 | */ |
---|
| 45 | /**@{*/ |
---|
| 46 | |
---|
| 47 | #ifdef __cplusplus |
---|
| 48 | extern "C" { |
---|
| 49 | #endif |
---|
| 50 | |
---|
[d7d7859] | 51 | /** |
---|
[d106ab3] | 52 | * @brief Obtain Current Time of Day (Classic TOD) |
---|
[ac7d5ef0] | 53 | * |
---|
[d106ab3] | 54 | * This routine implements the rtems_clock_get_tod directive. It returns |
---|
| 55 | * the current time of day in the format defined by RTEID. |
---|
[d7d7859] | 56 | * |
---|
[d106ab3] | 57 | * Clock Manager - rtems_clock_get_tod |
---|
[4efe1955] | 58 | * |
---|
[d106ab3] | 59 | * @param[in] time_buffer points to the time of day structure |
---|
[d7d7859] | 60 | * |
---|
[d106ab3] | 61 | * @retval This method returns RTEMS_SUCCESSFUL if there was not an |
---|
| 62 | * error. Otherwise, a status code is returned indicating the |
---|
| 63 | * source of the error. If successful, the time_buffer will |
---|
| 64 | * be filled in with the current time of day. |
---|
[d7d7859] | 65 | */ |
---|
| 66 | rtems_status_code rtems_clock_get_tod( |
---|
| 67 | rtems_time_of_day *time_buffer |
---|
| 68 | ); |
---|
| 69 | |
---|
| 70 | /** |
---|
[d106ab3] | 71 | * @brief Obtain TOD in struct timeval Format |
---|
[d7d7859] | 72 | * |
---|
[d106ab3] | 73 | * This routine implements the rtems_clock_get_tod_timeval |
---|
| 74 | * directive. |
---|
[d7d7859] | 75 | * |
---|
[d106ab3] | 76 | * @param[in] time points to the struct timeval variable to fill in |
---|
[d7d7859] | 77 | * |
---|
[d106ab3] | 78 | * @retval This method returns RTEMS_SUCCESSFUL if there was not an |
---|
| 79 | * error. Otherwise, a status code is returned indicating the |
---|
| 80 | * source of the error. If successful, the time will |
---|
| 81 | * be filled in with the current time of day. |
---|
[d7d7859] | 82 | */ |
---|
| 83 | rtems_status_code rtems_clock_get_tod_timeval( |
---|
| 84 | struct timeval *time |
---|
| 85 | ); |
---|
| 86 | |
---|
| 87 | /** |
---|
[d106ab3] | 88 | * @brief Obtain Seconds Since Epoch |
---|
[d7d7859] | 89 | * |
---|
[d106ab3] | 90 | * This routine implements the rtems_clock_get_seconds_since_epoch |
---|
| 91 | * directive. |
---|
[d7d7859] | 92 | * |
---|
[d106ab3] | 93 | * @param[in] the_interval points to the interval variable to fill in |
---|
[d7d7859] | 94 | * |
---|
[d106ab3] | 95 | * @retval This method returns RTEMS_SUCCESSFUL if there was not an |
---|
| 96 | * error. Otherwise, a status code is returned indicating the |
---|
| 97 | * source of the error. If successful, the time_buffer will |
---|
| 98 | * be filled in with the current time of day. |
---|
[d7d7859] | 99 | */ |
---|
| 100 | rtems_status_code rtems_clock_get_seconds_since_epoch( |
---|
| 101 | rtems_interval *the_interval |
---|
| 102 | ); |
---|
| 103 | |
---|
| 104 | /** |
---|
[f553c6e] | 105 | * @brief Gets the current ticks counter value. |
---|
[d7d7859] | 106 | * |
---|
[f553c6e] | 107 | * @return The current tick counter value. With a 1ms clock tick, this counter |
---|
| 108 | * overflows after 50 days since boot. |
---|
[d7d7859] | 109 | */ |
---|
[f553c6e] | 110 | RTEMS_INLINE_ROUTINE rtems_interval rtems_clock_get_ticks_since_boot(void) |
---|
| 111 | { |
---|
| 112 | return _Watchdog_Ticks_since_boot; |
---|
| 113 | } |
---|
[d7d7859] | 114 | |
---|
[96ec8ee8] | 115 | /** |
---|
| 116 | * @brief Returns the ticks counter value delta ticks in the future. |
---|
| 117 | * |
---|
| 118 | * @param[in] delta The ticks delta value. |
---|
| 119 | * |
---|
| 120 | * @return The tick counter value delta ticks in the future. |
---|
| 121 | */ |
---|
| 122 | RTEMS_INLINE_ROUTINE rtems_interval rtems_clock_tick_later( |
---|
| 123 | rtems_interval delta |
---|
| 124 | ) |
---|
| 125 | { |
---|
| 126 | return _Watchdog_Ticks_since_boot + delta; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | /** |
---|
| 130 | * @brief Returns the ticks counter value at least delta microseconds in the |
---|
| 131 | * future. |
---|
| 132 | * |
---|
| 133 | * @param[in] delta_in_usec The delta value in microseconds. |
---|
| 134 | * |
---|
| 135 | * @return The tick counter value at least delta microseconds in the future. |
---|
| 136 | */ |
---|
| 137 | RTEMS_INLINE_ROUTINE rtems_interval rtems_clock_tick_later_usec( |
---|
| 138 | rtems_interval delta_in_usec |
---|
| 139 | ) |
---|
| 140 | { |
---|
| 141 | rtems_interval us_per_tick = rtems_configuration_get_microseconds_per_tick(); |
---|
| 142 | |
---|
| 143 | /* |
---|
| 144 | * Add one additional tick, since we don't know the time to the clock next |
---|
| 145 | * tick. |
---|
| 146 | */ |
---|
| 147 | return _Watchdog_Ticks_since_boot |
---|
| 148 | + (delta_in_usec + us_per_tick - 1) / us_per_tick + 1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * @brief Returns true if the current ticks counter value indicates a time |
---|
| 153 | * before the time specified by the tick value and false otherwise. |
---|
| 154 | * |
---|
| 155 | * @param[in] tick The tick value. |
---|
| 156 | * |
---|
| 157 | * This can be used to write busy loops with a timeout. |
---|
| 158 | * |
---|
| 159 | * @code |
---|
| 160 | * status busy( void ) |
---|
| 161 | * { |
---|
| 162 | * rtems_interval timeout = rtems_clock_tick_later_usec( 10000 ); |
---|
| 163 | * |
---|
| 164 | * do { |
---|
| 165 | * if ( ok() ) { |
---|
| 166 | * return success; |
---|
| 167 | * } |
---|
| 168 | * } while ( rtems_clock_tick_before( timeout ) ); |
---|
| 169 | * |
---|
| 170 | * return timeout; |
---|
| 171 | * } |
---|
| 172 | * @endcode |
---|
| 173 | * |
---|
| 174 | * @retval true The current ticks counter value indicates a time before the |
---|
| 175 | * time specified by the tick value. |
---|
| 176 | * @retval false Otherwise. |
---|
| 177 | */ |
---|
| 178 | RTEMS_INLINE_ROUTINE bool rtems_clock_tick_before( |
---|
| 179 | rtems_interval tick |
---|
| 180 | ) |
---|
| 181 | { |
---|
| 182 | return (int32_t) ( tick - _Watchdog_Ticks_since_boot ) > 0; |
---|
| 183 | } |
---|
| 184 | |
---|
[d7d7859] | 185 | /** |
---|
[d106ab3] | 186 | * @brief Obtain Ticks Per Seconds |
---|
[d7d7859] | 187 | * |
---|
[d106ab3] | 188 | * This routine implements the rtems_clock_get_ticks_per_second |
---|
| 189 | * directive. |
---|
[d7d7859] | 190 | * |
---|
[6f55315] | 191 | * @retval This method returns the number of ticks per second. It cannot |
---|
[d106ab3] | 192 | * fail since RTEMS is always configured to know the number of |
---|
| 193 | * ticks per second. |
---|
[d7d7859] | 194 | */ |
---|
| 195 | rtems_interval rtems_clock_get_ticks_per_second(void); |
---|
| 196 | |
---|
[34799c3b] | 197 | /* Optimized variant for C/C++ without function call overhead */ |
---|
| 198 | #define rtems_clock_get_ticks_per_second() ( _Watchdog_Ticks_per_second ) |
---|
| 199 | |
---|
[d7d7859] | 200 | /** |
---|
[d106ab3] | 201 | * @brief Set the Current TOD |
---|
[ac7d5ef0] | 202 | * |
---|
[d106ab3] | 203 | * This routine implements the rtems_clock_set directive. It sets |
---|
| 204 | * the current time of day to that in the time_buffer record. |
---|
[d7d7859] | 205 | * |
---|
[d106ab3] | 206 | * @param[in] time_buffer points to the new TOD |
---|
[d7d7859] | 207 | * |
---|
[d106ab3] | 208 | * @retval This method returns RTEMS_SUCCESSFUL if there was not an |
---|
| 209 | * error. Otherwise, a status code is returned indicating the |
---|
| 210 | * source of the error. |
---|
[d7d7859] | 211 | * |
---|
[d106ab3] | 212 | * @note Activities scheduled based upon the current time of day |
---|
| 213 | * may be executed immediately if the time is moved forward. |
---|
[ac7d5ef0] | 214 | */ |
---|
| 215 | rtems_status_code rtems_clock_set( |
---|
[33c0f97] | 216 | const rtems_time_of_day *time_buffer |
---|
[ac7d5ef0] | 217 | ); |
---|
| 218 | |
---|
[d7d7859] | 219 | /** |
---|
[d106ab3] | 220 | * @brief Announce a Clock Tick |
---|
[ac7d5ef0] | 221 | * |
---|
[d106ab3] | 222 | * This routine implements the rtems_clock_tick directive. It is invoked |
---|
| 223 | * to inform RTEMS of the occurrence of a clock tick. |
---|
[d7d7859] | 224 | * |
---|
[d106ab3] | 225 | * @retval This directive always returns RTEMS_SUCCESSFUL. |
---|
[d7d7859] | 226 | * |
---|
[d106ab3] | 227 | * @note This method is typically called from an ISR and is the basis |
---|
| 228 | * for all timeouts and delays. This routine only works for leap-years |
---|
| 229 | * through 2099. |
---|
[ac7d5ef0] | 230 | */ |
---|
| 231 | rtems_status_code rtems_clock_tick( void ); |
---|
| 232 | |
---|
[d7d7859] | 233 | /** |
---|
[d106ab3] | 234 | * @brief Obtain the System Uptime |
---|
[812da54] | 235 | * |
---|
[d106ab3] | 236 | * This directive returns the system uptime. |
---|
[812da54] | 237 | * |
---|
[d106ab3] | 238 | * @param[in] uptime is a pointer to the time structure |
---|
[812da54] | 239 | * |
---|
[d106ab3] | 240 | * @retval This method returns RTEMS_SUCCESSFUL if there was not an |
---|
| 241 | * error. Otherwise, a status code is returned indicating the |
---|
[e858f70] | 242 | * source of the error. If successful, the @a uptime will be |
---|
[d106ab3] | 243 | * filled in. |
---|
[812da54] | 244 | */ |
---|
| 245 | rtems_status_code rtems_clock_get_uptime( |
---|
| 246 | struct timespec *uptime |
---|
| 247 | ); |
---|
| 248 | |
---|
[bfddb047] | 249 | /** |
---|
[d106ab3] | 250 | * @brief Gets the System Uptime in the Struct Timeval Format |
---|
[bfddb047] | 251 | * |
---|
[e858f70] | 252 | * @param[out] uptime is a pointer to a struct timeval structure. |
---|
| 253 | * |
---|
| 254 | * @retval This methods returns the system uptime. |
---|
| 255 | * |
---|
| 256 | * @note Pointer must not be NULL. |
---|
[bfddb047] | 257 | */ |
---|
| 258 | void rtems_clock_get_uptime_timeval( struct timeval *uptime ); |
---|
| 259 | |
---|
[90733a86] | 260 | /** |
---|
[d106ab3] | 261 | * @brief Returns the system uptime in seconds. |
---|
[90733a86] | 262 | * |
---|
[d106ab3] | 263 | * @retval The system uptime in seconds. |
---|
[90733a86] | 264 | */ |
---|
[98c01a5] | 265 | time_t rtems_clock_get_uptime_seconds( void ); |
---|
[90733a86] | 266 | |
---|
[2d1bdc8] | 267 | /** |
---|
| 268 | * @brief Returns the system uptime in nanoseconds. |
---|
| 269 | * |
---|
| 270 | * @retval The system uptime in nanoseconds. |
---|
| 271 | */ |
---|
| 272 | uint64_t rtems_clock_get_uptime_nanoseconds( void ); |
---|
| 273 | |
---|
[d7d7859] | 274 | /** |
---|
[d106ab3] | 275 | * @brief TOD Validate |
---|
[812da54] | 276 | * |
---|
[d106ab3] | 277 | * This support function returns true if @a the_tod contains |
---|
| 278 | * a valid time of day, and false otherwise. |
---|
[d7d7859] | 279 | * |
---|
[d106ab3] | 280 | * @param[in] the_tod is the TOD structure to validate |
---|
[d7d7859] | 281 | * |
---|
[d106ab3] | 282 | * @retval This method returns true if the TOD is valid and false otherwise. |
---|
[f1eb368d] | 283 | * |
---|
[d106ab3] | 284 | * @note This routine only works for leap-years through 2099. |
---|
[812da54] | 285 | */ |
---|
[484a769] | 286 | bool _TOD_Validate( |
---|
[9d06996] | 287 | const rtems_time_of_day *the_tod |
---|
[812da54] | 288 | ); |
---|
| 289 | |
---|
[d7d7859] | 290 | /** |
---|
[d106ab3] | 291 | * @brief TOD to Seconds |
---|
[d7d7859] | 292 | * |
---|
[d106ab3] | 293 | * This function returns the number seconds between the epoch and @a the_tod. |
---|
[d7d7859] | 294 | * |
---|
[d106ab3] | 295 | * @param[in] the_tod is the TOD structure to convert to seconds |
---|
[812da54] | 296 | * |
---|
[d106ab3] | 297 | * @retval This method returns the number of seconds since epoch represented |
---|
| 298 | * by @a the_tod |
---|
[812da54] | 299 | */ |
---|
| 300 | Watchdog_Interval _TOD_To_seconds( |
---|
[9d06996] | 301 | const rtems_time_of_day *the_tod |
---|
[812da54] | 302 | ); |
---|
| 303 | |
---|
[ac7d5ef0] | 304 | #ifdef __cplusplus |
---|
| 305 | } |
---|
| 306 | #endif |
---|
| 307 | |
---|
[067a96a] | 308 | /**@}*/ |
---|
| 309 | |
---|
[ac7d5ef0] | 310 | #endif |
---|
| 311 | /* end of include file */ |
---|