2016-04-01 13:55:34 -04:00
|
|
|
/// @file
|
|
|
|
/// @brief QF/C++ port to ThreadX, all supported compilers
|
|
|
|
/// @cond
|
|
|
|
////**************************************************************************
|
|
|
|
/// Last updated for version 5.6.2
|
|
|
|
/// Last updated on 2016-03-31
|
|
|
|
///
|
|
|
|
/// Q u a n t u m L e a P s
|
|
|
|
/// ---------------------------
|
|
|
|
/// innovating embedded systems
|
|
|
|
///
|
|
|
|
/// Copyright (C) Quantum Leaps, LLC. All rights reserved.
|
|
|
|
///
|
|
|
|
/// 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:
|
|
|
|
/// http://www.state-machine.com
|
|
|
|
/// mailto:info@state-machine.com
|
|
|
|
////**************************************************************************
|
|
|
|
/// @endcond
|
|
|
|
|
2014-05-10 12:39:28 -04:00
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qf_port.h" // QF port
|
|
|
|
#include "qf_pkg.h"
|
|
|
|
#ifdef Q_SPY // QS software tracing enabled?
|
|
|
|
#include "qs_port.h" // include QS port
|
|
|
|
#else
|
|
|
|
#include "qs_dummy.h" // disable the QS software tracing
|
|
|
|
#endif // Q_SPY
|
|
|
|
#include "qassert.h"
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
// namespace QP ==============================================================
|
2014-05-10 12:39:28 -04:00
|
|
|
namespace QP {
|
|
|
|
|
|
|
|
Q_DEFINE_THIS_MODULE("qf_port")
|
|
|
|
|
|
|
|
// Local objects -------------------------------------------------------------
|
2015-05-14 16:05:04 -04:00
|
|
|
static void thread_function(ULONG thread_input); // prototype
|
2014-05-10 12:39:28 -04:00
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
void QF::init(void) {
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
int_t QF::run(void) {
|
|
|
|
onStartup(); // the startup callback
|
|
|
|
return static_cast<int_t>(0); // return success
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QF::stop(void) {
|
|
|
|
onCleanup(); // the cleanup callback
|
|
|
|
}
|
|
|
|
//............................................................................
|
2015-05-14 16:05:04 -04:00
|
|
|
void QF::thread_(QMActive *act) {
|
2014-05-10 12:39:28 -04:00
|
|
|
// event loop of the active object thread
|
|
|
|
act->m_osObject = true; // enable the thread event-loop
|
|
|
|
while (act->m_osObject) {
|
|
|
|
QEvt const *e = act->get_(); // wait for event
|
|
|
|
act->dispatch(e); // dispatch to the active object's state machine
|
|
|
|
gc(e); // check if the event is garbage, and collect it if so
|
|
|
|
}
|
2016-04-01 13:55:34 -04:00
|
|
|
act->unsubscribeAll(); // unsubscribe from all events
|
|
|
|
QF::remove_(act); // remove this active object from QF
|
|
|
|
|
|
|
|
// cleanup of the queue and thread must succeed
|
|
|
|
Q_ALLEGE_ID(110, tx_queue_delete(&act->m_eQueue) == TX_SUCCESS);
|
|
|
|
Q_ALLEGE_ID(120, tx_thread_delete(&act->getThread()) == TX_SUCCESS);
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
static void thread_function(ULONG thread_input) { // ThreadX signature
|
|
|
|
// run the active-object thread
|
|
|
|
QF::thread_(reinterpret_cast<QMActive *>(thread_input));
|
2014-05-10 12:39:28 -04:00
|
|
|
}
|
|
|
|
//............................................................................
|
2015-05-14 16:05:04 -04:00
|
|
|
void QMActive::start(uint_fast8_t prio,
|
|
|
|
QEvt const *qSto[], uint_fast16_t qLen,
|
|
|
|
void *stkSto, uint_fast16_t stkSize,
|
|
|
|
QEvt const *ie)
|
2014-05-10 12:39:28 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
// allege that the ThreadX queue is created successfully
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ALLEGE_ID(210,
|
|
|
|
tx_queue_create(&m_eQueue,
|
|
|
|
"Q",
|
|
|
|
TX_1_ULONG,
|
|
|
|
static_cast<VOID *>(qSto),
|
|
|
|
static_cast<ULONG>(qLen * sizeof(ULONG)))
|
|
|
|
== TX_SUCCESS);
|
2014-05-10 12:39:28 -04:00
|
|
|
|
|
|
|
m_prio = prio; // save the QF priority
|
|
|
|
QF::add_(this); // make QF aware of this active object
|
|
|
|
init(ie); // execute initial transition
|
|
|
|
QS_FLUSH(); // flush the trace buffer to the host
|
|
|
|
|
|
|
|
// convert QF priority to the ThreadX priority
|
|
|
|
UINT tx_prio = QF_TX_PRIO_OFFSET + QF_MAX_ACTIVE - prio;
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ALLEGE_ID(220,
|
|
|
|
tx_thread_create(
|
2016-04-01 13:55:34 -04:00
|
|
|
&m_thread, // ThreadX thread control block
|
2015-05-14 16:05:04 -04:00
|
|
|
"AO", // thread name
|
2016-04-01 13:55:34 -04:00
|
|
|
&thread_function, // thread function
|
|
|
|
reinterpret_cast<ULONG>(this), // thread argument
|
2015-05-14 16:05:04 -04:00
|
|
|
stkSto, // stack start
|
|
|
|
stkSize, // stack size in bytes
|
|
|
|
tx_prio, // ThreadX priority
|
|
|
|
tx_prio, // preemption threshold disabled (same as priority)
|
|
|
|
TX_NO_TIME_SLICE,
|
|
|
|
TX_AUTO_START)
|
|
|
|
== TX_SUCCESS);
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QMActive::stop(void) {
|
2014-05-10 12:39:28 -04:00
|
|
|
m_osObject = false; // stop the thread loop
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
#ifndef Q_SPY
|
2015-05-14 16:05:04 -04:00
|
|
|
bool QMActive::post_(QEvt const * const e, uint_fast16_t const margin)
|
2014-05-10 12:39:28 -04:00
|
|
|
#else
|
2015-05-14 16:05:04 -04:00
|
|
|
bool QMActive::post_(QEvt const * const e, uint_fast16_t const margin,
|
|
|
|
void const * const sender)
|
2014-05-10 12:39:28 -04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
bool status;
|
|
|
|
QF_CRIT_STAT_
|
|
|
|
QF_CRIT_ENTRY_();
|
|
|
|
|
|
|
|
uint_fast16_t nFree =
|
|
|
|
static_cast<uint_fast16_t>(m_eQueue.tx_queue_available_storage);
|
|
|
|
|
|
|
|
if (nFree > margin) {
|
|
|
|
QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_FIFO, QS::priv_.aoObjFilter, this)
|
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_OBJ_(sender); // the sender object
|
|
|
|
QS_SIG_(e->sig); // the signal of the event
|
|
|
|
QS_OBJ_(this); // this active object (recipient)
|
|
|
|
QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt
|
2015-05-14 16:05:04 -04:00
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(nFree)); // # free entries
|
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(0)); // min # free (unknown)
|
2014-05-10 12:39:28 -04:00
|
|
|
QS_END_NOCRIT_()
|
|
|
|
|
|
|
|
// is it a pool event?
|
|
|
|
if (QF_EVT_POOL_ID_(e) != static_cast<uint8_t>(0)) {
|
|
|
|
QF_EVT_REF_CTR_INC_(e); // increment the reference counter
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:55:34 -04:00
|
|
|
QF_CRIT_EXIT_();
|
|
|
|
|
2014-05-10 12:39:28 -04:00
|
|
|
QEvt const *ep = const_cast<QEvt const *>(e);
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ALLEGE_ID(510,
|
|
|
|
tx_queue_send(&m_eQueue, static_cast<VOID *>(&ep), TX_NO_WAIT)
|
|
|
|
== TX_SUCCESS);
|
2016-04-01 13:55:34 -04:00
|
|
|
|
|
|
|
status = true; // report success
|
2014-05-10 12:39:28 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// can tolerate dropping evts?
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ASSERT_ID(520,
|
|
|
|
margin != static_cast<uint_fast16_t>(0));
|
2014-05-10 12:39:28 -04:00
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_ATTEMPT,
|
|
|
|
QS::priv_.aoObjFilter, this)
|
2014-05-10 12:39:28 -04:00
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_OBJ_(sender); // the sender object
|
|
|
|
QS_SIG_(e->sig); // the signal of the event
|
|
|
|
QS_OBJ_(this); // this active object (recipient)
|
|
|
|
QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt
|
2015-05-14 16:05:04 -04:00
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(nFree)); // # free entries
|
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(0)); // min # free (unknown)
|
2014-05-10 12:39:28 -04:00
|
|
|
QS_END_NOCRIT_()
|
|
|
|
|
2016-04-01 13:55:34 -04:00
|
|
|
QF_CRIT_EXIT_();
|
|
|
|
|
|
|
|
status = false; // report failure
|
2014-05-10 12:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
//............................................................................
|
2015-05-14 16:05:04 -04:00
|
|
|
void QMActive::postLIFO(QEvt const * const e) {
|
2014-05-10 12:39:28 -04:00
|
|
|
QF_CRIT_STAT_
|
|
|
|
QF_CRIT_ENTRY_();
|
|
|
|
|
|
|
|
QS_BEGIN_NOCRIT_(QS_QF_ACTIVE_POST_LIFO, QS::priv_.aoObjFilter, this)
|
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_SIG_(e->sig); // the signal of this event
|
|
|
|
QS_OBJ_(this); // this active object
|
|
|
|
QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt
|
|
|
|
// # free entries available
|
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(m_eQueue.tx_queue_available_storage));
|
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(0)); // min # free entries (unknown)
|
|
|
|
QS_END_NOCRIT_()
|
|
|
|
|
|
|
|
// is it a pool event?
|
|
|
|
if (QF_EVT_POOL_ID_(e) != static_cast<uint8_t>(0)) {
|
|
|
|
QF_EVT_REF_CTR_INC_(e); // increment the reference counter
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:55:34 -04:00
|
|
|
QF_CRIT_EXIT_();
|
|
|
|
|
2014-05-10 12:39:28 -04:00
|
|
|
// LIFO posting must succeed, see NOTE1
|
|
|
|
QEvt const *ep = const_cast<QEvt const *>(e);
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ALLEGE_ID(610,
|
|
|
|
tx_queue_front_send(&m_eQueue, static_cast<VOID *>(&ep), TX_NO_WAIT)
|
|
|
|
== TX_SUCCESS);
|
2014-05-10 12:39:28 -04:00
|
|
|
}
|
|
|
|
//............................................................................
|
2015-05-14 16:05:04 -04:00
|
|
|
QEvt const *QMActive::get_(void) {
|
2014-05-10 12:39:28 -04:00
|
|
|
QEvt const *e;
|
|
|
|
QS_CRIT_STAT_
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
Q_ALLEGE_ID(710,
|
|
|
|
tx_queue_receive(&m_eQueue, (VOID *)&e, TX_WAIT_FOREVER)
|
|
|
|
== TX_SUCCESS);
|
|
|
|
|
2014-05-10 12:39:28 -04:00
|
|
|
QS_BEGIN_(QS_QF_ACTIVE_GET, QS::priv_.aoObjFilter, this)
|
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_SIG_(e->sig); // the signal of this event
|
|
|
|
QS_OBJ_(this); // this active object
|
|
|
|
QS_2U8_(e->poolId_, e->refCtr_); // pool Id & refCtr of the evt
|
|
|
|
QS_EQC_(static_cast<QEQueueCtr>(0)); // min # free entries (unknown)
|
|
|
|
QS_END_()
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:55:34 -04:00
|
|
|
//............................................................................
|
|
|
|
void QFSchedLock::lock(uint_fast8_t prio) {
|
|
|
|
QS_CRIT_STAT_
|
|
|
|
m_lockHolder = tx_thread_identify();
|
|
|
|
|
|
|
|
/// @pre must be thread level, so current TX thread must be available
|
|
|
|
Q_REQUIRE_ID(700, m_lockHolder != static_cast<TX_THREAD *>(0));
|
|
|
|
|
|
|
|
// change the preemption threshold of the current thread
|
|
|
|
Q_ALLEGE_ID(710, tx_thread_preemption_change(m_lockHolder,
|
|
|
|
(QF_TX_PRIO_OFFSET + QF_MAX_ACTIVE - prio),
|
|
|
|
&m_prevThre) == TX_SUCCESS);
|
|
|
|
|
|
|
|
m_lockPrio = prio;
|
|
|
|
QS_BEGIN_(QS_SCHED_LOCK, static_cast<void *>(0), static_cast<void *>(0))
|
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_2U8_(static_cast<uint8_t>(QF_TX_PRIO_OFFSET + QF_MAX_ACTIVE
|
|
|
|
- m_prevThre),
|
|
|
|
static_cast<uint8_t>(m_lockPrio)); // new lock prio
|
|
|
|
QS_END_()
|
|
|
|
}
|
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
void QFSchedLock::unlock(void) const {
|
|
|
|
QS_CRIT_STAT_
|
|
|
|
|
|
|
|
/// @pre the lock holder TX thread must be available */
|
|
|
|
Q_REQUIRE_ID(800, m_lockHolder != static_cast<TX_THREAD *>(0));
|
|
|
|
|
|
|
|
QS_BEGIN_(QS_SCHED_UNLOCK,
|
|
|
|
static_cast<void *>(0), static_cast<void *>(0))
|
|
|
|
QS_TIME_(); // timestamp
|
|
|
|
QS_2U8_(static_cast<uint8_t>(m_lockPrio), /* prev lock prio */
|
|
|
|
static_cast<uint8_t>(QF_TX_PRIO_OFFSET + QF_MAX_ACTIVE
|
|
|
|
- m_prevThre)); // new lock prio
|
|
|
|
QS_END_()
|
|
|
|
|
|
|
|
// restore the preemption threshold of the lock holder
|
|
|
|
UINT old_thre;
|
|
|
|
Q_ALLEGE_ID(810, tx_thread_preemption_change(m_lockHolder,
|
|
|
|
m_prevThre,
|
|
|
|
&old_thre) == TX_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-05-10 12:39:28 -04:00
|
|
|
} // namespace QP
|