qpc/include/qf.h

870 lines
32 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
* @brief QF/C platform-independent public interface.
* @ingroup qf
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-12-24 14:33:20 -05:00
* Last updated for version 5.6.0
* Last updated on 2015-12-18
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2015-12-24 14:33:20 -05:00
* Copyright (C) Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:07:04 -04:00
*
* This program is open source software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
2012-08-14 18:07:04 -04:00
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact information:
2015-12-24 14:33:20 -05:00
* http://www.state-machine.com
* mailto:info@state-machine.com
2014-04-06 11:43:13 -04:00
******************************************************************************
2015-04-28 13:45:35 -04:00
* @endcond
2014-04-06 11:43:13 -04:00
*/
2012-08-14 18:07:04 -04:00
#ifndef qf_h
#define qf_h
/****************************************************************************/
#if (QF_MAX_ACTIVE < 1) || (63 < QF_MAX_ACTIVE)
#error "QF_MAX_ACTIVE not defined or out of range. Valid range is 1..63"
#endif
#ifndef QF_EVENT_SIZ_SIZE
2014-04-06 11:43:13 -04:00
/*! Default value of the macro configurable value in qf_port.h */
2012-08-14 18:07:04 -04:00
#define QF_EVENT_SIZ_SIZE 2
#endif
#if (QF_EVENT_SIZ_SIZE == 1)
2013-09-23 14:34:35 -04:00
typedef uint8_t QEvtSize;
#elif (QF_EVENT_SIZ_SIZE == 2)
2014-04-06 11:43:13 -04:00
/*! The data type to store the block-size defined based on
* the macro #QF_EVENT_SIZ_SIZE. */
/**
2012-08-14 18:07:04 -04:00
* The dynamic range of this data type determines the maximum block
* size that can be managed by the pool.
*/
typedef uint16_t QEvtSize;
#elif (QF_EVENT_SIZ_SIZE == 4)
typedef uint32_t QEvtSize;
#else
#error "QF_EVENT_SIZ_SIZE defined incorrectly, expected 1, 2, or 4"
#endif
#ifndef QF_MAX_EPOOL
2014-04-06 11:43:13 -04:00
/*! Default value of the macro configurable value in qf_port.h */
2013-09-23 14:34:35 -04:00
#define QF_MAX_EPOOL 3
2012-08-14 18:07:04 -04:00
#endif
2013-09-23 14:34:35 -04:00
#ifndef QF_MAX_TICK_RATE
2014-04-06 11:43:13 -04:00
/*! Default value of the macro configurable value in qf_port.h */
2013-09-23 14:34:35 -04:00
#define QF_MAX_TICK_RATE 1
#endif
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#ifndef QF_TIMEEVT_CTR_SIZE
2014-04-06 11:43:13 -04:00
/*! macro to override the default ::QTimeEvtCtr size. Valid values:
* 1, 2, or 4; default 2
2012-08-14 18:07:04 -04:00
*/
2013-09-23 14:34:35 -04:00
#define QF_TIMEEVT_CTR_SIZE 2
2012-08-14 18:07:04 -04:00
#endif
/****************************************************************************/
2014-04-06 11:43:13 -04:00
struct QEQueue; /* forward declaration */
2015-05-14 15:45:53 -04:00
/*! QMActive active object (based on QMsm-implementation) */
2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* Active objects in QP are encapsulated state machines (each embedding an
2013-09-23 14:34:35 -04:00
* event queue and a thread) that communicate with one another asynchronously
* by sending and receiving events. Within an active object, events are
2012-08-14 18:07:04 -04:00
* processed sequentially in a run-to-completion (RTC) fashion, while QF
* encapsulates all the details of thread-safe event exchange and queuing.
2015-05-01 08:40:16 -04:00
* @n@n
* QMActive represents an active object that uses the QMsm-style state
* machine implementation strategy. This strategy requires the use of the
* QM modeling tool to generate state machine code automatically, but the
* code is faster than in the QHsm-style implementation strategy and needs
* less run-time support (smaller event-processor).
2012-08-14 18:07:04 -04:00
*
2015-05-01 08:40:16 -04:00
* @note
* ::QMActive is not intended to be instantiated directly, but rather serves
* as the base class for derivation of active objects in the application.
*
* @sa ::QActive
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @usage
2012-08-14 18:07:04 -04:00
* The following example illustrates how to derive an active object from
2015-05-14 15:45:53 -04:00
* QMActive. Please note that the ::QActive member @c super is defined as the
2015-04-28 13:45:35 -04:00
* __first__ member of the derived struct (see @ref oop).
2015-05-01 08:40:16 -04:00
* @include qf_qmactive.c
2012-08-14 18:07:04 -04:00
*/
2014-04-06 11:43:13 -04:00
typedef struct {
2015-05-01 08:40:16 -04:00
QMsm super; /*!< inherits ::QMsm */
2012-08-14 18:07:04 -04:00
#ifdef QF_EQUEUE_TYPE
2014-04-06 11:43:13 -04:00
/*! OS-dependent event-queue type. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* The type of the queue depends on the underlying operating system or
* a kernel. Many kernels support "message queues" that can be adapted
* to deliver QF events to the active object. Alternatively, QF provides
* a native event queue implementation that can be used as well.
*
2015-04-28 13:45:35 -04:00
* @note The native QF event queue is configured by defining the macro
2012-08-14 18:07:04 -04:00
* #QF_EQUEUE_TYPE as ::QEQueue.
*/
QF_EQUEUE_TYPE eQueue;
#endif
#ifdef QF_OS_OBJECT_TYPE
2014-04-06 11:43:13 -04:00
/*! OS-dependent per-thread object. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* This data might be used in various ways, depending on the QF port.
* In some ports osObject is used to block the calling thread when
* the native QF queue is empty. In other QF ports the OS-dependent
* object might be used differently.
*/
QF_OS_OBJECT_TYPE osObject;
#endif
#ifdef QF_THREAD_TYPE
2014-04-06 11:43:13 -04:00
/*! OS-dependent representation of the thread of the active object. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* This data might be used in various ways, depending on the QF port.
* In some ports thread is used to store the thread handle. In other
* ports thread can be the pointer to the Thread-Local-Storage (TLS).
*/
QF_THREAD_TYPE thread;
#endif
2014-04-06 11:43:13 -04:00
/*! QF priority associated with the active object. */
uint_fast8_t prio;
2012-08-14 18:07:04 -04:00
2015-05-01 08:40:16 -04:00
} QMActive;
2012-08-14 18:07:04 -04:00
2015-05-01 08:40:16 -04:00
/*! protected "constructor" of an QMActive active object. */
void QMActive_ctor(QMActive * const me, QStateHandler initial);
/*! Virtual table for the ::QMActive class */
2014-04-06 11:43:13 -04:00
typedef struct {
QMsmVtbl super; /*!< inherits QMsmVtbl */
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
/*! virtual function to start the active object (thread) */
2015-04-28 13:45:35 -04:00
/** @sa QACTIVE_START() */
2015-05-01 08:40:16 -04:00
void (*start)(QMActive * const me, uint_fast8_t prio,
2014-04-06 11:43:13 -04:00
QEvt const *qSto[], uint_fast16_t qLen,
void *stkSto, uint_fast16_t stkSize,
2013-09-23 14:34:35 -04:00
QEvt const *ie);
#ifdef Q_SPY
2014-04-06 11:43:13 -04:00
/*! virtual function to asynchronously post (FIFO) an event to an AO */
2015-04-28 13:45:35 -04:00
/** @sa QACTIVE_POST() and QACTIVE_POST_X() */
2015-05-01 08:40:16 -04:00
bool (*post)(QMActive * const me, QEvt const * const e,
2014-04-06 11:43:13 -04:00
uint_fast16_t const margin, void const * const sender);
2013-09-23 14:34:35 -04:00
#else
2015-05-01 08:40:16 -04:00
bool (*post)(QMActive * const me, QEvt const * const e,
2014-04-06 11:43:13 -04:00
uint_fast16_t const margin);
2013-09-23 14:34:35 -04:00
#endif
2014-04-06 11:43:13 -04:00
/*! virtual function to asynchronously post (LIFO) an event to an AO */
2015-04-28 13:45:35 -04:00
/** @sa QACTIVE_POST_LIFO() */
2015-05-01 08:40:16 -04:00
void (*postLIFO)(QMActive * const me, QEvt const * const e);
2013-09-23 14:34:35 -04:00
2015-05-01 08:40:16 -04:00
} QMActiveVtbl;
2013-09-23 14:34:35 -04:00
2015-05-01 08:40:16 -04:00
/****************************************************************************/
/*! Active Object (based on QHsm-implementation) */
/**
* @description
* Active objects in QP are encapsulated state machines (each embedding an
* event queue and a thread) that communicate with one another asynchronously
* by sending and receiving events. Within an active object, events are
* processed sequentially in a run-to-completion (RTC) fashion, while QF
* encapsulates all the details of thread-safe event exchange and queuing.
* @n@n
2015-05-14 15:45:53 -04:00
* ::QActive represents an active object that uses the QHsm-style
2015-05-01 08:40:16 -04:00
* implementation strategy for state machines. This strategy is tailored
* to manual coding, but it is also supported by the QM modeling tool.
* The resulting code is slower than in the QMsm-style implementation
* strategy.
*
* @note
* ::QActive inherits ::QMActive exactly, without adding any new attributes
* (or operations) and therefore, ::QActive is typedef'ed as ::QMActive.
* ::QActive is not intended to be instantiated directly, but rather serves
* as the base class for derivation of active objects in the application.
*
* @sa ::QMActive
*
* @usage
* The following example illustrates how to derive an active object from
2015-05-14 15:45:53 -04:00
* ::QActive. Please note that the ::QActive member @c super is defined as the
2015-05-01 08:40:16 -04:00
* __first__ member of the derived struct (see @ref oop).
* @include qf_qactive.c
*/
typedef QMActive QActive;
/*! Virtual Table for the ::QActive class (inherited from ::QMActiveVtbl */
/**
* @note
* ::QActive inherits ::QMActive exactly, without adding any new virtual
* functions and therefore, ::QActiveVtbl is typedef'ed as ::QMActiveVtbl.
*/
typedef QMActiveVtbl QActiveVtbl;
2015-05-14 15:45:53 -04:00
/*! protected "constructor" of an ::QActive active object. */
2015-05-01 08:40:16 -04:00
void QActive_ctor(QActive * const me, QStateHandler initial);
2015-05-14 15:45:53 -04:00
/* public functions for ::QActive / ::QMActive... */
2014-04-06 11:43:13 -04:00
/*! Implementation of the active object start operation. */
2015-06-04 22:47:13 -04:00
void QActive_start_(QMActive * const me, uint_fast8_t prio,
2014-04-06 11:43:13 -04:00
QEvt const *qSto[], uint_fast16_t qLen,
void *stkSto, uint_fast16_t stkSize,
2013-12-30 17:37:40 -05:00
QEvt const *ie);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Polymorphically start an active object. */
/**
2015-04-28 13:45:35 -04:00
* @description
* Starts execution of the AO and registers the AO with the framework.
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @param[in,out] me_ pointer (see @ref oop)
* @param[in] prio_ priority at which to start the active object
* @param[in] qSto_ pointer to the storage for the ring buffer of the
* event queue (used only with the built-in ::QEQueue)
* @param[in] qLen_ length of the event queue (in events)
* @param[in] stkSto_ pointer to the stack storage (used only when
* per-AO stack is needed)
* @param[in] stkSize_ stack size (in bytes)
* @param[in] param_ pointer to the additional port-specific parameter(s)
* (might be NULL).
2015-12-24 14:33:20 -05:00
* @usage
* @include qf_start.c
2013-09-23 14:34:35 -04:00
*/
2015-04-28 13:45:35 -04:00
#define QACTIVE_START(me_, prio_, qSto_, qLen_, stkSto_, stkLen_, param_) \
2015-05-01 08:40:16 -04:00
((*((QMActiveVtbl const *)((me_)->super.vptr))->start)( \
2015-04-28 13:45:35 -04:00
(me_), (prio_), (qSto_), (qLen_), (stkSto_), (stkLen_), (param_)))
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#ifdef Q_SPY
2014-04-06 11:43:13 -04:00
/*! Implementation of the active object post (FIFO) operation */
2015-06-04 22:47:13 -04:00
bool QActive_post_(QMActive * const me, QEvt const * const e,
2015-04-28 13:45:35 -04:00
uint_fast16_t const margin,
void const * const sender);
2014-04-06 11:43:13 -04:00
/*! Polymorphically posts an event to an active object (FIFO)
* with delivery guarantee. */
/**
2015-04-28 13:45:35 -04:00
* @description
2013-09-23 14:34:35 -04:00
* This macro asserts if the queue overflows and cannot accept the event.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @param[in,out] me_ pointer (see @ref oop)
* @param[in] e_ pointer to the event to post
* @param[in] sender_ pointer to the sender object.
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @note The @p sendedr_ parameter is actually only used when QS tracing
2014-04-06 11:43:13 -04:00
* is enabled (macro #Q_SPY is defined). When QS software tracing is
2015-04-28 13:45:35 -04:00
* disenabled, the QACTIVE_POST() macro does not pass the @p sender_
2014-04-06 11:43:13 -04:00
* argument, so the overhead of passing this extra argument is entirely
* avoided.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @note the pointer to the sender object is not necessarily a pointer
2012-08-14 18:07:04 -04:00
* to an active object. In fact, if QACTIVE_POST() is called from an
* interrupt or other context, you can create a unique object just to
2013-09-23 14:34:35 -04:00
* unambiguously identify the sender of the event.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @sa #QACTIVE_POST_X, QActive_post_().
2012-08-14 18:07:04 -04:00
*/
#define QACTIVE_POST(me_, e_, sender_) \
2015-05-01 08:40:16 -04:00
((void)(*((QMActiveVtbl const *)((me_)->super.vptr))->post)((me_), \
2015-04-28 13:45:35 -04:00
(e_), (uint_fast16_t)0, (sender_)))
2014-04-06 11:43:13 -04:00
/*! Polymorphically posts an event to an active object (FIFO)
* without delivery guarantee. */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* This macro does not assert if the queue overflows and cannot accept
* the event with the specified margin of free slots remaining.
*
2015-04-28 13:45:35 -04:00
* @param[in,out] me_ pointer (see @ref oop)
* @param[in] e_ pointer to the event to post
* @param[in] margin_ the minimum free slots in the queue, which
* must still be available after posting the event
* @param[in] sender_ pointer to the sender object.
2014-04-06 11:43:13 -04:00
*
2015-04-28 13:45:35 -04:00
* @returns 'true' if the posting succeeded, and 'false' if the posting
2014-04-06 11:43:13 -04:00
* failed due to insufficient margin of free slots available in the queue.
*
2015-04-28 13:45:35 -04:00
* @note The @p sender_ parameter is actually only used when QS tracing
2014-04-06 11:43:13 -04:00
* is enabled (macro #Q_SPY is defined). When QS software tracing is
2015-04-28 13:45:35 -04:00
* disabled, the QACTIVE_POST() macro does not pass the @p sender_
2014-04-06 11:43:13 -04:00
* argument, so the overhead of passing this extra argument is entirely
* avoided.
2013-09-23 14:34:35 -04:00
*
2015-04-28 13:45:35 -04:00
* @note the pointer to the sender object is not necessarily a pointer
2013-09-23 14:34:35 -04:00
* to an active object. In fact, if QACTIVE_POST() is called from an
* interrupt or other context, you can create a unique object just to
* unambiguously identify the sender of the event.
*
2015-04-28 13:45:35 -04:00
* @usage
* @include qf_postx.c
2013-09-23 14:34:35 -04:00
*/
#define QACTIVE_POST_X(me_, e_, margin_, sender_) \
2015-05-01 08:40:16 -04:00
((*((QMActiveVtbl const *)((me_)->super.vptr))->post)((me_), \
2013-09-23 14:34:35 -04:00
(e_), (margin_), (sender_)))
2012-08-14 18:07:04 -04:00
#else
2015-06-04 22:47:13 -04:00
bool QActive_post_(QMActive * const me, QEvt const * const e,
uint_fast16_t const margin);
2013-09-23 14:34:35 -04:00
#define QACTIVE_POST(me_, e_, sender_) \
2015-05-01 08:40:16 -04:00
((void)(*((QMActiveVtbl const *)((me_)->super.vptr))->post)((me_), \
2014-04-06 11:43:13 -04:00
(e_), (uint_fast16_t)0))
2013-09-23 14:34:35 -04:00
#define QACTIVE_POST_X(me_, e_, margin_, sender_) \
2015-05-01 08:40:16 -04:00
((*((QMActiveVtbl const *)((me_)->super.vptr))->post)((me_), \
2013-09-23 14:34:35 -04:00
(e_), (margin_)))
2012-08-14 18:07:04 -04:00
#endif
2014-04-06 11:43:13 -04:00
/*! Implementation of the active object post LIFO operation */
2015-06-04 22:47:13 -04:00
void QActive_postLIFO_(QMActive * const me, QEvt const * const e);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Polymorphically posts an event to an active object using the
* Last-In-First-Out (LIFO) policy. */
/**
2015-04-28 13:45:35 -04:00
* @param[in,out] me_ pointer (see @ref oop)
* @param[in] e_ pointer to the event to post
2013-09-23 14:34:35 -04:00
*/
#define QACTIVE_POST_LIFO(me_, e_) \
2015-05-01 08:40:16 -04:00
((*((QMActiveVtbl const *)((me_)->super.vptr))->postLIFO)((me_), (e_)))
2013-09-23 14:34:35 -04:00
2012-08-14 18:07:04 -04:00
2015-05-14 15:45:53 -04:00
/* protected functions for ::QActive / ::QMActive ...*/
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Stops execution of an active object and removes it from the
* framework's supervision. */
2015-06-04 22:47:13 -04:00
void QActive_stop(QMActive * const me);
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Subscribes for delivery of signal @p sig to the active object @p me. */
2012-08-14 18:07:04 -04:00
void QActive_subscribe(QActive const * const me, enum_t const sig);
2015-04-28 13:45:35 -04:00
/*! Un-subscribes from the delivery of signal @p sig to the AO @p me. */
2012-08-14 18:07:04 -04:00
void QActive_unsubscribe(QActive const * const me, enum_t const sig);
2015-04-28 13:45:35 -04:00
/*! Un-subscribes from the delivery of all signals to the AO @p me. */
2012-08-14 18:07:04 -04:00
void QActive_unsubscribeAll(QActive const * const me);
2014-04-06 11:43:13 -04:00
/*! Defer an event to a given separate event queue. */
2015-06-04 22:47:13 -04:00
bool QActive_defer(QMActive * const me,
2014-04-06 11:43:13 -04:00
QEQueue * const eq, QEvt const * const e);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Recall a deferred event from a given event queue. */
2015-06-04 22:47:13 -04:00
bool QActive_recall(QMActive * const me, QEQueue * const eq);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Get an event from the event queue of an active object. */
2015-06-04 22:47:13 -04:00
QEvt const *QActive_get_(QMActive *const me);
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
/****************************************************************************/
2012-08-14 18:07:04 -04:00
#if (QF_TIMEEVT_CTR_SIZE == 1)
2013-09-23 14:34:35 -04:00
typedef uint8_t QTimeEvtCtr;
#elif (QF_TIMEEVT_CTR_SIZE == 2)
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! type of the Time Event counter, which determines the dynamic
* range of the time delays measured in clock ticks. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* This typedef is configurable via the preprocessor switch
* #QF_TIMEEVT_CTR_SIZE. The other possible values of this type are
2015-04-28 13:45:35 -04:00
* as follows: @n
* uint8_t when (QF_TIMEEVT_CTR_SIZE == 1), and @n
2012-08-14 18:07:04 -04:00
* uint32_t when (QF_TIMEEVT_CTR_SIZE == 4).
*/
typedef uint16_t QTimeEvtCtr;
#elif (QF_TIMEEVT_CTR_SIZE == 4)
typedef uint32_t QTimeEvtCtr;
#else
#error "QF_TIMEEVT_CTR_SIZE defined incorrectly, expected 1, 2, or 4"
#endif
2014-04-06 11:43:13 -04:00
/*! Time Event structure */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* 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 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 structure.
2012-08-14 18:07:04 -04:00
* Typically, you will use a time event as-is, but 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.
*
2013-09-23 14:34:35 -04:00
* Internally, the armed time events are organized into linked lists--one list
* for every supported ticking rate. These linked lists are scanned in every
2013-12-30 17:37:40 -05:00
* invocation of the QF_tickX_() function. Only armed (timing out) time events
2013-09-23 14:34:35 -04:00
* are in the list, so only armed time events consume CPU cycles.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @sa ::QTimeEvt for the description of the data members @n @ref oop
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @note QF manages the time events in the function QF_tickX_(), which
2012-08-14 18:07:04 -04:00
* must be called periodically, preferably from the clock tick ISR.
*
2015-04-28 13:45:35 -04:00
* @note In this version of QF QTimeEvt objects should be allocated statically
2012-08-14 18:07:04 -04:00
* rather than dynamically from event pools. Currently, QF will not correctly
* recycle the dynamically allocated Time Events.
*/
2014-04-06 11:43:13 -04:00
typedef struct QTimeEvt {
/*! base structure from which QTimeEvt derives */
2012-08-14 18:07:04 -04:00
QEvt super;
2014-04-06 11:43:13 -04:00
/*! link to the next time event in the list */
struct QTimeEvt * volatile next;
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! the active object that receives the time events */
2013-09-23 14:34:35 -04:00
void * volatile act;
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! the internal down-counter of the time event. */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* The down-counter is decremented by 1 in every QF_tickX_() invocation.
* The time event fires (gets posted or published) when the down-counter
* reaches zero.
2012-08-14 18:07:04 -04:00
*/
2013-09-23 14:34:35 -04:00
QTimeEvtCtr volatile ctr;
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! the interval for periodic time event (zero for one-shot time event) */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* 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.
2012-08-14 18:07:04 -04:00
*/
QTimeEvtCtr interval;
} QTimeEvt;
/* public functions */
2014-04-06 11:43:13 -04:00
/*! The extended "constructor" to initialize a Time Event. */
2015-06-04 22:47:13 -04:00
void QTimeEvt_ctorX(QTimeEvt * const me, QMActive * const act,
2014-04-06 11:43:13 -04:00
enum_t const sig, uint_fast8_t tickRate);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Arm a time event (one shot or periodic) for direct event posting. */
2013-09-23 14:34:35 -04:00
void QTimeEvt_armX(QTimeEvt * const me,
QTimeEvtCtr const nTicks, QTimeEvtCtr const interval);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Rearm a time event. */
bool QTimeEvt_rearm(QTimeEvt * const me, QTimeEvtCtr const nTicks);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Disarm a time event. */
bool QTimeEvt_disarm(QTimeEvt * const me);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Get the current value of the down-counter of a time event. */
2012-08-14 18:07:04 -04:00
QTimeEvtCtr QTimeEvt_ctr(QTimeEvt const * const me);
2014-04-06 11:43:13 -04:00
/****************************************************************************/
/* QF facilities */
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Subscriber-List structure */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* This data type represents a set of active objects that subscribe to
* a given signal. The set is represented as an array of bits, where each
* bit corresponds to the unique priority of an active object.
*
2015-04-28 13:45:35 -04:00
* @sa ::QSubscrList for the description of the data members
2012-08-14 18:07:04 -04:00
*/
2014-04-06 11:43:13 -04:00
typedef struct {
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! An array of bits representing subscriber active objects. */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* Each bit in the array corresponds to the unique priority of the AO.
* The size of the array is determined of the maximum number of AOs
* in the application configured by the #QF_MAX_ACTIVE macro.
2012-08-14 18:07:04 -04:00
* For example, an active object of priority p is a subscriber if the
* following is true: ((bits[QF_div8Lkup[p]] & QF_pwr2Lkup[p]) != 0)
*
2015-04-28 13:45:35 -04:00
* @sa QF_psInit(), ::QF_div8Lkup, ::QF_pwr2Lkup, #QF_MAX_ACTIVE
2012-08-14 18:07:04 -04:00
*/
uint8_t bits[((QF_MAX_ACTIVE - 1) / 8) + 1];
} QSubscrList;
/* public functions */
2014-04-06 11:43:13 -04:00
/*! QF initialization. */
2012-08-14 18:07:04 -04:00
void QF_init(void);
2014-04-06 11:43:13 -04:00
/*! Publish-subscribe initialization. */
2013-12-30 17:37:40 -05:00
void QF_psInit(QSubscrList * const subscrSto, enum_t const maxSignal);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Event pool initialization for dynamic allocation of events. */
2015-04-28 13:45:35 -04:00
void QF_poolInit(void * const poolSto, uint_fast32_t const poolSize,
2014-04-06 11:43:13 -04:00
uint_fast16_t const evtSize);
2012-08-14 18:07:04 -04:00
2015-09-04 12:08:22 -04:00
/*! Obtain the block size of any registered event pools */
uint_fast16_t QF_poolGetMaxBlockSize(void);
2014-04-06 11:43:13 -04:00
/*! Transfers control to QF to run the application. */
2013-12-30 17:37:40 -05:00
int_t QF_run(void);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Function invoked by the application layer to stop the QF
* application and return control to the OS/Kernel. */
2012-08-14 18:07:04 -04:00
void QF_stop(void);
2014-04-06 11:43:13 -04:00
/*! Startup QF callback. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* The timeline for calling QF_onStartup() depends on the particular
* QF port. In most cases, QF_onStartup() is called from QF_run(), right
* before starting any multitasking kernel or the background loop.
*/
void QF_onStartup(void);
2014-04-06 11:43:13 -04:00
/*! Cleanup QF callback. */
/**
2015-04-28 13:45:35 -04:00
* @description
2012-08-14 18:07:04 -04:00
* QF_onCleanup() is called in some QF ports before QF returns to the
* underlying operating system or RTOS.
*
* This function is strongly platform-specific and is not implemented in
* the QF, but either in the QF port or in the Board Support Package (BSP)
* for the given application. Some QF ports might not require implementing
* QF_onCleanup() at all, because many embedded applications don't have
* anything to exit to.
*
2015-04-28 13:45:35 -04:00
* @sa QF_stop()
2012-08-14 18:07:04 -04:00
*/
void QF_onCleanup(void);
#ifdef Q_SPY
2014-04-06 11:43:13 -04:00
/*! Publish event to the framework. */
2013-12-30 17:37:40 -05:00
void QF_publish_(QEvt const * const e, void const * const sender);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Invoke the event publishing facility QF_publish_(). */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* This macro is the recommended way of publishing events, because it
* provides the vital information for software tracing and avoids any
* overhead when the tracing is disabled.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @param[in] e_ pointer to the posted event
* @param[in] sender_ pointer to the sender object. This argument is
2014-04-06 11:43:13 -04:00
* actually only used when QS software tracing is enabled
* (macro #Q_SPY is defined). When QS software tracing is
* disabled, the macro calls QF_publish_() without the
2015-04-28 13:45:35 -04:00
* @p sender_ parameter, so the overhead of passing this
2014-04-06 11:43:13 -04:00
* extra argument is entirely avoided.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @note the pointer to the sender object is not necessarily a pointer
2012-08-14 18:07:04 -04:00
* to an active object. In fact, if QF_PUBLISH() is called from an
* interrupt or other context, you can create a unique object just to
* unambiguously identify the publisher of the event.
*
2015-04-28 13:45:35 -04:00
* @sa QF_publish_().
2012-08-14 18:07:04 -04:00
*/
2013-02-12 10:04:39 -05:00
#define QF_PUBLISH(e_, sender_) \
2013-12-30 17:37:40 -05:00
(QF_publish_((e_), (void const *)(sender_)))
2012-08-14 18:07:04 -04:00
#else
2013-12-30 17:37:40 -05:00
void QF_publish_(QEvt const * const e);
#define QF_PUBLISH(e_, dummy_) (QF_publish_(e_))
2012-08-14 18:07:04 -04:00
#endif
#ifdef Q_SPY
2014-04-06 11:43:13 -04:00
/*! Processes all armed time events at every clock tick. */
void QF_tickX_(uint_fast8_t const tickRate, void const * const sender);
/*! Invoke the system clock tick processing QF_tickX_(). */
/**
2015-04-28 13:45:35 -04:00
* @description
2014-04-06 11:43:13 -04:00
* This macro is the recommended way of invoking clock tick processing,
* because it provides the vital information for software tracing and
* avoids any overhead when the tracing is disabled.
*
2015-04-28 13:45:35 -04:00
* @param[in] tickRate clock tick rate to be serviced through this call
* @param[in] sender pointer to the sender object. This argument
2014-04-06 11:43:13 -04:00
* is actually only used when QS software tracing is enabled
* (macro #Q_SPY is defined)
2015-04-28 13:45:35 -04:00
* @note
2014-04-06 11:43:13 -04:00
* When QS software tracing is disabled, the macro calls QF_tickX_()
2015-04-28 13:45:35 -04:00
* without the @p sender parameter, so the overhead of passing this
2014-04-06 11:43:13 -04:00
* extra argument is entirely avoided.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @note
* The pointer to the sender object is not necessarily a pointer
2013-09-23 14:34:35 -04:00
* to an active object. In fact, when #QF_TICK_X() is called from
* an interrupt, you would create a unique object just to unambiguously
* identify the ISR as the sender of the time events.
2012-08-14 18:07:04 -04:00
*
2015-12-24 14:33:20 -05:00
* @usage
* The following example shows how to invoke QF_TICK_X() for different
* system tick rates:
* @include qf_tickx.c
*
2015-04-28 13:45:35 -04:00
* @sa QF_tickX_().
2012-08-14 18:07:04 -04:00
*/
2013-12-30 17:37:40 -05:00
#define QF_TICK_X(tickRate_, sender_) (QF_tickX_((tickRate_), (sender_)))
2012-08-14 18:07:04 -04:00
#else
2014-04-06 11:43:13 -04:00
void QF_tickX_(uint_fast8_t const tickRate);
2013-12-30 17:37:40 -05:00
#define QF_TICK_X(tickRate_, dummy) (QF_tickX_(tickRate_))
2012-08-14 18:07:04 -04:00
#endif
2014-04-06 11:43:13 -04:00
/*! Invoke the system clock tick processing for rate 0 */
#define QF_TICK(sender_) QF_TICK_X((uint_fast8_t)0, (sender_))
2013-12-30 17:37:40 -05:00
2014-04-06 11:43:13 -04:00
/*! Returns 'true' if there are no armed time events at a given tick rate */
bool QF_noTimeEvtsActiveX(uint_fast8_t const tickRate);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Register an active object to be managed by the framework */
2015-05-14 15:45:53 -04:00
void QF_add_(QMActive * const a);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Remove the active object from the framework. */
2015-05-14 15:45:53 -04:00
void QF_remove_(QMActive const * const a);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Obtain the minimum of free entries of the given event pool. */
uint_fast16_t QF_getPoolMin(uint_fast8_t const poolId);
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! This function returns the minimum of free entries of
* the given event queue. */
uint_fast16_t QF_getQueueMin(uint_fast8_t const prio);
2012-08-14 18:07:04 -04:00
2015-09-04 12:08:22 -04:00
/*! Internal QF implementation of the dynamic event allocator. */
2014-04-06 11:43:13 -04:00
QEvt *QF_newX_(uint_fast16_t const evtSize,
uint_fast16_t const margin, enum_t const sig);
2012-08-14 18:07:04 -04:00
2015-09-04 12:08:22 -04:00
/*! Internal QF implementation of the event reference creator. */
QEvt const *QF_newRef_(QEvt const * const e, QEvt const * const evtRef);
2014-04-06 11:43:13 -04:00
#ifdef Q_EVT_CTOR /* Shall the constructor for the QEvt class be provided? */
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#define Q_NEW(evtT_, sig_, ...) \
2014-04-06 11:43:13 -04:00
(evtT_##_ctor((evtT_ *)QF_newX_((uint_fast16_t)sizeof(evtT_), \
(uint_fast16_t)0, (enum_t)0), (sig_), ##__VA_ARGS__))
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#define Q_NEW_X(e_, evtT_, margin_, sig_, ...) do { \
2014-04-06 11:43:13 -04:00
(e_) = (evtT_ *)QF_newX_((uint_fast16_t)sizeof(evtT_), \
2013-09-23 14:34:35 -04:00
(margin_), (enum_t)0); \
if ((e_) != (evtT_ *)0) { \
evtT_##_ctor((e_), (sig_), ##__VA_ARGS__); \
} \
} while (0)
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
#else
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Allocate a dynamic event. */
/**
2015-04-28 13:45:35 -04:00
* @description
2015-09-04 12:08:22 -04:00
* The macro calls the internal QF function QF_newX_() with
* margin == 0, which causes an assertion when the event cannot be
* successfully allocated.
*
2015-04-28 13:45:35 -04:00
* @param[in] evtT_ event type (class name) of the event to allocate
* @param[in] sig_ signal to assign to the newly allocated event
*
2015-04-28 13:45:35 -04:00
* @returns a valid event pointer cast to the type @p evtT_.
*
2015-04-28 13:45:35 -04:00
* @note
* If #Q_EVT_CTOR is defined, the Q_NEW() macro becomes variadic and
* takes all the arguments needed by the constructor of the event
* class being allocated. The constructor is then called by means
* of the placement-new operator.
2012-08-14 18:07:04 -04:00
*
2015-04-28 13:45:35 -04:00
* @usage
2012-08-14 18:07:04 -04:00
* The following example illustrates dynamic allocation of an event:
2015-04-28 13:45:35 -04:00
* @include qf_post.c
2012-08-14 18:07:04 -04:00
*/
#define Q_NEW(evtT_, sig_) \
2014-04-06 11:43:13 -04:00
((evtT_ *)QF_newX_((uint_fast16_t)sizeof(evtT_), \
(uint_fast16_t)0, (sig_)))
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
/*! Allocate a dynamic event (non-asserting version). */
/**
2015-04-28 13:45:35 -04:00
* @description
* This macro allocates a new event and sets the pointer @p e_, while
* leaving at least @p margin_ of events still available in the pool
*
2015-04-28 13:45:35 -04:00
* @param[in] evtT_ event type (class name) of the event to allocate
* @param[in] margin_ number of events that must remain available
* in the given pool after this allocation
* @param[in] sig_ signal to assign to the newly allocated event
2013-09-23 14:34:35 -04:00
*
2015-04-28 13:45:35 -04:00
* @returns an event pointer cast to the type @p evtT_ or NULL if the
* event cannot be allocated with the specified @p margin.
*
2015-04-28 13:45:35 -04:00
* @note
* If #Q_EVT_CTOR is defined, the Q_NEW_X() macro becomes variadic and
* takes all the arguments needed by the constructor of the event
* class being allocated. The constructor is then called and all the
* extra arguments are passed to it.
*
2015-04-28 13:45:35 -04:00
* @usage
2013-09-23 14:34:35 -04:00
* The following example illustrates dynamic allocation of an event:
2015-04-28 13:45:35 -04:00
* @include qf_postx.c
2013-09-23 14:34:35 -04:00
*/
#define Q_NEW_X(e_, evtT_, margin_, sig_) ((e_) = \
2014-04-06 11:43:13 -04:00
(evtT_ *)QF_newX_((uint_fast16_t)sizeof(evtT_), (margin_), (sig_)))
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
#endif /* Q_EVT_CTOR */
2012-08-14 18:07:04 -04:00
2015-09-04 12:08:22 -04:00
/*! Create a new reference of the current event `e` */
/**
* @description
* The current event processed by an active object is available only for
* the duration of the run-to-completion (RTC) step. After that step, the
* current event is no longer available and the framework might recycle
* (garbage-collect) the event. The macro Q_NEW_REF() explicitly creates
* a new reference to the current event that can be stored and used beyond
* the current RTC step, until the reference is explicitly recycled by
* means of the macro Q_DELETE_REF().
*
* @param[in,out] evtRef_ event reference to create
* @param[in] evtT_ event type (class name) of the event refrence
*
* @usage
* The example **defer** in the directory `examples/win32/defer` illustrates
* the use of Q_NEW_REF()
*
* @sa Q_DELETE_REF()
*/
#define Q_NEW_REF(evtRef_, evtT_) \
((evtRef_) = (evtT_ const *)QF_newRef_(e, &(evtRef_)->super))
/*! Delete the event reference */
/**
* @description
* Every event reference created with the macro Q_NEW_REF() needs to be
* eventually deleted by means of the macro Q_DELETE_REF() to avoid leaking
* the event.
*
* @param[in,out] evtRef_ event reference to delete
*
* @usage
* The example **defer** in the directory `examples/win32/defer` illustrates
* the use of Q_DELETE_REF()
*
* @sa Q_NEW_REF()
*/
#define Q_DELETE_REF(evtRef_) do { \
QF_gc(&(evtRef_)->super); \
(evtRef_) = (void *)0; \
} while (0)
2012-08-14 18:07:04 -04:00
2014-04-06 11:43:13 -04:00
/*! Recycle a dynamic event. */
2012-08-14 18:07:04 -04:00
void QF_gc(QEvt const * const e);
2014-04-06 11:43:13 -04:00
/*! Clear a specified region of memory to zero. */
void QF_bzero(void * const start, uint_fast16_t len);
2013-12-30 17:37:40 -05:00
2013-09-23 14:34:35 -04:00
#ifndef QF_CRIT_EXIT_NOP
2014-04-06 11:43:13 -04:00
/*! No-operation for exiting a critical section */
/**
2015-04-28 13:45:35 -04:00
* @description
2013-09-23 14:34:35 -04:00
* In some QF ports the critical section exit takes effect only on the
* next machine instruction. If this next instruction is another entry
* to a critical section, the critical section won't be really exited,
* but rather the two adjacent critical sections would be merged.
* The #QF_CRIT_EXIT_NOP() macro contains minimal code required to
* prevent such merging of critical sections in QF ports, in which it
* can occur.
*/
#define QF_CRIT_EXIT_NOP() ((void)0)
#endif
2012-08-14 18:07:04 -04:00
2013-02-12 10:04:39 -05:00
/****************************************************************************/
/* Useful lookup tables ...*/
2015-04-28 13:45:35 -04:00
/*! Lookup table for @c (1 << ((n-1) % 8)), where n is the index into
2014-04-06 11:43:13 -04:00
* the table. */
/**
2015-04-28 13:45:35 -04:00
* @note Index range n = 0..64. The first index (n == 0) should never be used.
2012-08-14 18:07:04 -04:00
*/
2015-12-24 14:33:20 -05:00
extern uint8_t const QF_pwr2Lkup[65];
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Lookup table for @c ~(1 << ((n-1) % 8)), where n is the index
2014-04-06 11:43:13 -04:00
* into the table. */
/**
2015-04-28 13:45:35 -04:00
* @note Index range n = 0..64. The first index (n == 0) should never be used.
2012-08-14 18:07:04 -04:00
*/
2015-12-24 14:33:20 -05:00
extern uint8_t const QF_invPwr2Lkup[65];
2012-08-14 18:07:04 -04:00
2015-04-28 13:45:35 -04:00
/*! Lookup table for @c (n-1)/8 , where n is the index into the table. */
2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @note Index range n = 0..64. The first index (n == 0) should never be used.
2012-08-14 18:07:04 -04:00
*/
2015-12-24 14:33:20 -05:00
extern uint8_t const QF_div8Lkup[65];
2012-08-14 18:07:04 -04:00
2013-02-12 10:04:39 -05:00
/* Log-base-2 calculations ...*/
#ifndef QF_LOG2
2015-04-28 13:45:35 -04:00
/*! Macro to return (log2(n_) + 1), where @p n_ = 0..255. */
2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @description
2013-02-12 10:04:39 -05:00
* This macro delivers the 1-based number of the most significant 1-bit
* of a byte. This macro can be re-implemented in the QP ports, if the CPU
* supports special instructions, such as CLZ (count leading zeros).
*
* If the macro is not defined in the port, the default implementation
* uses a lookup table.
*/
2015-12-24 14:33:20 -05:00
#define QF_LOG2(n_) (QF_log2Lkup[(n_)])
2013-02-12 10:04:39 -05:00
2014-04-06 11:43:13 -04:00
/*! Lookup table for (log2(n) + 1), where n is the index into the table */
/**
2015-04-28 13:45:35 -04:00
* @description
2013-02-12 10:04:39 -05:00
* This lookup delivers the 1-based number of the most significant 1-bit
* of a byte.
*/
2015-12-24 14:33:20 -05:00
extern uint8_t const QF_log2Lkup[256];
2013-02-12 10:04:39 -05:00
2015-04-28 13:45:35 -04:00
/*! Macro to include the QF_log2Lkup table in the code or skip it,
* if undefined.
*/
2013-10-10 19:59:51 -04:00
#define QF_LOG2LKUP 1
2014-04-21 21:45:23 -04:00
2014-04-06 11:43:13 -04:00
#endif /* QF_LOG2 */
2013-02-12 10:04:39 -05:00
2014-04-06 11:43:13 -04:00
/*! array of registered active objects */
/**
2015-04-28 13:45:35 -04:00
* @note Not to be used by Clients directly, only in ports of QF
2012-08-14 18:07:04 -04:00
*/
2015-05-14 15:45:53 -04:00
extern QMActive *QF_active_[QF_MAX_ACTIVE + 1];
2012-08-14 18:07:04 -04:00
2013-09-23 14:34:35 -04:00
/****************************************************************************/
2015-05-14 15:45:53 -04:00
/*! get the current QF version number string of the form "X.Y.Z" */
#define QF_getVersion() (QP_versionStr)
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
#endif /* qf_h */
2013-09-23 14:34:35 -04:00