ChibiOS/RT  5.1.0
Time and Intervals
Collaboration diagram for Time and Intervals:

Detailed Description

Macros

#define CH_CFG_ST_RESOLUTION   32
 System time counter resolution. More...
 
#define CH_CFG_ST_FREQUENCY   1000
 System tick frequency. More...
 
#define CH_CFG_INTERVALS_SIZE   32
 Time intervals data size. More...
 
#define CH_CFG_TIME_TYPES_SIZE   32
 Time types data size. More...
 

Special time constants

#define TIME_IMMEDIATE   ((sysinterval_t)0)
 Zero interval specification for some functions with a timeout specification. More...
 
#define TIME_INFINITE   ((sysinterval_t)-1)
 Infinite interval specification for all functions with a timeout specification. More...
 
#define TIME_MAX_INTERVAL   ((sysinterval_t)-2)
 Maximum interval constant usable as timeout. More...
 
#define TIME_MAX_SYSTIME   ((systime_t)-1)
 Maximum system of system time before it wraps. More...
 

Fast time conversion utilities

#define TIME_S2I(secs)   ((sysinterval_t)((time_conv_t)(secs) * (time_conv_t)CH_CFG_ST_FREQUENCY))
 Seconds to time interval. More...
 
#define TIME_MS2I(msecs)
 Milliseconds to time interval. More...
 
#define TIME_US2I(usecs)
 Microseconds to time interval. More...
 
#define TIME_I2S(interval)
 Time interval to seconds. More...
 
#define TIME_I2MS(interval)
 Time interval to milliseconds. More...
 
#define TIME_I2US(interval)
 Time interval to microseconds. More...
 

Secure time conversion utilities

static sysinterval_t chTimeS2I (time_secs_t secs)
 Seconds to time interval. More...
 
static sysinterval_t chTimeMS2I (time_msecs_t msec)
 Milliseconds to time interval. More...
 
static sysinterval_t chTimeUS2I (time_usecs_t usec)
 Microseconds to time interval. More...
 
static time_secs_t chTimeI2S (sysinterval_t interval)
 Time interval to seconds. More...
 
static time_msecs_t chTimeI2MS (sysinterval_t interval)
 Time interval to milliseconds. More...
 
static time_usecs_t chTimeI2US (sysinterval_t interval)
 Time interval to microseconds. More...
 
static systime_t chTimeAddX (systime_t systime, sysinterval_t interval)
 Adds an interval to a system time returning a system time. More...
 
static sysinterval_t chTimeDiffX (systime_t start, systime_t end)
 Subtracts two system times returning an interval. More...
 
static bool chTimeIsInRangeX (systime_t time, systime_t start, systime_t end)
 Checks if the specified time is within the specified time range. More...
 

Typedefs

typedef uint64_t systime_t
 Type of system time. More...
 
typedef uint64_t sysinterval_t
 Type of time interval. More...
 
typedef uint32_t time_secs_t
 Type of seconds. More...
 
typedef uint32_t time_msecs_t
 Type of milliseconds. More...
 
typedef uint32_t time_usecs_t
 Type of microseconds. More...
 
typedef uint64_t time_conv_t
 Type of time conversion variable. More...
 

Macro Definition Documentation

#define TIME_IMMEDIATE   ((sysinterval_t)0)

Zero interval specification for some functions with a timeout specification.

Note
Not all functions accept TIME_IMMEDIATE as timeout parameter, see the specific function documentation.

Definition at line 45 of file chtime.h.

Referenced by chCondWaitTimeoutS(), chEvtWaitAllTimeout(), chEvtWaitAnyTimeout(), chEvtWaitOneTimeout(), chFifoSendObject(), chFifoSendObjectAhead(), chFifoSendObjectAheadS(), chFifoSendObjectS(), chSemWaitTimeoutS(), chThdEnqueueTimeoutS(), chThdSleepS(), chThdSuspendTimeoutS(), and chVTDoSetI().

#define TIME_INFINITE   ((sysinterval_t)-1)

Infinite interval specification for all functions with a timeout specification.

Note
Not all functions accept TIME_INFINITE as timeout parameter, see the specific function documentation.

Definition at line 53 of file chtime.h.

Referenced by chSchGoSleepTimeoutS().

#define TIME_MAX_INTERVAL   ((sysinterval_t)-2)

Maximum interval constant usable as timeout.

Definition at line 58 of file chtime.h.

Referenced by chTimeMS2I(), chTimeS2I(), and chTimeUS2I().

#define TIME_MAX_SYSTIME   ((systime_t)-1)

