欢迎光临
我们一直在努力

Zephyr Hello World应用开发构建完全指南

Zephyr Hello World应用开发构建完全指南

引言

Hello World是每个程序员学习新语言或新平台的第一步。对于Zephyr RTOS来说,Hello World应用不仅展示了基本的开发流程,更重要的是让开发者理解Zephyr的项目结构、构建系统和运行机制。

本文将详细介绍如何在Zephyr下创建、构建和运行一个Hello World应用,帮助您快速入门Zephyr开发。


一、开发环境准备

1.1 环境要求

在开始之前,确保您已经完成了以下准备工作:

#mermaid-svg-bK64vij3GS5zx7JM{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bK64vij3GS5zx7JM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bK64vij3GS5zx7JM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bK64vij3GS5zx7JM .error-icon{fill:#552222;}#mermaid-svg-bK64vij3GS5zx7JM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bK64vij3GS5zx7JM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bK64vij3GS5zx7JM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bK64vij3GS5zx7JM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bK64vij3GS5zx7JM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bK64vij3GS5zx7JM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bK64vij3GS5zx7JM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bK64vij3GS5zx7JM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bK64vij3GS5zx7JM .marker.cross{stroke:#333333;}#mermaid-svg-bK64vij3GS5zx7JM svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bK64vij3GS5zx7JM p{margin:0;}#mermaid-svg-bK64vij3GS5zx7JM .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-bK64vij3GS5zx7JM .cluster-label text{fill:#333;}#mermaid-svg-bK64vij3GS5zx7JM .cluster-label span{color:#333;}#mermaid-svg-bK64vij3GS5zx7JM .cluster-label span p{background-color:transparent;}#mermaid-svg-bK64vij3GS5zx7JM .label text,#mermaid-svg-bK64vij3GS5zx7JM span{fill:#333;color:#333;}#mermaid-svg-bK64vij3GS5zx7JM .node rect,#mermaid-svg-bK64vij3GS5zx7JM .node circle,#mermaid-svg-bK64vij3GS5zx7JM .node ellipse,#mermaid-svg-bK64vij3GS5zx7JM .node polygon,#mermaid-svg-bK64vij3GS5zx7JM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bK64vij3GS5zx7JM .rough-node .label text,#mermaid-svg-bK64vij3GS5zx7JM .node .label text,#mermaid-svg-bK64vij3GS5zx7JM .image-shape .label,#mermaid-svg-bK64vij3GS5zx7JM .icon-shape .label{text-anchor:middle;}#mermaid-svg-bK64vij3GS5zx7JM .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-bK64vij3GS5zx7JM .rough-node .label,#mermaid-svg-bK64vij3GS5zx7JM .node .label,#mermaid-svg-bK64vij3GS5zx7JM .image-shape .label,#mermaid-svg-bK64vij3GS5zx7JM .icon-shape .label{text-align:center;}#mermaid-svg-bK64vij3GS5zx7JM .node.clickable{cursor:pointer;}#mermaid-svg-bK64vij3GS5zx7JM .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-bK64vij3GS5zx7JM .arrowheadPath{fill:#333333;}#mermaid-svg-bK64vij3GS5zx7JM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bK64vij3GS5zx7JM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bK64vij3GS5zx7JM .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bK64vij3GS5zx7JM .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-bK64vij3GS5zx7JM .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bK64vij3GS5zx7JM .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-bK64vij3GS5zx7JM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bK64vij3GS5zx7JM .cluster text{fill:#333;}#mermaid-svg-bK64vij3GS5zx7JM .cluster span{color:#333;}#mermaid-svg-bK64vij3GS5zx7JM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bK64vij3GS5zx7JM .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-bK64vij3GS5zx7JM rect.text{fill:none;stroke-width:0;}#mermaid-svg-bK64vij3GS5zx7JM .icon-shape,#mermaid-svg-bK64vij3GS5zx7JM .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bK64vij3GS5zx7JM .icon-shape p,#mermaid-svg-bK64vij3GS5zx7JM .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-bK64vij3GS5zx7JM .icon-shape .label rect,#mermaid-svg-bK64vij3GS5zx7JM .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bK64vij3GS5zx7JM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-bK64vij3GS5zx7JM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-bK64vij3GS5zx7JM :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

