C

Qt Quick Ultralite freertos_app_switch Example

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

#include <qul/qul.h>
#include <qul/application.h>
#include <platforminterface/log.h>

#include "boardutils.h"
#include "appswitch.h"

#include <FreeRTOS.h>
#include <task.h>

static void App1_thread(void *argument);
static void App2_thread(void *argument);

#ifndef QUL_STACK_SIZE
#error QUL_STACK_SIZE must be defined.
#endif

TaskHandle_t QulTask = NULL, LedTask = NULL;

#define APP1 1
#define APP2 2
#define N_BLINKS 20

int main()
{
    Qul::initHardware();
    Qul::initPlatform();
    initLED();

    int boot = readBootValue();
    Qul::PlatformInterface::log("\r\nBootloader...\tbooting into application #%d\r\n", boot);

    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();

    // Should not reach this point
    return 1;
}

static void App1_thread(void *argument)
{
    (void) argument;

    Qul::Application app;
    static appswitch item;
    app.setRootItem(&item);
    app.exec();
}

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);
}