qpcpp/ports/freertos/qf_port.hpp

208 lines
8.2 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>
//============================================================================
2022-08-11 15:36:19 -04:00
//! @date Last updated on: 2022-06-30
//! @version Last updated for: @ref qpcpp_7_0_1
//!
//! @file
//! @brief QF/C++ port to FreeRTOS 10.x
2018-01-10 18:26:05 -05:00
2019-10-27 12:26:31 -04:00
#ifndef QF_PORT_HPP
#define QF_PORT_HPP
2018-01-10 18:26:05 -05:00
2019-03-28 11:59:31 -04:00
// FreeRTOS event queue and thread types
2022-08-11 15:36:19 -04:00
#define QF_EQUEUE_TYPE QueueHandle_t
#define QF_OS_OBJECT_TYPE StaticQueue_t
2018-01-10 18:26:05 -05:00
#define QF_THREAD_TYPE StaticTask_t
// The maximum number of active objects in the application, see NOTE1
2020-03-17 21:33:58 -04:00
#define QF_MAX_ACTIVE 32U
2018-01-10 18:26:05 -05:00
// QF interrupt disabling/enabling (task level)
#define QF_INT_DISABLE() taskDISABLE_INTERRUPTS()
#define QF_INT_ENABLE() taskENABLE_INTERRUPTS()
// QF critical section for FreeRTOS (task level), see NOTE2
// #define QF_CRIT_STAT_TYPE not defined
#define QF_CRIT_ENTRY(stat_) taskENTER_CRITICAL()
#define QF_CRIT_EXIT(stat_) taskEXIT_CRITICAL()
2022-08-11 15:36:19 -04:00
// FreeRTOS requires the "FromISR" API in QP/C++
#define QF_ISR_API 1
// enable the QActive::stop()
#define QF_ACTIVE_STOP 1
2020-03-17 21:33:58 -04:00
#include "FreeRTOS.h" // FreeRTOS master include file, see NOTE4/
2022-08-11 15:36:19 -04:00
#include "task.h" // FreeRTOS task management
#include "queue.h" // FreeRTOS queue management
2018-01-10 18:26:05 -05:00
2020-03-17 21:33:58 -04:00
#include "qep_port.hpp" // QEP port
2022-08-11 15:36:19 -04:00
#include "qequeue.hpp" // QF event queue (for deferring events)
#include "qmpool.hpp" // this QP port uses memory pool (for event pools)
2020-03-17 21:33:58 -04:00
#include "qf.hpp" // QF platform-independent public interface
2018-01-10 18:26:05 -05:00
// the "FromISR" versions of the QF APIs, see NOTE3
#ifdef Q_SPY
#define PUBLISH_FROM_ISR(e_, pxHigherPrioTaskWoken_, sender_) \
publishFromISR_((e_), (pxHigherPrioTaskWoken_), (sender_))
#define POST_FROM_ISR(e_, pxHigherPrioTaskWoken_, sender_) \
2022-08-11 15:36:19 -04:00
postFromISR_((e_), QF_NO_MARGIN, \
2018-01-10 18:26:05 -05:00
(pxHigherPrioTaskWoken_), (sender_))
#define POST_X_FROM_ISR(e_, margin_, pxHigherPrioTaskWoken_, sender_) \
postFromISR_((e_), (margin_), (pxHigherPrioTaskWoken_), (sender_))
#define TICK_X_FROM_ISR(tickRate_, pxHigherPrioTaskWoken_, sender_) \
2022-08-11 15:36:19 -04:00
tickFromISR_((tickRate_), (pxHigherPrioTaskWoken_), (sender_))
2018-01-10 18:26:05 -05:00
#else
#define PUBLISH_FROM_ISR(e_, pxHigherPrioTaskWoken_, dummy) \
2022-08-11 15:36:19 -04:00
publishFromISR_((e_), (pxHigherPrioTaskWoken_), nullptr)
2018-01-10 18:26:05 -05:00
#define POST_FROM_ISR(e_, pxHigherPrioTaskWoken_, dummy) \
2022-08-11 15:36:19 -04:00
postFromISR_((e_), QF_NO_MARGIN, (pxHigherPrioTaskWoken_), \
nullptr)
2018-01-10 18:26:05 -05:00
#define POST_X_FROM_ISR(e_, margin_, pxHigherPrioTaskWoken_, dummy) \
2022-08-11 15:36:19 -04:00
postFromISR_((e_), (margin_), (pxHigherPrioTaskWoken_), nullptr)
2018-01-10 18:26:05 -05:00
#define TICK_X_FROM_ISR(tickRate_, pxHigherPrioTaskWoken_, dummy) \
2022-08-11 15:36:19 -04:00
tickFromISR_((tickRate_), (pxHigherPrioTaskWoken_), nullptr)
2018-01-10 18:26:05 -05:00
#endif
#define TICK_FROM_ISR(pxHigherPrioTaskWoken_, sender_) \
2020-03-17 21:33:58 -04:00
TICK_X_FROM_ISR(0U, (pxHigherPrioTaskWoken_), (sender_))
2018-01-10 18:26:05 -05:00
#ifdef Q_EVT_CTOR
#define Q_NEW_FROM_ISR(evtT_, sig_, ...) \
2022-08-11 15:36:19 -04:00
(new(QP::QF::newXfromISR_(sizeof(evtT_), QF_NO_MARGIN, 0)) \
2018-01-10 18:26:05 -05:00
evtT_((sig_), ##__VA_ARGS__))
2020-03-17 21:33:58 -04:00
#define Q_NEW_X_FROM_ISR(e_, evtT_, margin_, sig_, ...) do { \
(e_) = static_cast<evtT_ *>( \
QP::QF::newXfromISR_(static_cast<std::uint_fast16_t>( \
sizeof(evtT_)), (margin_), 0)); \
if ((e_) != nullptr) { \
2018-01-10 18:26:05 -05:00
new((e_)) evtT_((sig_), ##__VA_ARGS__); \
} \
2019-12-31 15:56:23 -05:00
} while (false)
2018-01-10 18:26:05 -05:00
#else // QEvt is a POD (Plain Old Datatype)
2020-03-17 21:33:58 -04:00
#define Q_NEW_FROM_ISR(evtT_, sig_) \
(static_cast<evtT_ *>(QP::QF::newXfromISR_( \
static_cast<std::uint_fast16_t>(sizeof(evtT_)), \
2022-08-11 15:36:19 -04:00
QF_NO_MARGIN, (sig_))))
2018-01-10 18:26:05 -05:00
2020-03-17 21:33:58 -04:00
#define Q_NEW_X_FROM_ISR(e_, evtT_, margin_, sig_) \
((e_) = static_cast<evtT_ *>( \
QP::QF::newXfromISR_(sizeof(evtT_), (margin_), (sig_))))
2018-01-10 18:26:05 -05:00
#endif
2018-02-12 18:51:22 -05:00
namespace QP {
2018-01-10 18:26:05 -05:00
enum FreeRTOS_TaskAttrs {
TASK_NAME_ATTR
};
2018-02-12 18:51:22 -05:00
} // namespace QP
2018-01-10 18:26:05 -05:00
// FreeRTOS hooks prototypes (not provided by FreeRTOS)
extern "C" {
#if (configUSE_IDLE_HOOK > 0)
void vApplicationIdleHook(void);
#endif
#if (configUSE_TICK_HOOK > 0)
void vApplicationTickHook(void);
#endif
#if (configCHECK_FOR_STACK_OVERFLOW > 0)
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName);
#endif
#if (configSUPPORT_STATIC_ALLOCATION > 0)
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize);
#endif
} // extern "C"
//============================================================================
2018-01-10 18:26:05 -05:00
// interface used only inside QF, but not in applications
2022-08-28 22:12:27 -04:00
2018-01-10 18:26:05 -05:00
#ifdef QP_IMPL
2022-08-11 15:36:19 -04:00
#define FREERTOS_TASK_PRIO(qp_prio_) \
((UBaseType_t)((qp_prio_) + tskIDLE_PRIORITY))
/* FreeRTOS scheduler locking for QF_publish_() (task context only) */
#define QF_SCHED_STAT_ \
UBaseType_t curr_prio; \
TaskHandle_t curr_task;
#define QF_SCHED_LOCK_(prio_) do { \
curr_task = xTaskGetCurrentTaskHandle(); \
curr_prio = uxTaskPriorityGet(curr_task); \
if (FREERTOS_TASK_PRIO(prio_) > curr_prio) { \
vTaskPrioritySet(curr_task, FREERTOS_TASK_PRIO(prio_));\
} \
else { \
curr_prio = tskIDLE_PRIORITY; \
} \
} while (0)
#define QF_SCHED_UNLOCK_() \
if (curr_prio != tskIDLE_PRIORITY) { \
vTaskPrioritySet(curr_task, curr_prio); \
} else ((void)0)
2018-01-10 18:26:05 -05:00
// native QF event pool operations
#define QF_EPOOL_TYPE_ QMPool
#define QF_EPOOL_INIT_(p_, poolSto_, poolSize_, evtSize_) \
(p_).init((poolSto_), (poolSize_), (evtSize_))
2020-10-01 12:50:17 -04:00
#define QF_EPOOL_EVENT_SIZE_(p_) ((p_).getBlockSize())
#define QF_EPOOL_GET_(p_, e_, m_, qs_id_) \
((e_) = static_cast<QEvt *>((p_).get((m_), (qs_id_))))
#define QF_EPOOL_PUT_(p_, e_, qs_id_) ((p_).put((e_), (qs_id_)))
2018-01-10 18:26:05 -05:00
#endif /* ifdef QP_IMPL */
//============================================================================
2018-01-10 18:26:05 -05:00
// NOTE1:
// The maximum number of active objects QF_MAX_ACTIVE can be increased to 64,
// inclusive, but it can be reduced to save some memory. Also, the number of
// active objects cannot exceed the number of FreeRTOS task priorities,
// because each QP active object requires a unique priority level.
//
// NOTE2:
// The critical section definition applies only to the FreeRTOS "task level"
// APIs. The "FromISR" APIs are defined separately.
//
// NOTE3:
// The design of FreeRTOS requires using different APIs inside the ISRs
// (the "FromISR" variant) than at the task level. Accordingly, this port
// provides the "FromISR" variants for QP functions and "FROM_ISR" variants
// for QP macros to be used inside ISRs. ONLY THESE "FROM_ISR" VARIANTS
// ARE ALLOWED INSIDE ISRs AND CALLING THE TASK-LEVEL APIs IS AN ERROR.
//
2019-10-27 12:26:31 -04:00
#endif // QF_PORT_HPP
2022-08-28 22:12:27 -04:00