Quick Zephyr OS Setup Guide for STM32 MCUs on Windows

Zafer SEN
8 min readOct 8, 2023

--

In this article, I am not going to explain what zephyros is, its pros and cons and why zephyr. There are a bunch of articles about those. Even this article has the same instructions until Build the Blinky Sample title like the official zephyr os document has, see: https://docs.zephyrproject.org/latest/develop/getting_started/index.html

P.S.

If you think reading this article would be easy in Github, please check this out:

https://github.com/zafersn/zephyr-project/wiki/Quick-Getting-Started-Engineer-Guide-for-Windows-With-Python-Script

specific guidance for st boards starts with the Build the Blinky sample title. But benefit of the going through All the steps before it is all these commands in the steps are verified and tested on Windows. This article is a simplified version of the getting started guide document.

I have two different STM dev boards:

  1. ST STM32F072B Discovery which has stlink v2.1
  2. ST Nucleo U575ZI Q which has stlink v3

they require slightly different flash tools. I'll explain this later in steps

Install dependencies:

These instructions must be run in a cmd.exe command prompt. The required commands differ on PowerShell.

1. Install chocolatey:

a. Choose individual chocolatey installation

b. Run power shell as an admin

c. Copy and paste this command:

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'))

d. Wait a few seconds for the command to complete.

e. If you don’t see any errors, you are ready to use Chocolatey! Type choco or choco -? On cmd.exe

2. Open a cmd.exe window as Administrator. To do so, press the Windows key, type “cmd.exe”, right-click the result, and choose Run as Administrator.

3. Disable global confirmation to avoid having to confirm the installation of individual programs:

choco feature enable -n allowGlobalConfirmation

4. Use choco to install the required dependencies:

choco install cmake - installargs 'ADD_CMAKE_TO_PATH=System'
choco install ninja gperf python git dtc-msys2 wget 7zip

5. Close the window and open a new cmd.exe window as a regular user to continue.

NOTE: if you have any notifications given during installation to restart the pc, please do not postpone it before going next step you have to restart the PC.

Get Zephyr and install Python dependencies:

Next, clone Zephyr and its modules into a new west workspace named zephyrproject. You’ll also install Zephyr’s additional Python dependencies.

1. Create a new virtual environment:

cd C:\\
python -m venv zephyrproject\.venv

2. Activate the virtual environment:

zephyrproject\.venv\Scripts\activate.bat

Once activated your shell will be prefixed with (.venv). The virtual environment can be deactivated at any time by running deactivate.

Note

Remember to activate the virtual environment every time you start working.

3. Install west

pip install west

4. Get the Zephyr source code:

west init zephyrproject
cd zephyrproject

and run:

west update

5. Export a Zephyr CMake package. This allows CMake to automatically load boilerplate code required for building Zephyr applications.

west zephyr-export

6. Zephyr’s scripts\requirements.txt file declares additional Python dependencies. Install them with pip.

a. First add this path into environment variables for the user and system. =>system properties => environment variables

i. C:\Users\zsen_\AppData\Roaming\Python\Python311\Scripts

ii. Note: don’t forget to change the username (zsen_) with your own username

pip install -r zephyr\scripts\requirements.txt

while this command is being processed, you may have warnings about some python scripts requiring a user keyword. So run this command too.

pip install -r zephyr\scripts\requirements.txt --user

Install Zephyr SDK

The Zephyr Software Development Kit (SDK) contains toolchains for each of Zephyr’s supported architectures, which include a compiler, assembler, linker and other programs required to build Zephyr applications.

1) Open a cmd.exe window by pressing the Windows key typing “cmd.exe”.

2) Download the Zephyr SDK bundle:

a)  cd c:\\zephyrproject
b)  wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.3/zephyr-sdk-0.16.3_windows-x86_64.7z

3) Extract the Zephyr SDK bundle archive:

a)  7z x zephyr-sdk-0.16.3_windows-x86_64.7z

4) Run the Zephyr SDK bundle setup script:

cd zephyr-sdk-0.16.3
setup.cmd

Note

You only need to run the setup script once after extracting the Zephyr SDK bundle. You must rerun the setup script if you relocate the Zephyr SDK bundle directory after the initial setup

Build the Blinky Sample

If you are unsure what name west uses for your board, west boards can be used to obtain a list of all boards Zephyr supports.

See all sported boards:

  1. run command in zephyrproject
west boards

2) or… Go to the zephyr arm based boards folder under

C:\zephyrproject\zephyr\boards\arm

Build a sample:

I would suggest that it might be really helpful not playing with default zephyr os folders, libraries and samples in terms of having always clear files and configs provided by the vendor to see them if needed, So lets create a workspace to play with samples and place our projects

cd c:\zephyrproject
mkdir wsc
cd wsc
xcopy C:\zephyrproject\zephyr\samples\blinky blinky /E /H /C /I
cd blinky
west build -p always -b <your-board-name> blinky

now you can run build command:

west build -p always -b stm32f072b_disco

if you have any python version issue try this one:

west build -p always -b stm32f072b_disco samples\basic\blinky -DPYTHON_PREFER=C:/Python311/python.exe

do not forget to specify the python version to build

-DPYTHON_PREFER=C:/Python311/python.exe

The -p always option forces a pristine build, and is recommended for new users. Users may also use the -p auto option, which will use heuristics to determine if a pristine build is required, such as when building another sample.

Flash the Sample

