Yocto Stm32 Labs
Yocto Stm32 Labs
STM32MP1 variant
Practical Labs
https://bootlin.com
Training setup
Download files and directories used in practical labs
Lab data are now available in an yocto-stm32-labs directory in your home directory. This
directory contains directories and files used in the various practical labs. It will also be used as
working space, in particular to keep generated files separate when needed.
More guidelines
Can be useful throughout any of the labs
• Read instructions and tips carefully. Lots of people make mistakes or waste time because
they missed an explanation or a guideline.
1 This tool from Microsoft is Open Source! To try it on Ubuntu: sudo snap install code --classic
• Always read error messages carefully, in particular the first one which is issued. Some
people stumble on very simple errors just because they specified a wrong file path and
didn’t pay enough attention to the corresponding error message.
• Never stay stuck with a strange problem more than 5 minutes. Show your problem to
your colleagues or to the instructor.
• You should only use the root user for operations that require super-user privileges, such
as: mounting a file system, loading a kernel module, changing file ownership, configuring
the network. Most regular tasks (such as downloading, extracting sources, compiling...)
can be done as a regular user.
• If you ran commands from a root shell by mistake, your regular user may no longer be
able to handle the corresponding generated files. In this case, use the chown -R command
to give the new files back to your regular user.
Example: $ sudo chown -R myuser.myuser linux/
Setup
Before starting this lab, make sure your home directory is not encrypted using eCryptFS. Open-
Embedded cannot be used on top of an eCryptFS file system due to limitations in file name
lengths.
Go to the $HOME/yocto-stm32-labs/ directory.
Install the required packages:
sudo apt install bc build-essential chrpath cpio diffstat gawk git python texinfo wget gdisk
Download Yocto
Download the kirkstone version of Poky:
git clone https://git.yoctoproject.org/git/poky
cd $HOME/yocto-stm32-labs/poky
git checkout -b kirkstone-4.0.2 kirkstone-4.0.2
Return to your project root directory (cd $HOME/yocto-stm32-labs/) and download the Open-
Embedded and STM32MP layers:
git clone -b kirkstone https://git.openembedded.org/meta-openembedded
git clone https://github.com/STMicroelectronics/meta-st-stm32mp
cd meta-st-stm32mp
git checkout openstlinux-5.15-yocto-kirkstone-mp1-v22.06.15
Also, if you need to save disk space on your computer you can add INHERIT += "rm_work" in
the previous configuration file. This will remove the package work directory once a package is
built.
Don’t forget to make the configuration aware of the OpenEmbedded and STM32MP layers.
Edit the layer configuration file ($BUILDDIR/conf/bblayers.conf) and append the full path to
the meta-openembedded/meta-oe, meta-openembedded/meta-python and meta-st-stm32mp di-
rectory to the BBLAYERS variable.
Boot
Insert the SD card in the dedicated slot on the Discovery. You can now power-up the board by
connecting the USB-C cable to the board, in CN6, PWR_IN and to your PC at the other end.
You should see boot messages on the console. Wait until the login prompt, then enter root as
user. Congratulations! The board has booted and you now have a shell.
We can force bitbake to select another kernel by explicitly defining which one to use in our
local configuration. Try switching from linux-stm32mp to linux-dummy only using the local
configuration.
Then check the previous step worked by dry-running again BitBake.
BitBake tips
BitBake is a powerful tool which can be used to execute specific commands. Here is a list of
some useful ones, used with the virtual/kernel package.
• The Yocto recipes are divided into numerous tasks, you can print them by using: bitbake
-c listtasks virtual/kernel.
• BitBake allows to call a specific task only (and its dependencies) with: bitbake -c <task>
virtual/kernel. (<task> can be menuconfig here).
• You can force to rebuild a package by calling: bitbake -f virtual/kernel
• world is a special keyword for all packages. bitbake --runall=fetch world will download
all packages sources (and their dependencies).
• You can get a list of locally available packages and their current version with:
bitbake -s
• You can also find detailed information on available packages, their current version, depen-
dencies or the contact information of the maintainer by visiting:
http://recipes.yoctoproject.org/
For detailed information, please run bitbake -h
Going further
If you have some time left, let’s improve our setup to use TFTP, in order to avoid having to
reflash the SD card for every test. What you need to do is:
1. Install a TFTP server (package tftpd-hpa) on your system.
2. Copy the Linux kernel image and Device Tree to the TFTP server home directory (specified
in /etc/default/tftpd-hpa) so that they are made available by the TFTP server.
3. Change the U-Boot bootcmd to load the kernel image and the Device Tree over TFTP.
See the training materials of our Embedded Linux system development course for details!
bitbake ninvaders
Try to make the recipe on your own. Also eliminate the warnings related to your recipe: some
configuration variables are not mandatory but it is a very good practice to define them all.
If you hang on a problem, check the following points:
• The common recipe is included in the version specific one
• The checksum and the URI are valid
• The dependencies are explicitly defined
• The internal state has changed, clean the working directory:
bitbake -c cleanall ninvaders
One of the build failures you will face will generate many messages such as multiple definition
of `skill_level'; aliens.o:(.bss+0x674): first defined here.
The multiple definition issue is due to the code base of nInvaders being quite old, and
having multiple compilation units redefine the same symbols. While this was accepted by older
gcc versions, since gcc 10 this is no longer accepted by default.
While we could fix the nInvaders code base, we will take a different route: ask gcc to behave as
it did before gcc 10 and accept such redefinitions. This can be done by passing the -fcommon
gcc flag.
To achieve this, make sure to add -fcommon to the CFLAGS variable.
Tip: BitBake has command line flags to increase its verbosity and activate debug outputs. Also,
remember that you need to cross-compile nInvaders for ARM! Maybe, you will have to configure
your recipe to resolve some mistakes done in the application’s Makefile (which is often the case).
A bitbake variable permits to add some Makefile’s options, you should look for it.
Tools
You can access the configuration and state of layers with the bitbake-layers command. This
command can also be used to retrieve useful information about available recipes. Try the
following commands:
bitbake-layers show-layers
bitbake-layers show-recipes linux-yocto
bitbake-layers show-overlayed
bitbake-layers create-layer
Try adding the patches included in this lab to your BitBake append file. Do not forget to also add
the defconfig file provided alongside the patches. This file contains the kernel configuration.
Add KERNEL_DEFCONFIG = "" and KERNEL_EXTERNAL_DEFCONFIG = "defconfig" so it is used by
the linux-stm32mp recipe.
You can now rebuild the kernel to take the new patches into account:
bitbake virtual/kernel
We will connect it to the second I2C port of the CPU (i2c1), with pins available on the P9
connector.
SCL
PWR
GND SDA
Patch nInvaders
The nInvaders game uses keyboard events for its controls. We first need to apply a patch
introducing joystick support. The patch is located at ~/yocto-stm32-labs/bootlin-lab-data/
nunchuk/ninvaders/.
Add the patch to the nInvaders SRC_URI.
Then build a full core-image-minimal and update the NFS root directory.
Play nInvaders!
After booting the board you should be able to play nInvaders with the keyboard…and the
Nunchuk! The C button is used to confirm and to fire, and Z to pause the game.
Access the board command line through SSH, and launch the game:
$ ninvaders
DEFAULTTUNE = "cortexa7thf-neon-vfpv4"
require conf/machine/include/arm/armv7a/tune-cortexa7.inc
tar xf ctris-0.42-1-src.tar.bz2
tar xf ctris-0.42.tar.bz2
cd ctris-0.42
Then modify the Makefile, to make sure that the environment variables exported by the SDK
script are not overridden.
Try to compile the application. Just like nInvaders, ctris is also an old program and won’t build
with a recent toolchain. You will face these errors:
• The ctris makefile uses the native compiler, not the cross compiler provided by the SDK;
while you could fix it using make -e as done for nInvaders, try fixing it by editing the
Makefile this time; hint: you don’t need to write any code, just to delete two lines
• Building with a recent GCC will give the following error, not reported by older versions:
error: format not a string literal and no format arguments [-Werror=format-security]
Fix this by adding -Wno-error=format-security to CFLAGS
• As for nInvaders, you will see the multiple definition of... error; add the -fcommon
flag to CFLAGS also for ctris
You can check the application was successfully compiled for the right target by using the file
command. The ctris binary should be an ELF 32-bit LSB executable compiled for ARM.
Finally, you can copy the binary to the board, by using the scp command. Then run it and
play a bit to ensure it is working fine!