开发环境

操作系统

工具链

Zephyr SDK

West工具

Linux

macOS

Windows WSL

CMake

Ninja

Python 3

Git

ARM工具链

x86工具链

RISC-V工具链

1.2 安装步骤

步骤1:安装依赖工具

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y \\
build-essential \\
git \\
python3-pip \\
cmake \\
ninja-build \\
gperf \\
ccache \\
dfu-util \\
device-tree-compiler

# macOS
brew install cmake python ninja gperf ccache dfu-util dtc

# Windows (使用Chocolatey)
choco install cmake python ninja gperf

步骤2:安装West工具

pip3 install –user west
export PATH="$HOME/.local/bin:$PATH"

步骤3:初始化Zephyr工作空间

west init ~/zephyrproject
cd ~/zephyrproject
west update

步骤4:安装Zephyr SDK

# Linux
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.4/zephyr-sdk-0.16.4_linux-x86_64.tar.xz
tar -xf zephyr-sdk-0.16.4_linux-x86_64.tar.xz
cd zephyr-sdk-0.16.4
./setup.sh

# macOS
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.4/zephyr-sdk-0.16.4_macos-x86_64.tar.xz
tar -xf zephyr-sdk-0.16.4_macos-x86_64.tar.xz
cd zephyr-sdk-0.16.4
./setup.sh

步骤5:验证安装

# 检查west版本
west –version

# 检查工具链
arm-zephyr-eabi-gcc –version

# 检查QEMU
qemu-system-x86_64 –version


二、创建Hello World项目

2.1 使用West创建项目

方法1:使用官方模板

cd ~/zephyrproject

# 使用app模板创建项目
west create -t app my_hello_world

# 进入项目目录
cd my_hello_world

方法2:基于官方示例

# 复制官方示例
cp -r zephyr/samples/hello_world my_hello_world
cd my_hello_world

方法3:手动创建项目

# 创建目录结构
mkdir -p my_hello_world/src
cd my_hello_world

2.2 项目目录结构

my_hello_world/
├── CMakeLists.txt # CMake构建配置文件
├── prj.conf # Kconfig配置文件
├── src/ # 源代码目录
│ └── main.c # 主程序文件
├── boards/ # 板级配置(可选)
│ └── qemu_x86.overlay # 设备树覆盖文件(可选)
└── README.md # 项目说明文档

#mermaid-svg-H65qHitLWZbYiwfC{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-H65qHitLWZbYiwfC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-H65qHitLWZbYiwfC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-H65qHitLWZbYiwfC .error-icon{fill:#552222;}#mermaid-svg-H65qHitLWZbYiwfC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-H65qHitLWZbYiwfC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-H65qHitLWZbYiwfC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-H65qHitLWZbYiwfC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-H65qHitLWZbYiwfC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-H65qHitLWZbYiwfC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-H65qHitLWZbYiwfC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-H65qHitLWZbYiwfC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-H65qHitLWZbYiwfC .marker.cross{stroke:#333333;}#mermaid-svg-H65qHitLWZbYiwfC svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-H65qHitLWZbYiwfC p{margin:0;}#mermaid-svg-H65qHitLWZbYiwfC .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-H65qHitLWZbYiwfC .cluster-label text{fill:#333;}#mermaid-svg-H65qHitLWZbYiwfC .cluster-label span{color:#333;}#mermaid-svg-H65qHitLWZbYiwfC .cluster-label span p{background-color:transparent;}#mermaid-svg-H65qHitLWZbYiwfC .label text,#mermaid-svg-H65qHitLWZbYiwfC span{fill:#333;color:#333;}#mermaid-svg-H65qHitLWZbYiwfC .node rect,#mermaid-svg-H65qHitLWZbYiwfC .node circle,#mermaid-svg-H65qHitLWZbYiwfC .node ellipse,#mermaid-svg-H65qHitLWZbYiwfC .node polygon,#mermaid-svg-H65qHitLWZbYiwfC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-H65qHitLWZbYiwfC .rough-node .label text,#mermaid-svg-H65qHitLWZbYiwfC .node .label text,#mermaid-svg-H65qHitLWZbYiwfC .image-shape .label,#mermaid-svg-H65qHitLWZbYiwfC .icon-shape .label{text-anchor:middle;}#mermaid-svg-H65qHitLWZbYiwfC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-H65qHitLWZbYiwfC .rough-node .label,#mermaid-svg-H65qHitLWZbYiwfC .node .label,#mermaid-svg-H65qHitLWZbYiwfC .image-shape .label,#mermaid-svg-H65qHitLWZbYiwfC .icon-shape .label{text-align:center;}#mermaid-svg-H65qHitLWZbYiwfC .node.clickable{cursor:pointer;}#mermaid-svg-H65qHitLWZbYiwfC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-H65qHitLWZbYiwfC .arrowheadPath{fill:#333333;}#mermaid-svg-H65qHitLWZbYiwfC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-H65qHitLWZbYiwfC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-H65qHitLWZbYiwfC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H65qHitLWZbYiwfC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-H65qHitLWZbYiwfC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H65qHitLWZbYiwfC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-H65qHitLWZbYiwfC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-H65qHitLWZbYiwfC .cluster text{fill:#333;}#mermaid-svg-H65qHitLWZbYiwfC .cluster span{color:#333;}#mermaid-svg-H65qHitLWZbYiwfC div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-H65qHitLWZbYiwfC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-H65qHitLWZbYiwfC rect.text{fill:none;stroke-width:0;}#mermaid-svg-H65qHitLWZbYiwfC .icon-shape,#mermaid-svg-H65qHitLWZbYiwfC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H65qHitLWZbYiwfC .icon-shape p,#mermaid-svg-H65qHitLWZbYiwfC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-H65qHitLWZbYiwfC .icon-shape .label rect,#mermaid-svg-H65qHitLWZbYiwfC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H65qHitLWZbYiwfC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-H65qHitLWZbYiwfC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-H65qHitLWZbYiwfC :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

