Running a Linux application on STM32 MCUs - Embedded.com

Running a Linux application on STM32 MCUs

More and more functionality is expected from embedded systems for smart products. This can be easily achieved with Linux, which offers software for every case you wish. But the Linux requires rather large hardware resources, most notably memory, so the target platform will typically be rather expensive and have high power consumption. On the other hand, modern microcontrollers (MCUs) have a lot of resources that are enough for many smart tasks. Using a real-time operating system (RTOS) such as Embox RTOS, which allows use of Linux software everywhere including MCUs, developers can more easily develop smart devices that are less expensive and consume less power.

In this article, we discuss launching a basic Qt application on an embedded MCU running the Embox RTOS. We use two single-core Cortex-M7 based boards (216MHz): STM32F746g-Discovery and STM32F769i-Discovery, which have touchscreens with 480×272 and 800×480 screen resolution, respectively.

Some time ago we managed to launch a standard Qt example ‘moveblocks’ on STM32F746-Discovery [Note 1]. However we wanted to go further and launch an example with a touchscreen.We choose another standard example, ‘animatedtiles’. It is a rather nice example which looks cool even on desktops. It is interactive therefore we can use a touchscreen and also it looks like 3D animation.

We wanted to learn whether the STM32 MCU can provide enough performance. We were not sure if the CPU’s speed would be enough for that rendering scene to be smooth, since as we found out, even screen refreshing for an 800×480 display is an expensive operation [Note 2].

Building

First of all, let’s build our application as a part of Embox. The simplest way to do this is to build the application for QEMU, which is an open source emulator. Using QEMU means that we do not need to take care of memory consumption in this case. This will help us to make sure that all the necessary components are available. When ‘animatedtiles’ had started on QEMU, we easily added necessary components to config files for the STM32.

First launch on the target board

We successfully started ‘animatedtiles‘ on our target board STM32F746G-Discovery, but we noticed that only the top half of the screen was drawn. Of course, we could try to solve the trouble through debugging immediately on the board but there is an easier way: We can start and debug the example in the Linux environment first.

Debugging on host

To run this example and debug your application, you need the Qt library itself as well as QVFB, which is a Qt application providing a virtual screen. You can build QVFB as described in the official documentation.

Then you can launch QVFB with required screen resolutions (480×272 for STM32F746G-Discovery):

./qvfb -width 480 -height 272 -nocursor 

You also should build Qt library with an embedded profile, namely with ‘-embedded’ option. This option is designed to run Qt without X11 or existing graphical environments, QWS (Qt Windowing System) will be used instead. Also we disabled some other unnecessary options and modules. The resulting configuration line was the following:

./configure -opensource -confirm-license -debug \
            -embedded -qt-gfx-qvfb -qvfb \
            -no-javascript-jit -no-script -no-scripttools \
            -no-qt3support -no-webkit -nomake demos -nomake examples 

Finally, let’s build and start ‘animatedtiles’. The build stage is as usual for Qt (qmake; make;). You should specify QVFB as a display when starting the application:

./examples/animation/animatedtiles/animatedtiles -qws -display QVFb:0

We got the same picture in the top half of the screen. We found in the source code (examples/animation/animatedtiles/main.cpp) that the application starts with “view->show()”, that means not in the full-screen mode. After that, we were entirely convinced that the only top half of the screen was used to display the application. We modified the code of ‘animatedtiles’ a little, adding the ‘-fullscreen’ option.

Launching on STM32F746-discovery

We built our modified application in Embox and checked that it successfully ran on QEMU. But when we were launching the application on the target board we received the “Out Of Memory” error. It means that the size of the heap in our config is not enough for the application. We decided to use rich development tools in Linux to solve the trouble i.e. estimate required heap size.

We had started our application with valgrind. Valgrind is a tool for memory debugging and profiling, Massif is a heap profiler (it’s a part of Valgrind).

$ valgrind --tool=massif --massif-out-file=animatedtiles.massif./examples/animation/animatedtiles/animatedtiles -qws -fullscreen
$ ms_print animatedtiles.massif > animatedtiles.out

And found out that the application needed a heap size of about 2.7 MB heap size.

We set up the heap size to 3 MB in Embox config and started our demo again. It was successfully running in the fullscreen mode.

Launching on STM32F769I-Discovery.

We also wanted to try the example on a similar board but with a bigger display. This example used the  STM32F769I-Discovery with 800×480 display resolutions. In this case, it requires almost 1MB only for the framebuffer in 16bits colour mode (800x480x2=768000), but there is enough (16MB) external SDRAM on this board. So we only have to estimate the size of the heap.

We started QVFb with the new resolutions (800×480):

$./qvfb -width 800 -height 480 -nocursor & 

And use valgrind as the previous time:

$ valgrind --tool=massif --massif-out-file=animatedtiles.massif./examples/animation/animatedtiles/animatedtiles -qws -fullscreen
$ ms_print animatedtiles.massif > animatedtiles.out

We had found out that it needs about 6 MB. We had set up 6 MB heap size and ‘animatedtimes’ successfully launched on STM32F769I board.


Figure 1: QT animatedtiles example on STM32F769I-DISCOVERY board under Embox RTOS. (Source: Embox)

You can see how it works in this video (below) and you can reproduce the results yourself as described on our github wiki.

Notes:

  1. https://habr.com/ru/company/embox/blog/459730/] (in Russian, but also available in a third-party English translation: https://sudonull.com/post/25508-Porting-Qt-to-STM32-Embox-Blog
  2. https://alexkalmuk.medium.com/a-little-about-graphics-subsystem-internals-on-microcontrollers-d952cfd0966a

Anton Bondarev is the founder of Embox RTOS. Anton graduated from Saint Petersburg Electrotechnical University (LETI) in 2003 with a master degree in electrical engineering and attended postgraduate courses in Saint-Petersburg State University specializing in software engineering. He has over 20 years in embedded and system programming.
Alexander Kalmuk is the cofounder of Embox RTOS. Alexander graduated from Saint-Petersburg State University in 2014 with a master degree in mathematics and software engineering and attended postgraduate courses in Saint-Petersburg State University specializing in control theory. He has over 10 years in embedded systems programming.

Related Contents:

For more Embedded, subscribe to Embedded’s weekly email newsletter.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.