Pico PR

Getting Started with Zephyr OS on Raspberry Pi Pico2: A Comprehensive Guide to Environment Setup

記事内に商品プロモーションを含む場合があります

Introduction

This guide will walk you through the process of using Zephyr OS on the Raspberry Pi Pico2.

Having worked with RTOS for a considerable time, I’ve been intrigued by Zephyr OS but hadn’t had the chance to explore it hands-on.

Recently, I discovered that Zephyr OS is compatible with the Raspberry Pi Pico2. As information on setting up the environment, especially on Windows, is somewhat limited, I decided to create this comprehensive guide. I’ll cover everything from environment setup to flashing and running Zephyr OS on the Pico2, based on the official documentation.

If you’re asking, “What exactly is Zephyr OS?” or “How can I get it running on my Pico2 (RP2350)?”, then you’ve come to the right place!

This blog post is based on the official Zephyr OS documentation, with modifications and additions for the Pico2.


https://docs.zephyrproject.org/latest/develop/getting_started/index.html

Environment

The environment used in this blog post is as follows.

Environment Content Version, etc
Host OS Windows11
Language C++
環境 West Ver.1.3.0
Board RaspberryPi Pico2
debugger/flasher RaspberryPi Debug Probe
OpenOCD 0.10.0 (20240916)
OS Zephyr OS 

What is Zephyr OS?

※ Image from official Zephyr Github.

Zephyr OS is a lightweight, open-source real-time operating system (RTOS) tailored for IoT devices and embedded systems.

It’s designed to be versatile, capable of running on a wide range of devices, from resource-constrained microcontrollers to systems with more powerful processors.

Being a real-time OS, it’s particularly well-suited for applications demanding precise timing and control.

Official HP https://www.zephyrproject.org/
GitHub https://github.com/zephyrproject-rtos
Stable version 4.0.0  (2024/11/16)
licence Apache-2.0 license
GitHub Star 11.3k

※ The GitHub Star count is current as of January 2025.

Connecting to Raspberry Pi Pico2

Here’s how to connect your Pico2 to a Debug Probe:

Connect the Pico 2 to debug probe
Pin No. Contents Connection Point Remarks
SWDIO Debug Probe(Yellow) The cable included with the Debug Probe is used.
GND Debug Probe(Black)
SWCLK Debug Probe(Orange)

Pico Pin-Out

RaspberryPi Picoのピンアサイン(pinout)の画像

Quoted from the official Raspberry Pi website.

Parts in use

RaspberryPi Pico 2

If you’re getting a Pico now, we recommend the Pico2 – it’s 1.5 times faster than the Pico and almost the same price.

ノーブランド品
¥1,475 (2024/11/27 21:32時点 | Amazon調べ)

Ready-to-use kits are also available.

Ready to use kits with soldered headers, USB cable and pin layout chart are also available.

RaspberryPi Debug Probe

This is a low-cost, high-performance official debug probe from Raspberry Pi.

It allows you to monitor the program’s state and write programs without pressing the BOOTSEL button, using the SWD (Serial Wire Debug) line.

The included USB cable with the Raspberry Pi logo is another welcome feature.

Steps to Using Zephyr OS

We’ll break down the process into the following steps:

  • Installing the development environment on Windows
  • Installing build-related tools
  • Setting up a Python virtual environment
  • Installing West/West SDK
  • Building a sample program
  • Flashing and running the sample program

Installing the Development Environment on Windows

Let’s begin by setting up your development environment. Here’s my PC configuration.

Keep in mind that installing the build-related packages will consume several gigabytes of storage, so ensure you have ample free space.

Environment  Version etc… remarks
OS Windows11
Model Lenovo ThinkPad T14s  Gen2
CPU Intel Core i5
Memory 16GB
Storage 10GB or more free space recommended

Installing Chocolatey

We’ll use Chocolatey, a package manager for Windows, to streamline the installation of necessary tools.

Navigate to the link below, copy the command from the STEP2 section, and execute it in “PowerShell started with administrator privileges”.

