C
使用Qt Quick Ultralite搭配FreeRTOS
FreeRTOS 是一款专为嵌入式设备和微控制器平台设计的实时操作系统内核。它提供了线程(在FreeRTOS 中称为任务)、互斥锁、信号量和软件计时器。
本指南将向您介绍开始使用Qt Quick Ultralite 进行开发所需的条件,包括FreeRTOS 、Qt Quick Ultralite 以及FreeRTOS 的相关背景信息。
支持的架构、平台和FreeRTOS 版本
Qt Quick Ultralite 支持以下硬件:
| 硬件板 | MCU | 架构 | 编译器 | 支持FreeRTOS |
|---|---|---|---|---|
| NXP IMXRT1050-EVKB | MIMXRT1052DVL6A | ARM Cortex-M7 | GNU Arm GCC 12.3.rel1、IAR Arm 开发工具 V9.40 | FreeRTOS V10.0.1 |
| NXP IMXRT1064-EVK | MIMXRT1064DVL6A | ARM Cortex-M7 | GNU Arm GCC 12.3.rel1,IAR Arm 构建工具 V9.40 | FreeRTOS V10.0.1 |
| STM32F769I-DISCOVERY | STM32F769NI | ARM Cortex-M7 | GNU Arm GCC 12.3.rel1,IAR Arm 构建工具 V9.40 | FreeRTOS V10.0.1 |
| NXP IMXRT1170-EVKB | MIMXRT1176DVMAA | ARM Cortex-M7 和 ARM Cortex-M4 | GNU Arm GCC 12.3.rel1、IAR Arm 构建工具 V9.40 | FreeRTOS V10.0.1 |
| Renesas EK-RA8D1 | R7FA8D1BHECBD | ARM Cortex-M85 | GNU Arm GCC 12.3.rel1 | FreeRTOS V10.6.1 |
| Espressif ESP32-S3-BOX-3 | ESP32-S3 MCU | 双核 Xtensa LX7 | GNU Xtensa GCC 13.2.0 | FreeRTOS V10.5.1 |
| Infineon PSOC™ Edge E84 评估套件 | PSE846GPS2DBZC4 | ARM Cortex-M55 | GNU Arm GCC 14.2.1、适用于 Arm 的 LLVM 嵌入式工具链 19.1.5 | FreeRTOS V10.6.202 |
这些参考板受Qt 标准支持的支持。
注意:如果您 正在使用针对特定硬件平台的预编译平台库,则FreeRTOS 源代码的一部分已编译到该库中。如果您想更改FreeRTOS 的版本,则必须重新构建该平台库。
开发环境要求
先决条件
要为FreeRTOS 构建Qt Quick Ultralite,您需要以下内容:
- Qt Quick 已安装 UltraliteFreeRTOS 软件包。请参阅《获取Qt Quick Ultralite 版》(FreeRTOS )。
- FreeRTOS 源代码。您可以从FreeRTOS 网站获取FreeRTOS 。
- Qt Quick Ultralite 的先决条件及目标平台(参见“支持的目标开发板和开发主机”)。
环境设置
根据您使用的开发板,请按照《NXP 入门指南》(BareMetal 和FreeRTOS )以及《STM 入门指南》中的定义,设置平台特定的环境变量。
如果您使用的是 app_common ,您还需要设置FreeRTOS 源代码的路径:
-DFREERTOS_DIR=< FreeRTOS directory path >运行 cmake 时,请在平台名称后添加FreeRTOS 后缀,以生成FreeRTOS 项目构建文件。
示例:
cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=<Qul install path>/lib/cmake/Qul/toolchain/armgcc.cmake -DQUL_PLATFORM=<target platform>-freertos -DFREERTOS_DIR=<FreeRTOS directory path>cmake .. -G "Ninja" -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_TOOLCHAIN_FILE=<Qul install path>\lib\cmake\Qul\toolchain\armgcc.cmake -DQUL_PLATFORM=<target platform>-freertos -DFREERTOS_DIR=<FreeRTOS directory path>有关在受支持平台上进行单设备环境配置的信息,请参阅“受支持的架构、平台和FreeRTOS 版本”中的硬件板卡链接。
如何在以下平台上使用Qt Quick UltraliteFreeRTOS
Qt Quick 获取适用于FreeRTOS
对于受支持的平台, Qt Quick Ultralite 安装包默认即支持FreeRTOS 。若要为FreeRTOS 编译示例程序,请参阅《使用app_common 》。
启动Qt Quick Ultralite 线程
您需要调用两个函数才能在目标设备上运行Qt Quick Ultralite:
Qul::initPlatform();该函数用于初始化平台硬件和操作系统。应尽早调用此函数,最迟应在运行
Qul::appMain()或访问任何设备特定函数之前调用。有关FreeRTOS 中不同内存分配实现方式及其在Qt Quick Ultralite 项目中的使用方法,请参阅《更改堆策略》。
Qul::appMain();该函数负责初始化Qt Quick Ultralite,并充当其主循环。在FreeRTOS 中,应通过任务来调用此函数。
有关设置自定义入口点的更多信息,请参阅《在应用程序中运行Qt Quick Ultralite》。
另请参阅示例“main.cpp ”,了解如何在FreeRTOS 任务中运行Qt Quick Ultralite。
提供内存分配器
FreeRTOS 默认提供五种不同的内存分配器实现。它们位于FreeRTOS 的 MemMang 目录下,并提供不同的堆管理策略。有关不同实现的更多信息,请参阅FreeRTOS 开发者文档中的“内存管理”部分。
如果您在项目中使用app_common ,可以通过在项目中设置QUL_FREERTOS_HEAP_POLICY 目标属性来更改所使用的实现。有关更多信息,请参阅“更改堆策略”。
FreeRTOS 提供的内存分配器实现了pvPortMalloc 和vPortFree 接口。这些分配器被Qt Quick Ultralite 平台移植版本在内部使用,在大多数情况下,应用程序代码也应使用它们。可以在链接器配置中为标准库和FreeRTOS 分配器分别配置独立的堆区域。
在应用程序中使用FreeRTOS 堆分配器的一种方法是重载malloc 、free 和realloc 的默认实现。如果new 和delete 这些 C++ 关键字在内部使用的是malloc 和free ,则您的应用程序无需提供单独的重载。
包含realloc 的内存分配函数的 C 语言示例代码:
#include <FreeRTOS.h> #include <portable.h> #if defined(__ICCARM__) #include <string.h> #else #include <memory.h> #endif extern void *pvPortMalloc(size_t xWantedSize); extern void vPortFree(void *pv); void *malloc(size_t sz) { void *ptr = pvPortMalloc(sizeof(size_t) + sz); if (ptr == NULL) { return NULL; } *((size_t *) ptr) = sz; return ((char *) ptr) + sizeof(size_t); } void free(void *p) { if (p != NULL) { vPortFree(((char *) p) - sizeof(size_t)); } } void *realloc(void *ptr, size_t sz) { if (ptr == NULL) return malloc(sz); size_t oldSize = *(size_t *) ((unsigned long) ptr - sizeof(size_t)); if (sz == oldSize) return ptr; void *newPtr = NULL; if (sz > 0) { newPtr = malloc(sz); memcpy(newPtr, ptr, (sz > oldSize) ? oldSize : sz); } free(ptr); return newPtr; }
Qt Quick Ultralite 提供了一个默认分配器重写,您可以在CMakeLists.txt 中通过为应用程序添加一个属性来启用该功能:
qul_add_target(my_application …) set_target_properties(my_application PROPERTIES FREERTOS_PROVIDE_MEMORY_ALLOCATOR_OVERLOADS TRUE)
该属性必须在调用app_target_setup_os 之前设置。它会向可执行文件中添加 C 和 C++ 分配器重载。
注意: Qt Quick Ultralite 提供的分配器重载与 FreeRTOS heap_3.c 不兼容,因为其pvPortMalloc 函数在内部使用了malloc 。
注意:由于 内部调用了 malloc ,某些函数(如printf )可能会分配到已被FreeRTOS 分配过的内存。这会导致意外行为,可通过在app_common 中启用重载来避免。
线程栈大小
在FreeRTOS 中,每个单独的线程(或任务)都有自己的栈。Qt Quick Ultralite 所需的栈大小在很大程度上取决于项目的复杂程度。默认情况下,Qt Quick Ultralite 参考平台FreeRTOS 使用的线程栈大小为 6 千字,相当于 24 KiB。
注意: FreeRTOS 中栈大小以字(word)为单位,而非字节(byte)。
在FreeRTOS 任务中运行Qt Quick Ultralite 的main.cpp 示例
以下代码演示了如何为Qt Quick Ultralite创建一个基本的FreeRTOS 线程并运行它。
#include <qul/qul.h> #include <FreeRTOS.h> #include <task.h> static void Qul_Thread(void *argument); int main() { Qul::initPlatform(); if (xTaskCreate(Qul_Thread, "QulExec", 32*1024, 0, 4, 0) != pdPASS) { configASSERT(false); // Task creation failed } vTaskStartScheduler(); configASSERT(false); } static void Qul_Thread(void *argument) { (void) argument; Qul::appMain(); }
从其他应用程序与Qt Quick Ultralite 进行交互
请参阅《FreeRTOS 多任务示例》和《将C++代码与QML集成》。
构建FreeRTOS
请参阅《FreeRTOS 应用程序构建流程》。
有关app_common 的更多信息,请参阅FreeRTOS 应用程序构建流程。
FreeRTOS 多任务示例
使用FreeRTOS 和Qt Quick Ultralite 运行多个任务。