my_hello_world/

CMakeLists.txt

prj.conf

src/

boards/

构建配置

Kconfig选项

main.c

设备树覆盖


三、项目文件详解

3.1 CMakeLists.txt

cmake_minimum_required(VERSION 3.20.0)

# 查找Zephyr包
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

# 项目名称
project(my_hello_world)

# 添加源代码文件
target_sources(app PRIVATE src/main.c)

关键指令说明:

指令说明
cmake_minimum_required 指定CMake最低版本要求
find_package(Zephyr) 查找并加载Zephyr构建系统
project() 定义项目名称
target_sources() 指定编译源文件

3.2 prj.conf

# 基础配置
CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

# 控制台配置
CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y
CONFIG_CONSOLE_INPUT=y

# 调试配置
CONFIG_DEBUG=y
CONFIG_LOG=y
CONFIG_LOG_LEVEL_DBG=y

关键配置说明:

配置项说明默认值
CONFIG_KERNEL 启用内核 y
CONFIG_CONSOLE 启用控制台 y
CONFIG_SERIAL 启用串口 y
CONFIG_UART_CONSOLE 使用UART作为控制台 y
CONFIG_DEBUG 启用调试模式 n
CONFIG_LOG 启用日志系统 n

3.3 src/main.c

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

/* 定义线程栈大小 */
#define STACK_SIZE 1024

/* 定义线程优先级 */
#define PRIORITY 5

/* 线程栈定义 */
K_THREAD_STACK_DEFINE(my_thread_stack, STACK_SIZE);

/* 线程控制块 */
static struct k_thread my_thread_data;

/* 线程函数 */
void my_thread_func(void *p1, void *p2, void *p3)
{
while (1) {
printk("Hello from my thread!\\n");
k_msleep(1000);
}
}

/* 主函数 */
void main(void)
{
printk("Hello World! Zephyr RTOS\\n");

/* 创建线程 */
k_thread_create(&my_thread_data, my_thread_stack,
K_THREAD_STACK_SIZEOF(my_thread_stack),
my_thread_func, NULL, NULL, NULL,
PRIORITY, 0, K_NO_WAIT);
}

代码结构说明:

#mermaid-svg-9qZpWhRWAKkCVHAG{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9qZpWhRWAKkCVHAG .error-icon{fill:#552222;}#mermaid-svg-9qZpWhRWAKkCVHAG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9qZpWhRWAKkCVHAG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9qZpWhRWAKkCVHAG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9qZpWhRWAKkCVHAG .marker.cross{stroke:#333333;}#mermaid-svg-9qZpWhRWAKkCVHAG svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9qZpWhRWAKkCVHAG p{margin:0;}#mermaid-svg-9qZpWhRWAKkCVHAG .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster-label text{fill:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster-label span{color:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster-label span p{background-color:transparent;}#mermaid-svg-9qZpWhRWAKkCVHAG .label text,#mermaid-svg-9qZpWhRWAKkCVHAG span{fill:#333;color:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG .node rect,#mermaid-svg-9qZpWhRWAKkCVHAG .node circle,#mermaid-svg-9qZpWhRWAKkCVHAG .node ellipse,#mermaid-svg-9qZpWhRWAKkCVHAG .node polygon,#mermaid-svg-9qZpWhRWAKkCVHAG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9qZpWhRWAKkCVHAG .rough-node .label text,#mermaid-svg-9qZpWhRWAKkCVHAG .node .label text,#mermaid-svg-9qZpWhRWAKkCVHAG .image-shape .label,#mermaid-svg-9qZpWhRWAKkCVHAG .icon-shape .label{text-anchor:middle;}#mermaid-svg-9qZpWhRWAKkCVHAG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9qZpWhRWAKkCVHAG .rough-node .label,#mermaid-svg-9qZpWhRWAKkCVHAG .node .label,#mermaid-svg-9qZpWhRWAKkCVHAG .image-shape .label,#mermaid-svg-9qZpWhRWAKkCVHAG .icon-shape .label{text-align:center;}#mermaid-svg-9qZpWhRWAKkCVHAG .node.clickable{cursor:pointer;}#mermaid-svg-9qZpWhRWAKkCVHAG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9qZpWhRWAKkCVHAG .arrowheadPath{fill:#333333;}#mermaid-svg-9qZpWhRWAKkCVHAG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9qZpWhRWAKkCVHAG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9qZpWhRWAKkCVHAG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9qZpWhRWAKkCVHAG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9qZpWhRWAKkCVHAG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9qZpWhRWAKkCVHAG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster text{fill:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG .cluster span{color:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9qZpWhRWAKkCVHAG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9qZpWhRWAKkCVHAG rect.text{fill:none;stroke-width:0;}#mermaid-svg-9qZpWhRWAKkCVHAG .icon-shape,#mermaid-svg-9qZpWhRWAKkCVHAG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9qZpWhRWAKkCVHAG .icon-shape p,#mermaid-svg-9qZpWhRWAKkCVHAG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9qZpWhRWAKkCVHAG .icon-shape .label rect,#mermaid-svg-9qZpWhRWAKkCVHAG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9qZpWhRWAKkCVHAG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9qZpWhRWAKkCVHAG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9qZpWhRWAKkCVHAG :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

main.c

头文件包含

宏定义

线程栈

线程控制块

线程函数

main函数

kernel.h

printk.h

STACK_SIZE

PRIORITY

K_THREAD_STACK_DEFINE

k_thread结构体

my_thread_func

printk输出

k_thread_create

3.4 简化版Hello World

如果只需要简单的打印输出,可以使用更简洁的版本:

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

void main(void)
{
printk("Hello World! Zephyr RTOS\\n");

while (1) {
k_msleep(1000);
}
}


四、编译过程详解

4.1 编译命令

# 进入项目目录
cd ~/zephyrproject/my_hello_world

# 编译(指定QEMU x86平台)
west build -b qemu_x86 .

# 编译并清理旧构建
west build -b qemu_x86 . –clean

# 指定构建目录
west build -b qemu_x86 . -d build_qemu

# 使用多核编译
west build -b qemu_x86 . -j $(nproc)

4.2 编译流程

编译器

Ninja

CMake

West工具

用户

编译器

Ninja

CMake

West工具

用户