The official Chocolatey website is: https://chocolatey.org/install

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

  • On the linked page, there’s an email address field in STEP1, but it’s optional, so you don’t need to fill it in.
  • For STEP2, please select the default option, which is ‘Individual’.”

Now that Chocolatey is installed, you can close PowerShell.

Installing build tools

Using Chocolaty, we will install the tools necessary to build Zephyr itself and sample programs, such as CMake, Ninja, and Python.

The commands in this section must be executed in a command prompt opened with administrator privileges. WSL and PowerShell are not recommended.

Disabling Confirmation Messages

To disable installation confirmation messages, execute the following command:

choco feature enable -n allowGlobalConfirmation

Installing CMake

Install CMake using the following command:

choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System'

Installing Other Related Tools

Install the tools necessary for the build environment, such as Python, using the following command:

choco install ninja gperf python311 git dtc-msys2 wget 7zip

After the installation of the related tools is complete, close the command prompt opened with administrator privileges.

Setting Up the Python Virtual Environment and Obtaining the Zephyr Source Code

Follow these steps to set up the Python virtual environment and obtain the Zephyr OS source code.

From this point forward, execute the commands in a “Command Prompt” opened with a regular user account. WSL and PowerShell are not recommended.

Using an “administrator command prompt” will result in failure.

Setting Up the Python Virtual Environment

Navigate to the desired directory.

cd %HOMEPATH%

Create the Python virtual environment.

python -m venv zephyrproject.venv

★ Activating the Virtual Environment ★

Activate the Python virtual environment using the following command:

zephyrproject.venvScriptsactivate.bat

Installing West

Install “West”, a tool that centrally manages build and flash operations for Zephyr OS, using the following command:

Execute the commands from this point on while the command prompt displays (.venv) at the beginning (indicating you are in the Python virtual environment).

If .venv is not displayed, re-execute the activation command (marked with ★) to enable the virtual environment.

pip install west

Installing Zephyr

Obtain the Zephyr OS source code using the following command:

west init ~/zephyrproject
cd ~/zephyrproject
west update

Export the configurations and settings necessary for the build using the following command:

west zephyr-export

Install the required pip packages for Zephyr using the following command:

west packages pip --install

Installing the Zephyr SDK

Install the Zephyr Software Development Kit (SDK), which includes the toolchain (compiler, linker, etc.) necessary for building Zephyr applications.

For this section as well, continue to execute the commands under the following conditions:

  • “Command Prompt” opened with a regular user account
  • “Command Prompt” displays (.venv) at the beginning (indicating you are in the Python virtual environment)

cd %HOMEPATH%zephyrprojectzephyr
west sdk install

Installing OpenOCD

We will install “OpenOCD” for writing and debugging with the Raspberry Pi Debug Probe.

The official OpenOCD website only distributes the source code, and building it yourself is difficult. Therefore, we will download it from the sysprog page (a company that develops and sells debugging tools), which is also mentioned in the official Zephyr OS documentation.

Once the download is complete, unzip the file.

Copy the unzipped “OpenOCD” folder to “C:\Program Files”, and after copying, add “C:\Program Files\OpenOCD\bin” to the end of “Path” in the Windows “System Environment Variables”.

Building the L-blinka Sample Program

Once the environment construction is complete, build the sample program to light up the LED on the Pico2 itself (LED blinking).

Execute the following command:

cd %HOMEPATH%zephyrprojectzephyr
west build -p always -b rpi_pico2/rp2350a/m33 samples/basic/blinky

※「rpi_pico2/rp2350a/m33」の部分はPico2専用の設定です。

Flashing to Pico2

Write the program to the Pico2 with the following command:

west flash --openocd openocd-rp2350