本示例演示了如何创建多个相互交互的任务。该应用程序包含三个任务:
Qul_Thread运行 QML 应用程序实例的Qt Quick Ultraliteexec()循环,并通过 HardwareControl 方法将触摸事件通知给其他任务。Led_Thread负责 LED 的闪烁,并将最新的 LED 闪烁计数更新至 QML 应用程序(Qt Quick Ultralite 线程)。FanControl_Thread重新计算风扇动画的旋转周期,并将最新LED闪烁计数更新至QML应用程序(Qt Quick Ultralite线程)。
HardwareControl 类
HardwareControl 是一个Qul::Singleton 类,为QML应用程序与其他线程交互提供接口。该类负责处理与Led_Thread (使用FreeRTOS 任务通知)和FanControl_Thread (使用FreeRTOS 队列)之间的通信。
class HardwareControl : public Qul::Singleton<HardwareControl> { public: HardwareControl(); Qul::Property<int> fanSpeed; Qul::Property<int> ledCycleCount; Qul::Signal<void(int rotationPeriod)> fanRotationPeriodChanged; void updateSpeed(int newSpeed); private: void updateLedSpeed(); void updateFanSpeed(); ...
在此,首先将新值赋给speed 属性。随后,调用updateFanSpeed 和updateLedSpeed() 方法,以更新任务中关于速度变化的信息,并响应来自QML应用程序的onPressed 事件。
void HardwareControl::updateSpeed(int newSpeed) { fanSpeed.setValue(newSpeed); updateFanSpeed(); updateLedSpeed(); }
LED 闪烁速度通过updateSpeed() 方法进行更新:
void HardwareControl::updateLedSpeed() { xTaskNotify(LedTask, fanSpeed.value(), eSetValueWithOverwrite); }
QML 应用程序通过调用updateFanSpeed() 获取新的风扇旋转周期:
void HardwareControl::updateFanSpeed() { xQueueSend(getFanControlQueueHandle(), (void *) &(fanSpeed.value()), portMAX_DELAY); }
qul_thread.cpp
Qt Quick Ultralite 线程会创建应用程序实例并运行exec() 循环。
... void Qul_Thread(void *argument) { (void) argument; Qul::Application app; static multitask item; app.setRootItem(&item); app.exec(); }
源文件qul_thread.cpp还实现了函数postEventsToUI() 。该函数供其他线程使用,用于发送事件以修改 QML 属性Qul::Property fanSpeed 和Qul::Property ledCycleCount 。
void postEventsToUI(HardwareEvent &event) { static HardwareControlEventQueue eventQueue; eventQueue.postEvent(event); }
这些事件由onEvent() 回调函数按以下方式处理:
void HardwareControlEventQueue::onEvent(const HardwareEvent &event) { if (event.id == HardwareEventId::LedCycleCount) HardwareControl::instance().ledCycleCount.setValue(event.data); else if (event.id == HardwareEventId::FanRotationPeriod) HardwareControl::instance().fanRotationPeriodChanged(event.data); }
注意: 直接从其他线程更新 Qul 属性值并不具备 线程安全性。请按照上面的代码片段所示,改用Qul::EventQueue 。
led_thread.cpp
启动时,LED 线程会无限期地等待FreeRTOS 任务通知。当接收到来自Qt Quick Ultralite 线程的触摸事件时,它将解除阻塞并更新 LED 闪烁速度。在接收到第一个事件后,它将按照新计算出的速度值使 LED 闪烁。
void Led_Thread(void *argument) { ... while (true) { const TickType_t ticks = speed > 0 ? (350 / (portTICK_PERIOD_MS * speed)) : portMAX_DELAY; if (xTaskNotifyWait(0, ULONG_MAX, &newSpeed, ticks) == pdTRUE) { speed = newSpeed; } BoardUtils::toggleLED(); ...
LED线程还会计算闪烁次数,并基于Qul::EventQueue 中的postEventsToUI() 函数将此信息发送给QML应用程序。该计数由QML应用程序在屏幕上进行更新。
ledEvent.id = HardwareEventId::LedCycleCount; ledEvent.data = ledCycleCount; postEventsToUI(ledEvent); taskYIELD(); }
fan_thread.cpp
风扇控制线程监听FreeRTOS 事件队列,以便在接收到来自Qt Quick Ultralite线程的触摸事件时更新风扇转速。它会重新计算风扇动画的rotationPeriod ,并通过postEventsToUI() 函数将该值发送至UI。UI会根据rotationPeriod 的值更新动画速度。
... void FanControl_Thread(void *argument) { (void) argument; int newSpeed; HardwareEvent fanEvent; while (true) { if (xQueueReceive(fanControlQueue, &newSpeed, portMAX_DELAY) == pdTRUE) { int rotationPeriod = newSpeed == 0 ? 0 : 5000 / (newSpeed * 3); fanEvent.id = HardwareEventId::FanRotationPeriod; fanEvent.data = rotationPeriod; postEventsToUI(fanEvent); }
multitask.qml
multitask.qml 声明了将在设备屏幕上显示的用户界面。
import QtQuick 2.15 Rectangle { id: root Image { id: background source: "images/background-dark.png" anchors.fill: root } Column { anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top anchors.topMargin: 10 Text { anchors.horizontalCenter: parent.horizontalCenter horizontalAlignment: Text.AlignHCenter color: "white" text: "Speed: " + HardwareControl.fanSpeed font.pixelSize: 34 } Text { topPadding: 10 anchors.horizontalCenter: parent.horizontalCenter horizontalAlignment: Text.AlignHCenter color: "silver" text: "LED cycle count:" font.pixelSize: 17 } Text { anchors.horizontalCenter: parent.horizontalCenter horizontalAlignment: Text.AlignHCenter color: "white" text: HardwareControl.ledCycleCount font.pixelSize: 17 font.bold: true } } Text { horizontalAlignment: Text.AlignHCenter anchors.horizontalCenter: parent.horizontalCenter anchors.bottom: parent.bottom anchors.bottomMargin: 10 color: "gray" text: "Tap to change fan and LED speed!" } Image { id: fan source: "images/fan-off.png" anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter transform: Rotation { origin.x: fan.width / 2 origin.y: fan.height / 2 RotationAnimation on angle { id: imageRotation loops: Animation.Infinite from: 0 to: 360 duration: 0 running: false } } } MouseArea { id: ta anchors.fill: parent onPressed: { HardwareControl.updateSpeed((HardwareControl.fanSpeed + 1) % 6); } } HardwareControl.onFanRotationPeriodChanged: { imageRotation.duration = rotationPeriod imageRotation.running = rotationPeriod > 0 } Component.onCompleted: { HardwareControl.updateSpeed(HardwareControl.fanSpeed) } }
BoardUtils
BoardUtils 是一个命名空间,用于声明用于初始化和控制目标板 LED 的函数:
namespace BoardUtils { void initLED(); void toggleLED(); } // namespace BoardUtils
这些函数的定义因设备而异。以下示例摘自 STM32F769I-DISCOVERY 的实现代码。
namespace BoardUtils { void initLED() { BSP_LED_Init(LED1); } void toggleLED() { BSP_LED_Toggle(LED1); } } // namespace BoardUtils
main.cpp
main.cpp 包含硬件初始化以及Qt Quick Ultralite、LED 和风扇控制线程的创建。
int main() { Qul::initHardware(); Qul::initPlatform(); BoardUtils::initLED(); initFanControlQueue(); if (xTaskCreate(Qul_Thread, "QulExec", QUL_STACK_SIZE, 0, 4, &QulTask) != pdPASS) { Qul::PlatformInterface::log("Task creation failed!.\r\n"); configASSERT(false); } if (xTaskCreate(Led_Thread, "LedToggle", configMINIMAL_STACK_SIZE, 0, 4, &LedTask) != pdPASS) { Qul::PlatformInterface::log("LED task creation failed!.\r\n"); configASSERT(false); } if (xTaskCreate(FanControl_Thread, "FanControl", configMINIMAL_STACK_SIZE, 0, 4, &FanControlTask) != pdPASS) { Qul::PlatformInterface::log("Fan control task creation failed!.\r\n"); configASSERT(false); } vTaskStartScheduler(); // Should not reach this point return 1; }
前几个函数调用用于硬件初始化:
Qul::initPlatform(); BoardUtils::initLED();
首先通过Qul::initPlatform() 初始化开发板。BoardUtils::initLED() 函数用于初始化一块特定于该开发板的LED,使其闪烁。
if (xTaskCreate(Qul_Thread, "QulExec", QUL_STACK_SIZE, 0, 4, &QulTask) != pdPASS) { Qul::PlatformInterface::log("Task creation failed!.\r\n"); configASSERT(false); } if (xTaskCreate(Led_Thread, "LedToggle", configMINIMAL_STACK_SIZE, 0, 4, &LedTask) != pdPASS) { Qul::PlatformInterface::log("LED task creation failed!.\r\n"); configASSERT(false); } if (xTaskCreate(FanControl_Thread, "FanControl", configMINIMAL_STACK_SIZE, 0, 4, &FanControlTask) != pdPASS) { Qul::PlatformInterface::log("Fan control task creation failed!.\r\n"); configASSERT(false); }
使用xTaskCreate() 函数为Qt Quick Ultralite主循环、风扇控制线程和LED闪烁线程创建线程。所有这些线程均使用优先级4。Qt Quick Ultralite线程的栈大小为QUL_STACK_SIZE ,该值在FreeRTOSConfig.h 中被定义为32*1024个字。 LED 线程配置的栈大小为configMINIMAL_STACK_SIZE ,该值同样在FreeRTOSConfig.h 中配置,对于 STM32F769I-DISCOVERY 而言为 128 个字。有关xTaskCreate() 函数的详细信息,请参阅FreeRTOS API 参考文档中的 xTaskCreate 条目。
vTaskStartScheduler();
调用此函数将启动FreeRTOS 调度器,该调度器随后会调度先前创建的线程。请参阅FreeRTOS API参考文档中的vTaskStartScheduler。
在某些 Qt 许可证下可用。
了解更多信息。