About the QP Port to FreeRTOS
The ports/freertos/ directory contains a generic platform-independent QP/C++ port to FreeRTOS kernel (version 10). The provided QP port to FreeRTOS has been designed generically to rely exclusively on the existing FreeRTOS API. This means that the port should run without changes on any CPU/compiler platform supported by FreeRTOS.
The QP-FreeRTOS port works as follows:
- The QP port uses the static memory allocation of FreeRTOS. This requires the FreeRTOS configuration to define the configSUPPORT_STATIC_ALLOCATION
- Each QP active object executes in a separate FreeRTOS task and requires a private stack space.
- The task-level critical section used in QF and QS is based on the FreeRTOS APIs
taskENTER_CRITICAL()
/taskEXIT_CRITICAL()
.
- The ISR-level critical section used in QF and QS is based on the FreeRTOS APIs
taskENTER_CRITICAL_FROM_ISR()
/taskEXIT_CRITICAL_FROM_ISR()
.
- The QP port to FreeRTOS provides new "FromISR" APIs, which must be used in the ISRs (but cannot be used at the task-level)
- Attention
- The design of FreeRTOS requires the use of special "FromISR" API inside ISRs, which imposes the requirement to also provide the "FromISR" variants of the QP APIs, such as
QACTIVE_POST_FROM_ISR()
, QF_PUBLISH_FROM_ISR()
, etc. These "FromISR" QP APIs must be used inside ISRs instead of the task-level APIs (QACTIVE_POST()
, QF_PUBLISH()
, etc.) and conversely, they cannot be used inside tasks and active objects. Unfortunately, FreeRTOS provides no generic way to enforce the proper API use via assertions.
- The QP port uses the native event queue (QP::QEQueue) for active object event queues and internally calls the FreeRTOS API
xTaskNotifyGive()
to notify an active object when an event is posted to its event queue.
- The QP port internally calls the FreeRTOS API
ulTaskNotifyTake(pdTRUE, portMAX_DELAY)
to block an active object task when it waits for posting an event.
- The QP port uses the native QF memory pool (QP::QMPool) to implement event pools.
- The QP port does not mandate any specific method to manage the QP time events, but the provided examples use the FreeRTOS "hook"
vApplicationTickHook()
to periodically invoke the QP clock tick QF_TICK_X_FROM_ISR(). (NOTE: the vApplicationTickHook()
executes in the ISR context and therefore mandates the use of the "FromISR" APIs).
Example Code
The QP port to FreeRTOS comes with examples located in the directory qpc/examples/freertos/
. Currently, the examples are provided for the following boards and development toolchains:
- EK-TM4C123GXL (ARM Cortex-M4F), ARM-KEIL, GNU-ARM, IAR-ARM
- STM32F746G-Discovery (ARM Cortex-M7), ARM-KEIL, GNU-ARM, IAR-ARM
Writing ISRs for QP/FreeRTOS
The provided examples show how to write regular "kernel-aware" ISRs as well as "kernel-unaware" ISRs for QP/FreeRTOS. (See also the FreeRTOS documentation for configMAX_SYSCALL_INTERRUPT_PRIORITY.
Here is an example of a regular "kernel-aware" ISR (note the use of the "FromISR" QP APIs"):
void GPIOPortA_IRQHandler(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
AO_Table->POST_FROM_ISR(
Q_NEW_FROM_ISR(
QP::QEvt, DPP::MAX_PUB_SIG),
&xHigherPriorityTaskWoken,
&l_GPIOPortA_IRQHandler);
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
}
Here is an example of a "kernel-unaware" ISR (See also the FreeRTOS documentation for configMAX_SYSCALL_INTERRUPT_PRIORITY:
void UART0_IRQHandler(void);
void UART0_IRQHandler(void) {
UART0->ICR = status;
while ((UART0->FR & UART_FR_RXFE) == 0) {
}
}
unsigned long int uint32_t
exact-width 32-bit unsigned int
static bool rxPut(std::uint8_t const b) noexcept
Put one byte into the QS RX lock-free buffer.
Writing FreeRTOS Hooks Running in ISR Context
FreeRTOS provides "hooks" that are user functions that execute in the ISR context (e.g., vApplicationTickHook()
). Such ISR-level functions are closely related to ISRs and should also use exclusively only the "FromISR" APIs. Here is an example of the vApplicationTickHook()
:
void vApplicationTickHook(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
QP::QF::TICK_X_FROM_ISR(0U, &xHigherPriorityTaskWoken, &l_TickHook);
. . .
portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
}
Starting Active Objects in QP/FreeRTOS
As mentioned in the FreeRTOS port summary, the QP port to FreeRTOS uses the static memory allocation of FreeRTOS. This means that all memory for an active object, including the private queue buffer and the private stack for the the associated FreeRTOS task must be allocated by the user. Here is an example code that starts an active object:
int main() {
. . .
static QEvt const *tableQueueSto[N_PHILO];
static StackType_t tableStack[configMINIMAL_STACK_SIZE];
. . .
DPP::AO_Table->setAttr(QP::TASK_NAME_ATTR, "Table");
DPP::AO_Table->start(
tableQueueSto,
tableStack,
sizeof(tableStack),
. . .
}
unsigned int uint_fast8_t
fast at-least 8-bit unsigned int
static int_t run(void)
Transfers control to QF to run the application.
#define Q_DIM(array_)
Helper macro to calculate static dimension of a 1-dim array_.
Next: embOS