qpc/include/qxk.h

207 lines
7.9 KiB
C
Raw Normal View History

2015-12-24 14:33:20 -05:00
/**
* @file
2016-09-23 12:08:21 -04:00
* @brief QXK/C (preemptive dual-mode kernel) platform-independent
2015-12-24 14:33:20 -05:00
* public interface.
* @ingroup qxk
* @cond
******************************************************************************
2020-08-24 16:41:40 -04:00
* Last updated for version 6.9.0
* Last updated on 2020-08-11
2015-12-24 14:33:20 -05:00
*
2019-10-27 11:57:33 -04:00
* Q u a n t u m L e a P s
* ------------------------
* Modern Embedded Software
2015-12-24 14:33:20 -05:00
*
2020-08-24 16:41:40 -04:00
* Copyright (C) 2005-2019 Quantum Leaps, LLC. All rights reserved.
2015-12-24 14:33:20 -05: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
* (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
2019-10-27 11:57:33 -04:00
* along with this program. If not, see <www.gnu.org/licenses>.
2015-12-24 14:33:20 -05:00
*
* Contact information:
2019-12-31 15:55:08 -05:00
* <www.state-machine.com/licensing>
2019-10-27 11:57:33 -04:00
* <info@state-machine.com>
2015-12-24 14:33:20 -05:00
******************************************************************************
* @endcond
*/
2019-10-27 11:57:33 -04:00
#ifndef QXK_H
#define QXK_H
2015-12-24 14:33:20 -05:00
2016-09-23 12:08:21 -04:00
#include "qequeue.h" /* QXK kernel uses the native QP event queue */
#include "qmpool.h" /* QXK kernel uses the native QP memory pool */
#include "qpset.h" /* QXK kernel uses the native QP priority set */
2015-12-24 14:33:20 -05:00
/****************************************************************************/
2018-02-18 12:11:16 -05:00
/* QF configuration for QXK -- data members of the QActive class... */
2015-12-24 14:33:20 -05:00
2020-08-24 16:41:40 -04:00
/* QXK event-queue used for AOs */
2015-12-24 14:33:20 -05:00
#define QF_EQUEUE_TYPE QEQueue
2020-08-24 16:41:40 -04:00
/* QXK OS-object used to store the private stack poiner for extended threads.
2018-02-18 12:11:16 -05:00
* (The private stack pointer is NULL for basic-threads).
2015-12-24 14:33:20 -05:00
*/
2020-08-24 16:41:40 -04:00
#define QF_OS_OBJECT_TYPE void*
2017-07-06 13:30:17 -04:00
2020-08-24 16:41:40 -04:00
/* QXK thread type used to store the private Thread-Local Storage pointer. */
#define QF_THREAD_TYPE void*
2015-12-24 14:33:20 -05:00
2017-07-06 13:30:17 -04:00
/*! Access Thread-Local Storage (TLS) and cast it on the given @p type_ */
#define QXK_TLS(type_) ((type_)QXK_current()->thread)
/****************************************************************************/
struct QActive; /* forward declaration */
2015-12-24 14:33:20 -05:00
/****************************************************************************/
/*! attributes of the QXK kernel */
typedef struct {
2017-12-11 12:13:22 -05:00
struct QActive * volatile curr; /*!< current thread pointer (NULL=basic) */
struct QActive * volatile next; /*!< next thread pointer to execute */
uint8_t volatile actPrio; /*!< prio of the active AO */
uint8_t volatile lockPrio; /*!< lock prio (0 == no-lock) */
uint8_t volatile lockHolder; /*!< prio of the lock holder */
uint8_t volatile intNest; /*!< ISR nesting level */
2018-02-18 12:11:16 -05:00
struct QActive * idleThread; /*!< pointer to the idle thread */
2020-08-24 16:41:40 -04:00
QPSet readySet; /*!< ready-set of all threads */
2019-12-31 15:55:08 -05:00
} QXK_PrivAttr;
2015-12-24 14:33:20 -05:00
/*! global attributes of the QXK kernel */
2019-12-31 15:55:08 -05:00
extern QXK_PrivAttr QXK_attr_;
2015-12-24 14:33:20 -05:00
/****************************************************************************/
2018-02-18 12:11:16 -05:00
#ifdef QXK_ON_CONTEXT_SW
/*! QXK context switch callback (customized in BSPs for QXK) */
/**
* @description
* This callback function provides a mechanism to perform additional
* custom operations when QXK switches context from one thread to
* another.
*
* @param[in] prev pointer to the previous thread (active object)
* (prev==0 means that @p prev was the QXK idle thread)
* @param[in] next pointer to the next thread (active object)
* (next==0) means that @p next is the QXK idle thread)
* @attention
* QXK_onContextSw() is invoked with interrupts **disabled** and must also
* return with interrupts **disabled**.
*
* @note
* This callback is enabled by defining the macro #QXK_ON_CONTEXT_SW.
*
* @include qxk_oncontextsw.c
*/
void QXK_onContextSw(struct QActive *prev, struct QActive *next);
#endif /* QXK_ON_CONTEXT_SW */
2015-12-24 14:33:20 -05:00
/*! QXK idle callback (customized in BSPs for QXK) */
/**
2018-02-18 12:11:16 -05:00
* @description
* QXK_onIdle() is called continuously by the QXK idle thread. This callback
2015-12-24 14:33:20 -05:00
* gives the application an opportunity to enter a power-saving CPU mode,
* or perform some other idle processing.
*
2018-02-18 12:11:16 -05:00
* @note
* QXK_onIdle() is invoked with interrupts enabled and must also return with
* interrupts enabled.
2015-12-24 14:33:20 -05:00
*/
void QXK_onIdle(void);
2016-09-01 11:57:27 -04:00
/****************************************************************************/
2016-09-23 12:08:21 -04:00
/*! QXK scheduler finds the highest-priority thread ready to run */
uint_fast8_t QXK_sched_(void);
/*! QXK activator activates the next active object. The activated AO preempts
* the currently executing AOs.
*/
void QXK_activate_(void);
2015-12-24 14:33:20 -05:00
2017-07-06 13:30:17 -04:00
/****************************************************************************/
/*! return the currently executing active-object/thread */
struct QActive *QXK_current(void);
2015-12-24 14:33:20 -05:00
/****************************************************************************/
2017-08-21 18:20:16 -04:00
/*! QXK Scheduler locking */
2015-12-24 14:33:20 -05:00
2017-08-21 18:20:16 -04:00
/*! The scheduler lock status */
typedef uint_fast16_t QSchedStatus;
2015-12-24 14:33:20 -05:00
2017-08-21 18:20:16 -04:00
/*! QXK Scheduler lock */
QSchedStatus QXK_schedLock(uint_fast8_t ceiling);
2015-12-24 14:33:20 -05:00
2017-08-21 18:20:16 -04:00
/*! QXK Scheduler unlock */
void QXK_schedUnlock(QSchedStatus stat);
2015-12-24 14:33:20 -05:00
/****************************************************************************/
/* interface used only inside QP implementation, but not in applications */
#ifdef QP_IMPL
2016-03-30 18:50:33 -04:00
#ifndef QXK_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
*/
2020-03-17 21:33:20 -04:00
#define QXK_ISR_CONTEXT_() (QXK_attr_.intNest != 0U)
2016-03-30 18:50:33 -04:00
#endif /* QXK_ISR_CONTEXT_ */
/* QF-specific scheduler locking */
2016-09-29 18:08:33 -04:00
/*! Internal macro to represent the scheduler lock status
2016-09-01 11:57:27 -04:00
* that needs to be preserved to allow nesting of locks.
*/
2017-08-21 18:20:16 -04:00
#define QF_SCHED_STAT_ QSchedStatus lockStat_;
2016-09-01 11:57:27 -04:00
2016-09-29 18:08:33 -04:00
/*! Internal macro for selective scheduler locking. */
2019-10-27 11:57:33 -04:00
#define QF_SCHED_LOCK_(prio_) do { \
if (QXK_ISR_CONTEXT_()) { \
2020-03-17 21:33:20 -04:00
lockStat_ = 0xFFU; \
2019-10-27 11:57:33 -04:00
} else { \
2017-08-21 18:20:16 -04:00
lockStat_ = QXK_schedLock((prio_)); \
2019-10-27 11:57:33 -04:00
} \
2019-12-31 15:55:08 -05:00
} while (false)
2016-04-01 10:02:43 -04:00
2016-09-29 18:08:33 -04:00
/*! Internal macro for selective scheduler unlocking. */
2020-03-17 21:33:20 -04:00
#define QF_SCHED_UNLOCK_() do { \
if (lockStat_ != 0xFFU) { \
QXK_schedUnlock(lockStat_); \
} \
2019-12-31 15:55:08 -05:00
} while (false)
2016-03-30 18:50:33 -04:00
2015-12-24 14:33:20 -05:00
#define QACTIVE_EQUEUE_WAIT_(me_) \
2017-08-21 18:20:16 -04:00
(Q_ASSERT_ID(110, (me_)->eQueue.frontEvt != (QEvt *)0))
2015-12-24 14:33:20 -05:00
2019-10-27 11:57:33 -04:00
#define QACTIVE_EQUEUE_SIGNAL_(me_) do { \
2017-12-11 12:13:22 -05:00
QPSet_insert(&QXK_attr_.readySet, (uint_fast8_t)(me_)->prio); \
2019-10-27 11:57:33 -04:00
if (!QXK_ISR_CONTEXT_()) { \
2020-03-17 21:33:20 -04:00
if (QXK_sched_() != 0U) { \
2019-10-27 11:57:33 -04:00
QXK_activate_(); \
} \
} \
2019-12-31 15:55:08 -05:00
} while (false)
2015-12-24 14:33:20 -05:00
/* native QF event pool operations */
#define QF_EPOOL_TYPE_ QMPool
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
(QMPool_init(&(p_), (poolSto_), (poolSize_), (evtSize_)))
#define QF_EPOOL_EVENT_SIZE_(p_) ((uint_fast16_t)(p_).blockSize)
#define QF_EPOOL_GET_(p_, e_, m_) ((e_) = (QEvt *)QMPool_get(&(p_), (m_)))
#define QF_EPOOL_PUT_(p_, e_) (QMPool_put(&(p_), (e_)))
#endif /* QP_IMPL */
2019-10-27 11:57:33 -04:00
#endif /* QXK_H */