BACK TO BLOG

A Modern Approach to Building Embedded Solutions with Zephyr OS

Published Date

May 2, 2024

Read

11 minutes

Written By

Ankit Chauhan

In today's industry landscape, IoT solutions are gaining traction, bringing in cutting-edge technologies and innovations worldwide. These solutions offer a range of benefits that seamlessly integrate with various systems. The Linux-like system plays a crucial role in enabling this flexibility, providing scalable, portable, and readily deployable modules. This makes implementing such systems quicker, allowing businesses to tap into the thriving IoT market swiftly.

Using the Linux system in your solution typically demands a platform that can handle higher speeds, more storage, and complex hardware, which might not fit all IoT applications. IoT is meant to be a compact solution that simplifies daily life. That's why Microcontrollers are the top choice for building IoT solutions. However, developing microcontrollers can also be tough and time-consuming. Consider a system that has the advantage of a Linux-like environment seamlessly fitting into even the tiniest of controllers. This system boasts all the robust features of Linux without demanding hefty hardware and processing capabilities. Thanks to Zephyr OS, this once distant possibility is now a reality.

About Zephyr OS

Zephyr OS is an open-source project designed and developed by the Linux Foundation back in 2016. Similar to Linux systems, it offers widely compatible, customizable, secure, portable, and well-organized operating systems for the delicate devices used in IoT systems. As a real-time Operating System, Zephyr is highly adaptive, with a tiny core designed to provide power efficiency and security, aligning perfectly with its development concept. Zephyr is an event-driven system that offers the advantage of rescheduling along with the benefits of FreeRTOS event management systems.

Key Features of Zephyr

Zephyr offers a modular architecture, divided into five pillars: the Physical layer, Driver layer, Wrappers/APIs, Application layer, and User interface layer. These layers work together seamlessly, forming an integrated platform that eliminates the need for managing multiple libraries and extra middleware.

In contrast with the interconnection of these layers, it facilitates developers to leverage the layers as per the system requirements. Each layer handles its dependencies, compatibility, and building processes independently, simplifying user concerns about individual layers. Moreover, Zephyr boasts a sturdy and secure kernel supporting various scheduling algorithms, memory protection, and fault handling, adding a modern touch to its architecture.

Being open-source, Zephyr has fostered a vibrant community of developers worldwide. It provides extensive documentation, numerous samples, a wide array of libraries, and Software Development Kits (SDKs), enriching the development experience for users. Developers familiar with Linux systems can seamlessly transition to Zephyr. With its comprehensive toolkit, documentation, and sample resources, Zephyr can be adapted to virtually any IoT application.

Deep Drive into Zephyr Build System

West (Zephyr’s Meta-tool)

West is a meta-tool that supports its own language to manage an entire ecosystem. Consider a complex system that is divided into multiple repositories and platforms. The setup of such a system is a tedious job. But with West, that complexity melts away. The file, called west.yml, contains built-in commands that facilitate multiple repository management, including its submodules, library structuring, execution steps, and essential information for integrating required drivers into the project. All of this functionality is encapsulated within a single file, which Zephyr utilizes for building applications, flashing, and even debugging.

Let's take an example, if we are building a Zephyr sample application for ESP32, we can use 'west update' to obtain all the necessary repositories and files required for building the code for ESP. Then, 'west build' compiles the entire source code, generating the executable for ESP32. Finally, 'west flash' loads the executable onto the hardware, and voila! Your application is up and running on your hardware.

What's interesting here is that if we intend to build the same application for a Nordic chip like nRF, we can still employ the same commands—'west build' and 'west flash'—to compile and flash. This feature eliminates the need for configuring different environments for individual hardware platforms, selecting various toolchains, and using separate commands for each platform. With just one command, West manages it all.

Build System

As we all know, the Zephyr application has the capability to support multiple repositories. Therefore, a robust build system is essential to consolidate each repository into a single source and produce the executable using the chosen toolchain. CMake proves to be the ideal choice for such a scenario. It enables the selection of various configurations, libraries, HAL, modules, and board support for the application build process, amalgamating them into a unified binary.

A CMake build can be divided into two parts: Configuration and Building. The configuration part executes the CMakeLists.txt script, which is responsible for collecting and including the required libraries and modules for the application, and prepares your application for building. The building part finally builds each stage of the application, reconfigures them automatically if required, and generates the binary.

The simplest form of building a Zephyr application typically includes the following steps:

 

Zephyr build system

Device Tree

A device tree acts as a blueprint for the physical interfaces supported by a hardware device, such as a System on Chip (SoC). In simpler terms, it's like a map that lays out all the peripherals, clocks, and interfaces available on the hardware. Each of these components is represented as a node in the device tree file, making it easier for the software layer (specifically drivers) to access and utilize them.

To define such a structure of hardware, Zephyr provides a set of source files to describe the hardware and configuration of each board. The file is called Device Tress Source(.dts) file and Device Tree Source Include(.dtsi) files. The source file mainly contains the list of hardware interfaces in the form of nodes like USB, UART, I2C, SPI, Clock, Timer, etc. The include/interface file has the pin mapping, configuration, etc of each interface node. To modify the pins and interfaces as per the custom hardware, the device tree structure offers another file called overlay(.overlay). The overlay file can be hardware-specific or application-specific and can be used as a part of the application.

