qpcpp/ports/embos/qf_port.cpp

285 lines
10 KiB
C++
Raw Normal View History

2022-08-11 15:36:19 -04:00
//============================================================================
// QP/C++ Real-Time Embedded Framework (RTEF)
// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
//
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
//
// This software is dual-licensed under the terms of the open source GNU
// General Public License version 3 (or any later version), or alternatively,
// under the terms of one of the closed source Quantum Leaps commercial
// licenses.
//
// The terms of the open source GNU General Public License version 3
// can be found at: <www.gnu.org/licenses/gpl-3.0>
//
// The terms of the closed source Quantum Leaps commercial licenses
// can be found at: <www.state-machine.com/licensing>
//
// Redistributions in source code must retain this top-level comment block.
// Plagiarizing this software to sidestep the license obligations is illegal.
//
// Contact information:
// <www.state-machine.com>
// <info@state-machine.com>
//============================================================================
//! @date Last updated on: 2022-06-30
//! @version Last updated for: @ref qpcpp_7_0_1
//!
//! @file
//! @brief QF/C++ port to embOS RTOS kernel, all supported compilers
2015-05-14 16:05:04 -04:00
2019-10-27 12:26:31 -04:00
#define QP_IMPL // this is QP implementation
#include "qf_port.hpp" // QF port
#include "qf_pkg.hpp"
2014-09-22 11:48:11 -04:00
#include "qassert.h"
2019-10-27 12:26:31 -04:00
#ifdef Q_SPY // QS software tracing enabled?
2020-03-17 21:33:58 -04:00
#include "qs_port.hpp" // QS port
#include "qs_pkg.hpp" // QS package-scope internal interface
2014-09-22 11:48:11 -04:00
#else
2019-10-27 12:26:31 -04:00
#include "qs_dummy.hpp" // disable the QS software tracing
2014-09-22 11:48:11 -04:00
#endif // Q_SPY
2015-05-14 16:05:04 -04:00
// namespace QP ==============================================================
2014-09-22 11:48:11 -04:00
namespace QP {
Q_DEFINE_THIS_MODULE("qf_port")
2015-05-14 16:05:04 -04:00
//............................................................................
// define __TARGET_FPU_VFP symbol depending on the compiler...
#if defined (__CC_ARM) // ARM Compiler
// in ARM Compiler __TARGET_FPU_VFP is a pre-defined symbol
#elif defined (__ICCARM__) // IAR Compiler
#if defined __ARMVFP__
#define __TARGET_FPU_VFP 1
#endif
#elif defined (__GNUC__) // GNU Compiler
#if defined (__VFP_FP__) && !defined(__SOFTFP__)
#define __TARGET_FPU_VFP 1
#endif
#endif
2014-09-22 11:48:11 -04:00
//............................................................................
void QF::init(void) {
OS_InitKern(); // initialize embOS
OS_InitHW(); // initialize the hardware used by embOS
}
//............................................................................
int_t QF::run(void) {
2015-05-14 16:05:04 -04:00
onStartup(); // QF callback to configure and start interrupts
2020-08-24 16:42:48 -04:00
// produce the QS_QF_RUN trace record
QS_CRIT_STAT_
2020-10-01 12:50:17 -04:00
QS_BEGIN_PRE_(QS_QF_RUN, 0U)
2020-08-24 16:42:48 -04:00
QS_END_PRE_()
2015-05-14 16:05:04 -04:00
OS_Start(); // start embOS multitasking
Q_ERROR_ID(100); // OS_Start() should never return
2020-10-01 12:50:17 -04:00
return 0; // dummy return to make the compiler happy
2014-09-22 11:48:11 -04:00
}
//............................................................................
void QF::stop(void) {
onCleanup(); // cleanup callback
}
2015-05-14 16:05:04 -04:00
// thread for active objects -------------------------------------------------
2022-08-11 15:36:19 -04:00
void QActive::thread_(QActive *act) {
2019-02-10 21:01:38 -05:00
// event-loop
for (;;) { // for-ever
2015-05-14 16:05:04 -04:00
QEvt const *e = act->get_(); // wait for event
2020-10-01 12:50:17 -04:00
act->dispatch(e, act->m_prio); // dispatch to the AO's state machine
2022-08-11 15:36:19 -04:00
QF::gc(e); // check if the event is garbage, and collect it if so
2019-02-10 21:01:38 -05:00
}
2022-08-11 15:36:19 -04:00
act->unregister_(); // remove this object from QF
OS_TerminateTask(&act->m_thread);
2014-09-22 11:48:11 -04:00
}
2015-05-14 16:05:04 -04:00
2014-09-22 11:48:11 -04:00
//............................................................................
2015-05-14 16:05:04 -04:00
static void thread_function(void *pVoid) { // embOS signature
2016-12-01 10:31:49 -05:00
QActive *act = reinterpret_cast<QActive *>(pVoid);
2015-05-14 16:05:04 -04:00
#ifdef __TARGET_FPU_VFP
// does the task use the FPU? see NOTE1
2021-04-11 13:25:35 -04:00
if ((act->getOsObject() & TASK_USES_FPU) != 0U) {
2015-05-14 16:05:04 -04:00
OS_ExtendTaskContext_VFP();
}
#endif // __TARGET_FPU_VFP
2022-08-11 15:36:19 -04:00
QActive::thread_(act);
2015-05-14 16:05:04 -04:00
}
//............................................................................
2020-03-17 21:33:58 -04:00
void QActive::start(std::uint_fast8_t const prio,
QEvt const * * const qSto, std::uint_fast16_t const qLen,
void * const stkSto, std::uint_fast16_t const stkSize,
2019-12-31 15:56:23 -05:00
void const * const par)
2014-09-22 11:48:11 -04:00
{
// create the embOS message box for the AO
OS_CreateMB(&m_eQueue,
static_cast<OS_U16>(sizeof(QEvt *)),
static_cast<OS_UINT>(qLen),
static_cast<void *>(&qSto[0]));
2015-05-14 16:05:04 -04:00
m_prio = prio; // save the QF priority
2022-08-11 15:36:19 -04:00
register_(); // make QF aware of this active object
2020-10-01 12:50:17 -04:00
init(par, m_prio); // take the top-most initial tran.
2014-09-22 11:48:11 -04:00
QS_FLUSH(); // flush the trace buffer to the host
2015-05-14 16:05:04 -04:00
// create an embOS task for the AO
2014-09-22 11:48:11 -04:00
OS_CreateTaskEx(&m_thread,
2021-04-11 13:25:35 -04:00
#if (OS_TRACKNAME != 0)
m_thread.Name, // the configured task name
#elif
"AO", // a generic AO task name
#endif
2015-05-14 16:05:04 -04:00
static_cast<OS_PRIO>(prio), // embOS uses same numbering as QP
&thread_function,
static_cast<void OS_STACKPTR *>(stkSto),
static_cast<OS_UINT>(stkSize),
2020-03-17 21:33:58 -04:00
0U, // no AOs at the same prio
2015-05-14 16:05:04 -04:00
this);
2014-09-22 11:48:11 -04:00
}
//............................................................................
2021-04-11 13:25:35 -04:00
void QActive::setAttr(std::uint32_t attr1, void const *attr2) {
switch (attr1) {
case TASK_NAME_ATTR:
#if (OS_TRACKNAME != 0)
Q_ASSERT_ID(300, m_thread.Name == nullptr);
m_thread.Name = static_cast<char const *>(attr2);
2021-04-11 13:25:35 -04:00
#endif
break;
case TASK_USES_FPU:
m_osObject = attr1;
break;
/* ... */
default:
break;
}
2018-01-10 18:26:05 -05:00
}
//............................................................................
2020-03-17 21:33:58 -04:00
bool QActive::post_(QEvt const * const e, std::uint_fast16_t const margin,
2020-10-17 10:16:26 -04:00
void const * const sender) noexcept
2014-09-22 11:48:11 -04:00
{
2020-03-17 21:33:58 -04:00
std::uint_fast16_t nFree;
2014-09-22 11:48:11 -04:00
bool status;
QF_CRIT_STAT_
2020-10-01 12:50:17 -04:00
QF_CRIT_E_();
2020-03-17 21:33:58 -04:00
nFree = static_cast<std::uint_fast16_t>(m_eQueue.maxMsg - m_eQueue.nofMsg);
2014-09-22 11:48:11 -04:00
2017-09-24 21:02:45 -04:00
if (margin == QF_NO_MARGIN) {
2020-03-17 21:33:58 -04:00
if (nFree > 0U) {
2017-09-24 21:02:45 -04:00
status = true; // can post
}
else {
status = false; // cannot post
Q_ERROR_ID(510); // must be able to post the event
}
}
else if (nFree > static_cast<QEQueueCtr>(margin)) {
status = true; // can post
}
else {
status = false; // cannot post
}
if (status) { // can post the event?
2020-10-01 12:50:17 -04:00
QS_BEGIN_NOCRIT_PRE_(QS_QF_ACTIVE_POST, m_prio)
2020-03-17 21:33:58 -04:00
QS_TIME_PRE_(); // timestamp
QS_OBJ_PRE_(sender); // the sender object
QS_SIG_PRE_(e->sig); // the signal of the event
QS_OBJ_PRE_(this); // this active object (recipient)
2019-12-31 15:56:23 -05:00
QS_2U8_PRE_(e->poolId_, e->refCtr_); // pool Id & ref Count
2020-03-17 21:33:58 -04:00
QS_EQC_PRE_(nFree); // # free entries
QS_EQC_PRE_(0U); // min # free (unknown)
2019-12-31 15:56:23 -05:00
QS_END_NOCRIT_PRE_()
2014-09-22 11:48:11 -04:00
2020-03-17 21:33:58 -04:00
if (e->poolId_ != 0U) { // is it a pool event?
2014-09-22 11:48:11 -04:00
QF_EVT_REF_CTR_INC_(e); // increment the reference counter
}
2015-05-14 16:05:04 -04:00
2020-10-01 12:50:17 -04:00
QF_CRIT_X_();
2016-04-01 13:55:34 -04:00
2015-05-14 16:05:04 -04:00
// posting to the embOS mailbox must succeed, see NOTE3
2017-09-24 21:02:45 -04:00
Q_ALLEGE_ID(520,
2015-05-14 16:05:04 -04:00
OS_PutMailCond(&m_eQueue, static_cast<OS_CONST_PTR void *>(&e))
2020-03-17 21:33:58 -04:00
== 0);
2014-09-22 11:48:11 -04:00
}
else {
2015-05-14 16:05:04 -04:00
2020-10-01 12:50:17 -04:00
QS_BEGIN_NOCRIT_PRE_(QS_QF_ACTIVE_POST_ATTEMPT, m_prio)
2019-12-31 15:56:23 -05:00
QS_TIME_PRE_(); // timestamp
QS_OBJ_PRE_(sender); // the sender object
QS_SIG_PRE_(e->sig); // the signal of the event
QS_OBJ_PRE_(this); // this active object (recipient)
QS_2U8_PRE_(e->poolId_, e->refCtr_); // pool Id & ref Count
2020-03-17 21:33:58 -04:00
QS_EQC_PRE_(nFree); // # free entries
QS_EQC_PRE_(0U); // min # free (unknown)
2019-12-31 15:56:23 -05:00
QS_END_NOCRIT_PRE_()
2014-09-22 11:48:11 -04:00
2020-10-01 12:50:17 -04:00
QF_CRIT_X_();
2014-09-22 11:48:11 -04:00
}
return status;
}
//............................................................................
2020-10-17 10:16:26 -04:00
void QActive::postLIFO(QEvt const * const e) noexcept {
2014-09-22 11:48:11 -04:00
QF_CRIT_STAT_
2020-10-01 12:50:17 -04:00
QF_CRIT_E_();
2014-09-22 11:48:11 -04:00
2020-10-01 12:50:17 -04:00
QS_BEGIN_NOCRIT_PRE_(QS_QF_ACTIVE_POST_LIFO, m_prio)
2019-12-31 15:56:23 -05:00
QS_TIME_PRE_(); // timestamp
QS_SIG_PRE_(e->sig); // the signal of this event
QS_OBJ_PRE_(this); // this active object
QS_2U8_PRE_(e->poolId_, e->refCtr_); // pool Id & ref Count
2020-03-17 21:33:58 -04:00
QS_EQC_PRE_(m_eQueue.maxMsg - m_eQueue.nofMsg); // # free entries
QS_EQC_PRE_(0U); // min # free entries (unknown)
2019-12-31 15:56:23 -05:00
QS_END_NOCRIT_PRE_()
2014-09-22 11:48:11 -04:00
2020-03-17 21:33:58 -04:00
if (e->poolId_ != 0U) { // is it a pool event?
2014-09-22 11:48:11 -04:00
QF_EVT_REF_CTR_INC_(e); // increment the reference counter
}
2020-10-01 12:50:17 -04:00
QF_CRIT_X_();
2016-04-01 13:55:34 -04:00
2015-05-14 16:05:04 -04:00
// posting to the embOS mailbox must succeed, see NOTE3
Q_ALLEGE_ID(810,
OS_PutMailFrontCond(&m_eQueue, static_cast<OS_CONST_PTR void *>(&e))
== static_cast<char>(0));
2014-09-22 11:48:11 -04:00
}
//............................................................................
2020-10-17 10:16:26 -04:00
QEvt const *QActive::get_(void) noexcept {
2014-09-22 11:48:11 -04:00
QEvt const *e;
QS_CRIT_STAT_
2015-05-14 16:05:04 -04:00
OS_GetMail(&m_eQueue, &e);
2014-09-22 11:48:11 -04:00
2020-10-01 12:50:17 -04:00
QS_BEGIN_PRE_(QS_QF_ACTIVE_GET, m_prio)
2019-12-31 15:56:23 -05:00
QS_TIME_PRE_(); // timestamp
QS_SIG_PRE_(e->sig); // the signal of this event
QS_OBJ_PRE_(this); // this active object
QS_2U8_PRE_(e->poolId_, e->refCtr_); // pool Id & ref Count
2020-03-17 21:33:58 -04:00
QS_EQC_PRE_(m_eQueue.maxMsg - m_eQueue.nofMsg); // # free entries
2019-12-31 15:56:23 -05:00
QS_END_PRE_()
2014-09-22 11:48:11 -04:00
return e;
}
} // namespace QP
//============================================================================
2015-05-14 16:05:04 -04:00
// NOTE1:
// In case of hardware-supported floating point unit (FPU), a task must
// preserve the FPU registers accross the context switch. However, this
// additional overhead is necessary only for tasks that actually use the
// FPU. In this QP-embOS port, an active object task that uses the FPU is
// designated by the QF_TASK_USES_FPU attribute, which can be set wiht the
// QF_setEmbOsTaskAttr() function. The task attributes must be set *before*
// calling QACTIVE_START(). The task attributes are saved in
2016-12-01 10:31:49 -05:00
// QActive.m_osObject member.
2015-05-14 16:05:04 -04:00
//
// NOTE3:
// The event posting to embOS mailbox occurs inside a critical section,
// but this is OK, because the QF/embOS critical sections are designed
// to nest.
2019-12-31 15:56:23 -05:00
//