Reference Manual For QP/C 8.1.6
Loading...
Searching...
No Matches
QTimeEvt Class Reference

Time Event class. More...

Inheritance diagram for QTimeEvt:
QEvt

Public Member Functions

void QTimeEvt_ctorX (QTimeEvt *const me, QActive *const act, enum_t const sig, uint_fast8_t const tickRate)
 The "extended" constructor to initialize a Time Event.
void QTimeEvt_armX (QTimeEvt *const me, uint32_t const nTicks, uint32_t const interval)
 Arm a time event (extended version for one-shot or periodic time event).
bool QTimeEvt_disarm (QTimeEvt *const me)
 Disarm a time event.
bool QTimeEvt_rearm (QTimeEvt *const me, uint32_t const nTicks)
 Rearm a time event.
bool QTimeEvt_wasDisarmed (QTimeEvt *const me)
 Check whether the timeout being processed is valid (not stale).
QTimeEvtCtr QTimeEvt_getCtr (QTimeEvt const *const me)
 Get the current value of the down-counter of a time event.
Public Member Functions inherited from QEvt
void QEvt_ctor (QEvt *const me, enum_t const sig)
 QEvt constructor
QEvt * QEvt_init (QEvt *const me, uint8_t const dummy)
 Event without parameters initialization.

Static Public Member Functions

bool QTimeEvt_noActive (uint_fast8_t const tickRate)
 Check if any time events are active at a given clock tick rate.

Protected Attributes

QEvt super

Private Member Functions

QTimeEvt * QTimeEvt_expire_ (QTimeEvt *const me, QTimeEvt *const prev_link, QActive const *const act, uint_fast8_t const tickRate)

Static Private Member Functions

void QTimeEvt_init (void)
void QTimeEvt_tick_ (uint_fast8_t const tickRate, void const *const sender)
 Processes all armed time events at every clock tick.
void QTimeEvt_tick1_ (uint_fast8_t const tickRate, void const *const sender)
 Processes one (the next upcoming) clock tick for QUTest.

Private Attributes

struct QTimeEvt * next
 Link to the next time event in the list .
void * act
 Active object that receives the time events .
QTimeEvtCtr ctr
 Down-counter of the time event.
QTimeEvtCtr interval
 Interval for periodic time event (zero for one-shot time event).
uint8_t tickRate
uint8_t flags
QTimeEvt QTimeEvt_timeEvtHead_ [QF_MAX_TICK_RATE]
 Static array of heads of linked lists of time events (one for every clock tick rate).

Additional Inherited Members

Public Attributes inherited from QEvt
uint32_t sig: 16
 Event signal (see Event Signal).

Detailed Description

Time Event class.

Details

Time events are special QF events equipped with the notion of time passage. The basic usage model of the time events is as follows. An active object allocates one or more QTimeEvt objects (provides the storage for them). When the active object needs to arrange for a timeout, it arms one of its time events to fire either just once (one-shot) or periodically. Each time event times out independently from the others so that a QF application can make multiple parallel timeout requests (from the same or different active objects). When QF detects that the appropriate moment has arrived, it inserts the time event directly into the recipient's event queue. The recipient then processes the time event just like any other event.

Time events, as any other QF events, derive from the QEvt base class. Typically, you will use a time event as-is. Still, you can also further derive more specialized time events from it by adding some more data members and/or specialized functions that operate on the specialized time events.

Internally, the armed time events are organized into linked lists–one list for every supported ticking rate. These linked lists are scanned in every invocation of the QTIMEEVT_TICK_X() macro. Only armed (timing out) time events are in the list, so only armed time events consume CPU cycles.

Remarks
QF manages the time events in the QTIMEEVT_TICK_X() macro, which must be called periodically, from the clock tick ISR or from other periodic source. QTIMEEVT_TICK_X() caYou might also use the special QTicker active object.
Note
Even though QTimeEvt is a subclass of QEvt, QTimeEvt instances can NOT be allocated dynamically from event pools. In other words, it is illegal to allocate QTimeEvt instances with the Q_NEW() or Q_NEW_X() macros.

