Getting Started with
Zephyr RTOS
Jacob Beningo Mohammed Billoo
www.embeddedonlineconference.com
Simplifying Concepts, Delivering Success℠ 2
THE SPEAKER Beningo Embedded Group
CEO & Founder
Focus: Embedded Software Consulting and Training
Specializes in the design of real-time,
microcontroller based embedded software.
Blogs for Embedded.com, DesignNews.com, etc,
focused on embedded system design techniques
and challenges.
Visit www.beningo.com to learn more …
©2023 Beningo Embedded Group. All Rights Reserved.
0 Session Goals
“Without a defined path, every path is aimless”
- Unknown
AGENDA
Introducing The Zephyr
1 Project 4 “Hello World!” Example
2 Zephyr RTOS Fundamentals 5 Advanced Topics
3 Zephyr Application Model 6 Going Further
© EmbeddedOnlineConference.com All rights reserved EmbeddedOnlineConference.com
1 Introducing the Zephyr Project
Introducing the Zephyr Project
An open-source RTOS (Apache 2.0)
• Linux Foundation supported
• Built with safety and security in mind
• Lightweight kernel with supporting services
• Connectivity stacks like Wi-Fi, BLE, USB, MQTT, etc
• Developer Friendly
• Logging, tracing, built-in-shell, Windows/Linux/macOS
• Ships with SBOMs (Software Bill of Materials)
• Supports more than 500 boards
Additional Resources: Zephyr Project Overview Document
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Introducing the Zephyr Project
Zephyr RTOS Architecture
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Introducing the Zephyr Project
Zephyr APIs
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Introducing the Zephyr Project
POSIX API’s
POSIX (Portable Operating System Interface for Unix) is a set
of standards that define the API (Application Programming
Interface) for Unix-like operating systems
API Set Description / Use
pthread_create() Create a thread
pthread_mutex_lock() Lock a shared resource
pthread_cond_wait() Wait on a condition
sem_post() Give a semaphore token
open() Open a file
etc Other POSIX APIs
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Introducing the Zephyr Project
Zephyr Kernel APIs
API Set Description / Use
Threads Creating, scheduling, and deleting independent
executable threads of instructions
Scheduling Idle, sleep, yield
Workqueue Threads Uses a dedicated thread to process work items in
a first in, first out manner
Interrupts Multi-level interrupt handling
Semaphores Implements a traditional counting semaphore.
Mutexes Implements a traditional reentrant mutex
Events Implements traditional events
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
2 Zephyr RTOS Fundamentals
© EmbeddedOnlineConference.com All rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Tasks, Threads, and Processes
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Thread States
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Thread Creation
#define MY_STACK_SIZE (500U)
TBD
•#define MY_PRIORITY (5U)
extern void Thread_LedBlinky(void *, void *, void *);
K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);
Stack Definitions
struct k_thread my_thread_data;
k_tid_t my_tid = k_thread_create(&my_thread_data, my_stack_area,
K_THREAD_STACK_SIZEOF(my_stack_area),
my_entry_point, NULL, NULL, NULL,
MY_PRIORITY, 0, K_NO_WAIT);
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Thread Creation
K_THREAD_DEFINE(name, stack_size, entry, p1, p2, p3, prio,
options, delay)
#define MY_STACK_SIZE (500U)
#define MY_PRIORITY (5U)
extern void Thread_LedBlinky(void *, void *, void *);
K_THREAD_DEFINE(my_tid, // Name of the thread (not dev defined!)
MY_STACK_SIZE, // Stack size in bytes
Thread_LedBlinky, // Thread Entry Function
NULL, NULL, NULL, // Custom parameters
MY_PRIORITY, // Thread Priority
0, // Thread Options
0); // Start scheduling delay
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Semaphores
A semaphore is used to synchronize application behavior between
• An ISR and a task
• One task to another task
• Occasionally are used to protect a shared resource but this is not their
primary purpose (Not Recommended)
// Define a semaphore void consumer_thread(void){
struct k_sem my_sem; ...
k_sem_init(&my_sem, 0, 10);
if (k_sem_take(&my_sem, K_MSEC(50)) != 0) {
// Define a semaphore with macro printk("Input data not available!");
K_SEM_DEFINE(my_sem, 0, 1); } else { . . .
}
// Semaphore Operations
...
k_sem_give(&my_sem);
}
k_sem_take(&my_sem, K_MSEC(50)
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
Zephyr RTOS Fundamentals
Mutexes
A mutex is used to gain access to a shared resource such as
• A memory location
• A common peripheral like a UART
// Define a mutex void consumer_thread(void){
struct k_mutex my_mutex; ...
k_mutex_init(&my_mutex);
if (k_mutex_lock(&my_mutex, K_MSEC(50)) != 0) {
// Define a mutex with a macro printk(”Accessing Shared Data!");
K_MUTEX_DEFINE(my_mutex); } else {
// Mutex Operations
printk(”Cannot lock Shared Data!");
k_mutex_lock(&my_mutex, K_FOREVER);
}
k_mutex_unlock(&my_mutex); ...
}
©©
2022
E mBeningo
b e d d e dEmbedded
O n l i n e C oGroup,
n f e r e nLLC.
c e . All
com Rights
A l l Reserved
rights reserved EmbeddedOnlineConference.com
3 Introducing The Zephyr Project
RTOS
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
THE SPEAKER • Embedded Software
[email protected] Consultant
• Design Work
• Medical Devices
• Scientific Instruments
• LIDAR
• Custom ASIC
• FPGA
• Expertise
• Zephyr
• Yocto/Embedded Linux
/mab-embedded • Qt
@mabembedded
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
The Zephyr Project RTOS
Self-contained
• Use “West” to download everything
• Support for:
• CPUs
• Boards
• Bootloader
• Subsystems
• Peripherals
• Drivers are vetted
• Thriving open-source community with maintainers
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Getting Started
https://www.nordicsemi.com/Products/Developm
ent-tools/nrf-connect-for-desktop
nRF Connect For Desktop
• An application that sets up a complete
Zephyr development environment
• Manages West
• VS Code Plugins!
• Create new project
§ Based on example
• Open existing project
• Configure Zephyr
• Build and debug project
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
nRF Connect For Desktop
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
nRF Connect SDK
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
nRF Connect SDK
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
nRF Connect SDK
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Application Organization
Will review application
organization later
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Zephyr Organization
External libraries
RTOS
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Zephyr Organization
Third party FATFS library
(included with Zephyr)
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Zephyr Organization
Interface from Zephyr to
FATFS library
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Zephyr Organization
CPUs
Boards
Device drivers
CPUs
Core kernel Subsystem
Samples!!
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
4 Zephyr Application Model
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Key Elements
C/C++
source files
Relies on
CMake
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Project Configuration
Enables the GPIO
subsystem in Zephyr
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Project Configuration
Configuration can be
exhaustive!
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Performing a Build
First need to create a configuration
• Specify
• Target board
• Zephyr configuration
• “Devicetree Overlay”
§ Custom hardware connected to board
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Performing a Build
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Performing a Build
SUCCESS!!
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Flashing and Testing Blinky
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Debugging Blinky
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Memory Reports
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
5 “Hello World!” Example
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
6 Advanced Topics
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
KConfig
Borrowed from Linux
• Used to configure Zephyr features
• Subsystems
• Similar to specifying macros in other RTOSes
• Can configure all features in RTOS
• Not just kernel options
• Configuration
• Can be file based
§ Usually “prj.conf”
• Or use GUI
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
KConfig
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
KConfig
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Devicetree
Borrowed from Linux
• Used to configure hardware
• Pinmux
• Pinout
• Board peripherals
• Part of the build process
• Start with “top-level” device tree file (provided by vendor)
§ Contains references to other device tree files (e.g. pinout configuration)
• Can customize devicetree using “overlays”
§ Custom hardware
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Devicetree Overlay
https://github.com/mabembedded/zephyr-sd-spi
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
Improving IoT Devices With Device Reliability Engineering
• Device Performance Monitoring
• Device Debugging
• Over-the-Air Updates
• Built-in to Zephyr!
• Can try sample under samples/debug/memfault
§ Works with nrf7002 WiFi development kit
§ https://www.nordicsemi.com/Products/Development-
hardware/nRF7002-DK
© 2023 MAB Labs. All rights reserved EmbeddedOnlineConference.com
7 Going Further
Going Further
Interested in going deeper into Zephyr and RTOS development?
Here are some additional resources you may find helpful:
Register for $195 ($100 off) with coupon
code: ZEPHYR_WEB23
Live sessions start January 18, 2024 EMBEDDEDONLINECONFERENCE.COM
https://beningo.mykajabi.com/offers/xeJBY7k5/checkout
© EmbeddedOnlineConference.com All rights reserved EmbeddedOnlineConference.com
THANK YOU
w w w . e m b e d d e d o n l i n e c o n f e r e n c e . c o m