Maximum system of system time before it wraps.

Definition at line 63 of file chtime.h.

Referenced by chVTDoResetI(), chVTDoSetI(), and chVTDoTickI().

#define CH_CFG_ST_RESOLUTION   32

System time counter resolution.

Note
Allowed values are 16, 32 or 64 bits.

Definition at line 75 of file chtime.h.

#define CH_CFG_ST_FREQUENCY   1000

System tick frequency.

Frequency of the system timer that drives the system ticks. This setting also defines the system tick time unit.

Definition at line 84 of file chtime.h.

Referenced by chTimeI2MS(), chTimeI2S(), chTimeI2US(), chTimeMS2I(), chTimeS2I(), and chTimeUS2I().

#define CH_CFG_INTERVALS_SIZE   32

Time intervals data size.

Note
Allowed values are 16, 32 or 64 bits.

Definition at line 92 of file chtime.h.

#define CH_CFG_TIME_TYPES_SIZE   32

Time types data size.

Note
Allowed values are 16 or 32 bits.

Definition at line 100 of file chtime.h.

#define TIME_S2I (   secs)    ((sysinterval_t)((time_conv_t)(secs) * (time_conv_t)CH_CFG_ST_FREQUENCY))

Seconds to time interval.

Converts from seconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]secsnumber of seconds
Returns
The number of ticks.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 212 of file chtime.h.

#define TIME_MS2I (   msecs)
Value:
((sysinterval_t)((((time_conv_t)(msecs) * \
(time_conv_t)999) / (time_conv_t)1000))
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:150
uint64_t time_conv_t
Type of time conversion variable.
Definition: chtime.h:181
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chtime.h:84

Milliseconds to time interval.

Converts from milliseconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]msecsnumber of milliseconds
Returns
The number of ticks.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 228 of file chtime.h.

#define TIME_US2I (   usecs)
Value:
((sysinterval_t)((((time_conv_t)(usecs) * \
(time_conv_t)999999) / (time_conv_t)1000000))
uint64_t sysinterval_t
Type of time interval.
Definition: chtime.h:150
uint64_t time_conv_t
Type of time conversion variable.
Definition: chtime.h:181
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chtime.h:84

Microseconds to time interval.

Converts from microseconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]usecsnumber of microseconds
Returns
The number of ticks.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 246 of file chtime.h.

#define TIME_I2S (   interval)
Value:
(time_secs_t)(((time_conv_t)(interval) + \
uint32_t time_secs_t
Type of seconds.
Definition: chtime.h:162
uint64_t time_conv_t
Type of time conversion variable.
Definition: chtime.h:181
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chtime.h:84

Time interval to seconds.

Converts from system ticks number to seconds.

Note
The result is rounded up to the next second boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]intervalinterval in ticks
Returns
The number of seconds.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 264 of file chtime.h.

#define TIME_I2MS (   interval)
Value:
(time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000) + \
uint64_t time_conv_t
Type of time conversion variable.
Definition: chtime.h:181
uint32_t time_msecs_t
Type of milliseconds.
Definition: chtime.h:168
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chtime.h:84

Time interval to milliseconds.

Converts from system ticks number to milliseconds.

Note
The result is rounded up to the next millisecond boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]intervalinterval in ticks
Returns
The number of milliseconds.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 282 of file chtime.h.

#define TIME_I2US (   interval)
Value:
(time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000000) + \
uint64_t time_conv_t
Type of time conversion variable.
Definition: chtime.h:181
uint32_t time_msecs_t
Type of milliseconds.
Definition: chtime.h:168
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chtime.h:84

Time interval to microseconds.

Converts from system ticks number to microseconds.

Note
The result is rounded up to the next microsecond boundary.
Use of this macro for large values is not secure because integer overflows, make sure your value can be correctly converted.
Parameters
[in]intervalinterval in ticks
Returns
The number of microseconds.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 300 of file chtime.h.

Typedef Documentation

typedef uint64_t systime_t

Type of system time.

Note
It is selectable in configuration between 16, 32 or 64 bits.

Definition at line 138 of file chtime.h.

typedef uint64_t sysinterval_t

Type of time interval.

Note
It is selectable in configuration between 16, 32 or 64 bits.

Definition at line 150 of file chtime.h.

typedef uint32_t time_secs_t

Type of seconds.

Note
It is selectable in configuration between 16 or 32 bits.

Definition at line 162 of file chtime.h.

typedef uint32_t time_msecs_t

Type of milliseconds.

Note
It is selectable in configuration between 16 or 32 bits.