Backward Traceability

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 572 of file qp.h.

Member Function Documentation

◆ QTimeEvt_ctorX()

void QTimeEvt_ctorX ( QTimeEvt *const me,
QActive *const act,
enum_t const sig,
uint_fast8_t const tickRate )

The "extended" constructor to initialize a Time Event.

Details

When creating a time event, you must commit it to a specific active object act, tick rate tickRate, and event signal sig. You cannot change these attributes later.

Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
[in]actpointer to the active object associated with this time event. The time event will post itself to this AO.
[in]sigsignal to associate with this time event.
[in]tickRatesystem clock tick rate to associate with this time event in the range [0..(QF_MAX_TICK_RATE - 1)].
Note
You should call QTimeEvt_ctorX() exactly once for every Time Event object before arming the Time Event. The ideal place for calling QTimeEvt_ctorX() is the constructor of the associated AO.

Backward Traceability

  • [SRS_QP_REQ_420] QP/C Framework shall provide Time Event initialization.

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 50 of file qf_time.c.

◆ QTimeEvt_armX()

void QTimeEvt_armX ( QTimeEvt *const me,
uint32_t const nTicks,
uint32_t const interval )

Arm a time event (extended version for one-shot or periodic time event).

Details

Arms a time event to fire in a specified number of clock ticks and with a specified interval. If the interval is zero, the time event is armed for one shot ('one-shot' time event). When the timeout expires, the time event gets directly posted (using the FIFO policy) into the event queue of the host active object. After posting, a one-shot time event gets automatically disarmed while a periodic time event (interval != 0) is automatically re-armed.

A time event can be disarmed at any time by calling QTimeEvt_disarm(). Also, a time event can be re-armed to fire in a different number of clock ticks by calling the QTimeEvt_rearm().

Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
[in]nTicksnumber of clock ticks (at the associated rate) to arm the time event with, in the range [1..max], where max is the maximum value of QTimeEvtCtr ( 0xFF for QF_TIMEEVT_CTR_SIZE == 1U, 0xFFFF for QF_TIMEEVT_CTR_SIZE == 2U, 0xFFFFFFFF for 4U).
[in]intervalinterval (in clock ticks) for periodic time event, in the range [0..max] (0 means one-shot).
Note
If a timeout of this time event is still pending (posted, not acknowledged by QTimeEvt_wasDisarmed()), arming the time event makes that timeout stale (see QTimeEvt_wasDisarmed()).
Attention
Arming an already armed time event is not allowed and is considered a programming error. The QP/C framework will assert if it detects an attempt to arm an already armed time event.

Usage

The following example shows how to arm a periodic time event as well as a one-shot time event from a state machine of an active object:

