2015-12-31 14:56:37 -05:00
|
|
|
/// @file
|
|
|
|
/// @brief QXK/C++ preemptive extended (blocking) kernel, platform-independent
|
|
|
|
/// public interface.
|
|
|
|
/// @ingroup qxk
|
|
|
|
/// @cond
|
|
|
|
///***************************************************************************
|
2018-03-19 14:51:26 -04:00
|
|
|
/// Last updated for version 6.2.0
|
|
|
|
/// Last updated on 2018-03-16
|
2015-12-31 14:56:37 -05:00
|
|
|
///
|
|
|
|
/// Q u a n t u m L e a P s
|
|
|
|
/// ---------------------------
|
|
|
|
/// innovating embedded systems
|
|
|
|
///
|
2018-03-19 14:51:26 -04:00
|
|
|
/// Copyright (C) 2002-2018 Quantum Leaps. All rights reserved.
|
2015-12-31 14:56:37 -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
|
|
|
|
/// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
///
|
|
|
|
/// Contact information:
|
2018-02-18 12:08:21 -05:00
|
|
|
/// https://www.state-machine.com
|
2015-12-31 14:56:37 -05:00
|
|
|
/// mailto:info@state-machine.com
|
|
|
|
///***************************************************************************
|
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
#ifndef qxk_h
|
|
|
|
#define qxk_h
|
|
|
|
|
|
|
|
#include "qequeue.h" // QXK kernel uses the native QF event queue
|
|
|
|
#include "qmpool.h" // QXK kernel uses the native QF memory pool
|
|
|
|
#include "qpset.h" // QXK kernel uses the native QF priority set
|
|
|
|
|
|
|
|
//****************************************************************************
|
2018-02-18 12:08:21 -05:00
|
|
|
// QF configuration for QXK -- data members of the QActive class...
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2018-02-18 12:08:21 -05:00
|
|
|
//! Kernel-dependent type of the event queue used for QXK threads
|
|
|
|
//
|
2015-12-31 14:56:37 -05:00
|
|
|
/// @description
|
|
|
|
/// QXK uses the native QF event queue QEQueue.
|
|
|
|
///
|
|
|
|
#define QF_EQUEUE_TYPE QEQueue
|
|
|
|
|
2018-02-18 12:08:21 -05:00
|
|
|
//! Kernel-dependent OS-attribute of threads in QXK
|
|
|
|
//
|
2015-12-31 14:56:37 -05:00
|
|
|
/// @description
|
2018-02-18 12:08:21 -05:00
|
|
|
/// QXK uses this member to store the private stack poiner for extended
|
|
|
|
/// threads. (The private stack pointer is NULL for basic-threads).
|
2015-12-31 14:56:37 -05:00
|
|
|
///
|
2017-07-06 13:31:32 -04:00
|
|
|
#define QF_OS_OBJECT_TYPE void*
|
|
|
|
|
2018-02-18 12:08:21 -05:00
|
|
|
//! Kernel-dependent type of the thread attribute in QXK
|
|
|
|
//
|
2017-07-06 13:31:32 -04:00
|
|
|
/// @description
|
|
|
|
/// QXK uses this member to store the private Thread-Local Storage pointer.
|
|
|
|
///
|
2016-09-29 19:54:50 -04:00
|
|
|
#define QF_THREAD_TYPE void*
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2017-07-06 13:31:32 -04:00
|
|
|
//! Access Thread-Local Storage (TLS) and cast it on the given @p type_
|
|
|
|
#define QXK_TLS(type_) (static_cast<type_>(QXK_current()->m_thread))
|
|
|
|
|
|
|
|
|
|
|
|
//****************************************************************************
|
|
|
|
namespace QP {
|
|
|
|
class QActive; // forward declaration
|
|
|
|
} // namespace QP
|
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
//****************************************************************************
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
//! attributes of the QXK kernel
|
|
|
|
struct QXK_Attr {
|
2017-12-12 17:03:36 -05:00
|
|
|
QP::QActive * volatile curr; //!< currently executing thread
|
|
|
|
QP::QActive * volatile next; //!< next thread to execute
|
|
|
|
uint8_t volatile actPrio; //!< prio of the active basic thread
|
|
|
|
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:08:21 -05:00
|
|
|
QP::QActive * idleThread; //!< pointer to the idle thread
|
2016-09-29 19:54:50 -04:00
|
|
|
QP::QPSet readySet; //!< ready-set of basic- and extended-threads
|
2015-12-31 14:56:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
//! global attributes of the QXK kernel
|
|
|
|
extern QXK_Attr QXK_attr_;
|
|
|
|
|
2016-09-29 19:54:50 -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);
|
|
|
|
|
2017-07-06 13:31:32 -04:00
|
|
|
//! return the currently executing active-object/thread
|
|
|
|
QP::QActive *QXK_current(void);
|
|
|
|
|
2018-02-18 12:08:21 -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.cpp
|
|
|
|
///
|
|
|
|
void QXK_onContextSw(QP::QActive *prev, QP::QActive *next);
|
|
|
|
|
|
|
|
#endif // QXK_ON_CONTEXT_SW
|
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
//****************************************************************************
|
|
|
|
namespace QP {
|
|
|
|
|
2017-08-21 18:21:15 -04:00
|
|
|
//! The scheduler lock status
|
|
|
|
typedef uint_fast16_t QSchedStatus;
|
|
|
|
|
2015-12-31 14:56:37 -05:00
|
|
|
//****************************************************************************
|
|
|
|
//! QXK services.
|
|
|
|
/// @description
|
|
|
|
/// This class groups together QXK services. It has only static members and
|
|
|
|
/// should not be instantiated.
|
|
|
|
///
|
|
|
|
/// @note The QXK initialization and the QXK scheduler belong conceptually
|
|
|
|
/// to the QXK class (as static class members). However, to avoid C++
|
|
|
|
/// potential name-mangling problems in assembly language, these elements
|
|
|
|
/// are defined outside of the QXK class and outside the QP namespace with
|
|
|
|
/// the extern "C" linkage specification.
|
|
|
|
class QXK {
|
|
|
|
public:
|
2017-08-21 18:21:15 -04:00
|
|
|
//! QXK selective scheduler lock
|
|
|
|
static QSchedStatus schedLock(uint_fast8_t const ceiling);
|
2015-12-31 14:56:37 -05:00
|
|
|
|
2017-08-21 18:21:15 -04:00
|
|
|
//! QXK selective scheduler unlock
|
|
|
|
static void schedUnlock(QSchedStatus const stat);
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
//! QXK idle callback (customized in BSPs for QXK)
|
|
|
|
/// @description
|
|
|
|
/// QP::QXK::onIdle() is called continously by the QXK idle loop. This
|
|
|
|
/// callback gives the application an opportunity to enter a power-saving
|
|
|
|
/// CPU mode, or perform some other idle processing.
|
|
|
|
///
|
|
|
|
/// @note QP::QXK::onIdle() is invoked with interrupts enabled and must
|
|
|
|
/// also return with interrupts enabled.
|
|
|
|
///
|
|
|
|
/// @sa QP::QF::onIdle()
|
|
|
|
static void onIdle(void);
|
|
|
|
|
|
|
|
//! get the current QXK version number string of the form X.Y.Z
|
|
|
|
static char_t const *getVersion(void) {
|
|
|
|
return versionStr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace QP
|
|
|
|
|
|
|
|
|
|
|
|
//****************************************************************************
|
|
|
|
// interface used only inside QF, but not in applications
|
|
|
|
|
|
|
|
#ifdef QP_IMPL
|
|
|
|
|
|
|
|
#ifndef QXK_ISR_CONTEXT_
|
|
|
|
//! Internal port-specific macro that reports the execution context
|
|
|
|
// (ISR vs. thread).
|
|
|
|
/// @returns true if the code executes in the ISR context and false
|
|
|
|
/// otherwise
|
|
|
|
#define QXK_ISR_CONTEXT_() \
|
|
|
|
(QXK_attr_.intNest != static_cast<uint_fast8_t>(0))
|
|
|
|
#endif // QXK_ISR_CONTEXT_
|
|
|
|
|
2016-09-29 19:54:50 -04:00
|
|
|
// QXK-specific scheduler locking
|
|
|
|
//! Internal macro to represent the scheduler lock status
|
|
|
|
// that needs to be preserved to allow nesting of locks.
|
|
|
|
//
|
2017-08-21 18:21:15 -04:00
|
|
|
#define QF_SCHED_STAT_ QSchedStatus lockStat_;
|
2016-09-29 19:54:50 -04:00
|
|
|
|
|
|
|
//! Internal macro for selective scheduler locking.
|
|
|
|
#define QF_SCHED_LOCK_(prio_) do { \
|
2016-04-01 13:55:34 -04:00
|
|
|
if (QXK_ISR_CONTEXT_()) { \
|
2017-08-21 18:21:15 -04:00
|
|
|
lockStat_ = static_cast<QSchedStatus>(0xFF); \
|
2016-04-01 13:55:34 -04:00
|
|
|
} else { \
|
2017-08-21 18:21:15 -04:00
|
|
|
lockStat_ = QXK::schedLock((prio_)); \
|
2016-04-01 13:55:34 -04:00
|
|
|
} \
|
2016-09-29 19:54:50 -04:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
//! Internal macro for selective scheduler unlocking.
|
|
|
|
#define QF_SCHED_UNLOCK_() do { \
|
2017-08-21 18:21:15 -04:00
|
|
|
if (lockStat_ != static_cast<QSchedStatus>(0xFF)) { \
|
|
|
|
QXK::schedUnlock(lockStat_); \
|
2016-09-29 19:54:50 -04:00
|
|
|
} \
|
|
|
|
} while (false)
|
2016-04-01 13:55:34 -04:00
|
|
|
|
2016-10-08 12:46:03 -04:00
|
|
|
// QXK-specific native event queue operations...
|
2015-12-31 14:56:37 -05:00
|
|
|
#define QACTIVE_EQUEUE_WAIT_(me_) \
|
2016-10-08 12:46:03 -04:00
|
|
|
Q_ASSERT_ID(110, (me_)->m_eQueue.m_frontEvt != static_cast<QEvt *>(0))
|
2015-12-31 14:56:37 -05:00
|
|
|
|
|
|
|
#define QACTIVE_EQUEUE_SIGNAL_(me_) do { \
|
2017-12-12 17:03:36 -05:00
|
|
|
QXK_attr_.readySet.insert(static_cast<uint_fast8_t>((me_)->m_prio)); \
|
2015-12-31 14:56:37 -05:00
|
|
|
if (!QXK_ISR_CONTEXT_()) { \
|
2016-09-29 19:54:50 -04:00
|
|
|
if (QXK_sched_() != static_cast<uint_fast8_t>(0)) { \
|
|
|
|
QXK_activate_(); \
|
|
|
|
} \
|
2015-12-31 14:56:37 -05:00
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2016-10-08 12:46:03 -04:00
|
|
|
// QXK-specific native QF event pool operations...
|
2015-12-31 14:56:37 -05:00
|
|
|
#define QF_EPOOL_TYPE_ QMPool
|
|
|
|
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
|
|
|
|
(p_).init((poolSto_), (poolSize_), (evtSize_))
|
|
|
|
#define QF_EPOOL_EVENT_SIZE_(p_) \
|
|
|
|
static_cast<uint_fast16_t>((p_).getBlockSize())
|
|
|
|
#define QF_EPOOL_GET_(p_, e_, m_) \
|
|
|
|
((e_) = static_cast<QEvt *>((p_).get((m_))))
|
|
|
|
#define QF_EPOOL_PUT_(p_, e_) ((p_).put(e_))
|
|
|
|
|
|
|
|
#endif // QP_IMPL
|
|
|
|
|
|
|
|
#endif // qxk_h
|
2018-03-19 14:51:26 -04:00
|
|
|
|