Definition at line 168 of file chtime.h.

typedef uint32_t time_usecs_t

Type of microseconds.

Note
It is selectable in configuration between 16 or 32 bits.

Definition at line 174 of file chtime.h.

typedef uint64_t time_conv_t

Type of time conversion variable.

Note
This type must have double width than other time types, it is only used internally for conversions.

Definition at line 181 of file chtime.h.

Function Documentation

static sysinterval_t chTimeS2I ( time_secs_t  secs)
inlinestatic

Seconds to time interval.

Converts from seconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Parameters
[in]secsnumber of seconds
Returns
The number of ticks.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 339 of file chtime.h.

References CH_CFG_ST_FREQUENCY, chDbgAssert, and TIME_MAX_INTERVAL.

static sysinterval_t chTimeMS2I ( time_msecs_t  msec)
inlinestatic

Milliseconds to time interval.

Converts from milliseconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Parameters
[in]msecnumber of milliseconds
Returns
The number of ticks.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 360 of file chtime.h.

References CH_CFG_ST_FREQUENCY, chDbgAssert, and TIME_MAX_INTERVAL.

static sysinterval_t chTimeUS2I ( time_usecs_t  usec)
inlinestatic

Microseconds to time interval.

Converts from microseconds to system ticks number.

Note
The result is rounded upward to the next tick boundary.
Parameters
[in]usecnumber of microseconds
Returns
The number of ticks.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 382 of file chtime.h.

References CH_CFG_ST_FREQUENCY, chDbgAssert, and TIME_MAX_INTERVAL.

static time_secs_t chTimeI2S ( sysinterval_t  interval)
inlinestatic

Time interval to seconds.

Converts from system interval to seconds.

Note
The result is rounded up to the next second boundary.
Parameters
[in]intervalinterval in ticks
Returns
The number of seconds.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 404 of file chtime.h.

References CH_CFG_ST_FREQUENCY, and chDbgAssert.

static time_msecs_t chTimeI2MS ( sysinterval_t  interval)
inlinestatic

Time interval to milliseconds.

Converts from system interval to milliseconds.

Note
The result is rounded up to the next millisecond boundary.
Parameters
[in]intervalinterval in ticks
Returns
The number of milliseconds.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 427 of file chtime.h.

References CH_CFG_ST_FREQUENCY, and chDbgAssert.

static time_usecs_t chTimeI2US ( sysinterval_t  interval)
inlinestatic

Time interval to microseconds.

Converts from system interval to microseconds.

Note
The result is rounded up to the next microsecond boundary.
Parameters
[in]intervalinterval in ticks
Returns
The number of microseconds.
Function Class:
Special function, this function has special requirements see the notes.

Definition at line 450 of file chtime.h.

References CH_CFG_ST_FREQUENCY, and chDbgAssert.

static systime_t chTimeAddX ( systime_t  systime,
sysinterval_t  interval 
)
inlinestatic

Adds an interval to a system time returning a system time.

Parameters
[in]systimebase system time
[in]intervalinterval to be added
Returns
The new system time.
Function Class:
This is an X-Class API, this function can be invoked from any context.

Definition at line 472 of file chtime.h.

References chDbgCheck.

Referenced by chVTDoResetI(), chVTDoSetI(), chVTDoTickI(), and chVTGetTimersStateI().

static sysinterval_t chTimeDiffX ( systime_t  start,
systime_t  end 
)
inlinestatic

Subtracts two system times returning an interval.

Parameters
[in]startfirst system time
[in]endsecond system time
Returns
The interval representing the time difference.
Function Class:
This is an X-Class API, this function can be invoked from any context.

Definition at line 491 of file chtime.h.

Referenced by chThdSleepUntil(), chThdSleepUntilWindowed(), chVTDoResetI(), chVTDoSetI(), chVTDoTickI(), chVTGetTimersStateI(), and chVTTimeElapsedSinceX().

static bool chTimeIsInRangeX ( systime_t  time,
systime_t  start,
systime_t  end 
)
inlinestatic

Checks if the specified time is within the specified time range.

Note
When start==end then the function returns always true because the whole time range is specified.
Parameters
[in]timethe time to be verified
[in]startthe start of the time window (inclusive)
[in]endthe end of the time window (non inclusive)
Return values
truecurrent time within the specified time window.
falsecurrent time not within the specified time window.
Function Class:
This is an X-Class API, this function can be invoked from any context.

Definition at line 512 of file chtime.h.

Referenced by chThdSleepUntilWindowed(), chVTIsSystemTimeWithin(), and chVTIsSystemTimeWithinX().