Zephyr工程配置完全指南

Zephyr工程配置完全指南

引言

Zephyr RTOS以其高度模块化和可配置性著称,能够支持从资源受限的微控制器到复杂的物联网设备。这种灵活性主要来自于其强大的配置系统,包括Kconfig配置系统、设备树(Device Tree)机制、CMake构建系统,以及West项目管理系统。

本文将全面介绍如何配置Zephyr工程,涵盖从项目创建、配置系统理解、构建选项设置,到设备树配置和调试部署的完整流程。无论您是Zephyr新手还是希望深入了解其配置机制的开发者,本文都将提供详尽的指导。


一、Zephyr配置系统概述

1.1 配置系统架构

#mermaid-svg-TB1uDAvmjtrv3sai{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-TB1uDAvmjtrv3sai .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-TB1uDAvmjtrv3sai .error-icon{fill:#552222;}#mermaid-svg-TB1uDAvmjtrv3sai .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-TB1uDAvmjtrv3sai .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-TB1uDAvmjtrv3sai .marker{fill:#333333;stroke:#333333;}#mermaid-svg-TB1uDAvmjtrv3sai .marker.cross{stroke:#333333;}#mermaid-svg-TB1uDAvmjtrv3sai svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-TB1uDAvmjtrv3sai p{margin:0;}#mermaid-svg-TB1uDAvmjtrv3sai .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-TB1uDAvmjtrv3sai .cluster-label text{fill:#333;}#mermaid-svg-TB1uDAvmjtrv3sai .cluster-label span{color:#333;}#mermaid-svg-TB1uDAvmjtrv3sai .cluster-label span p{background-color:transparent;}#mermaid-svg-TB1uDAvmjtrv3sai .label text,#mermaid-svg-TB1uDAvmjtrv3sai span{fill:#333;color:#333;}#mermaid-svg-TB1uDAvmjtrv3sai .node rect,#mermaid-svg-TB1uDAvmjtrv3sai .node circle,#mermaid-svg-TB1uDAvmjtrv3sai .node ellipse,#mermaid-svg-TB1uDAvmjtrv3sai .node polygon,#mermaid-svg-TB1uDAvmjtrv3sai .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-TB1uDAvmjtrv3sai .rough-node .label text,#mermaid-svg-TB1uDAvmjtrv3sai .node .label text,#mermaid-svg-TB1uDAvmjtrv3sai .image-shape .label,#mermaid-svg-TB1uDAvmjtrv3sai .icon-shape .label{text-anchor:middle;}#mermaid-svg-TB1uDAvmjtrv3sai .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-TB1uDAvmjtrv3sai .rough-node .label,#mermaid-svg-TB1uDAvmjtrv3sai .node .label,#mermaid-svg-TB1uDAvmjtrv3sai .image-shape .label,#mermaid-svg-TB1uDAvmjtrv3sai .icon-shape .label{text-align:center;}#mermaid-svg-TB1uDAvmjtrv3sai .node.clickable{cursor:pointer;}#mermaid-svg-TB1uDAvmjtrv3sai .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-TB1uDAvmjtrv3sai .arrowheadPath{fill:#333333;}#mermaid-svg-TB1uDAvmjtrv3sai .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-TB1uDAvmjtrv3sai .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-TB1uDAvmjtrv3sai .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TB1uDAvmjtrv3sai .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-TB1uDAvmjtrv3sai .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TB1uDAvmjtrv3sai .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-TB1uDAvmjtrv3sai .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-TB1uDAvmjtrv3sai .cluster text{fill:#333;}#mermaid-svg-TB1uDAvmjtrv3sai .cluster span{color:#333;}#mermaid-svg-TB1uDAvmjtrv3sai 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-TB1uDAvmjtrv3sai .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-TB1uDAvmjtrv3sai rect.text{fill:none;stroke-width:0;}#mermaid-svg-TB1uDAvmjtrv3sai .icon-shape,#mermaid-svg-TB1uDAvmjtrv3sai .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TB1uDAvmjtrv3sai .icon-shape p,#mermaid-svg-TB1uDAvmjtrv3sai .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-TB1uDAvmjtrv3sai .icon-shape .label rect,#mermaid-svg-TB1uDAvmjtrv3sai .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TB1uDAvmjtrv3sai .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-TB1uDAvmjtrv3sai .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-TB1uDAvmjtrv3sai :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Zephyr配置系统
CMake构建系统
Kconfig配置系统
设备树系统
West项目管理系统
CMakeLists.txt
build.ninja
prj.conf
Kconfig文件
autoconf.h
.dts文件
.dtsi文件
devicetree.h
west.yml
.west/config

1.2 配置文件关系

