Post

ENG | Zephyr RTOS setup: first projects

First steps with Zephyr OS. Work in progress

ENG | Zephyr RTOS setup: first projects

Hello world

Project structure

The minimal project consists of these files:

1
2
3
4
5
6
7
#include <stdio.h>

int main(void)
{
  printf("Hello world\n");
  return 0;
}

Nothing Zephyr-specific here.

NOTE: make sure you won’t name file main.cpp but main.c. It’s automatic for me and I’m completely blind when CMake cannot find it 🤦‍♂️

1
2
3
4
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)
target_sources(app PRIVATE src/main.c)

This is pretty standard CMakeLists.txt file. It looks for Zephyr, preferably in directory set by ZEPHYR_BASE environment variable, which is hopefully set by virtual environment.

1
# Intentionally empty

Here are usually some drivers, but we don’t need any.

Building project

Linux/Native

1
2
3
4
5
6
7
8
9
10
[pavel@marten -=- /home/pavel]$ cd zephyrproject
[pavel@marten -=- /home/pavel/zephyrproject]$ source .venv/bin/activate
(.venv) [pavel@marten -=- /home/pavel/zephyrproject]$ cd my_projects 
(.venv) [pavel@marten -=- /home/pavel/zephyrproject/my_projects]$ west build -p -b native_sim/native/64 hello_world
(.venv) [pavel@marten -=- /home/pavel/zephyrproject/my_projects]$ build/zephyr/zephyr.exe 
*** Booting Zephyr OS build v4.1.0-5582-g2c5c0e24e8fd ***
Hello world

^C
Stopped at 6.030s

Windows/Native

Ehm, sorry. In theory west build --pristine --board native_sim/native/64 my_projects/hello_world should work. But there is no gcc which targets Windows/x64 or Windows/x86 platforms.

Windows->nRF52840 dongle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PS C:\Users\pavel> cd C:\dev-zephyr\zephyrproject\
PS C:\dev-zephyr\zephyrproject> .\.venv\Scripts\activate
(.venv) PS C:\dev-zephyr\zephyrproject> west build --pristine --board nrf52840dongle my_projects/hello_world
(.venv) PS C:\dev-zephyr\zephyrproject> C:\apps\nrfutil.exe pkg generate --hw-version 52 --sd-req=0x00 --application build\zephyr\zephyr.hex --application-version 1 hello_world.zip
Zip created at hello_world.zip
(.venv) PS C:\dev-zephyr\zephyrproject> c:\apps\nrfutil.exe dfu usb-serial -pkg .\hello_world.zip -p COM7
  [####################################]  100%
Device programmed.
(.venv) PS C:\dev-zephyr\zephyrproject> mpremote connect COM6
Connected to MicroPython at COM6
Use Ctrl-] or Ctrl-x to exit this shell
*** Booting Zephyr OS build v4.1.0-5418-g98ba754013b0 ***
Hello world
(.venv) PS C:\dev-zephyr\zephyrproject>

Yes, I’m aware than device is once COM7 and once COM6. This is not a bug - it’s a feature of Windows. Don’t ask me, it really uses different port in bootloader mode and when running. I guess it might be due to different VID:PID (USB vendor/product IDs) so for Windows, it’s a different device.

I’m also aware that mpremote is part of MicroPython installed by pip install mpremote, but it’s convenient.

TODO: RTC (supported)

HTU21 I2C temperature and humidity sensor (unsupported)

This post is licensed under CC BY 4.0 by the author.