Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 9122

Zephyr • Getting started with Zephyr on Raspberry Pi

$
0
0
Hi all,

The Zephyr Project is "a best-in-class small, scalable, real-time operating system (RTOS) optimized for resource-constrained devices, across multiple architectures." (https://www.zephyrproject.org/learn-about/) It primarily targets microcontrollers and embedded systems.

It provides support for a large number of sensors, middlewares and boards. Among those boards are the Raspberry Pi Picos, which use the RP2040 and RP2350 microcontrollers (https://www.raspberrypi.com/documentati ... ntrollers/).

You can find further information about the Zephyr support for the Pico boards here: https://docs.zephyrproject.org/latest/b ... index.html
And the full Zephyr Getting Started guide here: https://docs.zephyrproject.org/latest/d ... index.html

This guide is intended as an express way to get the Pico micocontrollers working from a Raspberry Pi. Compared to the full installation it will significantly reduce the memory taken up by the Zephyr toolchain and get things working more quickly. I have tested it shortly before writing this post, however Zephyr is constantly being updated and so this guide may be out of date in the future.

With that said, let's get started from a blank Raspberry Pi 5 image:

Code:

sudo apt updatesudo apt full-upgrade
Install Zephyr dependencies (there may be additional dependencies if you are using an OS other than Raspberry Pi OS, please see the Zephyr Getting Started guide):

Code:

sudo apt install -y --no-install-recommends cmake gperf ccache dfu-util libsdl2-dev
Create a directory to store everything related to Zephyr. Do not call it zephyr or things will get confusing later on!

Code:

mkdir zephyr_devcd zephyr_dev
Create a Python virtual environment, activate it and install Zephyr packages:

Code:

python -m venv .venv. .venv/bin/activatepip install west pyelftools
Create a manifest file and initialise the Zephyr workspace. This file contains the information which restricts the installation to Pico support. More about manifest files here: https://docs.zephyrproject.org/latest/d ... ifest.html

Code:

mkdir manifestecho "manifest:  self:    west-commands: scripts/west-commands.yml  remotes:    - name: zephyrproject-rtos      url-base: https://github.com/zephyrproject-rtos  projects:    - name: zephyr      remote: zephyrproject-rtos      revision: main      import:        # By using name-allowlist we can clone only the modules that are        # strictly needed by the application.        name-allowlist:          - cmsis_6      # required by the ARM port          - hal_rpi_pico # required for Pico board support          - hal_infineon # required for Infineon Wifi chip support" > manifest/west.ymlwest init -l manifest/
Update the west workspace (this stage may take several minutes as it downloads just over 1 Gb):

Code:

west update
Install Zephyr related Python packages, binary blobs and the Arm toolchain (may also take several minutes):

Code:

west packages pip --installwest blobs fetch hal_infineonwest zephyr-exportwest sdk install --install-dir . -t arm-zephyr-eabi
This should complete your installation with a total memory usage of around 4.5 Gb. That is quite slim for Zephyr!

Code:

du -h --max-depth=1 .711M    ./.venv8.0K    ./manifest2.3G    ./zephyr-sdk-0.17.4297M    ./modules8.0K    ./.west1.2G    ./zephyr4.5G    .
Congratulations! At this point, you should be able to build a blinky example for the Pico or Pico 2 (blinky is not yet supported for the W variants):

Code:

# Picowest build -p -b rpi_pico zephyr/samples/basic/blinky# Pico 2west build -p -b rpi_pico2/rp2350a/m33 zephyr/samples/basic/blinky
There are a few different ways to get this program running which we will look at.

Flash and debug with Debug Probe

The Debug Probe is designed to work with the Pico boards: https://www.raspberrypi.com/documentati ... probe.html

If you have one, wire it up now and install the Raspberry Pi fork of OpenOCD which supports the Pico boards: (Note: This has been upstreamed and should be part of the next OpenOCD release)

Code:

wget https://github.com/raspberrypi/pico-sdk-tools/releases/download/v2.1.0-0/openocd-0.12.0+dev-aarch64-lin.tar.gzmkdir openocdtar xvzf openocd-0.12.0+dev-aarch64-lin.tar.gz -C openocdrm openocd-0.12.0+dev-aarch64-lin.tar.gz
Then build your program again with some additional OpenOCD arguments:

Code:

# Picowest build -p -b rpi_pico zephyr/samples/basic/blinky -- -DOPENOCD=openocd/openocd -DOPENOCD_DEFAULT_PATH=openocd/scripts# Pico 2west build -p -b rpi_pico2/rp2350a/m33 zephyr/samples/basic/blinky -- -DOPENOCD=openocd/openocd -DOPENOCD_DEFAULT_PATH=openocd/scripts
If you have any issues in the next few steps, make sure the OpenOCD paths are correct and make them absolute paths if necessary.

You should now be able to flash your board:

Code:

west flash
And start the GDB debugger:

Code:

west debug
You can use this setup to debug visually, for example with the Raspberry Pi Pico Extension for VSCode. (https://marketplace.visualstudio.com/it ... ry-pi-pico) The binary can be found at build/zephyr/zephyr.elf

Program with copy and paste

If you do not have a debug, fear not! You can run the program on your Pico with just one USB cable between the Pico and your PC.

Before you plug in your Pico, hold down the BOOTSEL button. This puts the Pico into a mode where it can receive the program.

Following the instructions here (viewtopic.php?t=299680), mount the Pico and copy the UF2 file across:

Code:

sudo mkdir -p /mnt/picosudo mount /dev/sda1 /mnt/picocp build/zephyr/zephyr.uf2 /mnt/pico/sudo syncsudo umount /mnt/pico
Your Pico will now be running the Zephyr program!

Wifi Support

Both the Pico W and Pico 2W are supported by Zephyr and have access to the wonderful Zephyr networking stack! A great example to get started with is the Wifi shell:

Code:

# Pico Wwest build -p -b rpi_pico/rp2040/w zephyr/samples/net/wifi/shell/ -- -DOPENOCD=openocd/openocd -DOPENOCD_DEFAULT_PATH=openocd/scripts# Pico 2Wwest build -p -b rpi_pico2/rp2350a/m33/w zephyr/samples/net/wifi/shell/ -- -DOPENOCD=openocd/openocd -DOPENOCD_DEFAULT_PATH=openocd/scripts
Once you run this, open a serial terminal to the Pico via the Debug Probe:

Code:

# Install minicom if not installed already:sudo apt install minicom# Open a connectionminicom -D /dev/ttyACM0
Once your terminal is connected, hit enter to see the $ sign and perform a wifi scan:

Code:

uart:~$ wifi scanScan requestedNum  | SSID                             (len) | Chan (Band)   | RSSI | Security             | BS1    | TP-Link_AF55                     12    | 3    (2.4GHz) | -60  | WPA2-PSK             | DC2    | TP-Link_AF55                     12    | 3    (2.4GHz) | -61  | WPA2-PSK             | DC3    | TP-Link_AF55                     12    | 3    (2.4GHz) | -62  | WPA2-PSK             | DC
Connect to your WiFi and perform a ping (put your WiFi SSID and password in place of the <> values):

Code:

uart:~$ wifi connect -s "<My SSID>" -p "<my password>"Passphrase provided without security configurationConnectedConnection requested[00:01:58.536,000] <inf> net_dhcpv4: Received: 192.168.1.166uart:~$ net ping 8.8.8.8PING 8.8.8.828 bytes from 8.8.8.8 to 192.168.1.166: icmp_seq=1 ttl=115 time=22 ms28 bytes from 8.8.8.8 to 192.168.1.166: icmp_seq=3 ttl=115 time=50 msuart:~$
Please let me know if you have any issues and have fun!

Cheers,

Tim
Magpie Embedded

Statistics: Posted by MagpieEmbedded — Tue Sep 09, 2025 10:57 pm



Viewing all articles
Browse latest Browse all 9122

Trending Articles