#mermaid-svg-zTD9C70gDyLpm15q{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-zTD9C70gDyLpm15q .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zTD9C70gDyLpm15q .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zTD9C70gDyLpm15q .error-icon{fill:#552222;}#mermaid-svg-zTD9C70gDyLpm15q .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zTD9C70gDyLpm15q .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zTD9C70gDyLpm15q .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zTD9C70gDyLpm15q .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zTD9C70gDyLpm15q .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zTD9C70gDyLpm15q .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zTD9C70gDyLpm15q .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zTD9C70gDyLpm15q .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zTD9C70gDyLpm15q .marker.cross{stroke:#333333;}#mermaid-svg-zTD9C70gDyLpm15q svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zTD9C70gDyLpm15q p{margin:0;}#mermaid-svg-zTD9C70gDyLpm15q .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zTD9C70gDyLpm15q .cluster-label text{fill:#333;}#mermaid-svg-zTD9C70gDyLpm15q .cluster-label span{color:#333;}#mermaid-svg-zTD9C70gDyLpm15q .cluster-label span p{background-color:transparent;}#mermaid-svg-zTD9C70gDyLpm15q .label text,#mermaid-svg-zTD9C70gDyLpm15q span{fill:#333;color:#333;}#mermaid-svg-zTD9C70gDyLpm15q .node rect,#mermaid-svg-zTD9C70gDyLpm15q .node circle,#mermaid-svg-zTD9C70gDyLpm15q .node ellipse,#mermaid-svg-zTD9C70gDyLpm15q .node polygon,#mermaid-svg-zTD9C70gDyLpm15q .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zTD9C70gDyLpm15q .rough-node .label text,#mermaid-svg-zTD9C70gDyLpm15q .node .label text,#mermaid-svg-zTD9C70gDyLpm15q .image-shape .label,#mermaid-svg-zTD9C70gDyLpm15q .icon-shape .label{text-anchor:middle;}#mermaid-svg-zTD9C70gDyLpm15q .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-zTD9C70gDyLpm15q .rough-node .label,#mermaid-svg-zTD9C70gDyLpm15q .node .label,#mermaid-svg-zTD9C70gDyLpm15q .image-shape .label,#mermaid-svg-zTD9C70gDyLpm15q .icon-shape .label{text-align:center;}#mermaid-svg-zTD9C70gDyLpm15q .node.clickable{cursor:pointer;}#mermaid-svg-zTD9C70gDyLpm15q .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-zTD9C70gDyLpm15q .arrowheadPath{fill:#333333;}#mermaid-svg-zTD9C70gDyLpm15q .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zTD9C70gDyLpm15q .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zTD9C70gDyLpm15q .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zTD9C70gDyLpm15q .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-zTD9C70gDyLpm15q .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zTD9C70gDyLpm15q .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-zTD9C70gDyLpm15q .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zTD9C70gDyLpm15q .cluster text{fill:#333;}#mermaid-svg-zTD9C70gDyLpm15q .cluster span{color:#333;}#mermaid-svg-zTD9C70gDyLpm15q 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-zTD9C70gDyLpm15q .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-zTD9C70gDyLpm15q rect.text{fill:none;stroke-width:0;}#mermaid-svg-zTD9C70gDyLpm15q .icon-shape,#mermaid-svg-zTD9C70gDyLpm15q .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zTD9C70gDyLpm15q .icon-shape p,#mermaid-svg-zTD9C70gDyLpm15q .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-zTD9C70gDyLpm15q .icon-shape .label rect,#mermaid-svg-zTD9C70gDyLpm15q .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zTD9C70gDyLpm15q .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-zTD9C70gDyLpm15q .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-zTD9C70gDyLpm15q :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} west.yml
west update
下载仓库
CMakeLists.txt
CMake配置
Kconfig解析
设备树处理
prj.conf
autoconf.h
.dts/.dtsi
devicetree.h
编译产物


二、工程目录结构

2.1 标准工程结构

复制代码
my_zephyr_app/
├── CMakeLists.txt           # CMake主配置文件
├── prj.conf                # 应用配置(Kconfig)
├── app.overlay             # 设备树覆盖文件(可选)
├── boards/                 # 板级配置(可选)
│   └── myboard.dts
├── src/                    # 源代码目录
│   ├── main.c
│   └── module.c
├── include/                # 头文件目录
│   └── myheader.h
├── lib/                    # 本地库(可选)
├── tests/                  # 测试代码(可选)
└── build/                  # 构建输出目录(自动生成)

2.2 Zephyr SDK目录结构

复制代码
zephyrproject/
├── zephyr/                 # Zephyr主仓库
├── hal_nordic/            # Nordic HAL(按需)
├── bootloader/             # 引导加载器(按需)
└── modules/               # 外部模块

三、CMakeLists.txt配置

3.1 基本CMakeLists.txt

cmake 复制代码
# CMakeLists.txt - 基本配置

# 1. CMake版本要求
cmake_minimum_required(VERSION 3.20.0)

# 2. 包含Zephyr构建系统
# HINTS用于查找ZEPHYR_BASE环境变量
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

# 3. 定义项目名称
project(my_zephyr_app VERSION 1.0.0)

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

# 5. 添加头文件目录
target_include_directories(app PRIVATE
    include
)

3.2 高级CMakeLists.txt