#mermaid-svg-CuyGeM1MAXK9FYj9{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-CuyGeM1MAXK9FYj9 .error-icon{fill:#552222;}#mermaid-svg-CuyGeM1MAXK9FYj9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CuyGeM1MAXK9FYj9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CuyGeM1MAXK9FYj9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CuyGeM1MAXK9FYj9 .marker.cross{stroke:#333333;}#mermaid-svg-CuyGeM1MAXK9FYj9 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CuyGeM1MAXK9FYj9 p{margin:0;}#mermaid-svg-CuyGeM1MAXK9FYj9 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-CuyGeM1MAXK9FYj9 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-CuyGeM1MAXK9FYj9 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-CuyGeM1MAXK9FYj9 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-CuyGeM1MAXK9FYj9 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-CuyGeM1MAXK9FYj9 .sequenceNumber{fill:white;}#mermaid-svg-CuyGeM1MAXK9FYj9 #sequencenumber{fill:#333;}#mermaid-svg-CuyGeM1MAXK9FYj9 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-CuyGeM1MAXK9FYj9 .messageText{fill:#333;stroke:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-CuyGeM1MAXK9FYj9 .labelText,#mermaid-svg-CuyGeM1MAXK9FYj9 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .loopText,#mermaid-svg-CuyGeM1MAXK9FYj9 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-CuyGeM1MAXK9FYj9 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-CuyGeM1MAXK9FYj9 .noteText,#mermaid-svg-CuyGeM1MAXK9FYj9 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-CuyGeM1MAXK9FYj9 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-CuyGeM1MAXK9FYj9 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-CuyGeM1MAXK9FYj9 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-CuyGeM1MAXK9FYj9 .actorPopupMenu{position:absolute;}#mermaid-svg-CuyGeM1MAXK9FYj9 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-CuyGeM1MAXK9FYj9 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-CuyGeM1MAXK9FYj9 .actor-man circle,#mermaid-svg-CuyGeM1MAXK9FYj9 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-CuyGeM1MAXK9FYj9 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

west build -b qemu_x86 .

解析命令参数

设置环境变量

运行CMake配置

加载Zephyr模块

解析Kconfig配置

生成build.ninja

配置完成

执行ninja构建

编译源文件

编译main.c

链接目标文件

生成可执行文件

构建完成

输出构建结果

4.3 编译输出解析

编译过程输出:

$ west build -b qemu_x86 .
— west build: making build dir /home/user/zephyrproject/my_hello_world/build
— west build: sourcing /home/user/zephyrproject/zephyr/zephyr-env.sh
— Found Python3: /usr/bin/python3 (found version "3.10.12") found components: Interpreter
— Cache files will be written to: /home/user/.cache/zephyr
— Zephyr version: 3.6.0 (/home/user/zephyrproject/zephyr)
— Board: qemu_x86
— Found host-tools: zephyr-sdk-0.16.4 (/home/user/zephyr-sdk-0.16.4)
— Found toolchain: zephyr (/home/user/zephyr-sdk-0.16.4/x86_64-zephyr-elf)
— Configuring done
— Generating done
— Build files have been written to: /home/user/zephyrproject/my_hello_world/build
[1/234] Building C object zephyr/CMakeFiles/zephyr.dir/kernel/main.c.obj
[2/234] Building C object zephyr/CMakeFiles/zephyr.dir/kernel/sched.c.obj
...
[234/234] Linking C executable zephyr/zephyr.elf
Memory region Used Size Region Size %age Used
FLASH: 1024 B 256 KB 0.39%
RAM: 128 B 64 KB 0.20%
IDT_LIST: 0 GB 2 KB 0.00%

输出信息说明:

信息说明
Zephyr version Zephyr版本
Board: qemu_x86 目标板
toolchain 使用的工具链
[1/234] 编译进度
Memory region 内存使用情况
FLASH 代码占用Flash大小
RAM 数据占用RAM大小

4.4 编译产物

# 查看构建目录
ls -la build/zephyr/

# 输出:
# total 12M
# -rwxr-xr-x 1 user user 8.0M Jan 15 10:30 zephyr.elf
# -rw-r–r– 1 user user 1.2M Jan 15 10:30 zephyr.bin
# -rw-r–r– 1 user user 12K Jan 15 10:30 zephyr.map
# -rw-r–r– 1 user user 28K Jan 15 10:30 zephyr.lst
# -rw-r–r– 1 user user 2M Jan 15 10:30 zephyr.dbg

编译产物说明:

文件说明
zephyr.elf ELF格式可执行文件
zephyr.bin 二进制镜像文件
zephyr.map 链接映射文件
zephyr.lst 反汇编列表
zephyr.dbg 调试信息文件

五、运行Hello World

5.1 在QEMU中运行

# 方式1:使用west run
west build -t run

# 方式2:直接运行
cd build/zephyr
ninja run

# 方式3:手动启动QEMU
qemu-system-x86_64 \\
-kernel zephyr.elf \\
-append "console=ttyS0" \\
-nographic

5.2 运行输出

$ west build -t run
[1/1] To exit from QEMU enter: 'CTRL+a, x'
QEMU 7.0.0 monitor – type 'help' for more information
(qemu)
SeaBIOS (version rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org)
Booting from ROM..
Hello World! Zephyr RTOS
Hello from my thread!
Hello from my thread!
Hello from my thread!
...

输出说明:

输出说明
QEMU 7.0.0 monitor QEMU版本信息
Booting from ROM.. 启动过程
Hello World! Zephyr RTOS main函数输出
Hello from my thread! 线程函数输出

5.3 退出QEMU

# 在QEMU窗口中
# 按 Ctrl+a 然后按 x
# 或者直接按 Ctrl+C


六、在不同平台上运行

6.1 运行在硬件开发板上

步骤1:连接开发板

# 查看串口设备
ls /dev/ttyUSB*
ls /dev/ttyACM*

步骤2:编译目标平台

# 编译nRF52840开发板
west build -b nrf52840dk_nrf52840 .

# 编译STM32F4Discovery开发板
west build -b stm32f4_disco .

# 编译ESP32开发板
west build -b esp32 .

步骤3:烧录固件

# 方式1:使用west flash
west flash

# 方式2:使用开发板专用工具
# nRF52840: nrfjprog –program zephyr.bin –sectorerase –reset
# STM32: openocd -f board/stm32f4discovery.cfg -c "program zephyr.bin reset exit"

步骤4:查看输出

# 使用串口终端
minicom -D /dev/ttyACM0 -b 115200

# 或者使用picocom
picocom /dev/ttyACM0 -b 115200

6.2 运行在native_sim平台

# 编译native_sim平台
west build -b native_sim .

# 运行
west build -t run

native_sim特点:

特点说明
无需硬件 使用主机CPU模拟
速度快 直接在主机上运行
调试方便 使用GDB直接调试
文件系统 可访问主机文件系统

七、调试Hello World

7.1 使用GDB调试

# 方式1:启动QEMU等待GDB连接
west build -t debug

# 方式2:手动启动
qemu-system-x86_64 \\
-kernel build/zephyr/zephyr.elf \\
-append "console=ttyS0" \\
-nographic \\
-s -S

# 在另一个终端启动GDB
gdb-multiarch build/zephyr/zephyr.elf

# GDB命令
(gdb) target remote localhost:1234
(gdb) break main
(gdb) continue
(gdb) step
(gdb) print x
(gdb) info threads
(gdb) backtrace

7.2 使用VS Code调试

步骤1:安装扩展

# 安装Cortex-Debug扩展
ext install marus25.cortex-debug

步骤2:配置launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "Zephyr QEMU Debug",
"type": "cortex-debug",
"request": "launch",
"servertype": "qemu",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/build/zephyr/zephyr.elf",
"device": "generic",
"runToEntryPoint": "main",
"gdbPath": "x86_64-zephyr-elf-gdb"
}
]
}

步骤3:启动调试

# 在VS Code中按F5启动调试


八、常见问题与解决方案

8.1 编译错误

问题1:找不到Zephyr

# 错误信息:
# CMake Error: Could not find Zephyr!

# 解决:设置ZEPHYR_BASE环境变量
export ZEPHYR_BASE=~/zephyrproject/zephyr
source ~/zephyrproject/zephyr/zephyr-env.sh

问题2:缺少依赖

# 错误信息:
# ninja: error: loading 'build.ninja': No such file or directory

# 解决:清理并重新构建
west build -b qemu_x86 . –clean

问题3:编译超时

# 解决:增加并行任务数
west build -b qemu_x86 . -j $(nproc)

8.2 运行错误

问题1:QEMU未找到

# 错误信息:
# qemu-system-x86_64: command not found

# 解决:安装QEMU
sudo apt-get install qemu-system-x86

问题2:串口无法连接

# 错误信息:
# Can't open /dev/ttyACM0: Permission denied

# 解决:添加用户到dialout组
sudo usermod -aG dialout $USER

问题3:输出乱码

# 解决:确保串口波特率正确
minicom -D /dev/ttyACM0 -b 115200

8.3 其他问题

问题1:构建目录权限问题

# 解决:使用普通用户权限
sudo chown -R $USER:$USER build

问题2:Python版本问题

# 解决:确保使用Python 3
python3 –version
pip3 –version


九、深入理解Hello World

9.1 Zephyr启动流程

#mermaid-svg-EYCrm1mQdA8mnE6s{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EYCrm1mQdA8mnE6s .error-icon{fill:#552222;}#mermaid-svg-EYCrm1mQdA8mnE6s .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EYCrm1mQdA8mnE6s .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EYCrm1mQdA8mnE6s .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EYCrm1mQdA8mnE6s .marker.cross{stroke:#333333;}#mermaid-svg-EYCrm1mQdA8mnE6s svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EYCrm1mQdA8mnE6s p{margin:0;}#mermaid-svg-EYCrm1mQdA8mnE6s .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster-label text{fill:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster-label span{color:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster-label span p{background-color:transparent;}#mermaid-svg-EYCrm1mQdA8mnE6s .label text,#mermaid-svg-EYCrm1mQdA8mnE6s span{fill:#333;color:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s .node rect,#mermaid-svg-EYCrm1mQdA8mnE6s .node circle,#mermaid-svg-EYCrm1mQdA8mnE6s .node ellipse,#mermaid-svg-EYCrm1mQdA8mnE6s .node polygon,#mermaid-svg-EYCrm1mQdA8mnE6s .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EYCrm1mQdA8mnE6s .rough-node .label text,#mermaid-svg-EYCrm1mQdA8mnE6s .node .label text,#mermaid-svg-EYCrm1mQdA8mnE6s .image-shape .label,#mermaid-svg-EYCrm1mQdA8mnE6s .icon-shape .label{text-anchor:middle;}#mermaid-svg-EYCrm1mQdA8mnE6s .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EYCrm1mQdA8mnE6s .rough-node .label,#mermaid-svg-EYCrm1mQdA8mnE6s .node .label,#mermaid-svg-EYCrm1mQdA8mnE6s .image-shape .label,#mermaid-svg-EYCrm1mQdA8mnE6s .icon-shape .label{text-align:center;}#mermaid-svg-EYCrm1mQdA8mnE6s .node.clickable{cursor:pointer;}#mermaid-svg-EYCrm1mQdA8mnE6s .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EYCrm1mQdA8mnE6s .arrowheadPath{fill:#333333;}#mermaid-svg-EYCrm1mQdA8mnE6s .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EYCrm1mQdA8mnE6s .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EYCrm1mQdA8mnE6s .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EYCrm1mQdA8mnE6s .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EYCrm1mQdA8mnE6s .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EYCrm1mQdA8mnE6s .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster text{fill:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s .cluster span{color:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-EYCrm1mQdA8mnE6s .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EYCrm1mQdA8mnE6s rect.text{fill:none;stroke-width:0;}#mermaid-svg-EYCrm1mQdA8mnE6s .icon-shape,#mermaid-svg-EYCrm1mQdA8mnE6s .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EYCrm1mQdA8mnE6s .icon-shape p,#mermaid-svg-EYCrm1mQdA8mnE6s .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EYCrm1mQdA8mnE6s .icon-shape .label rect,#mermaid-svg-EYCrm1mQdA8mnE6s .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EYCrm1mQdA8mnE6s .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EYCrm1mQdA8mnE6s .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EYCrm1mQdA8mnE6s :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

复位

启动代码

内核初始化

main函数

应用代码

arch/x86/core/crt0.S

kernel_init

k_thread_init

设备初始化

用户main函数

线程创建

外设操作

9.2 printk原理

#include <zephyr/sys/printk.h>

/* printk是Zephyr的内核打印函数 */
printk("Hello World!\\n");

/* 与printf的区别 */
// printf: 用户空间标准库函数,需要libc支持
// printk: 内核空间打印函数,直接输出到控制台

/* 输出路径 */
// printk -> 控制台驱动 -> UART/USB/虚拟终端

