Implementing basic functions

Implementing basic functions

This chapter lists the basic functions that must be implemented to run Qt Quick Ultralite core in general. You can take the contents of platform/boards/qt/example-baremetal/platform.cpp as a basis for your own implementation and add your code at the mentioned locations.

Initializing hardware

Initialization of hardware components can be done in the Qul::Platform::initializeHardware function. It is called after initializing static variables and classes but before any graphics rendering. Hardware that needs early initialization need to be handled by the BSP before entering main(). This function can be used for peripherals that are needed after startup.

void initializeHardware()
#if 0
    Replace the following code with actual code for your device.


The resource system of Qt Quick Ultralite expects assets marked as preloadable, such as fonts and graphics, to be preloaded before starting the main loop. (See the Qt Quick Ultralite documentation on how to configure the asset preloading.) For this purpose the linker script defines symbols containing the source and destination addresses of the preloadable assets. The copy can be performed using DMA or as shown in the following example, using memcpy:

// Preloading assets
extern unsigned char _preloadable_assetdata_src;
extern unsigned char _preloadable_assetdata_dst_begin;
extern unsigned char _preloadable_assetdata_dst_end;

       &_preloadable_assetdata_dst_end - &_preloadable_assetdata_dst_begin);

Random numbers

To provide random numbers to QML applications, the platform requires a function to be implemented.

double rand()
    // Replace std::rand() by the proper call to the random number generator on your device, if available.
    const uint32_t number = std::rand();
    return number / (std::numeric_limits<uint32_t>::max() + 1.0);

Current timestamp

An essential part for rendering and timers is getting the current timestamp of the system. The function Qul::Platform::currentTimestamp has to return this system time.

uint64_t currentTimestamp()
    // Replace this line to make it return the system timestamp in milliseconds.
    return 0;

Renderloop callback schedule

The render loop and event processing of Qt Quick Ultralite must be called at certain times. The Qt Quick Ultralite core will notify the platform about the next timestamp it requires to be called back to perform tasks by calling Qul::Platform::scheduleEngineUpdate.

static uint64_t nextUpdate = 0;

void scheduleEngineUpdate(uint64_t timestamp)
    nextUpdate = timestamp;

The timestamp for the next required update is stored in a single variable. You may implement scheduling timeouts using hardware timers or OS-provided timers, in case your platform provides these.

Note: The implementation of Qul::Platform::scheduleEngineUpdate can get called from an interrupt and it must be safe for running within an interrupt context.

Main loop

Qt Quick Ultralite does not provide a main loop, but requires a custom main loop that calls the core engine at a given time.

The exec loop runs forever, or at least for as long as the application is running, and is responsible for calling Qul::PlatformInterface::updateEngine at appropriate times. When no update calls are required by the core engine, the device may enter a sleep state if possible.

The main loop has to use the timestamp set by the Qt Quick Ultralite core engine, as described in the previous section. A timestamp lesser than the current timestamp, or even 0, should result in calling Qul::PlatformInterface::updateEngine as soon as possible. A greater timestamp value than the current timestamp means that the platform implementation should call Qul::PlatformInterface::updateEngine at that given time. Until the scheduled time, the device may enter a sleep mode. Qul::PlatformInterface::updateEngine should not be called earlier than required.

void exec()
    while (true) {
        const uint64_t timestamp = currentTimestamp();

        if (timestamp >= nextUpdate) {
            // Handle deadline or pending events
        } else {
            // The core library has no pending actions.
            // The device may go to a sleep mode.

            // wait_for_interrupt(nextUpdate - timestamp);

You must replace the wait_for_interrupt() call with an appropriate call on your platform. The used function for sleeping must be able to return in case an event, like a touch event, was delivered to the Qt Quick Ultralite core from an interrupt, or the time for the next scheduled update is reached.

Note: In case a sleep or powersave is not implemented, the CPU will always run at full workload.

Available under certain Qt licenses.
Find out more.