cmake 复制代码
# CMakeLists.txt - 高级配置

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(my_advanced_app VERSION 1.0.0 LANGUAGES C ASM)

# 配置编译选项
target_compile_options(app PRIVATE
    -Wall
    -Wextra
    -Werror
    -ffunction-sections
    -fdata-sections
)

# 配置链接选项
target_link_libraries(app PRIVATE
    -Wl,--gc-sections
    -Wl,-Map=output.map
)

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

# 添加DEFINITIONS
target_compile_definitions(app PRIVATE
    MY_CONFIG_VALUE=42
    DEBUG_MODE
)

# 条件编译
if(CONFIG_DEBUG)
    target_sources(app PRIVATE src/debug.c)
endif()

3.3 CMake配置参数

参数 说明 示例
CMAKE_BUILD_TYPE 构建类型 Debug, Release, RelWithDebInfo
BOARD 目标板 nrf52840dk_nrf52840, qemu_x86
BOARD_ROOT 板级配置根目录 CMAKE_CURRENT_SOURCE_DIR
DTC_OVERLAY_FILE 设备树覆盖文件 app.overlay

3.4 CMake命令行选项

bash 复制代码
# 基本编译
west build -b <board> <app>

# 指定构建类型
west build -b <board> <app> -- -DCMAKE_BUILD_TYPE=Debug

# 指定设备树覆盖
west build -b <board> <app> -- -DDTC_OVERLAY_FILE=app.overlay

# 使用自定义板级配置
west build -b <board> <app> -- -DBOARD_ROOT=./custom_boards

四、Kconfig配置系统

4.1 prj.conf文件结构

kconfig 复制代码
# prj.conf - 应用配置文件

# ===== 基础配置 =====
# SoC和板级配置
CONFIG_SOC_SERIES_NRF52=y
CONFIG_SOC_NRF52840_QIAA=y
CONFIG_BOARD_NRF52840DK_NRF52840=y

# ===== 内核配置 =====
CONFIG_KERNEL=y
CONFIG_PRIVILEGED_MODE=y
CONFIG_INIT_STACKS=y

# ===== 线程配置 =====
CONFIG_NUM_COOP_PRIORITIES=16
CONFIG_NUM_PREEMPT_PRIORITIES=32
CONFIG_MAIN_THREAD_PRIORITY=0
CONFIG_MAIN_STACK_SIZE=2048

# ===== 调试配置 =====
CONFIG_DEBUG=y
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_DEBUG_THREAD_INFO=y
CONFIG_ASSERT=y

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

# ===== 驱动配置 =====
CONFIG_GPIO=y
CONFIG_I2C=y
CONFIG_SPI=y

# ===== 网络配置 =====
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y

# ===== 存储配置 =====
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_STORAGE=y

4.2 配置项分类

#mermaid-svg-MASBEaBEnMhH9xMD{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-MASBEaBEnMhH9xMD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MASBEaBEnMhH9xMD .error-icon{fill:#552222;}#mermaid-svg-MASBEaBEnMhH9xMD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MASBEaBEnMhH9xMD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MASBEaBEnMhH9xMD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MASBEaBEnMhH9xMD .marker.cross{stroke:#333333;}#mermaid-svg-MASBEaBEnMhH9xMD svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MASBEaBEnMhH9xMD p{margin:0;}#mermaid-svg-MASBEaBEnMhH9xMD .edge{stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .section--1 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section--1 path,#mermaid-svg-MASBEaBEnMhH9xMD .section--1 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section--1 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section--1 text{fill:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth--1{stroke-width:17;}#mermaid-svg-MASBEaBEnMhH9xMD .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-0 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-0 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-0 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-0 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-0 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-0{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-0{stroke-width:14;}#mermaid-svg-MASBEaBEnMhH9xMD .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-1 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-1 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-1 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-1 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-1 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-1{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-1{stroke-width:11;}#mermaid-svg-MASBEaBEnMhH9xMD .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-2 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-2 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-2 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-2 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-2 text{fill:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-2{stroke-width:8;}#mermaid-svg-MASBEaBEnMhH9xMD .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-3 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-3 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-3 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-3 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-3 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-3{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-3{stroke-width:5;}#mermaid-svg-MASBEaBEnMhH9xMD .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-4 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-4 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-4 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-4 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-4 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-4{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-4{stroke-width:2;}#mermaid-svg-MASBEaBEnMhH9xMD .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-5 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-5 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-5 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-5 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-5 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-5{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-5{stroke-width:-1;}#mermaid-svg-MASBEaBEnMhH9xMD .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-6 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-6 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-6 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-6 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-6 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-6{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-6{stroke-width:-4;}#mermaid-svg-MASBEaBEnMhH9xMD .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-7 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-7 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-7 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-7 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-7 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-7{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-7{stroke-width:-7;}#mermaid-svg-MASBEaBEnMhH9xMD .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-8 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-8 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-8 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-8 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-8 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-8{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-8{stroke-width:-10;}#mermaid-svg-MASBEaBEnMhH9xMD .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-9 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-9 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-9 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-9 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-9 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-9{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-9{stroke-width:-13;}#mermaid-svg-MASBEaBEnMhH9xMD .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-10 rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-10 path,#mermaid-svg-MASBEaBEnMhH9xMD .section-10 circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-10 polygon,#mermaid-svg-MASBEaBEnMhH9xMD .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-10 text{fill:black;}#mermaid-svg-MASBEaBEnMhH9xMD .node-icon-10{font-size:40px;color:black;}#mermaid-svg-MASBEaBEnMhH9xMD .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .edge-depth-10{stroke-width:-16;}#mermaid-svg-MASBEaBEnMhH9xMD .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled,#mermaid-svg-MASBEaBEnMhH9xMD .disabled circle,#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:lightgray;}#mermaid-svg-MASBEaBEnMhH9xMD .disabled text{fill:#efefef;}#mermaid-svg-MASBEaBEnMhH9xMD .section-root rect,#mermaid-svg-MASBEaBEnMhH9xMD .section-root path,#mermaid-svg-MASBEaBEnMhH9xMD .section-root circle,#mermaid-svg-MASBEaBEnMhH9xMD .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-MASBEaBEnMhH9xMD .section-root text{fill:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .section-root span{color:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .section-2 span{color:#ffffff;}#mermaid-svg-MASBEaBEnMhH9xMD .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-MASBEaBEnMhH9xMD .edge{fill:none;}#mermaid-svg-MASBEaBEnMhH9xMD .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-MASBEaBEnMhH9xMD :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} CONFIG项分类
内核配置
KERNEL
THREAD
SCHEDULER
MEMORY
驱动配置
GPIO
UART
I2C
SPI
PWM
ADC
网络配置
IPV4/IPV6
TCP/UDP
SOCKETS
LWM2M
存储配置
FLASH
FILE_SYSTEM
SETTINGS
安全配置
CRYPTO
TLS
SECURE_BOOT

