ChibiOS/RT
2.6.0
Time and Virtual Timers
Collaboration diagram for Time and Virtual Timers:

Detailed Description

Time and Virtual Timers related APIs and services.

Data Structures

struct  VirtualTimer
 Virtual Timer descriptor structure. More...
struct  VTList
 Virtual timers list header. More...

Functions

void _vt_init (void)
 Virtual Timers initialization.
void chVTSetI (VirtualTimer *vtp, systime_t time, vtfunc_t vtfunc, void *par)
 Enables a virtual timer.
void chVTResetI (VirtualTimer *vtp)
 Disables a Virtual Timer.
bool_t chTimeIsWithin (systime_t start, systime_t end)
 Checks if the current system time is within the specified time window.

Variables

VTList vtlist
 Virtual timers delta list header.
VTList vtlist
 Virtual timers delta list header.

Time conversion utilities

#define S2ST(sec)   ((systime_t)((sec) * CH_FREQUENCY))
 Seconds to system ticks.
#define MS2ST(msec)
 Milliseconds to system ticks.
#define US2ST(usec)
 Microseconds to system ticks.

Macro Functions

#define chVTDoTickI()
 Virtual timers ticker.
#define chVTIsArmedI(vtp)   ((vtp)->vt_func != NULL)
 Returns TRUE if the specified timer is armed.
#define chVTSet(vtp, time, vtfunc, par)
 Enables a virtual timer.
#define chVTReset(vtp)
 Disables a Virtual Timer.
#define chTimeNow()   (vtlist.vt_systime)
 Current system time.

Typedefs

typedef void(* vtfunc_t )(void *)
 Virtual Timer callback function.
typedef struct VirtualTimer VirtualTimer
 Virtual Timer structure type.

Function Documentation

void _vt_init ( void  )

Virtual Timers initialization.

Note:
Internal use only.
Function Class:
Not an API, this function is for internal use only.

Definition at line 50 of file chvt.c.

References VTList::vt_next, VTList::vt_prev, VTList::vt_systime, and VTList::vt_time.

Referenced by chSysInit().

void chVTSetI ( VirtualTimer vtp,
systime_t  time,
vtfunc_t  vtfunc,
void *  par 
)

Enables a virtual timer.

Note:
The associated function is invoked from interrupt context.
Parameters:
[out]vtpthe VirtualTimer structure pointer
[in]timethe number of ticks before the operation timeouts, the special values are handled as follow:
  • TIME_INFINITE is allowed but interpreted as a normal time specification.
  • TIME_IMMEDIATE this value is not allowed.
[in]vtfuncthe timer callback function. After invoking the callback the timer is disabled and the structure can be disposed or reused.
[in]para parameter that will be passed to the callback function
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 76 of file chvt.c.

References chDbgCheck, chDbgCheckClassI(), TIME_IMMEDIATE, VirtualTimer::vt_func, VirtualTimer::vt_next, VTList::vt_next, VirtualTimer::vt_par, VirtualTimer::vt_prev, VirtualTimer::vt_time, and vtlist.

Referenced by chSchGoSleepTimeoutS(), evtStart(), and chibios_rt::Timer::setI().

Here is the call graph for this function:

void chVTResetI ( VirtualTimer vtp)

Disables a Virtual Timer.

Note:
The timer MUST be active when this function is invoked.
Parameters:
[in]vtpthe VirtualTimer structure pointer
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 106 of file chvt.c.

References chDbgAssert, chDbgCheck, chDbgCheckClassI(), VirtualTimer::vt_func, VirtualTimer::vt_next, VirtualTimer::vt_prev, and VirtualTimer::vt_time.

Referenced by chSchGoSleepTimeoutS(), and chibios_rt::Timer::resetI().

Here is the call graph for this function:

bool_t chTimeIsWithin ( systime_t  start,
systime_t  end 
)

Checks if the current system time is within the specified time window.

