Creating an application
Your first Zephyr application
Create a directory in your git working directory. Create the following two files.
In CMakeLists.txt
:
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr)
project(my_zephyr_app)
target_sources(app PRIVATE src/main.c)
In src/main.c
:
#include <stdio.h>
int main() {
printf("Hello, world\n");
return 0;
}
You also need to create a configuration file prj.conf
. In our case, we do not want to configure anything, so it can be empty:
$ touch prj.conf
We will now build the application for our target. In this case, we will generate code for a PC using an Intel CPU in bare-board mode and emulate the PC using the QEMU emulator.
Provided that west
is in your $PATH
and that $ZEPHYR_BASE
is properly set if needed, you should be able to build your application for the board qemu_x86
:
$ west build -b qemu_x86
(if you do not use west
, you can use the equivalent cmake -B build -DBOARD=qemu_x86 -GNinja && ninja -C build zephyr/zephyr.elf
)
This builds zephyr/zephyr.elf
into the build
directory. You should ignore this directory in your .gitignore
as its content should never be added to the git repository.
You can now run the application. You do not need to give the board again as it is remembered.
$ west build -t run
-- west build: running target run
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build v4.2.0-48-g116c4a9e4014 ***
Hello, world
(without west
: ninja -C build run
)
Congratulations: you just ran your first Zephyr application. You can quit qemu with ctrl-a x
(control key + a
, then x
), or simply kill the qemu process with ctrl-c
.
Adding a shell
Zephyr comes with an interactive shell. We want to add it to our project.
Modify prj.conf
at the top-level of your application and add the option CONFIG_SHELL
to enable the shell. By default, a shell backend using the serial port will be enabled. You can customize the prompt through the CONFIG_SHELL_PROMPT_UART
option in prj.conf
as well:
CONFIG_SHELL=y
CONFIG_SHELL_PROMPT_UART="(zephyr) "
We can now rebuild and run the application:
$ west build -t run
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: qemu32,+nx,+pae
SeaBIOS (version zephyr-v1.0.0-0-g31d4e0e-dirty-20200714_234759-fv-az50-zephyr)
Booting from ROM..
*** Booting Zephyr OS build v4.2.0-48-g116c4a9e4014 ***
Hello, world
(Zephyr)
Use the "help" command in the shell to see what you can do with it in its default configuration. Later, you will be able to select what commands should be made available through the shell, and you'll even be able to add your own commands.
Do not forget to add prj.conf
to git. Commit, then push.