4.3 常用配置项速查表

配置项 说明 默认值
CONFIG_BOARD 板级配置 -
CONFIG_SOC SoC配置 -
CONFIG_KERNEL 启用内核 y
CONFIG_MAIN_STACK_SIZE 主线程栈大小 1024
CONFIG_NUM_COOP_PRIORITIES 协作线程优先级数 16
CONFIG_NUM_PREEMPT_PRIORITIES 抢占线程优先级数 32
CONFIG_DEBUG 启用调试 n
CONFIG_ASSERT 启用断言 n
CONFIG_LOG 启用日志 n
CONFIG_SHELL 启用Shell n
CONFIG_CONSOLE 启用控制台 y
CONFIG_UART_CONSOLE UART控制台 y

4.4 配置项前缀说明

前缀 说明 示例
CONFIG_SOC_* SoC相关配置 CONFIG_SOC_NRF52840
CONFIG_BOARD_* 板级配置 CONFIG_BOARD_NRF52840DK
CONFIG_* 内核/通用配置 CONFIG_KERNEL
CONFIG_DRIVER_* 驱动配置 CONFIG_GPIO
CONFIG_NETWORKING 网络配置 CONFIG_NET_IPV4

五、设备树配置

5.1 设备树文件结构

#mermaid-svg-hYEw23PMRX1aKlln{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-hYEw23PMRX1aKlln .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hYEw23PMRX1aKlln .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hYEw23PMRX1aKlln .error-icon{fill:#552222;}#mermaid-svg-hYEw23PMRX1aKlln .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hYEw23PMRX1aKlln .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hYEw23PMRX1aKlln .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hYEw23PMRX1aKlln .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hYEw23PMRX1aKlln .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hYEw23PMRX1aKlln .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hYEw23PMRX1aKlln .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hYEw23PMRX1aKlln .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hYEw23PMRX1aKlln .marker.cross{stroke:#333333;}#mermaid-svg-hYEw23PMRX1aKlln svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hYEw23PMRX1aKlln p{margin:0;}#mermaid-svg-hYEw23PMRX1aKlln .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-hYEw23PMRX1aKlln .cluster-label text{fill:#333;}#mermaid-svg-hYEw23PMRX1aKlln .cluster-label span{color:#333;}#mermaid-svg-hYEw23PMRX1aKlln .cluster-label span p{background-color:transparent;}#mermaid-svg-hYEw23PMRX1aKlln .label text,#mermaid-svg-hYEw23PMRX1aKlln span{fill:#333;color:#333;}#mermaid-svg-hYEw23PMRX1aKlln .node rect,#mermaid-svg-hYEw23PMRX1aKlln .node circle,#mermaid-svg-hYEw23PMRX1aKlln .node ellipse,#mermaid-svg-hYEw23PMRX1aKlln .node polygon,#mermaid-svg-hYEw23PMRX1aKlln .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hYEw23PMRX1aKlln .rough-node .label text,#mermaid-svg-hYEw23PMRX1aKlln .node .label text,#mermaid-svg-hYEw23PMRX1aKlln .image-shape .label,#mermaid-svg-hYEw23PMRX1aKlln .icon-shape .label{text-anchor:middle;}#mermaid-svg-hYEw23PMRX1aKlln .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hYEw23PMRX1aKlln .rough-node .label,#mermaid-svg-hYEw23PMRX1aKlln .node .label,#mermaid-svg-hYEw23PMRX1aKlln .image-shape .label,#mermaid-svg-hYEw23PMRX1aKlln .icon-shape .label{text-align:center;}#mermaid-svg-hYEw23PMRX1aKlln .node.clickable{cursor:pointer;}#mermaid-svg-hYEw23PMRX1aKlln .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hYEw23PMRX1aKlln .arrowheadPath{fill:#333333;}#mermaid-svg-hYEw23PMRX1aKlln .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hYEw23PMRX1aKlln .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hYEw23PMRX1aKlln .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hYEw23PMRX1aKlln .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hYEw23PMRX1aKlln .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hYEw23PMRX1aKlln .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hYEw23PMRX1aKlln .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hYEw23PMRX1aKlln .cluster text{fill:#333;}#mermaid-svg-hYEw23PMRX1aKlln .cluster span{color:#333;}#mermaid-svg-hYEw23PMRX1aKlln 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-hYEw23PMRX1aKlln .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hYEw23PMRX1aKlln rect.text{fill:none;stroke-width:0;}#mermaid-svg-hYEw23PMRX1aKlln .icon-shape,#mermaid-svg-hYEw23PMRX1aKlln .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hYEw23PMRX1aKlln .icon-shape p,#mermaid-svg-hYEw23PMRX1aKlln .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hYEw23PMRX1aKlln .icon-shape .label rect,#mermaid-svg-hYEw23PMRX1aKlln .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hYEw23PMRX1aKlln .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hYEw23PMRX1aKlln .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hYEw23PMRX1aKlln :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 设备树文件
.dtsi - 包含文件
.dts - 设备树源文件
.overlay - 覆盖文件
SoC级设备树
板级设备树
通用设备树
具体开发板配置
外设配置
引脚配置
调试配置
外设覆盖

