C
Qt Quick Ultralite freertos_app_switch 示例
演示如何通过软重置在Qt Quick 的Ultralite应用程序与另一个应用程序之间进行切换。
概述
freertos_app_switch 演示了如何通过软复位以及一个在两次软复位之间保存其状态的内存位置,在Qt Quick Ultralite应用程序与另一个应用程序之间进行切换。本示例还展示了如何重启Qt Quick Ultralite应用程序,从而重新触发该应用程序的初始化步骤。
启动时使用一个特定的内存地址在两个应用程序之间进行选择:
Application #1是Qt Quick Ultralite应用程序本身,为引导选择机制提供用户界面。
Application #2是另一个应用程序,它直接向帧缓冲区写入数据,使 LED 闪烁几秒钟,然后将设备重启至第一个应用程序。
注意: 使用软重置时请务必谨慎 。请确保硬件外设能够被正确重新初始化。某些外设可能需要重置信号,或者必须处于特定状态后才能进行编程。
注意: 此处介绍的相同重置机制 可加以调整,以使用硬重置(触发电源循环)和非易失性存储器来保存状态。
目标平台
代码概述
应用程序入口点
main() 函数负责硬件和平台的初始化,此外,它还使用特定于该板的InitLED() 函数对LED进行初始化。
... int main() { Qul::initHardware(); Qul::initPlatform(); initLED(); ...
接下来,从内存中读取boot 的值。
int boot = readBootValue();
其中,readBootValue() 方法只是从特定的内存位置读取数据,并在boardutils.c 中定义。
int readBootValue() { int *ptr = &__BOOT_VAL_ADDR; return *ptr; }
boot 变量的地址可通过board.cmake 文件中由链接器定义的符号进行设置。
set(BOOT_VAL_ADDR 0x81400000)
if (IAR)
target_link_options(freertos_app_switch PRIVATE --define_symbol __BOOT_VAL_ADDR=${BOOT_VAL_ADDR})
else()
target_link_options(freertos_app_switch PRIVATE -Xlinker --defsym=__BOOT_VAL_ADDR=${BOOT_VAL_ADDR})
endif()根据boot 的值,将创建一个不同的FreeRTOS 任务。该任务要么是运行Application #1 的Qt Quick Ultralite 任务,要么是实现Application #2 的另一个任务。
if (boot != APP2) { Qul::PlatformInterface::log("Application #1...QUL Application Switch\r\n"); if (xTaskCreate(App1_thread, "QulExec", QUL_STACK_SIZE, 0, 4, &QulTask) != pdPASS) { Qul::PlatformInterface::log("Task creation failed!\r\n"); configASSERT(false); } } else { Qul::PlatformInterface::log("Application #2...blinking LED\r\n"); if (xTaskCreate(App2_thread, "LedToggle", configMINIMAL_STACK_SIZE, 0, 4, &LedTask) != pdPASS) { Qul::PlatformInterface::log("LED task creation failed!\r\n"); configASSERT(false); } } vTaskStartScheduler(); ...
运行Qt Quick Ultralite 的Application #1 任务仅需设置应用程序的根项并调用exec() 函数。
static void App1_thread(void *argument) { (void) argument; Qul::Application app; static appswitch item; app.setRootItem(&item); app.exec(); }
Application #2 任务通过displayApp2Background() 将原始图像直接写入帧缓冲区,然后让LED闪烁几个周期。
static void App2_thread(void *argument) { (void) argument; displayApp2Background(); //Blink for N_BLINKS times then reboot into App1 for (int i = 0; i < N_BLINKS; ++i) { xTaskNotifyWait(0, ULONG_MAX, NULL, pdMS_TO_TICKS(500)); toggleLED(); taskYIELD(); } reset(APP1); }
QML
Qt Quick Ultralite 的用户界面在freertos_app_switch.qml 中实现。通过两个单选按钮选择下次启动时要执行的应用程序,重置按钮则触发软重置。
...
Button {
id: resetButton
text: "Reset!"
anchors.centerIn: parent
onClicked: {
console.log("Chosen application #", DevControl.application, "...Resetting system!")
DevControl.resetDevice();
}
}
...DevControl 对象用作 UI 与抽象化硬件之间的接口。
void DevControl::HWResetDevice() { reset(application.value()); }
公共方法resetDevice() 可将Qul::Property<int> application 的值写入启动内存位置,随后通过NVIC_SystemReset() 触发软重置。
void writeBootValue(int app) { int *ptr = &__BOOT_VAL_ADDR; *ptr = app; QUL_CleanInvalidateDCache_by_Addr((void *) ptr, 4); } void reset(int app) { writeBootValue(app); NVIC_SystemReset(); }
注意:在 NXP i.MX RT1170 刷写二进制文件后,应立即对设备进行一次重置。否则,通过NVIC_SystemReset() 进行的软重置可能无法正常工作。
文件:
- freertos_app_switch/CMakeLists.txt
- freertos_app_switch/appswitch.qml
- freertos_app_switch/mcu_freertos_app_switch.qmlproject
- freertos_app_switch/src/boardutils.h
- freertos_app_switch/src/devcontrol.cpp
- freertos_app_switch/src/devcontrol.h
- freertos_app_switch/src/main.cpp
- freertos_app_switch/src/mimxrt1050-evk-freertos/bg_image.h
- freertos_app_switch/src/mimxrt1050-evk-freertos/boardutils.c
- freertos_app_switch/src/mimxrt1064-evk-freertos/bg_image.h
- freertos_app_switch/src/mimxrt1064-evk-freertos/boardutils.c
- freertos_app_switch/src/mimxrt1170-evkb-freertos/bg_image.h
- freertos_app_switch/src/mimxrt1170-evkb-freertos/boardutils.c
- freertos_app_switch/src/mimxrt1170-evkb-freertos/qul_display_helpers.cpp
- freertos_app_switch/src/mimxrt1170-evkb-freertos/qul_display_helpers.h
- freertos_app_switch/src/stm32f769i-discovery-freertos/bg_image.h
- freertos_app_switch/src/stm32f769i-discovery-freertos/boardutils.c
另请参阅 FreeRTOS 应用程序的构建流程。
在某些 Qt 许可下提供。
了解更多。