If you’re looking to get started with Zephyr RTOS on STM32, you’ve chosen one of the most powerful combinations in embedded systems development. Zephyr RTOS is a scalable, open-source real-time operating system backed by the Linux Foundation, and STM32 microcontrollers from STMicroelectronics are among the most popular 32-bit MCUs used by hobbyists and engineers in India. This guide will walk you through everything you need to know to set up Zephyr RTOS on an STM32 board from scratch.
Table of Contents
- What is Zephyr RTOS?
- Why Use STM32 with Zephyr?
- Setting Up the Development Environment
- Your First Zephyr Project on STM32
- Understanding Threads and Scheduling
- Working with Peripherals (GPIO, UART, I2C)
- Debugging and Flashing
- Frequently Asked Questions
What is Zephyr RTOS?
Zephyr RTOS is a small, scalable real-time operating system designed for resource-constrained embedded systems. Unlike FreeRTOS or bare-metal programming, Zephyr provides a complete ecosystem including a device tree abstraction layer, Kconfig build system, and an extensive HAL (Hardware Abstraction Layer) for hundreds of boards.
Key features that make Zephyr attractive for Indian developers:
- Multi-architecture support: ARM Cortex-M, RISC-V, x86, ARC and more
- Device Tree: Hardware configuration separated from software — similar to Linux’s approach
- Bluetooth 5.0, Thread, Matter, and WiFi stacks built-in
- Active Linux Foundation backing — regular updates and long-term support
- Free and open-source — no licensing costs, critical for student projects and startups
Zephyr RTOS is increasingly used in commercial IoT products, and learning it opens doors to professional embedded firmware development.
Why Use STM32 with Zephyr?
STM32 microcontrollers are widely supported in Zephyr, with dozens of official board definitions available. The STM32 family spans from entry-level Cortex-M0 chips (like STM32F030) all the way to powerful Cortex-M7 processors (like STM32H7). For getting started with Zephyr RTOS on STM32, the STM32F4 Discovery or STM32 Nucleo boards are ideal choices because of their built-in ST-LINK debugger.
In India, STM32 Nucleo boards are available for approximately ₹800–₹2500 depending on the variant, making them accessible for students and hobbyists. The integrated debugger means you don’t need a separate JTAG/SWD probe to flash and debug Zephyr firmware.
Setting Up the Development Environment
Zephyr uses the West meta-tool for project management and building. Here’s how to set up your environment on Ubuntu/Debian Linux (recommended for Indian developers learning Zephyr):
Step 1: Install Dependencies
# Install Python and pip
sudo apt update
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 libmagic1
# Install West
pip3 install --user -U west
Step 2: Get the Zephyr Source
west init ~/zephyrproject
cd ~/zephyrproject
west update
west zephyr-export
pip3 install --user -r ~/zephyrproject/zephyr/scripts/requirements.txt
Step 3: Install the ARM Toolchain
# Download the ARM GNU Toolchain (for Cortex-M)
cd ~
wget https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz
tar xvf arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz
# Add to PATH in .bashrc
echo 'export PATH=$HOME/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Your First Zephyr Project on STM32
The classic “Hello World” equivalent in embedded systems is blinking an LED. Here’s how to do it with Zephyr RTOS on STM32:
/* src/main.c */
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
/* Get LED from device tree - works for any STM32 Nucleo board */
#define LED0_NODE DT_ALIAS(led0)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
if (!gpio_is_ready_dt(&led)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
return 0;
}
while (1) {
gpio_pin_toggle_dt(&led);
k_msleep(500); /* Sleep 500ms */
}
return 0;
}
Build and flash this to your STM32 Nucleo board:
cd ~/zephyrproject/zephyr
west build -p auto -b nucleo_f446re samples/basic/blinky
west flash
The -b nucleo_f446re flag specifies your target board. Change this to match your actual board (e.g., nucleo_l476rg, stm32f4_disco).
Understanding Threads and Scheduling
Zephyr RTOS is truly an RTOS — it supports preemptive multi-threading with configurable priority levels. Understanding this is what separates Zephyr from simple Arduino-style programming.
#include <zephyr/kernel.h>
/* Define two threads */
K_THREAD_DEFINE(thread_a_id, 1024, thread_a_entry, NULL, NULL, NULL, 5, 0, 0);
K_THREAD_DEFINE(thread_b_id, 1024, thread_b_entry, NULL, NULL, NULL, 7, 0, 0);
void thread_a_entry(void *p1, void *p2, void *p3)
{
while (1) {
printk("Thread A running at priority 5
");
k_sleep(K_MSEC(1000));
}
}
void thread_b_entry(void *p1, void *p2, void *p3)
{
while (1) {
printk("Thread B running at priority 7
");
k_sleep(K_MSEC(500));
}
}
Key concepts:
- Priority: Lower number = higher priority in Zephyr. Priority 0 is the highest.
- Stack size: Each thread gets its own stack (1024 bytes above is typical for simple tasks)
- Preemption: Higher priority threads preempt lower priority ones when they become ready
- Cooperative mode: Set priority to negative values for cooperative (non-preemptible) threads
Working with Peripherals (GPIO, UART, I2C)
Zephyr’s device tree abstraction makes peripheral access hardware-agnostic. The same code works on different STM32 variants:
UART Example
#include <zephyr/drivers/uart.h>
const struct device *uart_dev = DEVICE_DT_GET(DT_NODELABEL(usart2));
static void uart_cb(const struct device *dev, void *user_data)
{
uint8_t c;
if (!uart_irq_update(dev)) return;
if (uart_irq_rx_ready(dev)) {
uart_fifo_read(dev, &c, 1);
/* Echo back */
uart_fifo_fill(dev, &c, 1);
}
}
int main(void)
{
uart_irq_callback_set(uart_dev, uart_cb);
uart_irq_rx_enable(uart_dev);
while (1) { k_sleep(K_FOREVER); }
}
Debugging and Flashing
Zephyr supports multiple debug interfaces. For STM32 Nucleo boards with built-in ST-LINK, West handles flashing automatically:
# Flash the compiled firmware
west flash
# Start a GDB debug session
west debug
# Use OpenOCD for more control
west debugserver --runner openocd
For serial monitor output (printk statements), use any serial terminal at 115200 baud:
# On Linux/macOS
minicom -D /dev/ttyACM0 -b 115200
# Or use screen
screen /dev/ttyACM0 115200
Important Zephyr Kconfig options for debugging:
# In prj.conf
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_PRINTK=y
Frequently Asked Questions
Can I use Zephyr RTOS for commercial products in India?
Yes, Zephyr RTOS is licensed under Apache 2.0, which allows commercial use without royalties. This makes it ideal for Indian startups and product companies building IoT devices.
Which STM32 board is best for learning Zephyr in India?
The STM32 Nucleo-64 family (F411RE, L476RG, G071RB) is recommended. They cost ₹1000–₹2000, have built-in debuggers, and are fully supported by Zephyr with ready-made board definitions.
How does Zephyr compare to FreeRTOS for STM32?
Zephyr is more comprehensive — it includes networking stacks, USB, Bluetooth, and a device tree. FreeRTOS is simpler and lighter. For IoT applications, Zephyr is generally more powerful. For deeply resource-constrained projects, FreeRTOS may be preferred.
Does Zephyr work with STM32CubeMX HAL drivers?
Zephyr has its own HAL that wraps STM32 HAL libraries. You don’t use CubeMX directly, but Zephyr’s STM32 drivers are mature and cover most peripherals. The hal_stm32 module is automatically included when targeting STM32.
Where can I find Zephyr examples for STM32 in India?
The official Zephyr repository contains extensive samples in zephyr/samples/. The Zephyr Discord server and GitHub issues are active communities. Indian maker groups on Telegram and Facebook also have active embedded systems discussions.
Add comment