5.2 设备树基本语法

dts 复制代码
/* app.dts - 设备树示例 */

/ {
    model = "My Board";
    compatible = "mycompany,myboard";

    /* 内存配置 */
    memory@20000000 {
        device_type = "memory";
        reg = <0x20000000 0x40000>;  /* 256KB SRAM */
    };

    /* 根节点别名 */
    chosen {
        zephyr,console = &uart0;
        zephyr,shell-uart = &uart0;
        zephyr,sram = &sram0;
        zephyr,flash = &flash0;
    };

    /* GPIO控制器 */
    gpio0: gpio@50000000 {
        compatible = "nordic,nrf-gpio";
        reg = <0x50000000 0x1000>;
        status = "okay";
        label = "GPIO_0";
    };

    /* UART设备 */
    uart0: uart@40002000 {
        compatible = "nordic,nrf-uart";
        reg = <0x40002000 0x1000>;
        interrupts = <2 1>;
        status = "okay";
        current-speed = <115200>;
        label = "UART_0";
    };

    /* I2C设备 */
    i2c0: i2c@40003000 {
        compatible = "nordic,nrf-i2c";
        reg = <0x40003000 0x1000>;
        status = "okay";
        clock-frequency = <I2C_BITRATE_STANDARD>;
    };
};

5.3 设备树覆盖文件(.overlay)

dts 复制代码
/* app.overlay - 设备树覆盖文件 */

/ {
    /* 覆盖UART配置 */
    &uart0 {
        current-speed = <9600>;
    };

    /* 添加新外设 */
    my_sensor {
        compatible = "mycompany,my-sensor";
        reg = <0x40001000 0x100>;
        status = "okay";
    };
};

/* 修改已有节点 */
&gpio0 {
    status = "okay";
};

/* 禁用不需要的外设 */
&i2c0 {
    status = "disabled";
};

5.4 在代码中使用设备树

c 复制代码
/* devicetree_usage.c - 设备树使用示例 */

#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/uart.h>

/* 方式1:使用节点标签获取设备 */
#define UART_NODE DT_NODELABEL(uart0)

/* 方式2:使用别名获取设备 */
#define LED_NODE DT_ALIAS(led0)

/* 方式3:使用路径获取设备 */
#define GPIO_NODE DT_PATH(soc, gpio, 50000000)

/* 获取设备指针 */
const struct device *uart_dev = DEVICE_DT_GET(UART_NODE);

/* 检查设备就绪 */
if (device_is_ready(uart_dev)) {
    printk("UART device is ready\n");
}

/* 读取设备属性 */
uint32_t speed = DT_PROP(UART_NODE, current_speed);
printk("Baud rate: %d\n", speed);

/* GPIO示例 */
#define LED_PIN DT_GPIO_PIN(LED_NODE, gpios)

