2023-01-19 21:13:24 -05:00
|
|
|
//============================================================================
|
2025-01-18 18:33:14 -05:00
|
|
|
// QP/C Real-Time Embedded Framework (RTEF)
|
|
|
|
// Version 8.0.2
|
|
|
|
//
|
2023-01-19 21:13:24 -05:00
|
|
|
// Copyright (C) 2005 Quantum Leaps, LLC. All rights reserved.
|
|
|
|
//
|
2024-10-15 13:15:11 -04:00
|
|
|
// Q u a n t u m L e a P s
|
|
|
|
// ------------------------
|
|
|
|
// Modern Embedded Software
|
2023-01-19 21:13:24 -05:00
|
|
|
//
|
2024-10-15 13:15:11 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-QL-commercial
|
2023-01-19 21:13:24 -05:00
|
|
|
//
|
2025-01-18 18:33:14 -05:00
|
|
|
// This software is dual-licensed under the terms of the open-source GNU
|
2024-10-15 13:15:11 -04:00
|
|
|
// General Public License (GPL) or under the terms of one of the closed-
|
|
|
|
// source Quantum Leaps commercial licenses.
|
2023-01-19 21:13:24 -05:00
|
|
|
//
|
|
|
|
// Redistributions in source code must retain this top-level comment block.
|
|
|
|
// Plagiarizing this software to sidestep the license obligations is illegal.
|
|
|
|
//
|
2024-10-15 13:15:11 -04:00
|
|
|
// NOTE:
|
2025-01-18 18:33:14 -05:00
|
|
|
// The GPL does NOT permit the incorporation of this code into proprietary
|
|
|
|
// programs. Please contact Quantum Leaps for commercial licensing options,
|
|
|
|
// which expressly supersede the GPL and are designed explicitly for
|
|
|
|
// closed-source distribution.
|
2024-10-15 13:15:11 -04:00
|
|
|
//
|
|
|
|
// Quantum Leaps contact information:
|
|
|
|
// <www.state-machine.com/licensing>
|
2023-01-19 21:13:24 -05:00
|
|
|
// <info@state-machine.com>
|
|
|
|
//============================================================================
|
|
|
|
#define QP_IMPL // this is QP implementation
|
|
|
|
#include "qp_port.h" // QP port
|
|
|
|
#include "qp_pkg.h" // QP package-scope interface
|
|
|
|
#include "qsafe.h" // QP Functional Safety (FuSa) Subsystem
|
|
|
|
#ifdef Q_SPY // QS software tracing enabled?
|
|
|
|
#include "qs_port.h" // QS port
|
|
|
|
#include "qs_pkg.h" // QS package-scope internal interface
|
2022-04-30 18:29:48 -04:00
|
|
|
#else
|
2023-01-19 21:13:24 -05:00
|
|
|
#include "qs_dummy.h" // disable the QS software tracing
|
|
|
|
#endif // Q_SPY
|
2022-04-30 18:29:48 -04:00
|
|
|
|
|
|
|
Q_DEFINE_THIS_MODULE("qf_port")
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
struct k_spinlock QF_spinlock;
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
void QF_init(void) {
|
|
|
|
QF_spinlock = (struct k_spinlock){};
|
2024-10-15 13:15:11 -04:00
|
|
|
|
|
|
|
QF_bzero_(&QF_priv_, sizeof(QF_priv_));
|
|
|
|
QF_bzero_(&QActive_registry_[0], sizeof(QActive_registry_));
|
|
|
|
QTimeEvt_init(); // initialize QTimeEvts
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
int_t QF_run(void) {
|
|
|
|
QF_onStartup();
|
2023-01-19 21:13:24 -05:00
|
|
|
|
2022-04-30 18:29:48 -04:00
|
|
|
#ifdef Q_SPY
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
#if (CONFIG_NUM_PREEMPT_PRIORITIES > 0)
|
|
|
|
// lower the priority of the main thread to the level of idle thread
|
2022-04-30 18:29:48 -04:00
|
|
|
k_thread_priority_set(k_current_get(),
|
|
|
|
CONFIG_NUM_PREEMPT_PRIORITIES - 1);
|
|
|
|
#endif
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// produce the QS_QF_RUN trace record
|
|
|
|
QS_CRIT_STAT
|
|
|
|
QS_CRIT_ENTRY();
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_BEGIN_PRE(QS_QF_RUN, 0U)
|
|
|
|
QS_END_PRE()
|
2023-01-19 21:13:24 -05:00
|
|
|
QS_CRIT_EXIT();
|
|
|
|
|
|
|
|
// perform QS work...
|
2022-04-30 18:29:48 -04:00
|
|
|
while (true) {
|
2023-01-19 21:13:24 -05:00
|
|
|
QS_rxParse(); // parse any QS-RX bytes
|
|
|
|
QS_doOutput(); // perform the QS-TX output
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
#else
|
2023-01-19 21:13:24 -05:00
|
|
|
return 0; // return from the main Zephyr thread
|
2022-04-30 18:29:48 -04:00
|
|
|
#endif
|
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
void QF_stop(void) {
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_onCleanup(); // cleanup callback
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
static void thread_entry(void *p1, void *p2, void *p3) {
|
2023-01-19 21:13:24 -05:00
|
|
|
Q_UNUSED_PAR(p2);
|
|
|
|
Q_UNUSED_PAR(p3);
|
2022-04-30 18:29:48 -04:00
|
|
|
QActive *act = (QActive *)p1;
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// event-loop
|
|
|
|
for (;;) { // for-ever
|
2022-04-30 18:29:48 -04:00
|
|
|
QEvt const *e = QActive_get_(act);
|
2023-01-19 21:13:24 -05:00
|
|
|
// dispatch event (virtual call)
|
|
|
|
(*act->super.vptr->dispatch)(&act->super, e, act->prio);
|
|
|
|
QF_gc(e); // check if the event is garbage, and collect it if so
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
//
|
|
|
|
// In the Zephyr port the generic function QActive_setAttr() is used to
|
|
|
|
// set the options for the Zephyr thread (attr1) and thread name (attr2).
|
2024-08-28 03:03:05 -04:00
|
|
|
// QActive_setAttr() needs to be called *before* QActive_start() for the
|
2023-01-19 21:13:24 -05:00
|
|
|
// given active object.
|
|
|
|
//
|
|
|
|
// In this Zephyr port the attributes will be used as follows
|
2024-08-28 03:03:05 -04:00
|
|
|
// (see also QActive_start()):
|
2023-01-19 21:13:24 -05:00
|
|
|
// - attr1 - will be used for thread options in k_thread_create()
|
|
|
|
// - attr2 - will be used for thread name in k_thread_name_set()
|
|
|
|
//
|
2022-04-30 18:29:48 -04:00
|
|
|
void QActive_setAttr(QActive *const me, uint32_t attr1, void const *attr2) {
|
2023-01-19 21:13:24 -05:00
|
|
|
me->thread.base.order_key = attr1; // will be used for thread options
|
|
|
|
me->thread.init_data = (void *)attr2; // will be used for thread name
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2024-08-28 03:03:05 -04:00
|
|
|
void QActive_start(QActive * const me,
|
|
|
|
QPrioSpec const prioSpec,
|
2024-10-15 13:15:11 -04:00
|
|
|
QEvtPtr * const qSto, uint_fast16_t const qLen,
|
2024-08-28 03:03:05 -04:00
|
|
|
void * const stkSto, uint_fast16_t const stkSize,
|
|
|
|
void const * const par)
|
2022-04-30 18:29:48 -04:00
|
|
|
{
|
2023-01-19 21:13:24 -05:00
|
|
|
me->prio = (uint8_t)(prioSpec & 0xFFU); // QF-priority of the AO
|
2023-11-02 12:59:50 -04:00
|
|
|
me->pthre = 0U; // preemption-threshold (not used for AO registration)
|
2024-10-15 13:15:11 -04:00
|
|
|
QActive_register_(me); // make QF aware of this active object
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// initialize the Zephyr message queue
|
2024-10-15 13:15:11 -04:00
|
|
|
k_msgq_init(&me->eQueue, (char *)qSto, sizeof(QEvtPtr), (uint32_t)qLen);
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// top-most initial tran. (virtual call)
|
|
|
|
(*me->super.vptr->init)(&me->super, par, me->prio);
|
|
|
|
QS_FLUSH(); // flush the trace buffer to the host
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2024-01-30 20:48:12 -05:00
|
|
|
// The Zephyr priority of the AO thread can be specified in two ways:
|
2023-11-02 12:59:50 -04:00
|
|
|
//
|
|
|
|
// 1. Implictily based on the AO's priority (Zephyr uses the reverse
|
|
|
|
// priority numbering scheme than QP). This option is chosen, when
|
|
|
|
// the higher-byte of the prioSpec parameter is set to zero.
|
|
|
|
//
|
|
|
|
// 2. Explicitly as the higher-byte of the prioSpec parameter.
|
|
|
|
// This option is chosen when the prioSpec parameter is not-zero.
|
|
|
|
// For example, Q_PRIO(10U, -1U) will explicitly specify AO priority
|
|
|
|
// as 10 and Zephyr priority as -1.
|
|
|
|
//
|
|
|
|
// NOTE: The explicit Zephyr priority is NOT sanity-checked,
|
|
|
|
// so it is the responsibility of the application to ensure that
|
2024-01-30 20:48:12 -05:00
|
|
|
// it is consistent with the AO's priority. An example of
|
2023-11-02 12:59:50 -04:00
|
|
|
// inconsistent setting would be assigning Zephyr priorities that
|
|
|
|
// would result in a different relative priritization of AO's threads
|
|
|
|
// than indicated by the AO priorities assigned.
|
|
|
|
//
|
2023-12-16 09:49:45 -05:00
|
|
|
int zephyr_prio = (int)((int16_t)prioSpec >> 8);
|
2023-11-02 12:59:50 -04:00
|
|
|
if (zephyr_prio == 0) {
|
|
|
|
zephyr_prio = (int)QF_MAX_ACTIVE - (int)me->prio;
|
|
|
|
}
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// extract data temporarily saved in me->thread by QActive_setAttr()
|
2022-04-30 18:29:48 -04:00
|
|
|
uint32_t opt = me->thread.base.order_key;
|
|
|
|
#ifdef CONFIG_THREAD_NAME
|
|
|
|
char const *name = (char const *)me->thread.init_data;
|
|
|
|
#endif
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// clear the Zephyr thread structure before creating the thread
|
2022-04-30 18:29:48 -04:00
|
|
|
me->thread = (struct k_thread){};
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// create a Zephyr thread for the AO...
|
2022-04-30 18:29:48 -04:00
|
|
|
k_thread_create(&me->thread,
|
|
|
|
(k_thread_stack_t *)stkSto,
|
|
|
|
(size_t)stkSize,
|
|
|
|
&thread_entry,
|
2023-01-19 21:13:24 -05:00
|
|
|
(void *)me, // p1
|
|
|
|
(void *)0, // p2
|
|
|
|
(void *)0, // p3
|
2023-11-02 12:59:50 -04:00
|
|
|
zephyr_prio,// Zephyr priority
|
2023-01-19 21:13:24 -05:00
|
|
|
opt, // thread options
|
|
|
|
K_NO_WAIT); // start immediately
|
2022-04-30 18:29:48 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_THREAD_NAME
|
2023-01-19 21:13:24 -05:00
|
|
|
// set the Zephyr thread name, if initialized, or the default name "AO"
|
2022-04-30 18:29:48 -04:00
|
|
|
k_thread_name_set(&me->thread, (name != (char *)0) ? name : "AO");
|
|
|
|
#endif
|
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
bool QActive_post_(QActive * const me, QEvt const * const e,
|
|
|
|
uint_fast16_t const margin, void const * const sender)
|
|
|
|
{
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_STAT
|
|
|
|
QF_CRIT_ENTRY();
|
2024-10-15 13:15:11 -04:00
|
|
|
|
|
|
|
Q_REQUIRE_INCRIT(200, e != (QEvt *)0);
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// NOTE: k_msgq_num_free_get() can be safely called from crit-section
|
2022-04-30 18:29:48 -04:00
|
|
|
uint_fast16_t nFree = (uint_fast16_t)k_msgq_num_free_get(&me->eQueue);
|
|
|
|
|
|
|
|
bool status;
|
|
|
|
if (margin == QF_NO_MARGIN) {
|
|
|
|
if (nFree > 0U) {
|
2023-01-19 21:13:24 -05:00
|
|
|
status = true; // can post
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-01-19 21:13:24 -05:00
|
|
|
status = false; // cannot post
|
2024-10-15 13:15:11 -04:00
|
|
|
Q_ERROR_INCRIT(210); // must be able to post the event
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nFree > (QEQueueCtr)margin) {
|
2023-01-19 21:13:24 -05:00
|
|
|
status = true; // can post
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-01-19 21:13:24 -05:00
|
|
|
status = false; // cannot post
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
if (status) { // can post the event?
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_BEGIN_PRE(QS_QF_ACTIVE_POST, me->prio)
|
|
|
|
QS_TIME_PRE(); // timestamp
|
|
|
|
QS_OBJ_PRE(sender); // the sender object
|
|
|
|
QS_SIG_PRE(e->sig); // the signal of the event
|
|
|
|
QS_OBJ_PRE(me); // this active object (recipient)
|
2025-01-18 18:33:14 -05:00
|
|
|
QS_2U8_PRE(e->poolNum_, e->refCtr_);// pool-Id & ref-Count
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_EQC_PRE(nFree); // # free entries available
|
|
|
|
QS_EQC_PRE(0U); // min # free entries (unknown)
|
|
|
|
QS_END_PRE()
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2025-01-18 18:33:14 -05:00
|
|
|
if (e->poolNum_ != 0U) { // is it a pool event?
|
|
|
|
Q_ASSERT_INCRIT(205, e->refCtr_ < (2U * QF_MAX_ACTIVE));
|
2023-01-19 21:13:24 -05:00
|
|
|
QEvt_refCtr_inc_(e); // increment the reference counter
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
|
2024-10-15 13:15:11 -04:00
|
|
|
QF_CRIT_EXIT(); // exit crit.sect. before calling Zephyr API
|
2023-01-19 21:13:24 -05:00
|
|
|
int err = k_msgq_put(&me->eQueue, (void const *)&e, K_NO_WAIT);
|
2024-10-15 13:15:11 -04:00
|
|
|
QF_CRIT_ENTRY(); // re-enter crit.sect. after calling Zephyr API
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// posting to the Zephyr message queue must succeed, see NOTE1
|
2024-10-15 13:15:11 -04:00
|
|
|
Q_ASSERT_INCRIT(220, err == 0);
|
|
|
|
#ifdef Q_UNSAFE
|
|
|
|
Q_UNUSED_PAR(err);
|
|
|
|
#endif
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_BEGIN_PRE(QS_QF_ACTIVE_POST_ATTEMPT, me->prio)
|
|
|
|
QS_TIME_PRE(); // timestamp
|
|
|
|
QS_OBJ_PRE(sender); // the sender object
|
|
|
|
QS_SIG_PRE(e->sig); // the signal of the event
|
|
|
|
QS_OBJ_PRE(me); // this active object (recipient)
|
2025-01-18 18:33:14 -05:00
|
|
|
QS_2U8_PRE(e->poolNum_, e->refCtr_);
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_EQC_PRE(nFree); // # free entries available
|
|
|
|
QS_EQC_PRE(0U); // min # free entries (unknown)
|
|
|
|
QS_END_PRE()
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_EXIT();
|
2022-04-30 18:29:48 -04:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
void QActive_postLIFO_(QActive * const me, QEvt const * const e) {
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_STAT
|
|
|
|
QF_CRIT_ENTRY();
|
|
|
|
|
2024-10-15 13:15:11 -04:00
|
|
|
Q_REQUIRE_INCRIT(300, e != (QEvt *)0);
|
|
|
|
|
|
|
|
QS_BEGIN_PRE(QS_QF_ACTIVE_POST_LIFO, me->prio)
|
|
|
|
QS_TIME_PRE(); // timestamp
|
|
|
|
QS_SIG_PRE(e->sig); // the signal of this event
|
|
|
|
QS_OBJ_PRE(me); // this active object
|
2025-01-18 18:33:14 -05:00
|
|
|
QS_2U8_PRE(e->poolNum_, e->refCtr_);
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_EQC_PRE(k_msgq_num_free_get(&me->eQueue)); // # free entries
|
|
|
|
QS_EQC_PRE(0U); // min # free entries (unknown)
|
|
|
|
QS_END_PRE()
|
2023-01-19 21:13:24 -05:00
|
|
|
|
2025-01-18 18:33:14 -05:00
|
|
|
if (e->poolNum_ != 0U) { // is it a pool event?
|
|
|
|
Q_ASSERT_INCRIT(305, e->refCtr_ < (2U * QF_MAX_ACTIVE));
|
2023-01-19 21:13:24 -05:00
|
|
|
QEvt_refCtr_inc_(e); // increment the reference counter
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
// NOTE: Zephyr message queue does not currently support LIFO posting
|
|
|
|
// so normal FIFO posting is used instead.
|
2024-10-15 13:15:11 -04:00
|
|
|
QF_CRIT_EXIT(); // exit crit.sect. before calling Zephyr API
|
2023-01-19 21:13:24 -05:00
|
|
|
int err = k_msgq_put(&me->eQueue, (void *)&e, K_NO_WAIT);
|
2024-10-15 13:15:11 -04:00
|
|
|
QF_CRIT_ENTRY(); // re-enter crit.sect. after calling Zephyr API
|
|
|
|
|
|
|
|
Q_ASSERT_INCRIT(310, err == 0);
|
|
|
|
#ifdef Q_UNSAFE
|
|
|
|
Q_UNUSED_PAR(err);
|
|
|
|
#endif
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_EXIT();
|
2022-04-30 18:29:48 -04:00
|
|
|
}
|
2023-01-19 21:13:24 -05:00
|
|
|
//............................................................................
|
2022-04-30 18:29:48 -04:00
|
|
|
QEvt const *QActive_get_(QActive * const me) {
|
2023-01-19 21:13:24 -05:00
|
|
|
// wait for an event (forever)
|
2024-10-15 13:15:11 -04:00
|
|
|
QEvtPtr e;
|
2023-01-19 21:13:24 -05:00
|
|
|
int err = k_msgq_get(&me->eQueue, (void *)&e, K_FOREVER);
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_STAT
|
|
|
|
QF_CRIT_ENTRY();
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2024-10-15 13:15:11 -04:00
|
|
|
Q_ASSERT_INCRIT(410, err == 0); // queue-get must succeed
|
|
|
|
#ifdef Q_UNSAFE
|
|
|
|
Q_UNUSED_PAR(err);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QS_BEGIN_PRE(QS_QF_ACTIVE_GET, me->prio)
|
|
|
|
QS_TIME_PRE(); // timestamp
|
|
|
|
QS_SIG_PRE(e->sig); // the signal of this event
|
|
|
|
QS_OBJ_PRE(me); // this active object
|
2025-01-18 18:33:14 -05:00
|
|
|
QS_2U8_PRE(e->poolNum_, e->refCtr_);
|
2024-10-15 13:15:11 -04:00
|
|
|
QS_EQC_PRE(k_msgq_num_free_get(&me->eQueue));// # free entries
|
|
|
|
QS_END_PRE()
|
2022-04-30 18:29:48 -04:00
|
|
|
|
2023-01-19 21:13:24 -05:00
|
|
|
QF_CRIT_EXIT();
|
|
|
|
|
2022-04-30 18:29:48 -04:00
|
|
|
return e;
|
|
|
|
}
|