Note:
When start==end then the function returns always true because the whole time range is specified.
Parameters:
[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:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 134 of file chvt.c.

References chTimeNow.

Referenced by chibios_rt::System::isTimeWithin().


Variable Documentation

Virtual timers delta list header.

Definition at line 42 of file chvt.c.

Referenced by chVTSetI().

Virtual timers delta list header.

Definition at line 42 of file chvt.c.

Referenced by chVTSetI().


Define Documentation

#define S2ST (   sec)    ((systime_t)((sec) * CH_FREQUENCY))

Seconds to system ticks.

Converts from seconds to system ticks number.

Note:
The result is rounded upward to the next tick boundary.
Parameters:
[in]secnumber 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 53 of file chvt.h.

#define MS2ST (   msec)
Value:
((systime_t)(((((msec) - 1L) * CH_FREQUENCY) /          \
                                   1000L) + 1L))

Milliseconds to system ticks.

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:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 65 of file chvt.h.

Referenced by test_start_timer().

#define US2ST (   usec)
Value:
((systime_t)(((((usec) - 1L) * CH_FREQUENCY) /          \
                                  1000000L) + 1L))

Microseconds to system ticks.

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:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 78 of file chvt.h.

#define chVTDoTickI ( )
Value:
{                                                     \
  vtlist.vt_systime++;                                                      \
  if (&vtlist != (VTList *)vtlist.vt_next) {                                \
    VirtualTimer *vtp;                                                      \
                                                                            \
    --vtlist.vt_next->vt_time;                                              \
    while (!(vtp = vtlist.vt_next)->vt_time) {                              \
      vtfunc_t fn = vtp->vt_func;                                           \
      vtp->vt_func = (vtfunc_t)NULL;                                        \
      vtp->vt_next->vt_prev = (void *)&vtlist;                              \
      (&vtlist)->vt_next = vtp->vt_next;                                    \
      chSysUnlockFromIsr();                                                 \
      fn(vtp->vt_par);                                                      \
      chSysLockFromIsr();                                                   \
    }                                                                       \
  }                                                                         \
}

Virtual timers ticker.

Note:
The system lock is released before entering the callback and re-acquired immediately after. It is callback's responsibility to acquire the lock if needed. This is done in order to reduce interrupts jitter when many timers are in use.
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 137 of file chvt.h.

Referenced by chSysTimerHandlerI().

#define chVTIsArmedI (   vtp)    ((vtp)->vt_func != NULL)

Returns TRUE if the specified timer is armed.

Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 160 of file chvt.h.

Referenced by chSchGoSleepTimeoutS(), evtStart(), chibios_rt::Timer::isArmedI(), and chibios_rt::Timer::resetI().

#define chVTSet (   vtp,
  time,
  vtfunc,
  par 
)
Value:
{                                   \
  chSysLock();                                                              \
  chVTSetI(vtp, time, vtfunc, par);                                         \
  chSysUnlock();                                                            \
}

Enables a virtual timer.

Note:
The associated function is invoked from interrupt context.
Parameters:
[out]vtpthe VirtualTimer structure pointer
[in]timethe number of ticks before the operation timeouts, the special values are handled as follow:
  • TIME_INFINITE is allowed but interpreted as a normal time specification.
  • TIME_IMMEDIATE this value is not allowed.
[in]vtfuncthe timer callback function. After invoking the callback the timer is disabled and the structure can be disposed or reused.
[in]para parameter that will be passed to the callback function
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 181 of file chvt.h.

Referenced by test_start_timer().

#define chVTReset (   vtp)
Value:
{                                                    \
  chSysLock();                                                              \
  if (chVTIsArmedI(vtp))                                                    \
    chVTResetI(vtp);                                                        \
  chSysUnlock();                                                            \
}

Disables a Virtual Timer.

Note:
The timer is first checked and disabled only if armed.
Parameters:
[in]vtpthe VirtualTimer structure pointer
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 195 of file chvt.h.

Referenced by evtStop().

#define chTimeNow ( )    (vtlist.vt_systime)

Current system time.

Returns the number of system ticks since the chSysInit() invocation.

Note:
The counter can reach its maximum and then restart from zero.
This function is designed to work with the chThdSleepUntil().
Returns:
The system time in ticks.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 213 of file chvt.h.

Referenced by chThdSleepUntil(), chTimeIsWithin(), dbg_trace(), chibios_rt::System::getTime(), macWaitReceiveDescriptor(), macWaitTransmitDescriptor(), and test_wait_tick().


Typedef Documentation

typedef void(* vtfunc_t)(void *)

Virtual Timer callback function.

Definition at line 85 of file chvt.h.

typedef struct VirtualTimer VirtualTimer

Virtual Timer structure type.

Definition at line 90 of file chvt.h.