const struct device *led_dev = DEVICE_DT_GET(DT_GPIO_CTLR(LED_NODE, gpios));
gpio_pin_configure(led_dev, LED_PIN, GPIO_OUTPUT);
gpio_pin_set(led_dev, LED_PIN, 1);

/* 获取中断号 */
#define MY_IRQ DT_IRQ_BY_IDX(DT_NODELABEL(my_device), 0, irq);
#define MY_IRQ_PRI DT_IRQ_BY_IDX(DT_NODELABEL(my_device), 0, priority);

5.5 常用设备树宏

说明
DT_NODELABEL(name) 通过标签获取节点
DT_ALIAS(alias) 通过别名获取节点
DT_PATH(...) 通过路径获取节点
DT_PROP(node_id, prop) 读取节点属性
DT_IRQ(node_id, idx) 获取中断号
DT_IRQ_BY_IDX(node_id, idx) 获取指定索引的中断
DT_GPIO_CTLR(node_id, prop) 获取GPIO控制器设备
DT_GPIO_PIN(node_id, prop) 获取GPIO引脚号

六、West Manifest配置

6.1 west.yml基本结构

yaml 复制代码
# west.yml - West Manifest文件

manifest:
  # 版本声明
  version: "0.17.2"
  
  # 远程仓库定义
  remotes:
    - name: zephyr
      url-base: https://github.com/zephyrproject-rtos
    - name: nordic
      url-base: https://github.com/nrfconnect
  
  # 默认配置
  defaults:
    remote: zephyr
    revision: main
    clone-depth: 1
  
  # 项目定义
  projects:
    # Zephyr核心(必须)
    - name: zephyr
      path: zephyr
    
    # HAL仓库
    - name: hal_nordic
      path: modules/hal/nordic
    
    # 库文件
    - name: mbedtls
      path: modules/lib/mbedtls
    
    # 引导加载器
    - name: mcuboot
      path: bootloader/mcuboot
      revision: main
  
  # 自包含配置
  self:
    path: zephyr
    west-commands: scripts/west-commands.yml

6.2 自定义Manifest配置

yaml 复制代码
# my-manifest.yml - 自定义Manifest

manifest:
  remotes:
    - name: zephyr
      url-base: https://github.com/zephyrproject-rtos
  
  defaults:
    remote: zephyr
    revision: main
    clone-depth: 1
  
  projects:
    # 核心Zephyr
    - name: zephyr
      path: zephyr
    
    # 仅需要的HAL(按需添加)
    - name: hal_nordic
      path: modules/hal/nordic
    
    - name: hal_stm32
      path: modules/hal/stm32
    
    # 协议栈
    - name: hostap
      path: modules/lib/hostap
    
    - name: openthread
      path: modules/lib/openthread

6.3 Manifest项目过滤

bash 复制代码
# 禁用特定项目
west config manifest.project-filter -- -hal_stm32

# 仅启用特定项目
west config manifest.project-filter -- +tflite-micro

# 组合使用
west config manifest.project-filter -- -hal_stm32,-hal_espressif,+tflite-micro

# 清除过滤
west config manifest.project-filter -- ""

七、构建配置详解

7.1 编译命令选项

bash 复制代码
# 基本编译命令
west build -b <board> <app>

# 指定构建目录
west build -b <board> <app> -d <build_dir>

# 清理构建
west build -t clean

# 完全清理
west build -t pristine

# 指定构建类型
west build -b <board> <app> -- -DCMAKE_BUILD_TYPE=Debug

# 使用menuconfig
west build -b <board> <app> -- -DOVERLAYER_CONFIG=app.overlay

# 运行
west build -t run

# 仅配置(不编译)
west build -t cmake

7.2 构建配置选项

选项 说明 示例
-b 目标板 -b nrf52840dk_nrf52840
-d 构建目录 -d build/debug
-t 构建目标 -t clean, -t run
-- CMake选项 -- -DCMAKE_BUILD_TYPE=Debug

7.3 CMake工具链文件

cmake 复制代码
# toolchain.cmake - 自定义工具链

# 设置工具链前缀
set(CMAKE_C_COMPILER_PREFIX "arm-zephyr-eabi-")
set(CMAKE_CXX_COMPILER_PREFIX "arm-zephyr-eabi-")

# 设置工具链路径
set(TOOLCHAIN_ROOT "/opt/zephyr-sdk")

# 设置编译器选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common -fno-strict-aliasing")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wno-unused-parameter")

# 设置链接器选项
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")

八、多目标板配置

8.1 多目标板支持

cmake 复制代码
# CMakeLists.txt - 多目标板支持

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

# 根据目标板选择配置
if(CONFIG_BOARD_NRF52840DK_NRF52840)
    target_sources(app PRIVATE src/nrf52840.c)
    set(TARGET_UART "UART_0")
elseif(CONFIG_BOARD_NRF52840DK_NRF52840)
    target_sources(app PRIVATE src/nrf52840.c)
    set(TARGET_UART "UART_0")