QState Game_show_logo(Tunnel * const me, QEvt const * const e) {
QState status_;
switch (e->sig) {
case Q_ENTRY_SIG: {
// arm periodic time event
QTimeEvt_armX(&me->blinkTimeEvt, // <===
BSP_TICKS_PER_SEC/2U, // one-time delay
BSP_TICKS_PER_SEC/2U); // interval
// arm a one-shot time event
QTimeEvt_armX(&me->screenTimeEvt,
BSP_TICKS_PER_SEC*5U, // one-time delay
0U); // interval (0 == no interval)
. . .
status_ = Q_HANDLED();
break;
}
. . .
#define Q_HANDLED()
Indicate that an action has been "handled." Applies to entry/exit actions and internal transitions.
Definition qp.h:307
#define Q_ENTRY_SIG
Reserved signal sent to state handler to execute the entry case. \clearpage .
Definition qp.h:202
uint_fast8_t QState
Type returned from state-handler functions \clearpage .
Definition qp.h:141
Event class.
Definition qp.h:95
uint32_t sig
Event signal (see Event Signal).
Definition qp.h:96
void QTimeEvt_armX(QTimeEvt *const me, uint32_t const nTicks, uint32_t const interval)
Arm a time event (extended version for one-shot or periodic time event).
Definition qf_time.c:78

Backward Traceability

  • [SRS_QP_REQ_421] QP/C Framework shall allow a Time Event to be armed both for one-shot and periodic expiry.

Forward Traceability

  • [MISRA_QP_DVR_MC5_R11_5] MISRA-C:2025 Rule 11.5(Advisory): A conversion should not be performed from pointer to void into pointer to object
  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 78 of file qf_time.c.

◆ QTimeEvt_disarm()

bool QTimeEvt_disarm ( QTimeEvt *const me)

Disarm a time event.

Details

Disarm the time event so it can be safely reused.

Remarks
Disarming an already disarmed time event is fine.
Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
Return values
trueIf the time event was truly disarmed, that is, it was running.
falseIf the time event was not running (never armed, already disarmed explicitly, or a one-shot time event that has been automatically disarmed upon expiration). In the last case, the time event has already been posted and should be expected in the active object's event queue.
Note
If a timeout of this time event is still pending (posted, not acknowledged by QTimeEvt_wasDisarmed()), disarming the time event makes that timeout stale (see QTimeEvt_wasDisarmed()).

Backward Traceability

  • [SRS_QP_REQ_422] QP/C Framework shall allow a Time Event to be explicitly disarmed.

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 148 of file qf_time.c.

◆ QTimeEvt_rearm()

bool QTimeEvt_rearm ( QTimeEvt *const me,
uint32_t const nTicks )

Rearm a time event.

Details

Rearms a time event with a new number of clock ticks. This function can be used to adjust the current period of a periodic time event or to prevent a one-shot time event from expiring (e.g., a watchdog time event). Rearming a periodic timer leaves the interval unchanged and is a convenient method to adjust the phasing of a periodic time event.

Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
[in]nTicksnumber of clock ticks (at the associated rate) to rearm the time event with, in the range [1..max] (max as in QTimeEvt_armX()).

After the call, the time event is armed to expire in nTicks clock ticks, regardless of the returned status. The interval is left unchanged, so a time event that was last armed as periodic stays periodic. If a timeout of this time event is still pending (posted, not acknowledged by QTimeEvt_wasDisarmed()), rearming the time event makes that timeout stale (see QTimeEvt_wasDisarmed()).

Return values
trueIf the time event was running as it was rearmed.
falseIf the time event was not running (never armed, disarmed explicitly, or a one-shot time event that has been automatically disarmed upon expiration). In the last case, the previous timeout has already been posted and should be expected in the active object's event queue.

Backward Traceability

  • [SRS_QP_REQ_423] QP/C Framework shall allow a Time Event to be rearmed.

Forward Traceability

  • [MISRA_QP_DVR_MC5_R11_5] MISRA-C:2025 Rule 11.5(Advisory): A conversion should not be performed from pointer to void into pointer to object
  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 194 of file qf_time.c.

◆ QTimeEvt_wasDisarmed()

bool QTimeEvt_wasDisarmed ( QTimeEvt *const me)

Check whether the timeout being processed is valid (not stale).

Details

Recognizes a stale timeout in the recipient active object when a time event is reused in successive states (armed in the entry action, disarmed in the exit action): a timeout posted by the clock tick and not processed yet when the time event was disarmed, armed, or rearmed is stale and must be ignored, because it belongs to the previous use of the time event. The function is called in every state that processes the timeout of the time event (typically as a guard). The name reflects the typical case: 'true' means that the previous use ended with the time event truly disarmed (nothing pending), so the timeout belongs to the current use.

The time event tracks one pending timeout: QTimeEvt_tick_() marks the time event as "posted" when it posts the timeout; QTimeEvt_armX(), QTimeEvt_rearm(), and QTimeEvt_disarm() make the pending timeout stale; this function acknowledges the timeout. A time event that has not posted any timeout yet reports its first timeout as valid.

Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
Return values
trueThe timeout being processed is valid.
falseThe timeout being processed is stale (posted before the time event was disarmed, armed, or rearmed) and should be ignored.
Note
This function has a side effect: it acknowledges the timeout being processed, so the next call returns 'true' unless the time event is disarmed, armed, or rearmed while a timeout is pending. Call it exactly once per received timeout.
Attention
At most one timeout of the time event can be pending at any time for a correct result: the number of clock ticks must be longer than the worst-case delay of processing the events in the recipient's queue (see SSM_QP_ASR_514).

Usage

QState Blinky_waiting(Blinky * const me, QEvt const * const e) {
QState status_;
switch (e->sig) {
case Q_ENTRY_SIG: {
QTimeEvt_armX(&me->timeEvt, 10U, 0U); // precaution
status_ = Q_HANDLED();
break;
}
case Q_EXIT_SIG: {
(void)QTimeEvt_disarm(&me->timeEvt);
status_ = Q_HANDLED();
break;
}
case DONE_SIG: { // the expected event
status_ = Q_TRAN(&Blinky_next); // reuses me->timeEvt
break;
}
case TIMEOUT_SIG: {
if (QTimeEvt_wasDisarmed(&me->timeEvt)) { // valid timeout?
status_ = Q_TRAN(&Blinky_failed);
}
else { // stale timeout from the previous use: ignore
status_ = Q_HANDLED();
}
break;
}
default: {
status_ = Q_SUPER(&QHsm_top);
break;
}
}
return status_;
}
#define Q_EXIT_SIG
Reserved signal sent to state handler to execute the exit case. \clearpage .
Definition qp.h:203
#define Q_TRAN(target_)
Take transition to the specified target_ state.
Definition qp.h:298
#define Q_SUPER(super_)
Designates the superstate of a given state. Applicable only to QHsm subclasses.
Definition qp.h:304
bool QTimeEvt_wasDisarmed(QTimeEvt *const me)
Check whether the timeout being processed is valid (not stale).
Definition qf_time.c:271
bool QTimeEvt_disarm(QTimeEvt *const me)
Disarm a time event.
Definition qf_time.c:148

Backward Traceability

  • [SRS_QP_REQ_425] QP/C Framework shall provide an operation to check whether a received timeout of a Time Event is valid or stale.

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 271 of file qf_time.c.

◆ QTimeEvt_getCtr()

QTimeEvtCtr QTimeEvt_getCtr ( QTimeEvt const *const me)

Get the current value of the down-counter of a time event.

Details

Useful for checking how many clock ticks (at the tick rate associated with the time event) remain until the time event expires.

Parameters
[in,out]meinstance pointer (see [SAS_QP_TECH_20])
Return values
valueFor an armed time event, the function returns the current value of the down-counter of the given time event. The value should be != 0.
0If the time event is not armed, the function returns 0.

Backward Traceability

  • [SRS_QP_REQ_424] QP/C Framework shall provide an operation to obtain the current value of the down-counter of a Time Event.

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 388 of file qf_time.c.

◆ QTimeEvt_init()

void QTimeEvt_init ( void )
staticprivate

Definition at line 396 of file qf_time.c.

◆ QTimeEvt_tick_()

void QTimeEvt_tick_ ( uint_fast8_t const tickRate,
void const *const sender )
staticprivateinternal

Processes all armed time events at every clock tick.

Details

This internal helper function processes all armed QTimeEvt objects associated with the tick rate tickRate.

This function must be called periodically from a time-tick ISR or from a task so that QF can manage the timeout events assigned to the given system clock tick rate.

Parameters
[in]tickRateclock tick rate serviced in this call [0..(QF_MAX_TICK_RATE - 1)].
[in]senderpointer to a sender object (only for QS tracing)
Note
This function should be called only via the macro QTIMEEVT_TICK_X().
Attention
The loop over the linked time events has the fixed upper bound QTE_MAX_LINKED (SSRS_QP_SREQ_301). More than QTE_MAX_LINKED time events linked at one tick rate are treated as corruption of the list (assertion 891).
Remarks
The calls to QTIMEEVT_TICK_X() (QTimeEvt_tick_()) with different tickRate parameter can preempt each other. For example, higher clock tick rates might be serviced from interrupts while others from tasks (active objects).

Backward Traceability

  • [SRS_QP_REQ_411] QP/C Framework shall provide clock-tick processing operations for all supported clock rates that QP/C Application must call periodically to service the armed Time Events.
  • [SRS_QP_REQ_412] QP/C Framework shall support up to max(8, 2 x QF_MAX_ACTIVE) Time Events linked at any single clock tick rate.
  • [SSRS_QP_SREQ_301] QP/C software component shall apply the fixed upper loop bound to all internal loops that are intended to terminate.
  • [SSRS_QP_SREQ_400] QP/C Framework shall treat every condition that prevents the delivery of an event with event delivery guarantee as a fault.

Forward Traceability

  • [MISRA_QP_DVR_MC5_R11_5] MISRA-C:2025 Rule 11.5(Advisory): A conversion should not be performed from pointer to void into pointer to object
  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 290 of file qf_time.c.

◆ QTimeEvt_expire_()

QTimeEvt * QTimeEvt_expire_ ( QTimeEvt *const me,
QTimeEvt *const prev_link,
QActive const *const act,
uint_fast8_t const tickRate )
private

Definition at line 410 of file qf_time.c.

◆ QTimeEvt_tick1_()

void QTimeEvt_tick1_ ( uint_fast8_t const tickRate,
void const *const sender )
staticprivateinternal

Processes one (the next upcoming) clock tick for QUTest.

Forward Traceability

  • [STP_QP_NOTEST_CODE] Code elements verified other than by a direct test trace.

◆ QTimeEvt_noActive()

bool QTimeEvt_noActive ( uint_fast8_t const tickRate)
static

Check if any time events are active at a given clock tick rate.

Parameters
[in]tickRatesystem clock tick rate to find out about.
Return values
trueIf no time events are armed at the given tick rate
falseOtherwise.
Note
This function should be called in a critical section.
Remarks
A time event disarmed since the last QTimeEvt_tick_() call at the given tick rate stays linked until that next call and therefore still counts as active ('false' return). This is conservative.

Backward Traceability

  • [SRS_QP_REQ_430] QP/C Framework shall provide an operation to check whether any Time Events are armed for a given tick rate.

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 375 of file qf_time.c.

Member Data Documentation

◆ super

QEvt super
protected

Definition at line 573 of file qp.h.

◆ next

struct QTimeEvt* next
private

Link to the next time event in the list .

Definition at line 575 of file qp.h.

◆ act

void* act
private

Active object that receives the time events .

Definition at line 576 of file qp.h.

◆ ctr

QTimeEvtCtr ctr
private

Down-counter of the time event.

Details

The down-counter is decremented by 1 in every QTimeEvt_tick_() call. The time event fires (gets posted or published) when the down-counter reaches zero.

Definition at line 577 of file qp.h.

◆ interval

QTimeEvtCtr interval
private

Interval for periodic time event (zero for one-shot time event).

Details

The value of the interval is re-loaded to the internal down-counter when the time event expires, so that the time event keeps timing out periodically.

Definition at line 578 of file qp.h.

◆ tickRate

uint8_t tickRate
private

Definition at line 579 of file qp.h.

◆ flags

uint8_t flags
private

Definition at line 580 of file qp.h.

◆ QTimeEvt_timeEvtHead_

QTimeEvt QTimeEvt_timeEvtHead_[QF_MAX_TICK_RATE]
private

Static array of heads of linked lists of time events (one for every clock tick rate).

Forward Traceability

  • [TUN_QP_qf_time16] Unit test for qf_time: QTimeEvt class implementation (16-bit time-event counters)
  • [TUN_QP_qf_time32] Unit test for qf_time: QTimeEvt class implementation (32-bit time-event counters)

Definition at line 48 of file qp_pkg.h.