Zephyr logo

Zephyr tutorial 101

Zephyr tutorial 101

Javad Rahimipetroudi (Mind embedded software consultant)
Javad Rahamipetroudi
10/01/2024

For a long time, starting from 2010, I used STM32 microcontrollers on a regular basis. During those old times, the only firmware option available for an embedded engineer was a buggy firmware developed by STMicroelectronics, which they called the standard library. To make it worse, there weren’t many tutorials or manuals available during that dark age of internet technology. On the other hand, for a computer hardware engineering student, purchasing a commercial license for professional software such as Keil or IAR was nearly impossible.Zephyr logo

Most developers at the time used OEM firmware to develop their projects. In cases where they needed any OS-level support, they usually had to use FreeRTOS or some other commercial RTOS that was available for microcontrollers. After Zephyr OS was released in 2016, a new player entered the embedded world.

Good or bad, embedded engineers are known to be cautious and risk-averse individuals.They like to make sure everything is in order before moving forward. Furthermore, IoT technology was also in its infancy. As a result, for a long time, no one knew too much about Zephyr OS.

After the IoT left the dark and damp corners of the academic environment, as well as the crazy competition of different companies to provide different models of low-power chips that can be used in IoT applications, the need for reliable middleware for the rapid development of IoT applications was quite noticeable. At this time, Embedded engineers started to get their hands on Zephyr OS.

Zephyr is a small-footprint yet scalable RTOS that supports a wide variety of boards from small sensor nodes to multi-core complex systems. The main features of Zephyr OS as it is stated on its official website are as follows:

  • Comprehensive, lightweight, kernel & supporting services
    • Fits where Linux is too big
  • Inherently portable & secure
  • Highly connected
    • Bluetooth 5.0 & BLE
    • Wi-Fi, Ethernet, CANbus, …
    • IoT protocols: CoAP, LwM2M, MQTT, OpenThread, …
    • USB & USB-C
  • Developer-friendly
    • Logging, tracing, debugging, built-in shell, Windows/Linux/macOS support, …

With all of these awesome features, any curious engineer should definitely take a closer look at Zephyr OS. That’s why I’m excited to share a series of tutorials to help you become familiar with this amazing operating system. So let’s get started!

For this tutorial series, we will use the B-U585I-IOT02A development board from STMicroelectronics. It is a development board primarily designed for IoT applications, with a bunch of sensors and modules that are installed on it.

Setup Zephyr Os

Based on your development system, you can easily follow the installation instructions provided by the Zephyr official website to install Zephyr and its SDK.

Hello World from Zephyr

As an old tradition, the first step is a simple Hello World application. To do this, we first clone an example project from Zephyr’s official repository:

cd ~/workspace
git clone https://github.com/zephyrproject-rtos/example-application helloworld

Next, go to helloworld/app/src/ , delete main.c and create a new file with the following lines of code:

#include <stdio.h>

int main(void)
{
  printf("Hello World! %s\n", CONFIG_BOARD);
  return 0;
}

That’s all! No register definition, No peripheral initialization.

Let’s build the project. From the helloworld directory, run the following command:

west build --force -p always -b b_u585i_iot02a app/

If everything goes well, the final build results will be reported:

-- Zephyr version: 3.5.99 (/home/javad/zephyrproject/zephyr), build: zephyr-v3.5.0-2706-gede9b0337c72
[126/127] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       14844 B         2 MB      0.71%
             RAM:        4160 B       768 KB      0.53%
        IDT_LIST:          0 GB         2 KB      0.00%

Now, let’s burn our board with the new firmware. Before anything, you have to install STM32CubeProgrammer based on the type of your OS.

After installing the programmer, connect your board to your computer and run the following command:

west flash

If the programmer was connected correctly and the STM32CubeProgrammer was installed correctly. We will see the following output reports

$ west flash
-- west flash: rebuilding
-- west flash: using runner stm32cubeprogrammer
-- runners.stm32cubeprogrammer: mass erase requested
      -------------------------------------------------------------------
                        STM32CubeProgrammer v2.15.0                  
     -------------------------------------------------------------------
ST-LINK SN  : 0030002B3432511630343838
ST-LINK FW  : V3J13M4
Board       : B-U585I-IOT02A
Voltage     : 3,27V
SWD freq    : 8000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x482
Revision ID : Rev X
Device name : STM32U575/STM32U585
Flash size  : 2 MBytes
Device type : MCU
Device CPU  : Cortex-M33
BL Version  : 0x92
Debug in Low Power mode enabled
.
.
.
Memory Programming ...
.
.
.
Download in Progress:
[==================================================] 100% 
File download complete
Time elapsed during download operation: 00:00:00.175
RUNNING Program ... 
  Address:      : 0x8000000
Application is running, Please Hold on...
Start operation achieved successfully

Now, Let’s open the serial port and see the output.

The USART1 of the board is connected to the programmer Virtual com port (VCP). So, by connecting the board to the computer, a VCP will be opened. In Linux, the port is /dev/ttyACM0 . To open the com port, we use picocom as follows:

sudo picocom -b 115200 /dev/ttyACM0

Now, reset the board by pressing the RST button on the board. The following test should be send from board to the computer:

picocom v3.1
port is        : /dev/ttyACM0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
stopbits are   : 1
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
hangup is      : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv -E
imap is        :
omap is        :
emap is        : crcrlf,delbs,
logfile is     : none
initstring     : none
exit_after is  : not set
exit is        : no

Type [C-a] [C-h] to see available commands
Terminal ready
*** Booting Zephyr OS build zephyr-v3.5.0-2706-gede9b0337c72 ***
Hello World! b_u585i_iot02a

To exit picocom hold CTRL and press A + Q successively.

Conclusion and next steps

In this tutorial, we have demonstrated how to set up a development environment for Zephyr OS. Although it will be a long journey, taking the first step is the most important. In the next tutorial, we will discuss the Device Tree model, which is an important tool in the world of embedded systems. The Device tree helps us to decouple our application from the hardware, making it possible to write our application once and run it on different platforms with minimal modifications. Let’s go and discover more!

Presentations

Drop the docs and embrace the model with Gaphor Fosdem '24 - Frank Van Bever 20 March, 2024 Read more
How to update your Yocto layer for embedded systems? ER '23 -Charles-Antoine Couret 28 September, 2023 Read more
Tracking vulnerabilities with Buildroot & Yocto EOSS23 conference - Arnout Vandecapelle 12 July, 2023 Read more
Lua for the lazy C developer Fosdem '23 - Frank Van Bever 5 February, 2023 Read more
Exploring a Swedish smart home hub Fosdem '23 - Hannah Kiekens 4 February, 2023 Read more
prplMesh An Open-source Implementation of the Wi-Fi Alliance® Multi-AP (Arnout Vandecappelle) 25 October, 2018 Read more

 

News