Skip to content

CSSE4011: Tute 1 - Getting Started with Zephyr RTOS

1.0 Zephyr RTOS: Overview

Zephyr Real-Time Operating System (RTOS) managed by the Linux foundation, is designed for connected and resource constrained embedded devices. Unlike many other real-time operating systems (like free-RTOS), Zephyr is more than just a kernel, it is a software eco-system. Zephyr integrates a broad range of functionality such as:

  1. Bluetooth
  2. LoRaWan
  3. USB / Serial Console / Shell(CLI)
  4. SPI/I2C/UART
  5. Flash Drivers
  6. Filesystems

and more... Depending on what particular feature your application might require, Zephyr allows you to build a program using only the necessary functionality (i.e BLE), this can optimize the overall footprint of binary. This is done using config file(s) that we will cover in the coming tutorials.

1.1 Zephyr Kernel Overview

The Zephyr kernel is similar to that of free-RTOS at a high level, in that it is primarily concerned with task-scheduling, inter-task communication and synchronization.

  1. Zephyr offers a pre-emptive scheduler, that is, the code context can change at anytime.
  2. Thread based, where 'threads' mean the same thing as a 'task' in free-RTOS.
  3. It is 'Soft' real-time provided by system ticks

1.2 Zephyr West Meta-Tool

Zephyr includes a command line tool named west. This tool provides a multiple repository management system, and Zephyr uses west to provide conveniences for building, flashing, debugging and cleaning applications. For example, to build a program you could use,

west build
This will look at predefined build configuration settings and build an application as specified. The west meta-tool can be used to perform many other functions, see here for a detailed breakdown of it's features. In this course, you will be using west to build, flash and maintain your workspaces.

1.3 Zephyr Documentation

The Zephyr documentation is well written and contains all the intricate details about Zephyr and it's features. Throughout this course and application development, you will need to refer back to these documentation to understand;

  1. The Build and Configuration Systems
  2. Device Tree (A way of describing hardware config to the OS)
  3. Application Development (App config options/Kernel config)
  4. API Usage
  5. Using an Interactive Kconfig interfaces (Optional)

Take some time to read through these documents to get an idea of how Zephyr is implemented. This will be useful later when you are required to implement advanced features using Zephyr.

2.0 Installing Zephyr RTOS

The following guide is made in reference to the Zephyr "Getting Started Guide" documentation, and is intended to be used on a Debian based linux distribution with the "apt" (package manager installed).

First update the system and any installed packages on your VM

sudo apt update 
sudo apt full-upgrade
Reboot (If this was a fresh OS install, typically kernel is upgraded)
sudo reboot
[Skip kitware for Debian: This is for Ubuntu] Update sources list by adding the Kitware APT repo (contains cmake etc...)
cd ~  

wget https://apt.kitware.com/kitware-archive.sh
sudo bash kitware-archive.sh
sudo rm bash kitware-archive.sh                                   - Remove script

Install cmake, python3 and dtc (device tree compiler)

sudo apt install --no-install-recommends git cmake ninja-build gperf \
        ccache dfu-util device-tree-compiler wget \
        python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \
        make gcc gcc-multilib g++-multilib libsdl2-dev
[Installing CMAKE for Debian]
sudo apt remove cmake                   - Remove out of date default package
pip3 install cmake --upgrade            - Install upto date CMAKE
Log out and back in for pip3 app path to be exported into your environment.

Verify dependencies are installed before continuing

cmake --version
python3 --version
dtc --version

3.0 Get Zephyr and Install Python dependencies

Install West (Zephyr Meta-tool)

pip3 install --user -U west

echo 'export PATH=~/.local/bin:"$PATH"' >> ~/.bashrc
source ~/.bashrc
Setup Zephyr Folder
cd /home/user
mkdir csse4011/
cd csse4011/

west init zephyrproject/               - Latest version of Zephyr (the development tree)
Get Zephyr Source Code (Gets ALL of the source)
cd zephyrproject
west update                                 - Might take awhile 
Export a Zephyr CMake package. This allows CMake to automatically load boilerplate code required for building Zephyr applications.
west zephyr-export
Zephyr’s scripts/requirements.txt file declares additional Python dependencies. Install them with pip3.
pip3 install --user -r ~/csse4011/zephyrproject/zephyr/scripts/requirements.txt

4.0 Installing the toolchains

This tutorial will focus on installing the Zephyr SDK. Download Link.

Download and extract the Zephyr SDK installer. It is recommended to download the minimal installation for you architecture (e.g. x86 for intel CPU based computers)

cd ~

tar -xvf <name_of_downloadfile_.gz>
Install the SDK to /opt/ , see here for other potential installation locations
chmod +x zephyr-sdk-<version_namber>linux-x86_64-setup.run

sudo ./zephyr-sdk--<version_namber>-linux-x86_64-setup.run -- -d /opt/zephyr-sdk-0.13.2
Remove SDK Installer (Optional)
cd ~
rm zephyr-sdk--<version_namber>-linux-x86_64-setup.run
Set udev rules, which allow you to flash most Zephyr boards as a regular user
sudo cp /opt/zephyr-sdk--<version_namber>/sysroots/x86_64-pokysdk-linux/usr/share/openocd/contrib/60-openocd.rules /etc/udev/rules.d

sudo udevadm control --reload

5.0 Build a Sample Program (Blinky) for the a Board

Refer to tutorial that covers the board, you are using.

6.0 Setting Zephyr Base

Setting $ZEPHYR_BASE allows you to invoke west from outside the source directory to build your applications. So we will add this environment to be set at login by bashrc. If you are using a different shell fish or zsh, make sure you set this within those config files.

If you installed Zephyr elsewhere, adjust the following accordingly.

echo "# Add Zephyr Base Location to Path
export ZEPHYR_BASE=~/csse4011/zephyrproject/zephyr" >> ~/.bashrc 

source ~/.bashrc

Last update: 2023-02-21