How to turn Helper Code into a Zephyr Module

Originally published at: How to turn Helper Code into a Zephyr Module - Golioth

Embedded developers always maintain sets of helper code that get used across multiple projects. With Zephyr RTOS, you can easily turn your helper code into a portable Zephyr module. Creating a Zephyr module means you can version control your code, making changes in one centralized, while targetting a specific git commit, tag, or branch when using the code in a project. You only need to add two or three files to qualify as a Zephyr module. And once the module is available you can make the code available to your Zephyr projects simply by adding it to your manifest file. A Working Example You may remember reading about Ostentus, Golioth’s custom faceplate for conference demos. This i2c device can be added to any Zephyr project by leveraging some helper code that simplifies sending data from the device to the faceplate. As this code will change in the future, it isn’t maintainable to copy it between projects, so we turned it into a Zephyr module. Let’s use this as an example. Here are the steps: Create a new repository to store your helper code Add a CMake file and an optional Kconfig file Add a module.yml file that tells Zephyr where to find files in this repo That creates the module, and the final step is to add it to your project’s West manifest. 1. Create a new repository For our example, we have just one C file and one header file. I created a new repository to store these files. Place the header file in a directory named include, you may add subdirectories if you want there be a category-like prefix when the header is included (e.g. #include <yoursubdirectory/yourfile.h>). Here…