elseif(CONFIG_BOARD_QEMU_X86)
    target_sources(app PRIVATE src/qemu.c)
    set(TARGET_UART "UART_1")
endif()

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

8.2 条件编译

c 复制代码
// conditional_compilation.c

#include <zephyr/kernel.h>
#include <zephyr/device.h>

void main(void)
{
#if defined(CONFIG_BOARD_NRF52840DK_NRF52840)
    printk("Running on nRF52840 DK\n");
#elif defined(CONFIG_BOARD_QEMU_X86)
    printk("Running on QEMU x86\n");
#else
    printk("Running on unknown board\n");
#endif

#ifdef CONFIG_DEBUG
    printk("Debug mode enabled\n");
#endif

#ifdef CONFIG_NETWORKING
    printk("Networking enabled\n");
#endif
}

九、调试配置

9.1 调试环境配置

bash 复制代码
# 使用Segger J-Link调试
west flash --runner jlink

# 使用OpenOCD调试
west flash --runner openocd

# 使用ST-Link调试
west flash --runner stm32cubeprogrammer

# 使用QEMU调试
west build -t run

9.2 调试配置选项

选项 说明 示例
--runner 指定调试器 --runner jlink
--board-id 目标板ID --board-id 00112233
--dt-flash 设备树Flash地址 --dt-flash 0x1000

9.3 GDB调试配置

bash 复制代码
# 启动QEMU并等待调试
qemu-system-arm \
    -M microbit \
    -kernel build/zephyr/zephyr.elf \
    -serial null \
    -serial mon:stdio \
    -gdb tcp::1234 \
    -S &

# 连接GDB
arm-none-eabi-gdb build/zephyr/zephyr.elf

# GDB命令
(gdb) target remote localhost:1234
(gdb) break main
(gdb) continue
(gdb) info registers
(gdb) backtrace

十、常见配置问题与解决

10.1 配置不生效

问题:修改prj.conf后配置未生效

解决

bash 复制代码
# 清理并重新构建
west build -t pristine
west build -b <board> <app>

# 检查配置是否写入
cat build/zephyr/include/generated/autoconf.h | grep CONFIG_XXX

10.2 设备树冲突

问题:设备树配置与其他配置冲突

解决

bash 复制代码
# 检查设备树覆盖
cat app.overlay

# 验证设备树编译
west build -b <board> <app> -- -DDTC_OVERLAY_FILE=app.overlay

# 查看生成的设备树
cat build/zephyr/include/generated/devicetree.h

10.3 编译错误

问题:编译时出现链接错误

解决

bash 复制代码
# 检查是否正确包含库
grep "target_link_libraries" CMakeLists.txt

