Zephyr主仓库目录结构完全指南
引言
Zephyr RTOS的主仓库是整个项目的核心,包含了内核、驱动、协议栈、构建系统等关键组件。对于Zephyr开发者来说,熟悉主仓库的目录结构是掌握Zephyr开发的基础。
本文将详细介绍Zephyr主仓库(zephyr)的目录结构、功能和重要文件,帮助开发者快速定位所需代码,理解项目组织方式。
一、Zephyr仓库概述
1.1 Zephyr项目架构
#mermaid-svg-OfZvcQNut0s0pkIZ{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-OfZvcQNut0s0pkIZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-OfZvcQNut0s0pkIZ .error-icon{fill:#552222;}#mermaid-svg-OfZvcQNut0s0pkIZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OfZvcQNut0s0pkIZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OfZvcQNut0s0pkIZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OfZvcQNut0s0pkIZ .marker.cross{stroke:#333333;}#mermaid-svg-OfZvcQNut0s0pkIZ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OfZvcQNut0s0pkIZ p{margin:0;}#mermaid-svg-OfZvcQNut0s0pkIZ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster-label text{fill:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster-label span{color:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster-label span p{background-color:transparent;}#mermaid-svg-OfZvcQNut0s0pkIZ .label text,#mermaid-svg-OfZvcQNut0s0pkIZ span{fill:#333;color:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ .node rect,#mermaid-svg-OfZvcQNut0s0pkIZ .node circle,#mermaid-svg-OfZvcQNut0s0pkIZ .node ellipse,#mermaid-svg-OfZvcQNut0s0pkIZ .node polygon,#mermaid-svg-OfZvcQNut0s0pkIZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-OfZvcQNut0s0pkIZ .rough-node .label text,#mermaid-svg-OfZvcQNut0s0pkIZ .node .label text,#mermaid-svg-OfZvcQNut0s0pkIZ .image-shape .label,#mermaid-svg-OfZvcQNut0s0pkIZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-OfZvcQNut0s0pkIZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-OfZvcQNut0s0pkIZ .rough-node .label,#mermaid-svg-OfZvcQNut0s0pkIZ .node .label,#mermaid-svg-OfZvcQNut0s0pkIZ .image-shape .label,#mermaid-svg-OfZvcQNut0s0pkIZ .icon-shape .label{text-align:center;}#mermaid-svg-OfZvcQNut0s0pkIZ .node.clickable{cursor:pointer;}#mermaid-svg-OfZvcQNut0s0pkIZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-OfZvcQNut0s0pkIZ .arrowheadPath{fill:#333333;}#mermaid-svg-OfZvcQNut0s0pkIZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-OfZvcQNut0s0pkIZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-OfZvcQNut0s0pkIZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OfZvcQNut0s0pkIZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-OfZvcQNut0s0pkIZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OfZvcQNut0s0pkIZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster text{fill:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ .cluster span{color:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ 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-OfZvcQNut0s0pkIZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-OfZvcQNut0s0pkIZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-OfZvcQNut0s0pkIZ .icon-shape,#mermaid-svg-OfZvcQNut0s0pkIZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OfZvcQNut0s0pkIZ .icon-shape p,#mermaid-svg-OfZvcQNut0s0pkIZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-OfZvcQNut0s0pkIZ .icon-shape .label rect,#mermaid-svg-OfZvcQNut0s0pkIZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OfZvcQNut0s0pkIZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-OfZvcQNut0s0pkIZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-OfZvcQNut0s0pkIZ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Zephyr项目
主仓库 zephyr
子仓库 hal_xxx
子仓库 lib_xxx
子仓库 modules_xxx
内核
驱动
架构支持
构建系统
板级支持
HAL库
协议栈
扩展模块
1.2 仓库获取方式
bash
# 完整克隆(包含所有历史)
git clone https://github.com/zephyrproject-rtos/zephyr
# 浅克隆(推荐,仅获取最新代码)
git clone --depth 1 https://github.com/zephyrproject-rtos/zephyr
# 查看当前版本
git describe --tags
二、顶层目录结构
2.1 目录概览
zephyr/
├── arch/ # 架构特定代码
├── boards/ # 板级支持
├── cmake/ # CMake构建相关
├── doc/ # 文档
├── drivers/ # 驱动程序
├── dts/ # 设备树文件
├── include/ # 头文件
├── kernel/ # 微内核
├── lib/ # 库文件
├── modules/ # 模块(外部仓库)
├── samples/ # 示例程序
├── scripts/ # 构建脚本
├── soc/ # SoC支持
├── subsys/ # 子系统
├── tests/ # 测试代码
├── CMakeLists.txt # 顶层CMake文件
├── Kconfig # 顶层Kconfig
├── west.yml # West Manifest
├── pyproject.toml # Python项目配置
└── README.rst # 项目说明
2.2 目录大小分布
#mermaid-svg-LGZuoDdylEZepG5b{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-LGZuoDdylEZepG5b .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-LGZuoDdylEZepG5b .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-LGZuoDdylEZepG5b .error-icon{fill:#552222;}#mermaid-svg-LGZuoDdylEZepG5b .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-LGZuoDdylEZepG5b .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-LGZuoDdylEZepG5b .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-LGZuoDdylEZepG5b .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-LGZuoDdylEZepG5b .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-LGZuoDdylEZepG5b .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-LGZuoDdylEZepG5b .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-LGZuoDdylEZepG5b .marker{fill:#333333;stroke:#333333;}#mermaid-svg-LGZuoDdylEZepG5b .marker.cross{stroke:#333333;}#mermaid-svg-LGZuoDdylEZepG5b svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-LGZuoDdylEZepG5b p{margin:0;}#mermaid-svg-LGZuoDdylEZepG5b .pieCircle{stroke:#000000;stroke-width:2px;opacity:0.7;}#mermaid-svg-LGZuoDdylEZepG5b .pieOuterCircle{stroke:#000000;stroke-width:1px;fill:none;}#mermaid-svg-LGZuoDdylEZepG5b .pieTitleText{text-anchor:middle;font-size:25px;fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-LGZuoDdylEZepG5b .slice{font-family:"trebuchet ms",verdana,arial,sans-serif;fill:#000000;font-size:17px;}#mermaid-svg-LGZuoDdylEZepG5b .legend text{fill:#000000;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:17px;}#mermaid-svg-LGZuoDdylEZepG5b :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 22% 20% 18% 12% 10% 10% 8% Zephyr仓库目录大小分布 arch drivers subsys include kernel modules 其他
三、核心目录详解
3.1 arch目录 - 架构支持
功能:包含所有支持的CPU架构代码
arch/
├── arm/ # ARM Cortex-M/A架构
│ ├── core/
│ │ ├── armv6-m/ # Cortex-M0/M0+
│ │ ├── armv7-m/ # Cortex-M3/M4/M7
│ │ ├── armv7-r/ # Cortex-R系列
│ │ └── armv8-m/ # Cortex-M33/M55/M85
│ ├── include/ # ARM通用头文件
│ └── soc/ # ARM SoC相关
├── riscv/ # RISC-V架构
│ ├── core/
│ ├── include/
│ └── soc/
├── x86/ # x86架构
│ ├── core/
│ └── soc/
├── arc/ # ARC架构
├── nios2/ # NIOS II架构
├── sparc/ # SPARC架构
└── xtensa/ # Xtensa架构
关键文件:
| 文件/目录 | 说明 |
|---|---|
arch/core/ |
架构特定的内核代码 |
arch/include/ |
架构特定的头文件 |
arch/Kconfig |
架构Kconfig配置 |
3.2 boards目录 - 板级支持
功能:包含所有支持的开发板配置文件
boards/
├── arm/
│ ├── nordic_nrf/
│ │ ├── nrf52840dk_nrf52840/
│ │ │ ├── board.cmake
│ │ │ ├── board.hdef
│ │ │ └── Kconfig.board
│ │ └── nrf52dk_nrf52832/
│ ├── st_stm32/
│ │ ├── stm32f401xe/
│ │ └── stm32f429xx/
│ └── nxp/
│ ├── frdm_k64f/
│ └── lpcxpresso55s69/
├── riscv/
│ ├── esp32c3_devkitm/
│ └── hifive_unmatched/
└── x86/
└── qemu_x86/
关键文件:
| 文件 | 说明 |
|---|---|
board.cmake |
板级CMake配置 |
Kconfig.board |
板级Kconfig配置 |
dts/bindings/ |
设备树绑定 |
3.3 drivers目录 - 驱动程序
功能:包含所有外设驱动程序
drivers/
├── adc/
│ ├── adc_adc12.c
│ ├── adc_ite_it8xxx2.c
│ └── Kconfig
├── bluetooth/
│ ├── hci_driver.c
│ └── hci_driver.h
├── can/
│ ├── can_mcux_flexcan.c
│ ├── can_stm32.c
│ └── Kconfig
├── clock_control/
│ ├── clock_control_nrf.c
│ ├── clock_control_stm32.c
│ └── Kconfig
├── counter/
├── crypto/
├── dac/
├── debug/
├── display/
├── dma/
├── entropy/
├── espi/
├── ethernet/
├── flash/
├── gpio/
├── i2c/
├── i2s/
├── interrupt_controller/
├── lora/
├── modem/
├── net/
├── pinmux/
├── pwm/
├── regulator/
├── reset/
├── rtc/
├── sensor/
├── serial/
├── spi/
├── timer/
├── usb/
├── video/
├── watchdog/
└── wifi/
驱动结构示例:
c
// drivers/gpio/gpio_driver.h
struct gpio_driver_api {
int (*configure)(struct device *port, int access_op,
uint32_t pin, int flags);
int (*write)(struct device *port, int access_op,
uint32_t pin, uint32_t value);
int (*read)(struct device *port, int access_op,
uint32_t pin, uint32_t *value);
};
3.4 dts目录 - 设备树
功能:包含设备树源文件和绑定
dts/
├── bindings/
│ ├── adc/
│ ├── bluetooth/
│ ├── can/
│ ├── clock/
│ ├── display/
│ ├── gpio/
│ ├── i2c/
│ ├── interrupt-controller/
│ ├── pinmux/
│ ├── serial/
│ ├── spi/
│ └── ...
├── common/
│ ├── skeleton.dtsi
│ └── reserved-memory.dtsi
├── arm/
│ ├── nordic/
│ │ ├── nrf52840.dtsi
│ │ └── nrf52840_qiaa.dtsi
│ └── st/
│ ├── stm32f401.dtsi
│ └── stm32f429.dtsi
├── riscv/
│ └── esp32c3/
│ └── esp32c3.dtsi
└── xtensa/
└── esp32/
└── esp32.dtsi
设备树文件类型:
| 文件后缀 | 说明 |
|---|---|
.dtsi |
设备树包含文件(可重用) |
.dts |
设备树源文件(具体开发板) |
3.5 include目录 - 头文件
功能:包含所有公共头文件
include/
├── arch/
│ ├── arc/
│ ├── arm/
│ ├── riscv/
│ └── x86/
├── bluetooth/
│ ├── att.h
│ ├── conn.h
│ ├── gatt.h
│ └── hci.h
├── debug/
├── device.h # 设备驱动模型
├── devicetree.h # 设备树API
├── drivers/
│ └── interrupt_controller/
│ └── interrupt_controller.h
├── kernel/
│ ├── kernel.h
│ ├── thread.h
│ ├── semaphore.h
│ └── mutex.h
├── net/
│ ├── buf.h
│ ├── socket.h
│ └── mqtt.h
├── posix/
│ ├── time.h
│ └── unistd.h
├── sys/
│ ├── printk.h
│ └── ring_buffer.h
└── toolchain/
├── gcc.h
└── clang.h
3.6 kernel目录 - 微内核
功能:Zephyr微内核实现
kernel/
├── Kconfig
├── CMakeLists.txt
├── device.c # 设备管理
├── errno.c # 错误处理
├── idl.c # 中断描述语言
├── init.c # 系统初始化
├── Kconfig.target # 内核目标配置
├── main.c # 主函数
├── mailbox.c # 邮箱实现
├── mutex.c # 互斥锁实现
├── pipe.c # 管道实现
├── queue.c # 队列实现
├── scheduler.c # 调度器
├── semaphore.c # 信号量实现
├── stack.c # 栈管理
├── sys_thread.c # 系统线程管理
├── thread.c # 线程管理
├── timer.c # 定时器实现
└── include/
├── kernel_internal.h
├── kernel_structs.h
└── toolchain.h
3.7 soc目录 - SoC支持
功能:芯片厂商的SoC特定代码
soc/
├── arm/
│ ├── nordic_nrf/
│ │ ├── nrf52/
│ │ │ ├── Kconfig.soc
│ │ │ ├── Kconfig.defconfig
│ │ │ └── soc.c
│ │ └── nrf91/
│ ├── st_stm32/
│ │ ├── stm32f4/
│ │ │ ├── Kconfig.soc
│ │ │ └── soc.c
│ │ └── stm32l4/
│ └── nxp_imx/
│ ├── imx6/
│ └── imx7/
├── riscv/
│ ├── riscv-ite/
│ │ └── it8xxx2/
│ └── esp32c3/
└── xtensa/
└── esp32/
四、subsys子系统目录
4.1 子系统概览
subsys/
├── adc/ # ADC子系统
├── bluetooth/ # Bluetooth协议栈
├── canbus/ # CAN总线子系统
├── console/ # 控制台子系统
├── cordac/ # CoRDAC
├── crypto/ # 加密子系统
├── debug/ # 调试子系统
├── dfu/ # 设备固件更新
├── disk/ # 磁盘/存储子系统
├── display/ # 显示子系统
├── doc/ # 文档子系统
├── emul/ # 仿真子系统
├── fs/ # 文件系统
├── ipc/ # 进程间通信
├── logging/ # 日志子系统
├── mgmt/ # 管理子系统
├── modbus/ # Modbus协议
├── net/ # 网络协议栈
├── mgmt/ # 管理接口
├── openthread/ # Thread协议
├── pm/ # 电源管理
├── sd/ # SD卡支持
├── sensor/ # 传感器子系统
├── shell/ # Shell子系统
├── test/ # 测试框架
├── usb/ # USB子系统
└── Zephyr.mk # 子系统构建文件
4.2 重要子系统详解
Bluetooth子系统 (subsys/bluetooth)
subsys/bluetooth/
├── controller/ # 控制器
├── host/ # 主机
│ ├── att.c # 属性协议
│ ├── conn.c # 连接管理
│ ├── gatt.c # 通用属性协议
│ ├── hci.c # HCI接口
│ └── l2cap.c # 逻辑链路控制适配协议
├── mesh/ # 蓝牙Mesh
└── settings/ # 设置存储
网络子系统 (subsys/net)
subsys/net/
├── ip/ # IP协议
├── tcp.c # TCP协议
├── udp.c # UDP协议
├── lib/
│ ├── dhcpv4/ # DHCPv4
│ ├── dns/ # DNS
│ ├── http/ # HTTP
│ ├── lwm2m/ # LwM2M
│ ├── mqtt/ # MQTT
│ └── coap/ # CoAP
└── ip/
├── icmpv4.c # ICMPv4
├── ipv4.c # IPv4
├── ipv6.c # IPv6
└── tcp.c # TCP
五、构建系统目录
5.1 cmake目录
cmake/
├── extensions.cmake # CMake扩展
├── sys_extensions.cmake # 系统扩展
├── linker/
│ ├── common/
│ │ ├── common.cmake
│ │ ├── debug.cmake
│ │ └── linkerer_script.cmake
│ └── target/
│ └── linker.cmake
├── modules/
│ ├── FindDtc.cmake
│ ├── FindDeviceTree.cmake
│ └── ...
├── app/
│ └── app.cmake # 应用构建
├── host/
│ └── host.cmake # 主机工具构建
└── pkgconf/
└── pkgconf.cmake # 配置工具
5.2 scripts目录
scripts/
├── build/
│ ├── build.info
│ ├── buildwar.py
│ └── kconfiglib.py # Kconfig解析
├── dts/
│ ├── gen_defines.py # 设备树代码生成
│ ├── python-devicetree/
│ │ └── devicetree/
│ │ ├── __init__.py
│ │ └── codegen.py
├── tests/
│ └── ...
├── west-commands.yml # West命令定义
├── zephyr_module.py # 模块处理
└── sanitycheck # 自动化测试脚本
六、其他重要目录
6.1 samples目录 - 示例程序
samples/
├── basic/
│ ├── blinky/ # LED闪烁
│ ├── button/ # 按钮输入
│ └── hello_world/ # Hello World
├── bluetooth/
│ ├── beacon/ # 蓝牙信标
│ ├── central_hr/ # 中心角色
│ └── peripheral/ # 外设角色
├── net/
│ ├── sockets/
│ │ ├── http_get/ # HTTP客户端
│ │ └── echo_client/ # Echo客户端
│ └── mqtt_subscriber/ # MQTT订阅
├── drivers/
│ ├── adc/
│ ├── gpio/
│ └── spi/
└── subsys/
├── shell/
└── logging/
6.2 tests目录 - 测试代码
tests/
├── lib/
│ ├── devicetree/
│ ├── cmock/
│ └── onoff/
├── drivers/
│ ├── adc/
│ ├── can/
│ └── gpio/
├── kernel/
│ ├── mem_slab/
│ ├── mutex/
│ ├── pipe/
│ ├── queue/
│ └── timer/
└── subsys/
├── bluetooth/
├── net/
└── shell/
6.3 doc目录 - 文档
doc/
├── _static/ # 静态资源
├── _templates/ # 模板
├── conf.py # Sphinx配置
├── index.rst # 文档首页
├── getting_started/
│ └── index.rst # 入门指南
├── reference/
│ ├── api/
│ ├── kernel/
│ └── build/
└── guides/
├── userguides/
└── devguides/
七、关键配置文件
7.1 CMakeLists.txt结构
#mermaid-svg-sBKzQhG1f4HAOKKh{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-sBKzQhG1f4HAOKKh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sBKzQhG1f4HAOKKh .error-icon{fill:#552222;}#mermaid-svg-sBKzQhG1f4HAOKKh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sBKzQhG1f4HAOKKh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sBKzQhG1f4HAOKKh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sBKzQhG1f4HAOKKh .marker.cross{stroke:#333333;}#mermaid-svg-sBKzQhG1f4HAOKKh svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sBKzQhG1f4HAOKKh p{margin:0;}#mermaid-svg-sBKzQhG1f4HAOKKh .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster-label text{fill:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster-label span{color:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster-label span p{background-color:transparent;}#mermaid-svg-sBKzQhG1f4HAOKKh .label text,#mermaid-svg-sBKzQhG1f4HAOKKh span{fill:#333;color:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh .node rect,#mermaid-svg-sBKzQhG1f4HAOKKh .node circle,#mermaid-svg-sBKzQhG1f4HAOKKh .node ellipse,#mermaid-svg-sBKzQhG1f4HAOKKh .node polygon,#mermaid-svg-sBKzQhG1f4HAOKKh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-sBKzQhG1f4HAOKKh .rough-node .label text,#mermaid-svg-sBKzQhG1f4HAOKKh .node .label text,#mermaid-svg-sBKzQhG1f4HAOKKh .image-shape .label,#mermaid-svg-sBKzQhG1f4HAOKKh .icon-shape .label{text-anchor:middle;}#mermaid-svg-sBKzQhG1f4HAOKKh .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-sBKzQhG1f4HAOKKh .rough-node .label,#mermaid-svg-sBKzQhG1f4HAOKKh .node .label,#mermaid-svg-sBKzQhG1f4HAOKKh .image-shape .label,#mermaid-svg-sBKzQhG1f4HAOKKh .icon-shape .label{text-align:center;}#mermaid-svg-sBKzQhG1f4HAOKKh .node.clickable{cursor:pointer;}#mermaid-svg-sBKzQhG1f4HAOKKh .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-sBKzQhG1f4HAOKKh .arrowheadPath{fill:#333333;}#mermaid-svg-sBKzQhG1f4HAOKKh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-sBKzQhG1f4HAOKKh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-sBKzQhG1f4HAOKKh .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sBKzQhG1f4HAOKKh .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-sBKzQhG1f4HAOKKh .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sBKzQhG1f4HAOKKh .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster text{fill:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh .cluster span{color:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh 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-sBKzQhG1f4HAOKKh .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-sBKzQhG1f4HAOKKh rect.text{fill:none;stroke-width:0;}#mermaid-svg-sBKzQhG1f4HAOKKh .icon-shape,#mermaid-svg-sBKzQhG1f4HAOKKh .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-sBKzQhG1f4HAOKKh .icon-shape p,#mermaid-svg-sBKzQhG1f4HAOKKh .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-sBKzQhG1f4HAOKKh .icon-shape .label rect,#mermaid-svg-sBKzQhG1f4HAOKKh .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-sBKzQhG1f4HAOKKh .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-sBKzQhG1f4HAOKKh .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-sBKzQhG1f4HAOKKh :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 顶层CMakeLists.txt
project/zephyr
include_root
find_package
add_subdirectory
arch/
boards/
drivers/
dtb/
kernel/
soc/
subsys/
tests/
samples/
版本: 3.5.0
描述: Zephyr RTOS
7.2 Kconfig配置
# Kconfig层级结构
Kconfig # 顶层Kconfig
├── arch/Kconfig # 架构配置
├── boards/Kconfig # 板级配置
├── drivers/Kconfig # 驱动配置
├── kernel/Kconfig # 内核配置
├── soc/Kconfig # SoC配置
└── subsys/Kconfig # 子系统配置
7.3 west.yml Manifest
yaml
# West Manifest配置
manifest:
remotes:
- name: zephyr
url-base: https://github.com/zephyrproject-rtos
projects:
- name: zephyr
remote: zephyr
revision: main
path: zephyr
# 导入外部模块
- name: hal_nordic
remote: zephyr
path: modules/hal/nordic
八、目录关系图
8.1 代码查找流程
#mermaid-svg-eIGXky64BuMqJYfr{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-eIGXky64BuMqJYfr .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-eIGXky64BuMqJYfr .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-eIGXky64BuMqJYfr .error-icon{fill:#552222;}#mermaid-svg-eIGXky64BuMqJYfr .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-eIGXky64BuMqJYfr .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-eIGXky64BuMqJYfr .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-eIGXky64BuMqJYfr .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-eIGXky64BuMqJYfr .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-eIGXky64BuMqJYfr .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-eIGXky64BuMqJYfr .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-eIGXky64BuMqJYfr .marker{fill:#333333;stroke:#333333;}#mermaid-svg-eIGXky64BuMqJYfr .marker.cross{stroke:#333333;}#mermaid-svg-eIGXky64BuMqJYfr svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-eIGXky64BuMqJYfr p{margin:0;}#mermaid-svg-eIGXky64BuMqJYfr .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-eIGXky64BuMqJYfr .cluster-label text{fill:#333;}#mermaid-svg-eIGXky64BuMqJYfr .cluster-label span{color:#333;}#mermaid-svg-eIGXky64BuMqJYfr .cluster-label span p{background-color:transparent;}#mermaid-svg-eIGXky64BuMqJYfr .label text,#mermaid-svg-eIGXky64BuMqJYfr span{fill:#333;color:#333;}#mermaid-svg-eIGXky64BuMqJYfr .node rect,#mermaid-svg-eIGXky64BuMqJYfr .node circle,#mermaid-svg-eIGXky64BuMqJYfr .node ellipse,#mermaid-svg-eIGXky64BuMqJYfr .node polygon,#mermaid-svg-eIGXky64BuMqJYfr .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-eIGXky64BuMqJYfr .rough-node .label text,#mermaid-svg-eIGXky64BuMqJYfr .node .label text,#mermaid-svg-eIGXky64BuMqJYfr .image-shape .label,#mermaid-svg-eIGXky64BuMqJYfr .icon-shape .label{text-anchor:middle;}#mermaid-svg-eIGXky64BuMqJYfr .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-eIGXky64BuMqJYfr .rough-node .label,#mermaid-svg-eIGXky64BuMqJYfr .node .label,#mermaid-svg-eIGXky64BuMqJYfr .image-shape .label,#mermaid-svg-eIGXky64BuMqJYfr .icon-shape .label{text-align:center;}#mermaid-svg-eIGXky64BuMqJYfr .node.clickable{cursor:pointer;}#mermaid-svg-eIGXky64BuMqJYfr .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-eIGXky64BuMqJYfr .arrowheadPath{fill:#333333;}#mermaid-svg-eIGXky64BuMqJYfr .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-eIGXky64BuMqJYfr .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-eIGXky64BuMqJYfr .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eIGXky64BuMqJYfr .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-eIGXky64BuMqJYfr .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eIGXky64BuMqJYfr .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-eIGXky64BuMqJYfr .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-eIGXky64BuMqJYfr .cluster text{fill:#333;}#mermaid-svg-eIGXky64BuMqJYfr .cluster span{color:#333;}#mermaid-svg-eIGXky64BuMqJYfr 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-eIGXky64BuMqJYfr .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-eIGXky64BuMqJYfr rect.text{fill:none;stroke-width:0;}#mermaid-svg-eIGXky64BuMqJYfr .icon-shape,#mermaid-svg-eIGXky64BuMqJYfr .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-eIGXky64BuMqJYfr .icon-shape p,#mermaid-svg-eIGXky64BuMqJYfr .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-eIGXky64BuMqJYfr .icon-shape .label rect,#mermaid-svg-eIGXky64BuMqJYfr .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-eIGXky64BuMqJYfr .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-eIGXky64BuMqJYfr .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-eIGXky64BuMqJYfr :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 应用程序
CMakeLists.txt
查找Kconfig
查找设备树
配置驱动
arch/
soc/
boards/
dts/
.dtsi文件
.dts文件
drivers/
device.h
device_get_binding
8.2 构建过程与目录关系
#mermaid-svg-wlsLQT7bn4mYK8q0{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-wlsLQT7bn4mYK8q0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wlsLQT7bn4mYK8q0 .error-icon{fill:#552222;}#mermaid-svg-wlsLQT7bn4mYK8q0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wlsLQT7bn4mYK8q0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .marker.cross{stroke:#333333;}#mermaid-svg-wlsLQT7bn4mYK8q0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wlsLQT7bn4mYK8q0 p{margin:0;}#mermaid-svg-wlsLQT7bn4mYK8q0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster-label text{fill:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster-label span{color:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster-label span p{background-color:transparent;}#mermaid-svg-wlsLQT7bn4mYK8q0 .label text,#mermaid-svg-wlsLQT7bn4mYK8q0 span{fill:#333;color:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .node rect,#mermaid-svg-wlsLQT7bn4mYK8q0 .node circle,#mermaid-svg-wlsLQT7bn4mYK8q0 .node ellipse,#mermaid-svg-wlsLQT7bn4mYK8q0 .node polygon,#mermaid-svg-wlsLQT7bn4mYK8q0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .rough-node .label text,#mermaid-svg-wlsLQT7bn4mYK8q0 .node .label text,#mermaid-svg-wlsLQT7bn4mYK8q0 .image-shape .label,#mermaid-svg-wlsLQT7bn4mYK8q0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-wlsLQT7bn4mYK8q0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .rough-node .label,#mermaid-svg-wlsLQT7bn4mYK8q0 .node .label,#mermaid-svg-wlsLQT7bn4mYK8q0 .image-shape .label,#mermaid-svg-wlsLQT7bn4mYK8q0 .icon-shape .label{text-align:center;}#mermaid-svg-wlsLQT7bn4mYK8q0 .node.clickable{cursor:pointer;}#mermaid-svg-wlsLQT7bn4mYK8q0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .arrowheadPath{fill:#333333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wlsLQT7bn4mYK8q0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wlsLQT7bn4mYK8q0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wlsLQT7bn4mYK8q0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster text{fill:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 .cluster span{color:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 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-wlsLQT7bn4mYK8q0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wlsLQT7bn4mYK8q0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-wlsLQT7bn4mYK8q0 .icon-shape,#mermaid-svg-wlsLQT7bn4mYK8q0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wlsLQT7bn4mYK8q0 .icon-shape p,#mermaid-svg-wlsLQT7bn4mYK8q0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wlsLQT7bn4mYK8q0 .icon-shape .label rect,#mermaid-svg-wlsLQT7bn4mYK8q0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wlsLQT7bn4mYK8q0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wlsLQT7bn4mYK8q0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wlsLQT7bn4mYK8q0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} west build
CMake配置阶段
读取CMakeLists.txt
解析Kconfig
处理设备树
include/
drivers/
kernel/
生成autoconf.h
生成devicetree.h
构建阶段
Ninja构建
输出到build/
九、常用开发场景与目录对应
9.1 添加新驱动
bash
# 驱动目录结构
drivers/
└── my_driver/
├── CMakeLists.txt
├── Kconfig
├── my_driver.c
└── my_driver.h
9.2 添加新板级支持
bash
# 板级目录结构
boards/
└── arm/
└── myboard/
├── myboard_defconfig
├── myboard.dts
├── myboard.yaml
├── board.cmake
├── board.h
└── Kconfig.board
9.3 添加新SoC支持
bash
# SoC目录结构
soc/
└── arm/
└── myvendor_mysoc/
├── Kconfig.soc
├── Kconfig.defconfig
├── soc.c
└── include/
└── soc.h
9.4 创建新应用程序
bash
# 应用目录结构
app/
├── CMakeLists.txt
├── prj.conf
├── src/
│ └── main.c
└── Kconfig
十、快速参考指南
10.1 常用目录速查表
| 功能需求 | 对应目录 |
|---|---|
| 修改内核行为 | kernel/ |
| 添加新驱动 | drivers/ |
| 板级配置 | boards/ |
| SoC支持 | soc/ |
| 架构代码 | arch/ |
| 头文件 | include/ |
| 设备树 | dts/ |
| Bluetooth开发 | subsys/bluetooth/ |
| 网络开发 | subsys/net/ |
| 添加测试 | tests/ |
| 参考示例 | samples/ |
10.2 关键文件速查表
| 文件名 | 位置 | 说明 |
|---|---|---|
CMakeLists.txt |
根目录 | 顶层构建配置 |
Kconfig |
根目录 | 顶层配置 |
west.yml |
根目录 | West Manifest |
devicetree.h |
include/ |
设备树API |
device.h |
include/ |
驱动模型 |
kernel.h |
include/ |
内核API |
10.3 构建命令参考
bash
# 清理构建
west build -t clean
# 重新构建
west build -t rebuild
# 指定构建目录
west build -b <board> <app> -d builddir
# 查看配置
west build -b <board> <app> -- menuconfig
结束语
通过本文的介绍,相信您已经对Zephyr主仓库的目录结构有了全面的了解:
| 目录 | 主要内容 |
|---|---|
arch/ |
CPU架构支持代码 |
boards/ |
开发板配置文件 |
drivers/ |
外设驱动程序 |
dts/ |
设备树源文件和绑定 |
include/ |
公共头文件 |
kernel/ |
微内核实现 |
soc/ |
SoC特定代码 |
subsys/ |
子系统(Bluetooth、网络等) |
cmake/ |
构建系统 |
scripts/ |
构建脚本和工具 |
samples/ |
示例程序 |
tests/ |
测试代码 |
熟悉这些目录结构,将帮助您:
- 快速定位代码:知道去哪里找所需的源文件
- 理解项目组织:掌握Zephyr的模块化设计
- 高效开发:遵循已有的目录结构和规范
- 调试问题:快速定位问题所在的模块
建议在实际开发中多查阅这些目录,结合官方文档深入理解每个组件的实现细节。
参考资料: