C

Using Qt Quick Ultralite with FreeRTOS

FreeRTOS is a real-time operating system kernel designed for embedded devices and microcontroller platforms. It provides threads (tasks in FreeRTOS), mutexes, semaphores and software timers.

This guide tells you what is needed to start developing Qt Quick Ultralite with FreeRTOS, Qt Quick Ultralite + background information on FreeRTOS.

Supported architectures, platforms and FreeRTOS versions

Qt Quick Ultralite supports following hardware:

Hardware boardMCUArchitectureCompilerSupported FreeRTOS
EK-RA6M3GR7FA6M3AH3CFCARM Cortex-M4GNU Arm GCC 10.3-2021.10, IAR Build Tools for Arm V9.20FreeRTOS V10.4.6
NXP IMXRT1050-EVKBMIMXRT1052DVL6AARM Cortex-M7GNU Arm GCC 10.3-2021.10, IAR Build Tools for Arm V9.20FreeRTOS V10.0.1
NXP IMXRT1064-EVKMIMXRT1064DVL6AARM Cortex-M7GNU Arm GCC 10.3-2021.10, IAR Build Tools for Arm V9.20FreeRTOS V10.0.1
STM32F769I-DISCOVERYSTM32F769NIARM Cortex-M7GNU Arm GCC 10.3-2021.10, IAR Build Tools for Arm V9.20FreeRTOS V10.0.1
NXP IMXRT1170-EVKMIMXRT1176DVMAAARM Cortex-M7 and ARM Cortex-M4GNU Arm GCC 10.3-2021.10, IAR Build Tools for Arm V9.20FreeRTOS V10.0.1

These reference boards are supported under the Qt Standard Support.

Note: If you are using the precompiled platform library for a given hardware platform then part of the FreeRTOS sources are already compiled into it. If you would like to change the FreeRTOS version then the platform library has to be rebuilt.

Development environment requirements

Prerequisites

In order to build Qt Quick Ultralite for FreeRTOS, you'll need following things:

Environment setup

Based on the board you are using, set the platform-specific environment variables as defined in Getting started on NXP and Getting started on STM.

If you are using app_common you'll also need to setup the path to FreeRTOS sources:

-DFREERTOS_DIR=< FreeRTOS directory path >

When running cmake, use the FreeRTOS suffix for the platform name to generate FreeRTOS project build files.

Example:

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>

For per device environment setups on supported platforms, see hardware board links in Supported architectures, platforms and FreeRTOS versions.

How to use Qt Quick Ultralite on FreeRTOS

Obtaining Qt Quick Ultralite for FreeRTOS

For supported platforms the Qt Quick Ultralite installation comes with FreeRTOS support out of the box. To compile an example for FreeRTOS, see using app_common.

Starting Qt Quick Ultralite thread

You'll need two functions to be called to get Qt Quick Ultralite running on target:

  1. Qul::initPlatform();

    This function initializes the platform hardware and the operating system. This should be called as early as possible but at latest before Qul::appMain() is run or any device specific function is accessed.

    See Changing heap policies for information about different memory allocation implementations in FreeRTOS and how to use them in Qt Quick Ultralite projects.

  2. Qul::appMain();

    This function initializes and also acts as a main loop for Qt Quick Ultralite. In FreeRTOS, this should be run from a task.

For more information regarding setting up custom entry point, see Entry point to Qt Quick Ultralite application.

See also Example main.cpp for running Qt Quick Ultralite in a FreeRTOS task.

Providing memory allocator

FreeRTOS offers five different memory allocator implementations by default. They are located in FreeRTOS' MemMang directory and provide different strategies for heap management. See FreeRTOS developer docs, Memory management for more information about different implementations.

If you are using app_common in your project, you can change the used implementation by setting QUL_FREERTOS_HEAP_POLICY target property in your project. See Changing heap policies for more information.

The memory allocators provided with FreeRTOS implement pvPortMalloc and vPortFree. These allocators are used internally by the Qt Quick Ultralite platform ports and in most cases they should be used by application code as well. It is possible to configure separate heap areas for the standard library and the FreeRTOS allocators in the linker configuration.

One way of using FreeRTOS heap allocators in your application is to overload the default implementation of malloc, free, and realloc. If the new and delete C++ keywords are using malloc and free internally, your application need not provide separate overloads.

Example C code for memory allocation functions including realloc:

#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 is providing a default allocator override that can be enabled for your application in CMakeLists.txt by adding a property to your application:

qul_add_target(my_application …)
set_target_properties(my_application PROPERTIES FREERTOS_PROVIDE_MEMORY_ALLOCATOR_OVERLOADS TRUE)

The property needs to be set before calling app_target_setup_os. It adds C and C++ allocator overloads to the executable.

Note: The provided allocator overloads by Qt Quick Ultralite are not compatible with the FreeRTOS heap_3.c because its pvPortMalloc function is using malloc internally.

Note: Due to internal malloc calls, some functions such as printf, may allocate memory that might have already been allocated by FreeRTOS. This leads to unexpected behaviour, which can be avoided by enabling overloads in app_common.

Thread stack size

In FreeRTOS each individual thread (or task) has its own stack, The amount of stack Qt Quick Ultralite needs is largely dependent on the complexity of the project. For example, most examples and demos use a stack size of 32 kilowords.

Note: FreeRTOS defines stack size in words, not bytes.

Example main.cpp for running Qt Quick Ultralite in a FreeRTOS task

The following code shows how to create a basic FreeRTOS thread for Qt Quick Ultralite and run it.

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

Interacting with Qt Quick Ultralite from other applications

