Skip to content

Zephyr RTOS - Beginner Core Concepts

Introduction to Zephyr RTOS

Zephyr RTOS is an open-source, real-time operating system designed for embedded systems, particularly IoT, industrial, and automotive applications. It is lightweight, modular, and highly configurable, supporting a wide range of hardware architectures.

Why Choose Zephyr?

Real-time & deterministic – suitable for time-critical applications.
Scalable & lightweight – runs on low-power microcontrollers (MCUs).
Secure & safety-certified – follows CII Best Practices and supports safety-critical systems.
Multi-architecture support – ARM Cortex-M, RISC-V, x86, etc.
Built-in connectivity – Bluetooth, Wi-Fi, LoRa, CAN, etc.


Core Concepts in Zephyr RTOS

1. Kernel and Thread Management

Zephyr is a preemptive, priority-based RTOS that supports: - Cooperative scheduling (low-power tasks yield control).
- Preemptive scheduling (higher-priority tasks interrupt lower ones).
- Multi-threading for concurrency.

🔹 Creating a Thread in Zephyr

#include <zephyr.h>
#include <sys/printk.h>

void my_thread(void) {
    while (1) {
        printk("Hello from Zephyr!\n");
        k_sleep(K_SECONDS(1));  // Sleep for 1 second
    }
}

K_THREAD_DEFINE(my_thread_id, 1024, my_thread, NULL, NULL, NULL, 7, 0, 0);
Creates a thread that runs in the background indefinitely.


2. Memory Management

Zephyr provides:
Static and dynamic memory allocation.
Memory pools for efficient memory handling.
Heap and stack management with runtime checks.

🔹 Using a Memory Pool in Zephyr

K_MEM_POOL_DEFINE(my_pool, 64, 256, 4, 4); // Define a memory pool
void *ptr = k_mem_pool_malloc(&my_pool, 128); // Allocate memory
k_free(ptr); // Free allocated memory

Prevents heap fragmentation and improves real-time performance.


3. Synchronization and Inter-thread Communication (IPC)

Zephyr supports:
Mutexes for resource locking.
Semaphores for task synchronization.
Message Queues & FIFOs for inter-thread communication.

🔹 Using a Semaphore in Zephyr

K_SEM_DEFINE(my_sem, 0, 1);

void thread_function(void) {
    k_sem_take(&my_sem, K_FOREVER); // Wait for semaphore
    printk("Semaphore acquired!\n");
}

void another_thread(void) {
    k_sem_give(&my_sem); // Release semaphore
}

Ensures controlled access to shared resources.


4. Device Drivers and Peripherals

Zephyr has a hardware abstraction layer (HAL) with built-in drivers for:
✅ GPIO, UART, I2C, SPI, PWM, ADC
✅ Bluetooth, Wi-Fi, LoRa, CAN

🔹 Using GPIO in Zephyr

#include <zephyr.h>
#include <drivers/gpio.h>

#define LED_PORT DT_LABEL(DT_NODELABEL(gpio0))
#define LED_PIN 13

void main() {
    const struct device *dev = device_get_binding(LED_PORT);
    gpio_pin_configure(dev, LED_PIN, GPIO_OUTPUT);
    gpio_pin_set(dev, LED_PIN, 1);
}

Configures and toggles an LED using Zephyr’s GPIO API.


5. Power Management

Zephyr supports:
Tickless mode – Saves power by disabling unnecessary ticks.
CPU idle & deep sleep modes.

🔹 Enabling Low Power Mode

void main() {
    while (1) {
        k_sleep(K_MSEC(500)); // Puts the CPU to sleep
    }
}

Minimizes power consumption in battery-operated devices.


Getting Started with Zephyr

1. Installation Steps

  • Install Zephyr SDK:
    west init -m https://github.com/zephyrproject-rtos/zephyr.git zephyrproject
    cd zephyrproject
    west update
    
  • Build & Flash an Application:
    west build -b <board_name> samples/hello_world
    west flash
    
    Runs Zephyr on supported development boards (e.g., Nordic nRF, STM32, ESP32, etc.).

Conclusion

This guide covers Zephyr RTOS core concepts for beginners, including:
- Thread management & scheduling
- Memory management
- Synchronization & IPC
- Device drivers & power management