9.3 线程创建过程

/* 创建线程的步骤 */
k_thread_create(
&my_thread_data, // 线程控制块
my_thread_stack, // 线程栈
K_THREAD_STACK_SIZEOF(my_thread_stack), // 栈大小
my_thread_func, // 线程函数
NULL, NULL, NULL, // 函数参数
PRIORITY, // 优先级
0, // 选项
K_NO_WAIT // 延迟启动
);

/* 线程状态转换 */
// 创建 -> 就绪 -> 运行 -> 阻塞 -> 就绪 -> 运行 -> 终止

9.4 内存占用分析

# 查看内存占用报告
west build -b qemu_x86 . — -Dprintk-conf

# 查看ROM/RAM报告
west build -b qemu_x86 . — -Dcreate-rom-ram-report

# 输出示例:
# Memory region Used Size Region Size %age Used
# FLASH: 1024 B 256 KB 0.39%
# RAM: 128 B 64 KB 0.20%


十、扩展Hello World

10.1 添加LED闪烁

#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>

#define LED_NODE DT_ALIAS(led0)
#define LED_GPIO DT_GPIO_CTLR(LED_NODE, gpios)
#define LED_PIN DT_GPIO_PIN(LED_NODE, gpios)
#define LED_FLAGS DT_GPIO_FLAGS(LED_NODE, gpios)

const struct device *led_dev = DEVICE_DT_GET(LED_GPIO);

void main(void)
{
printk("Hello World! LED Blink\\n");

if (!device_is_ready(led_dev)) {
printk("LED device not ready\\n");
return;
}

gpio_pin_configure(led_dev, LED_PIN, GPIO_OUTPUT_INACTIVE | LED_FLAGS);

while (1) {
gpio_pin_set(led_dev, LED_PIN, 1);
printk("LED ON\\n");
k_msleep(500);

gpio_pin_set(led_dev, LED_PIN, 0);
printk("LED OFF\\n");
k_msleep(500);
}
}

10.2 添加按键输入

#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/printk.h>

#define SW_NODE DT_ALIAS(sw0)
#define SW_GPIO DT_GPIO_CTLR(SW_NODE, gpios)
#define SW_PIN DT_GPIO_PIN(SW_NODE, gpios)
#define SW_FLAGS DT_GPIO_FLAGS(SW_NODE, gpios)

const struct device *sw_dev = DEVICE_DT_GET(SW_GPIO);

void button_callback(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
printk("Button pressed!\\n");
}

static struct gpio_callback button_cb_data;

void main(void)
{
printk("Hello World! Button Test\\n");

if (!device_is_ready(sw_dev)) {
printk("Button device not ready\\n");
return;
}

gpio_pin_configure(sw_dev, SW_PIN, GPIO_INPUT | SW_FLAGS);
gpio_pin_interrupt_configure(sw_dev, SW_PIN, GPIO_INT_EDGE_TO_ACTIVE);

gpio_init_callback(&button_cb_data, button_callback, BIT(SW_PIN));
gpio_add_callback(sw_dev, &button_cb_data);

while (1) {
k_msleep(100);
}
}


结束语

通过本文的详细介绍,相信您已经成功创建并运行了第一个Zephyr Hello World应用:

步骤内容
环境准备 安装依赖、West、Zephyr SDK
创建项目 west create或手动创建
项目结构 CMakeLists.txt、prj.conf、src/main.c
编译 west build -b qemu_x86
运行 west build -t run
调试 GDB或VS Code

Hello World背后的知识:

  • CMake构建系统:理解CMakeLists.txt的作用
  • Kconfig配置:理解prj.conf的配置选项
  • 内核启动流程:从复位到main函数
  • 线程管理:创建和调度线程
  • 设备驱动:控制台、GPIO等
  • 下一步学习建议:

  • 修改prj.conf添加更多功能
  • 添加LED闪烁功能
  • 添加按键输入功能
  • 学习Zephyr内核API
  • 尝试在真实硬件上运行

  • 参考资料:

    • Zephyr官方文档
    • Hello World示例
    • Zephyr内核API
    • Zephyr设备驱动
    赞(0)
    未经允许不得转载:171主机测评 » Zephyr Hello World应用开发构建完全指南
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址