Every board, hardware, or SoC comes with its own device tree files, which can be easily utilized in any Zephyr application by simply selecting the board. Once selected, the building process automatically generates code using device tree binding files and a set of rules to produce the necessary C definitions for that board.

Board Support

A board in Zephyr consists of a collection of device tree files, configurations, and bindings that describe the hardware system.

Zephyr, as an open-source community, boasts over 500 readily available boards from various architectures and vendors worldwide. These boards are equipped to cater to a diverse range of needs. Whether it's applications, sensors, peripherals, communication or radio interfaces such as BLE, Wi-Fi and more, Zephyr's got it covered with its extensive library of sample codes. So, all you need to do is pick your board, select your sample code, and kickstart your project by running the west build command. It's that simple – your binary will be generated, and your sample application will be up and running on your hardware in no time.

Additionally, if you have custom hardware, you can utilize the available boards and modify your hardware configurations through the overlay file. Zephyr also allows you to create your custom board tailored to your hardware and immediately integrate it into your project. The process of creating a new board is straightforward, typically involving the creation of a few configuration files and the utilization of existing board support file references.

Device Drivers

Device drivers are sets of code that interact with the physical layer and exchange data. Drawing inspiration from the Linux system, Zephyr device drivers manage hardware functionality across three distinct levels.

At the first level, we have Driver Implementations. These implementations directly interact with hardware through low-level APIs, facilitating communication down to the register levels. For example, the I2C protocol exchanges data via SDA and SCL pins. Moving up, the second level encompasses Driver Instances. These instances offer encapsulated access to driver APIs tailored for particular protocols and methods of data exchange. For instance, driver files for components like temperature sensors or LED drivers utilize interfaces like I2C for communication. Lastly, we have the Device Driver APIs at the third level. These APIs furnish generic interfaces for accessing actual data. For instance, a callback API like "fetch value" can retrieve temperature readings from any compatible sensor.

This is possible in Zephyr due to its consistent hardware abstraction layer (HAL) and containers, enabling applications to run across a wide range of edge devices with minimal or no modifications. The Device Driver model is designed to facilitate the creation of new custom drivers by referencing available drivers and integrating them into Zephyr's main streamline effortlessly.

Zephyr device driver architecture

Zephyr has already listed each driver type (e.g., UART, SPI, I2C, USB, BT, etc.), which is ready to use and platform-independent. This means the same driver can be used on different hardware platforms as stated in the device tree.

Development

Zephyr leveraged the ongoing development stream and techniques, enabling easy adaptation by developers. With its strong foundation in Linux, developers experienced in Linux can seamlessly transition to Zephyr development. The suite of tools provided by Zephyr automates many tasks, allowing users to focus entirely on building their applications during development.

Taking full advantage of its open-source nature, Zephyr boasts a rich array of resources, including samples, documentation, tutorials, and guides, facilitating developers' initial foray into the platform. Furthermore, Zephyr has built a strong and supportive community, providing guidance and assistance to developers with issues and doubts.

Each module in Zephyr's documentation guides users step-by-step through setting up, assembling, and testing their IoT solutions. Furthermore, vendors like Nordic, Espressif, NXP, etc., share their own SDKs that support Zephyr, tailored to their respective IoT solutions.

Security

As a lightweight Linux-like system, Zephyr operates using a microkernel design that separates the kernel from other components such as device drivers, file systems, and applications. This design effectively manages application threads and queues while maintaining compatibility with various hardware layers.

In addition to its application perspective, Zephyr incorporates MCUboot as a bootloader dedicated to handling the booting of Microcontrollers, ensuring only valid and trusted applications are launched. MCUboot leverages the secure boot process and can easily handle the booting of multi-core controllers. The USB and BLE Device Firmware Update (DFU) feature, managed by MCUboot, ensures that only firmware signed with a valid key can be loaded remotely onto the hardware. If an invalid application is detected, MCUboot will discard the firmware and roll back to the previous version.

About ACL Digital

ACL Digital specializes in leveraging the power of Zephyr OS to develop cutting-edge solutions for a variety of industries, including IoT and Healthcare sectors. Our team of experts is dedicated to helping our clients build next-generation embedded solutions that meet their unique needs and exceed their expectations. By harnessing the capabilities of Zephyr OS, we streamline the development process and reduce the complexity of the end product, ensuring our clients achieve their goals efficiently and effectively.

To know more about ACL Digital's prowess in embedded engineering, feel free to contact us. Reach out via email at business@acldigital.com to connect with our team.

About the Author

Ankit Chauhan Senior Engineer

Ankit is a Senior Firmware Engineer at ACL Digital. He has been associated with the Embedded Domain for the last 8 years, mainly contributing to the design and development of embedded systems, IoT solutions, home automation/safety solutions, and healthcare solutions. He has expertise in Linux user space and kernel space applications, Zephyr OS, RTOS, and bare-metal application and driver development. He contributes his expertise in Zephyr OS to the development of healthcare solutions.

Related Posts

A Modern Approach to Building Embedded Solutions with Zephyr OS

Published Date: May 02, 2024

By: Ankit Chauhan

Unleashing the Potential of Android Open Source Project (AOSP)

Published Date: January 04, 2024

By: ACL Digital

Best Practices of Porting Android OS to Embedded Platforms

Published Date: January 04, 2024

By: ACL Digital