# 检查库路径
ls build/modules/*/

# 查看详细错误信息
west build -v -b <board> <app>

10.4 内存不足

问题:编译成功但运行时内存不足

解决

kconfig 复制代码
# 减小栈大小
CONFIG_MAIN_STACK_SIZE=1024

# 减小堆大小
CONFIG_HEAP_MEM_POOL_SIZE=1024

# 优化代码
CONFIG_DEBUG=n
CONFIG_LOG=n
CONFIG_SHELL=n

十一、配置模板参考

11.1 基础应用配置

kconfig 复制代码
# prj_basic.conf - 基础应用配置

CONFIG_SOC_SERIES_NRF52=y
CONFIG_SOC_NRF52840_QIAA=y
CONFIG_BOARD_NRF52840DK_NRF52840=y

CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

CONFIG_GPIO=y

11.2 网络应用配置

kconfig 复制代码
# prj_network.conf - 网络应用配置

CONFIG_SOC_SERIES_NRF52=y
CONFIG_SOC_NRF52840_QIAA=y
CONFIG_BOARD_NRF52840DK_NRF52840=y

CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=n
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NET_SOCKETS=y

CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

CONFIG_GPIO=y

11.3 BLE应用配置

kconfig 复制代码
# prj_ble.conf - BLE应用配置

CONFIG_SOC_SERIES_NRF52=y
CONFIG_SOC_NRF52840_QIAA=y
CONFIG_BOARD_NRF52840DK_NRF52840=y

CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="MyDevice"
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_CENTRAL=n

CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

CONFIG_GPIO=y

十二、最佳实践

12.1 配置管理建议

#mermaid-svg-NMmE00wFCtj0GGAB{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-NMmE00wFCtj0GGAB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NMmE00wFCtj0GGAB .error-icon{fill:#552222;}#mermaid-svg-NMmE00wFCtj0GGAB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NMmE00wFCtj0GGAB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NMmE00wFCtj0GGAB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NMmE00wFCtj0GGAB .marker.cross{stroke:#333333;}#mermaid-svg-NMmE00wFCtj0GGAB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NMmE00wFCtj0GGAB p{margin:0;}#mermaid-svg-NMmE00wFCtj0GGAB .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-NMmE00wFCtj0GGAB .cluster-label text{fill:#333;}#mermaid-svg-NMmE00wFCtj0GGAB .cluster-label span{color:#333;}#mermaid-svg-NMmE00wFCtj0GGAB .cluster-label span p{background-color:transparent;}#mermaid-svg-NMmE00wFCtj0GGAB .label text,#mermaid-svg-NMmE00wFCtj0GGAB span{fill:#333;color:#333;}#mermaid-svg-NMmE00wFCtj0GGAB .node rect,#mermaid-svg-NMmE00wFCtj0GGAB .node circle,#mermaid-svg-NMmE00wFCtj0GGAB .node ellipse,#mermaid-svg-NMmE00wFCtj0GGAB .node polygon,#mermaid-svg-NMmE00wFCtj0GGAB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NMmE00wFCtj0GGAB .rough-node .label text,#mermaid-svg-NMmE00wFCtj0GGAB .node .label text,#mermaid-svg-NMmE00wFCtj0GGAB .image-shape .label,#mermaid-svg-NMmE00wFCtj0GGAB .icon-shape .label{text-anchor:middle;}#mermaid-svg-NMmE00wFCtj0GGAB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NMmE00wFCtj0GGAB .rough-node .label,#mermaid-svg-NMmE00wFCtj0GGAB .node .label,#mermaid-svg-NMmE00wFCtj0GGAB .image-shape .label,#mermaid-svg-NMmE00wFCtj0GGAB .icon-shape .label{text-align:center;}#mermaid-svg-NMmE00wFCtj0GGAB .node.clickable{cursor:pointer;}#mermaid-svg-NMmE00wFCtj0GGAB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NMmE00wFCtj0GGAB .arrowheadPath{fill:#333333;}#mermaid-svg-NMmE00wFCtj0GGAB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NMmE00wFCtj0GGAB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NMmE00wFCtj0GGAB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NMmE00wFCtj0GGAB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NMmE00wFCtj0GGAB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NMmE00wFCtj0GGAB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NMmE00wFCtj0GGAB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NMmE00wFCtj0GGAB .cluster text{fill:#333;}#mermaid-svg-NMmE00wFCtj0GGAB .cluster span{color:#333;}#mermaid-svg-NMmE00wFCtj0GGAB 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-NMmE00wFCtj0GGAB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NMmE00wFCtj0GGAB rect.text{fill:none;stroke-width:0;}#mermaid-svg-NMmE00wFCtj0GGAB .icon-shape,#mermaid-svg-NMmE00wFCtj0GGAB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NMmE00wFCtj0GGAB .icon-shape p,#mermaid-svg-NMmE00wFCtj0GGAB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NMmE00wFCtj0GGAB .icon-shape .label rect,#mermaid-svg-NMmE00wFCtj0GGAB .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NMmE00wFCtj0GGAB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NMmE00wFCtj0GGAB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NMmE00wFCtj0GGAB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 配置管理
版本控制
模块化
文档化
纳入Git
区分环境
提取公共配置
按功能分组
注释配置项
维护CHANGELOG

12.2 实践建议

建议 说明
分层配置 将配置分为基础、应用、功能层
版本控制 配置文件纳入版本控制
环境区分 dev/prod使用不同配置
文档化 注释关键配置项
自动化 使用模板生成配置

12.3 构建优化

bash 复制代码
# 使用ccache加速编译
export CCACHE_MAXSIZE=10G
ccache -M 10G

# 并行编译
west build -b <board> <app> -- -j$(nproc)

# 仅重新编译修改的文件
west build -b <board> <app>

结束语

通过本文的详细介绍,相信您已经对Zephyr工程配置有了全面的理解:

配置系统 作用 关键文件
CMake 构建系统 CMakeLists.txt
Kconfig 配置系统 prj.conf
设备树 硬件描述 .dts, .dtsi, .overlay
West 项目管理 west.yml

掌握这些配置系统:

  1. 精准配置:按需启用/禁用功能
  2. 资源优化:优化内存和存储使用
  3. 硬件适配:适配不同硬件平台
  4. 高效开发:利用配置系统提高开发效率

建议在实际项目中:

  1. 从最小配置开始,逐步添加功能
  2. 使用版本控制管理配置文件
  3. 参考官方示例和文档
  4. 使用menuconfig等工具辅助配置

参考资料

相关推荐
ScilogyHunter3 小时前
Zephyr SDK按需配置完全指南
zephyr
ScilogyHunter3 小时前
Zephyr编译生成的build目录完全解析
zephyr
ScilogyHunter4 小时前
Zephyr开发中的Manifest文件完全解析
manifest·zephyr·west
ScilogyHunter1 天前
Zephyr概述
zephyr
ScilogyHunter1 天前
Zephyr SDK 目录结构详解
zephyr
ScilogyHunter1 天前
Zephyr Shell完全指南
shell·zephyr
ScilogyHunter1 天前
West工具完全指南
zephyr·west
ScilogyHunter1 天前
Zephyr SDK 完全指南
zephyr
Molesidy2 天前
【Embedded Development】【Zephyr】【Windows】基于STM32的Zephyr移植
stm32·led·zephyr