Connect your board, usually via USB, and turn it on if there’s a power switch. If in doubt about what to do, check your board’s page in Supported Boards.

Installing Host tools

We have three options to flash a hex file to an stm dev board, the one that we are going to use j-link which is compatible with nrf vs code extantion, the second one is stm32cube programmer provided by st with official support, the other one is the generic one openocd.

NOTE: stm32cube cannot be used for debugging purposes.

1) J-LINK ST for the boards listed here

a) Make sure that the ST-LINK USB drivers are installed: Download

b) Download st-link reflash utility: https://www.segger.com/downloads/jlink/#STLink_Reflash

check your st board compatibility here: https://www.segger.com/products/debug-probes/j-link/models/other-j-links/st-link-on-board/

c) Run stlinkreflash.exe

d) Agree to the license terms

e) Connect ST-LINK on-board to PC

g) Select “Upgrade to J-Link”

h) Wait for the operation to complete

i) Run this command below in blink or helloworld project to flash the hex

west flash - runner jlink

j) if you don’t want to specify every time –runner jlink:

i) go to board.cmake under your board directory: i.e: C:\zephyrproject\zephyr\boards\arm\stm32f072b_disco\board.cmake

and include jlink cmake file but make sure it is placed on top of the other includes like this:

include(${ZEPHYR_BASE}/boards/common/jlink.board.cmake)
include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)
include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)

and add board runner args as well, this time it is it doesn’t matter where you place it

board_runner_args(jlink "--device=STM32F072RB" "--speed=4000")

Note: This is not an option for the ST Nucleo U575ZI Q which has stlink v3 dev board, if you have to use J-link for this board. you need to use a j-link debugger and then you can use it through the CN5 header which is an external programming input on the board

see for more details: 6.3.4 Using an external debug tool to program and debug the on-board STM32 page 14 in the https://www.st.com/resource/en/user_manual/um2861-stm32u5-nucleo144-board-mb1549-stmicroelectronics.pdf

these next two of them are optional, and not necessary if you are happy with your j-link… But just a heads up:

2) STM32CUBEPROGRAMMER only for programming

a) get the software and install it: https://www.st.com/en/development-tools/stm32cubeprog.html

b)include the programmer tool into board.cmake file if it does not exist there:

board_runner_args(stm32cubeprogrammer "--erase" "--port=swd" "--reset-mode=hw")
board_runner_args(stm32cubeprogrammer "--port=swd" "--reset-mode=hw")

include(${ZEPHYR_BASE}/boards/common/stm32cubeprogrammer.board.cmake)

c) restart the vs-code/cmd terminal if it is open.

d) Then run: west flash — runner stm32cubeprogrammer

3) OpenOCD(debugging & programming):

a) Download openocd for Windows from here: OpenOCD Windows: http://gnutoolchains.com/arm-eabi/openocd/

b) Copy bin and share dirs to C:\Program Files\OpenOCD\

c) Add C:\Program Files\OpenOCD\bin to ‘PATH’ environment variable

d) Then flash the sample using west flash:

e) west flash — runner openocd

-DBOARD_ROOT:STRING=”C:\zephyrproject\wsc\blinky”

VS CODE Implementation

In this step, I prefer 3rd party app which is the nRF Connect for VS Code from nordic.

Here I need to highlight one point, I have worked for a long time with nordic MCUs and their tools, So I had a Zephyr OS and vs code extension automatically installed by nrf connect desktop. So the first step I’ll explain I didn’t exactly run it on my system. But I believe there will be no problem, in any keys you can continue with nrf desktop tools.

1. Stand-alone installation of nRF Connect for VS Code extension:

  1. go to vs-code and install nRF Connect for VS Code with the required extensions such as nRF DeviceTree, nRF Kconfig, nRF Terminal etc..

Name: nRF Connect
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=nordic-semiconductor.nrf-connect

here is the extension list you need to install

after installing extensions, you may need to specify the toolchain and west manifest on the welcome page. it should be seems like this:

if this 1st step above does not work, then

2. Installation of nRF Connect for VS Code extension through the nRF connect Desktop

(1) Download required software: https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-desktop/download

(2)Install the toolchain manager and open it.

(3) install the latest sdk v2.4.2(latest)

(4) After completion nrf extension’s welcome page should be seen like this below.

(a) Manage toolchains v2.4.2

(b) Zephyr v2.4.2

(5) you need to change manage toolchains and manage SDKs with your own zephyr os and toolchain installed in the zephyrproject folder.

If you have done so far everything explained in this article, you should be able to see your st dev board in the connected devices list.

So if you have an issue and cannot see a connected device, try this:

Go to file => preference => settings => workspace => extensions => nrf connect => device provider

And change it nrfutil to nrfproj. This is only necessary to have flashing and debugging ability through the nrf extension other than using cmd west commands.

Check if you are in a workspace or not.

a) go into C:\zephyrproject\wsc you should see a file with “.code-workspace” extension.

b) If you cannot see a file:

i) Go vs code and and click file => save workspace as..

ii) Make sure you specify the correct path: C:\zephyrproject\wsc

iii) Save the workspace.

Creating a workspace is necessary to keep all project together

Create a new application through the nrf extension.

a) Go to the welcome page and click create a new application

b) Click Copy a sample

c) Select your sample

d) Type workspace location c:\zephyrproject\wsc to place there

e) Verify or rename the sample project you chose

f) Attention!! There is a pop up here and click on the add to workspace button.

--

--

No responses yet