qpcpp/include/qk.h

209 lines
7.3 KiB
C
Raw Normal View History

2015-05-14 16:05:04 -04:00
/// @file
/// @brief QK/C++ platform-independent public interface.
/// @ingroup qk
/// @cond
2014-04-13 21:35:34 -04:00
///***************************************************************************
2016-05-05 12:22:15 -04:00
/// Last updated for version 5.6.4
/// Last updated on 2016-05-04
2014-04-13 21:35:34 -04:00
///
/// Q u a n t u m L e a P s
/// ---------------------------
/// innovating embedded systems
///
2016-04-01 13:55:34 -04:00
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
2014-04-13 21:35:34 -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
/// (at your option) any later version.
2012-08-14 18:00:48 -04:00
///
2014-04-13 21:35:34 -04:00
/// 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-09-29 11:34:38 -04:00
/// http://www.state-machine.com
/// mailto:info@state-machine.com
2014-04-13 21:35:34 -04:00
///***************************************************************************
2015-05-14 16:05:04 -04:00
/// @endcond
2014-04-13 21:35:34 -04:00
#ifndef qk_h
#define qk_h
2012-08-14 18:00:48 -04:00
2015-05-14 16:05:04 -04:00
#include "qequeue.h" // QK kernel uses the native QF event queue
#include "qmpool.h" // QK kernel uses the native QF memory pool
#include "qpset.h" // QK kernel uses the native QF priority set
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
// QF configuration for QK
2014-04-13 21:35:34 -04:00
//! This macro defines the type of the event queue used for active objects.
2015-05-14 16:05:04 -04:00
/// @note
2014-04-13 21:35:34 -04:00
/// This is just an example of the macro definition. Typically, you need
2012-08-14 18:00:48 -04:00
/// to define it in the specific QF port file (qf_port.h). In case of QK,
/// which always depends on the native QF queue, this macro is defined at the
/// level of the platform-independent interface qk.h.
2015-12-31 14:56:37 -05:00
#define QF_EQUEUE_TYPE QEQueue
//! OS-dependent per-thread operating-system object
/// @description
/// The use of this member depends on the CPU. For example, in port to
/// ARM Cortex-M with FPU this member is used to store the LR.
#define QF_OS_OBJECT_TYPE void*
//! OS-dependent representation of the private thread */
/// @description
/// QK uses this member to store the start priority of the AO,
/// which is needed when the QK priority-ceiling mutex is used.
#define QF_THREAD_TYPE uint_fast8_t
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
namespace QP {
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
2014-04-13 21:35:34 -04:00
//! QK services.
2015-05-14 16:05:04 -04:00
/// @description
2012-08-14 18:00:48 -04:00
/// This class groups together QK services. It has only static members and
/// should not be instantiated.
///
2015-05-14 16:05:04 -04:00
// @note The QK scheduler, QK priority, QK ready set, etc. belong conceptually
2012-08-14 18:00:48 -04:00
/// to the QK class (as static class members). However, to avoid C++ potential
/// name-mangling problems in assembly language, these elements are defined
/// outside of the QK class and use the extern "C" linkage specification.
class QK {
public:
2015-05-14 16:05:04 -04:00
//! get the current QK version number string of the form X.Y.Z
2015-12-31 14:56:37 -05:00
static char_t const *getVersion(void) {
2015-05-14 16:05:04 -04:00
return versionStr;
2013-10-10 20:01:51 -04:00
}
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
//! QK idle callback (customized in BSPs for QK)
2015-05-14 16:05:04 -04:00
/// @description
2014-04-13 21:35:34 -04:00
/// QP::QK::onIdle() is called continously by the QK idle loop. This
/// callback gives the application an opportunity to enter a power-saving
/// CPU mode, or perform some other idle processing.
2012-08-14 18:00:48 -04:00
///
2015-05-14 16:05:04 -04:00
/// @note QP::QK::onIdle() is invoked with interrupts enabled and must
2014-04-13 21:35:34 -04:00
/// also return with interrupts enabled.
2012-08-14 18:00:48 -04:00
///
2015-05-14 16:05:04 -04:00
/// @sa QP::QF::onIdle()
2012-08-14 18:00:48 -04:00
static void onIdle(void);
2015-12-31 14:56:37 -05:00
};
2012-08-14 18:00:48 -04:00
2016-04-01 13:55:34 -04:00
/*! Priority-ceiling Mutex the QK preemptive kernel */
2016-05-05 12:22:15 -04:00
class QKMutex {
2015-12-31 14:56:37 -05:00
public:
2016-04-01 13:55:34 -04:00
void init(uint_fast8_t const prio);
2015-12-31 14:56:37 -05:00
void lock(void);
void unlock(void);
2012-08-14 18:00:48 -04:00
2015-12-31 14:56:37 -05:00
private:
2016-04-01 13:55:34 -04:00
uint_fast8_t m_lockPrio; //!< lock prio (priority ceiling)
uint_fast8_t m_prevPrio; //!< previoius lock prio
friend class QF;
2012-08-14 18:00:48 -04:00
};
2014-04-13 21:35:34 -04:00
} // namespace QP
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
2012-08-14 18:00:48 -04:00
extern "C" {
2014-04-13 21:35:34 -04:00
//! QK initialization
2012-08-14 18:00:48 -04:00
void QK_init(void);
2014-04-13 21:35:34 -04:00
//! The QK scheduler
void QK_sched_(uint_fast8_t p);
2012-08-14 18:00:48 -04:00
2014-04-13 21:35:34 -04:00
//! Find the highest-priority task ready to run
uint_fast8_t QK_schedPrio_(void);
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
#if (QF_MAX_ACTIVE <= 8)
2014-04-13 21:35:34 -04:00
extern QP::QPSet8 QK_readySet_; //!< ready set of AOs
2013-10-10 20:01:51 -04:00
#else
2014-04-13 21:35:34 -04:00
extern QP::QPSet64 QK_readySet_; //!< ready set of AOs
2013-10-10 20:01:51 -04:00
#endif
2014-04-13 21:35:34 -04:00
extern uint_fast8_t volatile QK_currPrio_; //!< current task/ISR priority
2016-04-01 13:55:34 -04:00
extern uint_fast8_t volatile QK_lockPrio_; //!< lock prio (0 == no-lock)
2015-09-29 11:34:38 -04:00
#ifndef QK_ISR_CONTEXT_
extern uint_fast8_t volatile QK_intNest_; //!< interrupt nesting level
#endif // QK_ISR_CONTEXT_
2013-10-10 20:01:51 -04:00
2014-04-13 21:35:34 -04:00
} // extern "C"
2012-08-14 18:00:48 -04:00
2013-10-10 20:01:51 -04:00
//****************************************************************************
// interface used only inside QF, but not in applications
2014-04-13 21:35:34 -04:00
#ifdef QP_IMPL
2015-05-14 16:05:04 -04:00
#ifndef QK_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
2015-09-29 11:34:38 -04:00
#define QK_ISR_CONTEXT_() \
(QK_intNest_ != static_cast<uint_fast8_t>(0))
2015-05-14 16:05:04 -04:00
#endif // QK_ISR_CONTEXT_
2016-04-01 13:55:34 -04:00
// QF-specific scheduler locking
//! Internal port-specific macro to represent the scheduler lock status
// that needs to be preserved to allow nesting of locks.
2016-05-05 12:22:15 -04:00
#define QF_SCHED_STAT_TYPE_ QKMutex
2016-04-01 13:55:34 -04:00
//! Internal port-specific macro for selective scheduler locking.
#define QF_SCHED_LOCK_(pLockStat_, prio_) do { \
if (QK_ISR_CONTEXT_()) { \
(pLockStat_)->m_lockPrio = \
static_cast<uint_fast8_t>(QF_MAX_ACTIVE + 1); \
} else { \
(pLockStat_)->init((prio_)); \
(pLockStat_)->lock(); \
} \
} while (false)
//! Internal port-specific macro for selective scheduler unlocking.
#define QF_SCHED_UNLOCK_(pLockStat_) (pLockStat_)->unlock()
2015-05-14 16:05:04 -04:00
// native event queue operations...
2013-10-10 20:01:51 -04:00
#define QACTIVE_EQUEUE_WAIT_(me_) \
2015-09-29 11:34:38 -04:00
Q_ASSERT_ID(0, (me_)->m_eQueue.m_frontEvt != static_cast<QEvt *>(0))
2015-05-14 16:05:04 -04:00
2013-10-10 20:01:51 -04:00
#define QACTIVE_EQUEUE_SIGNAL_(me_) do { \
QK_readySet_.insert((me_)->m_prio); \
2015-05-14 16:05:04 -04:00
if (!QK_ISR_CONTEXT_()) { \
2014-04-13 21:35:34 -04:00
uint_fast8_t p = QK_schedPrio_(); \
if (p != static_cast<uint_fast8_t>(0)) { \
2013-10-10 20:01:51 -04:00
QK_sched_(p); \
} \
} \
} while (false)
#define QACTIVE_EQUEUE_ONEMPTY_(me_) \
QK_readySet_.remove((me_)->m_prio)
2015-05-14 16:05:04 -04:00
// native QF event pool operations...
2013-10-10 20:01:51 -04:00
#define QF_EPOOL_TYPE_ QMPool
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
2013-12-30 17:41:15 -05:00
(p_).init((poolSto_), (poolSize_), (evtSize_))
#define QF_EPOOL_EVENT_SIZE_(p_) \
2014-04-13 21:35:34 -04:00
static_cast<uint_fast16_t>((p_).getBlockSize())
2013-10-10 20:01:51 -04:00
#define QF_EPOOL_GET_(p_, e_, m_) \
((e_) = static_cast<QEvt *>((p_).get((m_))))
2013-12-30 17:41:15 -05:00
#define QF_EPOOL_PUT_(p_, e_) ((p_).put(e_))
2013-10-10 20:01:51 -04:00
2015-05-14 16:05:04 -04:00
#endif // QP_IMPL
2013-10-10 20:01:51 -04:00
2015-05-14 16:05:04 -04:00
#endif // qk_h