See FreeRTOS Multitasking Example and Integrating C++ code with QML.

Building FreeRTOS

See FreeRTOS application build process.

See FreeRTOS application build process for more information regarding app_common.

FreeRTOS Multitasking Example

Running multiple tasks with FreeRTOS and Qt Quick Ultralite.

This example demonstrates how to create multiple tasks that interact with each other. One task is running Qt Quick Ultralite while other is blinking LED on a device. The LED blinking speed is controlled from Qt Quick Ultralite thread.

FanControl Class

FanControl is a Qul::Singleton class that provides an interface for QML to interact with device's LED. The class handles LED blinking speed and communication between Qt Quick Ultralite and LED blinking tasks.

class FanControl : public Qul::Singleton<FanControl>
{
public:
    FanControl();
    Qul::Property<int> speed;
    void updateSpeed(int newSpeed);

private:
    void updateLedSpeed();
};

The LED blinking speed is updated when QML calls updateSpeed():

void FanControl::updateSpeed(int newSpeed)
{
    speed.setValue(newSpeed);
    updateLedSpeed();
}

Here we first set a new value to speed property. After that we call updateLedSpeed() which handles notificating LED blinking task of the speed change:

void FanControl::updateLedSpeed()
{
#ifndef NO_LED
    xTaskNotify(LedTask, speed.value(), eSetValueWithOverwrite);
#endif
}

The communication with LED blinking task is done by simply calling FreeRTOS' xTaskNotify() with the new speed value. The notification implementation is in main.cpp. For xTaskNotify() documentation, see FreeRTOS API Reference, xTaskNotify.

Main.qml

Main.qml declares the UI which is shown on device's screen.

import QtQuick 2.15

Rectangle {
    id: root

    Image {
        id: background
        source: "images/background-dark.png"
        anchors.fill: root
    }

    Row {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 10

        Text {
            color: "white"
            text: "Speed: "
            font.pixelSize: 30;
        }

        Text {
            color: "white"
            text: FanControl.speed
            font.pixelSize: 30;
        }
    }

    Text {
        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 {
                loops: Animation.Infinite
                from: 0
                to: 360
                duration: FanControl.speed === 0 ? 0 : 5000 / (FanControl.speed * 3)
                running: FanControl.speed > 0 ? true : false
            }
        }
    }

    MouseArea {
        id: ta
        anchors.fill: parent
        onPressed: {
            FanControl.updateSpeed((FanControl.speed + 1) % 6);
        }
    }
}

BoardUtils

BoardUtils is a namespace where we declare functions that initialize and control target board's LED:

namespace BoardUtils {
void initLED();
void toggleLED();
} // namespace BoardUtils

The definitions of these functions vary per device. The following example is from the implementation for STM32F769I-DISCOVERY.

namespace BoardUtils {
void initLED()
{
    BSP_LED_Init(LED1);
}

void toggleLED()
{
    BSP_LED_Toggle(LED1);
}
} // namespace BoardUtils

main.cpp

The main.cpp contains hardware initialization, creation of Qt Quick Ultralite and LED blinking tasks and the main loop of the program:

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

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

#include <board_utils/led.h>
#include "freertos_multitask.h"

static void Qul_Thread(void *argument);
static void Led_Thread(void *argument);

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

TaskHandle_t QulTask = NULL, LedTask = NULL;

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

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

    vTaskStartScheduler();

    // Should not reach this point
    return 1;
}

static void Qul_Thread(void *argument)
{
    (void) argument;
    Qul::Application app;
    static freertos_multitask item;
    app.setRootItem(&item);
    app.exec();
}

static void Led_Thread(void *argument)
{
    (void) argument;
    uint32_t speed = 1;
    uint32_t newSpeed = 0;
    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();
        taskYIELD();
    }
}

The first function calls are for hardware initialization:

    Qul::initPlatform();
    BoardUtils::initLED();

We first initialize the board with Qul::initPlatform(). The BoardUtils::initLED() function is used to initialize a board-specific LED for blinking.

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

We use function xTaskCreate() to create tasks for Qt Quick Ultralite main loop and LED blinking loop. Both tasks are using priority 4. The stack size of Qt Quick Ultralite task is QUL_STACK_SIZE which is defined in FreeRTOSConfig.h as 32*1024 words. Led task is configured to have a stack size of configMINIMAL_STACK_SIZE which is also configured in FreeRTOSConfig.h and is in STM32F769I-DISCOVERY's case 128 words. For detailed information about xTaskCreate() function, see FreeRTOS API Reference, xTaskCreate.

    vTaskStartScheduler();

Calling this starts the FreeRTOS scheduler which then schedules previously created threads. See FreeRTOS API Reference, vTaskStartScheduler.

static void Qul_Thread(void *argument)
{
    (void) argument;
    Qul::Application app;
    static freertos_multitask item;
    app.setRootItem(&item);
    app.exec();
}

Qul_Thread function defines contents of the Qt Quick Ultralite task. Qul::appMain initializes Qt Quick Ultralite and contains the main loop for Qt Quick Ultralite functionality.

static void Led_Thread(void *argument)
{
    (void) argument;
    uint32_t speed = 1;
    uint32_t newSpeed = 0;
    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();
        taskYIELD();
    }
}

Led_Thread defines contents of the LED blinking task. After initializing speed to 1 and newSpeed to 0, we enter an infinite loop where LED blinking occurs. First we determine the amount of ticks xTaskNotifyWait() waits for a notification before the LED is toggled using BoardUtils::toggleLED(). See FreeRTOS API Reference, xTaskNotifyWait.

Available under certain Qt licenses.
Find out more.