There are many occasions when we want to automatically run an application directly following the boot of our embedded Linux solution. Doing this is very simple, however, it does require a little work within PetaLinux.
In this blog, we are going to walk through how to do this using the MicroZed and PetaLinux 2023.2.
First we need to create a hardware design in Vivado. This block design will be very simple because it has PS has a LED connected which we can blink. Therefore, the Vivado block diagram will be a simple instantiation of the Zynq 7000 processing system configured for the MicroZed.
We’ve done this several times over the years, however, the images below highlight a step-by-step method to creating the hardware design element.
Once the bitstream has been generated, the next step is to create a PetaLinux project. For this, we will need a Linux machine or Linux virtual machine. Check out my Hackster project if you are unsure how to create a Linux virtual machine.
We need to create a new PetaLinux project in the Linux environment. This can be done by using the command below which created a project targeting the Zynq.
petalinux-create -t project -n mz531 --template zynq
With the project created, we can change the directory into the newly created project and then copy and paste the XSA export from Vivado into this directory. Running the command below will import the XSA into the PetaLinux project and configure it for the project we have just created.
petalinux-config --get-hw-description=.
Close the dialog which opens automatically and save the configuration when prompted. Do not make any changes.
Next, we need to configure the root file system to auto login. Once the Linux solution has booted, use the command below.
petalinux-config -c rootfs
When the dialog opens, enable the option serial-autologin-root under the image features option. This will enable the serial terminal to auto login which is quite useful when first working with RAM-based file systems because it removes the need to keep changing the password each time the system boots.
The next step is to create an application using the following command:
petalinux-create -t apps -n mz531app –enable
This will create a new application under the meta-user recipes-apps directory.
Here you will see a BitBake recipe and a directory which contains an example C file and makefile.
You will see a simple hello world application by opening the C file which I modified to include a reference to the blog issue number. This is the application we are going to get to auto run.
For this example, we do not need to make any more changes to source files. You might, however, want to explore the makefile and the BitBake recipe to understand what they are doing.
The next step is to create a new application which uses a template that has a pre-built binary.
petalinux-create -t apps --template install -n mz531-init --enable
Again, this will create an application under the met-user recipes-apps area. This time, however, you will see a script under the files area.
We are going to make some changes to the script and the BitBake recipe and create a new service.
Creating the service is the first thing we are going to do by building a new file in the files directory called mz531-init.service and adding in the following:
[Unit]
Description=mz531-init
[Service]
ExecStart=/usr/bin/mz531-init
StandardOutput=journal+console
[Install]
WantedBy=multi-user.target
Then we are going to edit the script to wait for 30 seconds and call the application we have created.
#!/bin/sh
sleep 30s
echo "Hello PetaLinux World"
mz531app
Finally, we are going to edit the BitBake recipe as below:
#
# This file is the mz531-init recipe.
#
SUMMARY = "Simple mz531-init application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://mz531-init \
"
S = "${WORKDIR}"
FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
inherit update-rc.d systemd
INITSCRIPT_NAME = "mz531-init"
INITSCRIPT_PARAMS = "start 99 S ."
SYSTEMD_PACKAGES = "${PN}"
SYSTEMD_SERVICE:${PN} = "mz531-init.service"
SYSTEMD_AUTO_ENABLE:${PN} = "enable"
do_install() {
if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then
install -d ${D}${sysconfdir}/init.d/
install -m 0755 ${WORKDIR}/mz531-init ${D}${sysconfdir}/init.d/
fi
install -d ${D}${bindir}
install -m 0755 ${WORKDIR}/mz531-init ${D}${bindir}/
install -d ${D}${systemd_system_unitdir}
install -m 0644 ${WORKDIR}/mz531-init.service ${D}${systemd_system_unitdir}
}
FILES:${PN} += "${@bb.utils.contains('DISTRO_FEATURES','sysvinit','${sysconfdir}/*', '', d)}"
With that completed, we can build PetaLinux using the command:
petalinux-build
Once the build has completed, we change directory into the image/linux directory within the project and run the command to create a boot.bin:
petalinux-package --boot --fsbl zynq_fsbl.elf --u-boot u-boot.elf --fpga system.bit –force
Copy the boot.bin boot.scr and image.ub onto the SD card, ensure the MicroZed is set to boot from SD and power on the board.
By observing the serial terminal output in a terminal you will notice the message from the script and from the C application that we created earlier.
We can use this approach to set up our system and automatically run applications etc. We may also want to modify the service to start the script later.
Workshops and Webinars
If you enjoyed the blog why not take a look at the free webinars, workshops and training courses we have created over the years. Highlights include
Professional PYNQ Learn how to use PYNQ in your developments
Introduction to Vivado learn how to use AMD Vivado
Ultra96, MiniZed & ZU1 three day course looking at HW, SW and Petalinux
Arty Z7-20 Class looking at HW, SW and Petalinux
Mastering MicroBlaze learn how to create MicroBlaze solutions
HLS Hero Workshop learn how to create High Level Synthesis based solutions
Perfecting Petalinux learn how to create and work with petalinux OS
Embedded System Book
Do you want to know more about designing embedded systems from scratch? Check out our book on creating embedded systems. This book will walk you through all the stages of requirements, architecture, component selection, schematics, layout, and FPGA / software design. We designed and manufactured the board at the heart of the book! The schematics and layout are available in Altium here Learn more about the board (see previous blogs on Bring up, DDR validation, USB, Sensors) and view the schematics here.
Sponsored by AMD
Comments