qpc/include/qk.h

178 lines
6.2 KiB
C
Raw Normal View History

2014-04-06 11:43:13 -04:00
/**
2015-04-28 13:45:35 -04:00
* @file
* @brief QK/C (preemptive non-blocking kernel) platform-independent
* public interface.
* @ingroup qk
* @cond
2014-04-06 11:43:13 -04:00
******************************************************************************
2016-12-16 10:34:09 -05:00
* Last updated for version 5.8.1
* Last updated on 2016-12-14
2012-08-14 18:07:04 -04:00
*
* Q u a n t u m L e a P s
* ---------------------------
* innovating embedded systems
*
2016-03-30 18:50:33 -04: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:
2016-03-30 18:50:33 -04: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 qk_h
#define qk_h
2016-09-23 12:08:21 -04:00
#include "qequeue.h" /* QK kernel uses the native QP event queue */
#include "qmpool.h" /* QK kernel uses the native QP memory pool */
#include "qpset.h" /* QK kernel uses the native QP priority set */
2012-08-14 18:07:04 -04:00
/****************************************************************************/
/* QF configuration for QK */
2014-04-06 11:43:13 -04:00
/*! This macro defines the type of the event queue used for the
* active objects. */
/**
2015-12-24 14:33:20 -05:00
* @description
2016-09-01 11:57:27 -04:00
* QK uses the native QF event queue QEQueue.
2015-12-24 14:33:20 -05:00
*/
2016-09-01 11:57:27 -04:00
#define QF_EQUEUE_TYPE QEQueue
2012-08-14 18:07:04 -04:00
2016-09-01 11:57:27 -04:00
/****************************************************************************/
/*! attributes of the QK kernel */
typedef struct {
2016-09-29 18:08:33 -04:00
uint_fast8_t volatile actPrio; /*!< prio of the active AO */
uint_fast8_t volatile nextPrio; /*!< prio of the next AO to execute */
2016-09-01 11:57:27 -04:00
uint_fast8_t volatile lockPrio; /*!< lock prio (0 == no-lock) */
2016-09-29 18:08:33 -04:00
uint_fast8_t volatile lockHolder; /*!< prio of the AO holding the lock */
2016-09-01 11:57:27 -04:00
#ifndef QK_ISR_CONTEXT_
uint_fast8_t volatile intNest; /*!< ISR nesting level */
#endif /* QK_ISR_CONTEXT_ */
2016-09-23 12:08:21 -04:00
QPSet readySet; /*!< QK ready-set of AOs */
2016-09-01 11:57:27 -04:00
} QK_Attr;
2012-08-14 18:07:04 -04:00
2016-09-01 11:57:27 -04:00
/*! global attributes of the QK kernel */
extern QK_Attr QK_attr_;
2012-08-14 18:07:04 -04:00
/****************************************************************************/
2014-04-06 11:43:13 -04:00
/*! QK idle callback (customized in BSPs for QK) */
/**
2013-02-12 10:04:39 -05:00
* QK_onIdle() is called continuously by the QK idle loop. This callback
2012-08-14 18:07:04 -04:00
* gives the application an opportunity to enter a power-saving CPU mode,
* or perform some other idle processing.
*
2015-04-28 13:45:35 -04:00
* @note QK_onIdle() is invoked with interrupts enabled and must also
2012-08-14 18:07:04 -04:00
* return with interrupts enabled.
*
2015-04-28 13:45:35 -04:00
* @sa QV_onIdle()
2012-08-14 18:07:04 -04:00
*/
void QK_onIdle(void);
2016-09-01 11:57:27 -04:00
/****************************************************************************/
2016-09-29 18:08:33 -04:00
/*! QK scheduler finds the highest-priority thread ready to run */
uint_fast8_t QK_sched_(void);
2016-09-01 11:57:27 -04:00
2016-09-29 18:08:33 -04:00
/*! QK activator activates the next active object. The activated AO preempts
* the currently executing AOs.
*/
void QK_activate_(void);
2016-09-01 11:57:27 -04:00
2015-12-24 14:33:20 -05:00
/****************************************************************************/
2016-03-30 18:50:33 -04:00
/*! QK priority-ceiling mutex class */
2015-12-24 14:33:20 -05:00
typedef struct {
2016-09-01 11:57:27 -04:00
uint_fast8_t lockPrio; /*!< lock prio (priority ceiling) */
uint_fast8_t prevPrio; /*!< previoius lock prio */
2015-12-24 14:33:20 -05:00
} QMutex;
2016-09-01 11:57:27 -04:00
/*! The QMutex initialization */
2016-03-30 18:50:33 -04:00
void QMutex_init(QMutex * const me, uint_fast8_t prio);
2014-04-06 11:43:13 -04:00
2016-03-30 18:50:33 -04:00
/*! QMutex lock */
2015-12-24 14:33:20 -05:00
void QMutex_lock(QMutex * const me);
2014-04-06 11:43:13 -04:00
2016-03-30 18:50:33 -04:00
/*! QMutex unlock */
2016-04-01 10:02:43 -04:00
void QMutex_unlock(QMutex * const me);
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 QK version number string of the form "X.Y.Z" */
#define QK_getVersion() (QP_versionStr)
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
/****************************************************************************/
/* interface used only inside QP implementation, but not in applications */
#ifdef QP_IMPL
2013-09-23 14:34:35 -04:00
2016-03-30 18:50:33 -04:00
#ifndef QK_ISR_CONTEXT_
2016-09-29 18:08:33 -04:00
/*! Internal macro that reports the execution context (ISR vs. thread)
2016-03-30 18:50:33 -04:00
*/
/*! @returns true if the code executes in the ISR context and false
* otherwise
*/
2016-09-01 11:57:27 -04:00
#define QK_ISR_CONTEXT_() (QK_attr_.intNest != (uint_fast8_t)0)
2016-03-30 18:50:33 -04:00
#endif /* QK_ISR_CONTEXT_ */
2016-09-29 18:08:33 -04:00
/* QK-specific scheduler locking */
/*! Internal macro to represent the scheduler lock status
2016-03-30 18:50:33 -04:00
* that needs to be preserved to allow nesting of locks.
*/
2016-09-29 18:08:33 -04:00
#define QF_SCHED_STAT_ QMutex schedLock_;
2016-03-30 18:50:33 -04:00
2016-09-29 18:08:33 -04:00
/*! Internal macro for selective scheduler locking. */
#define QF_SCHED_LOCK_(prio_) do { \
2016-03-30 18:50:33 -04:00
if (QK_ISR_CONTEXT_()) { \
2016-09-29 18:08:33 -04:00
schedLock_.lockPrio = (uint_fast8_t)0; \
2016-03-30 18:50:33 -04:00
} else { \
2016-09-29 18:08:33 -04:00
QMutex_init(&schedLock_, (prio_)); \
QMutex_lock(&schedLock_); \
2016-03-30 18:50:33 -04:00
} \
} while (0)
2016-09-29 18:08:33 -04:00
/*! Internal macro for selective scheduler unlocking. */
#define QF_SCHED_UNLOCK_() do { \
if (schedLock_.lockPrio != (uint_fast8_t)0) { \
QMutex_unlock(&schedLock_); \
} \
} while (0)
2016-03-30 18:50:33 -04:00
2016-09-29 18:08:33 -04:00
/* native event queue operations... */
2013-09-23 14:34:35 -04:00
#define QACTIVE_EQUEUE_WAIT_(me_) \
2014-04-06 11:43:13 -04:00
(Q_ASSERT_ID(0, (me_)->eQueue.frontEvt != (QEvt *)0))
2013-09-23 14:34:35 -04:00
2015-12-24 14:33:20 -05:00
#define QACTIVE_EQUEUE_SIGNAL_(me_) do { \
2016-09-23 12:08:21 -04:00
QPSet_insert(&QK_attr_.readySet, (me_)->prio); \
2015-12-24 14:33:20 -05:00
if (!QK_ISR_CONTEXT_()) { \
2016-09-29 18:08:33 -04:00
if (QK_sched_() != (uint_fast8_t)0) { \
QK_activate_(); \
2013-09-23 14:34:35 -04:00
} \
2015-12-24 14:33:20 -05:00
} \
} while (0)
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
/* native QF event pool operations */
2013-09-23 14:34:35 -04:00
#define QF_EPOOL_TYPE_ QMPool
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
2013-12-30 17:37:40 -05:00
(QMPool_init(&(p_), (poolSto_), (poolSize_), (evtSize_)))
2014-04-06 11:43:13 -04:00
#define QF_EPOOL_EVENT_SIZE_(p_) ((uint_fast16_t)(p_).blockSize)
2013-09-23 14:34:35 -04:00
#define QF_EPOOL_GET_(p_, e_, m_) ((e_) = (QEvt *)QMPool_get(&(p_), (m_)))
#define QF_EPOOL_PUT_(p_, e_) (QMPool_put(&(p_), (e_)))
2014-04-06 11:43:13 -04:00
#endif /* QP_IMPL */
2013-09-23 14:34:35 -04:00
2014-04-06 11:43:13 -04:00
#endif /* qk_h */