zephyr_template
A minimal and scalable template for developing embedded applications with Zephyr RTOS. Includes structured project organization, unit testing with ZTest, and CI/CD integration
Summary
| Latest Version | Unknown |
|---|---|
| License | Unknown |
| CI Status | Failing |
| Stars | 1 |
| Forks | 0 |
| Open Issues | 0 |
| Last Commit | 2025-04-21 |
| Downloads | 0 |
| Last Indexed | 2026-09-07 06:08 |
Tags
Installation
nimble install zephyr_template
choosenim install zephyr_template
git clone https://gitlab.com/amoralesruiz/stm32l0_zephyr_template
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| zephyr_template | - | - | - | - | - | - | - | - | - | ✓ |
Source
| Repository | https://gitlab.com/amoralesruiz/stm32l0_zephyr_template |
|---|---|
| Homepage | https://gitlab.com/amoralesruiz/stm32l0_zephyr_template |
| Registry Source | gitlab |
README
Zephyr Project Template Documentation
Introduction
The purpose of this repository is to create a template for using Zephyr in designs with ST microcontrollers. I will use the Nucleo_L053R8 board because I have one available, and the intention is to explore the use of the low-power resources of this family of microcontrollers from ST with Zephyr.
Additionally, I want to add unit tests to the template using Ztest and execute the tests in a docker container using CI/CD with a GitLab runner on each commit.
Finally, I aim to control the code format using clang-format or astyle and generate the repository documentation using Doxygen.
Prerequisites
Before you begin, ensure you have the following prerequisites:
- Hardware:
-
Nucleo_L053R8 board (Note: This template can be used with any compatible hardware, but it is currently tested with the Nucleo_L053R8 board)
-
Software:
- Zephyr SDK
- Docker
- Git
- CMake
- Python 3.x
- West (Zephyr's meta-tool)
- Astyle (for code formatting)
-
Doxygen (for generating documentation)
-
Accounts:
-
GitLab account (for CI/CD setup)
-
Knowledge:
- Basic understanding of Zephyr RTOS
- Familiarity with Docker and GitLab CI/CD
- Basic knowledge of C/C++ programming
Repository Structure
The repository is organized as follows:
├── boards/ # Board-specific configuration and source files.
|
├── docker/ # Docker configuration files.
│ └── Dockerfile # Dockerfile for setting up the CI jobs environment.
|
├── docs/ # Documentation directory.
│ └── ... # Doxygen generated docs.
|
├── include/ # Header files directory.
│ └── ... # Header files for the project.
|
├── src/ # Source code directory.
│ ├── main.c # Main application code.
│ └── ... # Additional source files.
|
├── modules/ # Modules source code directory (maybe a git submodule).
│ ├── Template/ # Module template.
| | ├── zephyr/ # Zephyr-specific module files and configurations.
| | | └── module.yaml # Zephyr module template info metadata and configuration information.
| | ├── src/ # Source folder for module implementation.
| | | ├── template.c # Module template C file containing the implementation of the module's functionality.
| | | └── template.h # Module template header file defining the module's interface and public functions.
| | ├── CMakeLists.txt # CMake configuration file for building the module.
| | └── Kconfig # Configuration file for defining module-specific options.
│ └── ... # Other modules directories.
|
├── tests/ # Directory containing test-related files for the project.
│ ├── Template/ # Template module tests.
| | ├── src/ # Source folder for test implementation files.
| | | └── test_template.c # C file containing the module test implementation.
| | ├── CMakeLists.txt # CMake configuration file for building the test suite.
| | ├── prj.conf # Configuration file for defining test-specific options.
| | └── testcase.yaml # YAML file describing the test cases and their configurations.
│ └── ... # Other modules tests.
|
├── scripts/ # Utility scripts.
│ └── ... # Scripts for formating code and git hooks.
|
├── .astylerc # Astyle configuration file for code formatting.
├── .gitignore # Specifies files and directories to be ignored by Git.
├── .gitlab-ci.yml # GitLab CI/CD configuration file.
├── CMakeLists.txt # CMake build configuration file.
├── Doxyfile # Doxygen configuration.
├── LICENSE # License file for the project.
├── prj.conf # Project configuration file for Zephyr.
└── README.md # Project documentation. (This file)
Cloning the Repository
To properly clone this repository, follow these steps:
- Open a terminal on your system.
- Use the
git clonecommand to clone the repository:shell git clone https://gitlab.com/amoralesruiz/stm32l0_zephyr_template.gitYou are now ready to start working with the repository.
Setting Up the Development Environment
To set up the Zephyr development environment, follow these steps:
- Install Required Tools:
- Install Docker, Git, CMake, and Python 3.x using your system's package manager.Zephyr Getting Started Guide.
shell sudo apt install --no-install-recommends git cmake ninja-build gperf \ ccache dfu-util device-tree-compiler wget doxygen\ python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \ make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1 - Install virtual environment for python and create one env for zephyr python tools Zephyr python environment
shell sudo apt install python3-venv python3 -m venv ~/zephyrproject/.venv source ~/zephyrproject/.venv/bin/activate -
Install West, Zephyr's meta-tool, by running:
shell pip install west -
Initialize the Zephyr Workspace:
- Create a directory for your Zephyr workspace:
shell mkdir zephyrproject cd zephyrproject - Clone the Zephyr repository:
shell west init ~/zephyrproject cd ~/zephyrproject west update -
Export Zephyr environment variables:
shell west zephyr-export -
Install Python Dependencies:
-
Navigate to the Zephyr directory and install required Python dependencies:
shell west packages pip --install -
Add ZEPHYR path to bash*
- Edit your bash file
shell nano ~/.bashrc - Add this lines to the end of the file
shell export ZEPHYR_BASE=~/zephyrproject/zephyr export ZEPHYR_SDK_INSTALL_DIR=~/zephyr-sdk-0.17.0/ export ZEPHYR_TOOLCHAIN_VARIANT=zephyr -
Load changes
shell source ~/.bashrc -
Verify the Installation:
- Test your setup by listing all west packages:
shell west list
For more details, visit the Zephyr Project Documentation.
Building the Project
- Build native_sim aplication
shell west build -p always -b native_sim
Running Unit Tests with ztest
- Run ztest with twister
shell west twister -T ./tests -p native_sim
Continuous Integration and Deployment (CI/CD)
The CI/CD setup uses a local runner that executes various jobs within a local Docker container. This approach ensures a consistent and isolated environment for running tasks, leveraging the Docker image built specifically for the Zephyr environment.
Generate the Docker image containing a Zephyr environment
- Navigate to the template directory and enter the docker directory containing the generation Dockerfile:
This step takes a while.
shell cd docker docker build -t zephyr-runner . - Check the image is available.
shell docker images - Run the Docker image
shell docker run -it --rm --name zephyr-runner zephyr-runner /bin/bash - Test west in the container
shell west list - Exit the container.
shell exit
Setting Up a Local GitLab Runner
This template uses a GitLab Runner to execute CI/CD jobs. For more details about GitLab Runners, refer to the GitLab Runners Documentation.
To install and configure a local GitLab Runner, follow these steps:
- Download the GitLab Runner Binary:
-
Open a terminal and download the latest GitLab Runner binary for your operating system:
shell curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 -
Set Permissions:
-
Make the binary executable:
shell chmod +x /usr/local/bin/gitlab-runner -
Install GitLab Runner as a Service:
-
Install and start GitLab Runner as a system service:
shell gitlab-runner install gitlab-runner start -
Register the GitLab Runner:
- Register the runner with your GitLab instance by running:
shell gitlab-runner register -
During registration, provide the following details:
- GitLab URL: The URL of your GitLab instance (e.g.,
https://gitlab.com). - Registration Token: Found in your GitLab project under Settings > CI/CD > Runners.
- Runner Description: A name for the runner (e.g.,
local-runner). - Tags: Tags to identify the runner (e.g.,
build,test). - Executor: Select
shellas the executor type.
- GitLab URL: The URL of your GitLab instance (e.g.,
-
Verify the Runner:
- Navigate to your GitLab project under Settings > CI/CD > Runners to confirm the runner is active and registered.
For additional details, refer to the GitLab Runner Installation Guide.
- Test the Runner:
-
Add a test
.gitlab-ci.ymlfile to your repository with a simple pipeline: ```yaml stages:- build
- test
build: stage: build script: - echo "Compiling the project..." - sleep 5 - echo "Compilation successful"
test: stage: test script: - echo "Running tests with Twister..." - sleep 5 - echo "Tests completed" ``` - Commit the file and push it to your repository. The runner should pick up the job and execute it.
Adding New Modules
To create and integrate new modules into the project structure, follow these steps:
- Create a New Module Directory:
- Navigate to the
modules/directory: -
Create a new directory for your module:
-
Set Up the Module Structure:
-
Use the
Templatemodule as a reference to structure your new module. The directory should include the following:├── zephyr/ # Zephyr-specific module files and configurations. │ └── module.yaml # Metadata and configuration information for the module. ├── src/ # Source folder for module implementation. │ ├── <module>.c # C file containing the implementation of the module's functionality. │ └── <module>.h # Header file defining the module's interface and public functions. ├── CMakeLists.txt # CMake configuration file for building the module. └── Kconfig # Configuration file for defining module-specific options. -
Edit
module.yaml: -
Update the
module.yamlfile in thezephyr/directory with the appropriate metadata for your module. For example:yaml name: <ModuleName> build: cmake: ./ kconfig: Kconfig -
Implement Module Functionality:
- Write the implementation of your module in the
<module>.cfile. -
Define the module's public interface in the
<module>.hfile. -
Configure CMake:
-
Update the
CMakeLists.txtfile to include the source files and dependencies for your module. For example: ```cmake # add the module source files directory zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)add the module source files
zephyr_library_sources(${CMAKE_CURRENT_SOURCE_DIR}/src/
.c) ``` -
Define Module Options:
-
Use the
Kconfigfile to define configuration options specific to your module. For example:kconfig config <MODULE_NAME> bool "Enable <ModuleName>" default y help Enable the <ModuleName> module. -
Integrate the Module:
-
Add the module to the project by including it in the
CMakeLists.txtfile of the main application or other modules that depend on it.cmake set(ZEPHYR_EXTRA_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/modules/<ModuleName>") -
Test the Module:
- Create a corresponding test directory under
tests/(e.g.,tests/<ModuleName>). -
Follow the structure of the
Templatemodule tests to implement unit tests for your module. -
Build and Verify:
- Build the project to ensure the module is correctly integrated:
shell west build -p always -b <board_name> - Run the tests to verify the module's functionality:
shell west twister -T ./tests -p <board_name>
Generate the Code Docs
To generate the code documentation using Doxygen, follow these steps:
- Install Doxygen: Ensure Doxygen is installed on your system. You can install it using your package manager:
sudo apt install doxygen
-
Locate the Doxyfile: The
Doxyfileconfiguration file is located in the root of the repository. -
Generate the Documentation: Run the following command in the repository root to generate the documentation:
doxygen Doxyfile
-
View the Documentation: The generated documentation will be available in the
docs/directory. Open theindex.htmlfile in a web browser to view the documentation. -
Export the documentation to a PDF file
To export the documentation to a PDF file, follow these steps:
Ensure that LaTeX and the required tools are installed on your system. You can install them using your package manager:
sudo apt install texlive texlive-latex-extra texlive-fonts-recommended latexmk
Navigate to the LaTeX Directory and run the make command to compile the LaTeX files and generate the PDF:
cd docs/latex
make pdf
After the process completes, the generated PDF file will be available in the same directory. Look for a file named refman.pdf.
For more details on configuring and using Doxygen, refer to the Doxygen Manual. For more details on LaTeX and troubleshooting, refer to the LaTeX Project Documentation.
Debugging and Troubleshooting
TODO: Provide tips and common solutions for debugging and resolving issues during development.
Contributing Guidelines
TODO: Define the process for contributing to the project, including code style, pull requests, and issue tracking.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Additional Resources
TODO: List references, documentation links, and any further reading materials for users and contributors.