-- west flash: rebuilding
ninja: no work to do.
-- west flash: using runner openocd
-- runners.openocd: Flashing file: C:/Users/*******/zephyrproject/zephyr/build/zephyr/zephyr.hex
Open On-Chip Debugger 0.10.0 (2024-09-16)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
set_adapter_speed_if_not_set
Info : Hardware thread awareness created
Info : Hardware thread awareness created
ocd_process_reset_inner
checking adapter speed...
Error: BUG: unknown adapter clock mode
adapter speed: 5000 kHz
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=E66258881785842B
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: Atomic commands supported
Info : CMSIS-DAP: Test domain timer supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x4c013477
Info : [rp2350.cm0] Cortex-M33 r1p0 processor detected
Info : [rp2350.cm0] target has 8 breakpoints, 4 watchpoints
Info : [rp2350.cm0] Examination succeed
Info : [rp2350.cm1] Cortex-M33 r1p0 processor detected
Info : [rp2350.cm1] target has 8 breakpoints, 4 watchpoints
Info : [rp2350.cm1] Examination succeed
Info : starting gdb server for rp2350.cm0 on 3333
Info : Listening on port 3333 for gdb connections
    TargetName         Type       Endian TapName            State
--  ------------------ ---------- ------ ------------------ ------------
 0* rp2350.cm0         cortex_m   little rp2350.cpu         unknown
 1  rp2350.cm1         cortex_m   little rp2350.cpu         unknown
[rp2350.cm0] halted due to breakpoint, current mode: Thread
xPSR: 0xf9000000 pc: 0x00000088 msp: 0xf0000000
[rp2350.cm1] halted due to debug-request, current mode: Thread
xPSR: 0xf9000000 pc: 0x00000088 msp: 0xf0000000
Error executing event reset-init on target rp2350.cm0:
C:Program Files/OpenOCD/share/openocd/scripts/target/rp2350.cfg:78: Error: invalid command name "rp2xxx"
in procedure 'ocd_process_reset'
in procedure 'ocd_process_reset_inner' called at file "embedded:startup.tcl", line 1225
at file "C:Program Files/OpenOCD/share/openocd/scripts/target/rp2350.cfg", line 78
Info : RP2040 Flash Probe: 33554432 bytes @0x10000000, in 8192 sectors

Info : Padding image section 0 at 0x1000001c with 228 bytes
Info : Padding image section 1 at 0x10003ec0 with 64 bytes (bank write end alignment)
Warn : Adding extra erase range, 0x10003f00 .. 0x10003fff
auto erase enabled
wrote 16128 bytes from file C:/Users/******/zephyrproject/zephyr/build/zephyr/zephyr.hex in 0.425238s (37.038 KiB/s)
shutdown command invoked

About openocd-rp2350

As of February 2025, the official “openocd.exe” does not support writing to Pico2, so you need to specify “openocd-rp2350.exe”, which can write to Pico2, with the “–openocd” option.

Currently, I have only created the blog post in Japanese. For more details, please refer to the blog post below.

https://tech-and-investment.com/raspberrypi-pico2-04-openocd

Results of running the program

When flashing is complete, the LED on the Pico2 itself, located at the arrow in the image below, will blink.

Zephyr OSのサンプルプログラムを実行して、Pico2の本体LEDが点滅している画像
サンプルプログラムの実行結果

Summary

This blog post described how to use Zephyr OS on the Raspberry Pi Pico2.

The official procedure is well-made, but it is general-purpose, so I explained the procedure limited to Pico2.

I had a lot of trouble with OpenOCD, but I hope this will be helpful for those who want to use Zephyr OS on Pico2.

Want to know more about Zephyr OS!

For those who want to know more about Zephyr OS, there seems to be a Zephyr OS book released in September 2024. It is in full English and I cannot read it, but if you want to challenge yourself, please check the following.

質問・要望 大歓迎です

「こんな解説記事作って」「こんなことがしたいけど、〇〇で困ってる」など、コメント欄で教えてください。 質問・要望に、中の人ができる限り対応します。

使えたよ・設定できたよの一言コメントも大歓迎。気軽に足跡を残してみてください。記事を紹介したい方はブログ、SNSにバシバシ貼ってもらってOKです。

ABOUT ME
えす
現役のソフトウェアエンジニアです。 C++ C# Python を使ってます。10年ちょい設計/開発部門にいましたが、今はQAエンジニアっぽいことをしています。

COMMENT

Your email address will not be published. Required fields are marked *

Index