使用Kconfig配置Zephyr工程完全指南

使用Kconfig配置Zephyr工程完全指南

引言

Kconfig是Zephyr RTOS的核心配置系统,它源自Linux内核,提供了一种灵活的配置机制,可以在编译时选择启用或禁用功能。理解和掌握Kconfig是高效开发Zephyr应用的关键。

本文将深入介绍Kconfig的工作原理、配置方式、语法规则,以及如何在实际项目中使用Kconfig来配置Zephyr工程。


一、Kconfig配置系统概述

1.1 Kconfig工作原理

#mermaid-svg-NdOmbvN2L94JScC4{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-NdOmbvN2L94JScC4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NdOmbvN2L94JScC4 .error-icon{fill:#552222;}#mermaid-svg-NdOmbvN2L94JScC4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NdOmbvN2L94JScC4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NdOmbvN2L94JScC4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NdOmbvN2L94JScC4 .marker.cross{stroke:#333333;}#mermaid-svg-NdOmbvN2L94JScC4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NdOmbvN2L94JScC4 p{margin:0;}#mermaid-svg-NdOmbvN2L94JScC4 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-NdOmbvN2L94JScC4 .cluster-label text{fill:#333;}#mermaid-svg-NdOmbvN2L94JScC4 .cluster-label span{color:#333;}#mermaid-svg-NdOmbvN2L94JScC4 .cluster-label span p{background-color:transparent;}#mermaid-svg-NdOmbvN2L94JScC4 .label text,#mermaid-svg-NdOmbvN2L94JScC4 span{fill:#333;color:#333;}#mermaid-svg-NdOmbvN2L94JScC4 .node rect,#mermaid-svg-NdOmbvN2L94JScC4 .node circle,#mermaid-svg-NdOmbvN2L94JScC4 .node ellipse,#mermaid-svg-NdOmbvN2L94JScC4 .node polygon,#mermaid-svg-NdOmbvN2L94JScC4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NdOmbvN2L94JScC4 .rough-node .label text,#mermaid-svg-NdOmbvN2L94JScC4 .node .label text,#mermaid-svg-NdOmbvN2L94JScC4 .image-shape .label,#mermaid-svg-NdOmbvN2L94JScC4 .icon-shape .label{text-anchor:middle;}#mermaid-svg-NdOmbvN2L94JScC4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NdOmbvN2L94JScC4 .rough-node .label,#mermaid-svg-NdOmbvN2L94JScC4 .node .label,#mermaid-svg-NdOmbvN2L94JScC4 .image-shape .label,#mermaid-svg-NdOmbvN2L94JScC4 .icon-shape .label{text-align:center;}#mermaid-svg-NdOmbvN2L94JScC4 .node.clickable{cursor:pointer;}#mermaid-svg-NdOmbvN2L94JScC4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NdOmbvN2L94JScC4 .arrowheadPath{fill:#333333;}#mermaid-svg-NdOmbvN2L94JScC4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NdOmbvN2L94JScC4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NdOmbvN2L94JScC4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NdOmbvN2L94JScC4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NdOmbvN2L94JScC4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NdOmbvN2L94JScC4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NdOmbvN2L94JScC4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NdOmbvN2L94JScC4 .cluster text{fill:#333;}#mermaid-svg-NdOmbvN2L94JScC4 .cluster span{color:#333;}#mermaid-svg-NdOmbvN2L94JScC4 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-NdOmbvN2L94JScC4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NdOmbvN2L94JScC4 rect.text{fill:none;stroke-width:0;}#mermaid-svg-NdOmbvN2L94JScC4 .icon-shape,#mermaid-svg-NdOmbvN2L94JScC4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NdOmbvN2L94JScC4 .icon-shape p,#mermaid-svg-NdOmbvN2L94JScC4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NdOmbvN2L94JScC4 .icon-shape .label rect,#mermaid-svg-NdOmbvN2L94JScC4 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NdOmbvN2L94JScC4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NdOmbvN2L94JScC4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NdOmbvN2L94JScC4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Kconfig文件
Kconfig解析器
配置合并
autoconf.h生成
编译应用
prj.conf
Kconfig文件
board.conf
默认值
用户配置
依赖关系

1.2 Kconfig配置流程

编译器 Kconfig解析器 CMake West工具 用户 编译器 Kconfig解析器 CMake West工具 用户 #mermaid-svg-1LvqfZPGIkGQhGpf{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-1LvqfZPGIkGQhGpf .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1LvqfZPGIkGQhGpf .error-icon{fill:#552222;}#mermaid-svg-1LvqfZPGIkGQhGpf .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1LvqfZPGIkGQhGpf .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1LvqfZPGIkGQhGpf .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1LvqfZPGIkGQhGpf .marker.cross{stroke:#333333;}#mermaid-svg-1LvqfZPGIkGQhGpf svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1LvqfZPGIkGQhGpf p{margin:0;}#mermaid-svg-1LvqfZPGIkGQhGpf .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1LvqfZPGIkGQhGpf text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-1LvqfZPGIkGQhGpf .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-1LvqfZPGIkGQhGpf .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-1LvqfZPGIkGQhGpf #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-1LvqfZPGIkGQhGpf .sequenceNumber{fill:white;}#mermaid-svg-1LvqfZPGIkGQhGpf #sequencenumber{fill:#333;}#mermaid-svg-1LvqfZPGIkGQhGpf #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-1LvqfZPGIkGQhGpf .messageText{fill:#333;stroke:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1LvqfZPGIkGQhGpf .labelText,#mermaid-svg-1LvqfZPGIkGQhGpf .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .loopText,#mermaid-svg-1LvqfZPGIkGQhGpf .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .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-1LvqfZPGIkGQhGpf .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-1LvqfZPGIkGQhGpf .noteText,#mermaid-svg-1LvqfZPGIkGQhGpf .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-1LvqfZPGIkGQhGpf .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1LvqfZPGIkGQhGpf .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1LvqfZPGIkGQhGpf .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1LvqfZPGIkGQhGpf .actorPopupMenu{position:absolute;}#mermaid-svg-1LvqfZPGIkGQhGpf .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-1LvqfZPGIkGQhGpf .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1LvqfZPGIkGQhGpf .actor-man circle,#mermaid-svg-1LvqfZPGIkGQhGpf line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-1LvqfZPGIkGQhGpf :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} west build -b <board> 启动CMake配置 解析Kconfig文件 读取prj.conf 解析依赖关系 生成配置头文件 编译应用 生成可执行文件

1.3 Kconfig配置文件层次

层级 文件 优先级 说明
1 boards/<board>/board.conf 最高 板级默认配置
2 prj.conf 应用配置
3 overlay.conf 配置覆盖
4 Kconfig 配置定义

二、prj.conf配置文件详解

2.1 prj.conf基本结构

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

# ===== 基础配置 =====
# 目标板配置(由west build -b指定)
# 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              # UART控制台
CONFIG_CONSOLE_INPUT=y             # 控制台输入
CONFIG_CONSOLE_HANDLER=y           # 控制台处理器

# ===== 驱动配置 =====
CONFIG_GPIO=y                     # GPIO驱动
CONFIG_I2C=y                      # I2C驱动
CONFIG_SPI=y                      # SPI驱动
CONFIG_UART=y                     # UART驱动

# ===== 网络配置 =====
CONFIG_NETWORKING=y               # 启用网络
CONFIG_NET_IPV4=y                 # IPv4支持
CONFIG_NET_IPV6=y                 # IPv6支持
CONFIG_NET_TCP=y                  # TCP支持
CONFIG_NET_UDP=y                  # UDP支持

# ===== 存储配置 =====
CONFIG_FLASH=y                    # Flash驱动
CONFIG_FLASH_MAP=y                # Flash映射
CONFIG_FILE_SYSTEM=y              # 文件系统
CONFIG_FAT_FILESYSTEM=y           # FAT文件系统

2.2 配置项类型

类型 语法 示例 说明
布尔型 CONFIG_XXX=y/n CONFIG_DEBUG=y 启用(=y)或禁用(=n)
整型 CONFIG_XXX=数字 CONFIG_MAIN_STACK_SIZE=2048 整数值
字符串型 CONFIG_XXX="值" CONFIG_BT_DEVICE_NAME="MyDevice" 字符串值
选择型 CONFIG_XXX=选项 CONFIG_LOG_LEVEL=LOG_LEVEL_DEBUG 从预设选项中选择

2.3 配置项命名规范

#mermaid-svg-2UMTKndBqESDy8Jd{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-2UMTKndBqESDy8Jd .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2UMTKndBqESDy8Jd .error-icon{fill:#552222;}#mermaid-svg-2UMTKndBqESDy8Jd .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2UMTKndBqESDy8Jd .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2UMTKndBqESDy8Jd .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2UMTKndBqESDy8Jd .marker.cross{stroke:#333333;}#mermaid-svg-2UMTKndBqESDy8Jd svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2UMTKndBqESDy8Jd p{margin:0;}#mermaid-svg-2UMTKndBqESDy8Jd .edge{stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .section--1 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section--1 path,#mermaid-svg-2UMTKndBqESDy8Jd .section--1 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section--1 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section--1 text{fill:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth--1{stroke-width:17;}#mermaid-svg-2UMTKndBqESDy8Jd .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-0 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-0 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-0 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-0 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-0 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-0{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-0{stroke-width:14;}#mermaid-svg-2UMTKndBqESDy8Jd .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-1 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-1 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-1 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-1 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-1 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-1{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-1{stroke-width:11;}#mermaid-svg-2UMTKndBqESDy8Jd .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-2 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-2 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-2 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-2 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-2 text{fill:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-2{stroke-width:8;}#mermaid-svg-2UMTKndBqESDy8Jd .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-3 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-3 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-3 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-3 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-3 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-3{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-3{stroke-width:5;}#mermaid-svg-2UMTKndBqESDy8Jd .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-4 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-4 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-4 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-4 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-4 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-4{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-4{stroke-width:2;}#mermaid-svg-2UMTKndBqESDy8Jd .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-5 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-5 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-5 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-5 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-5 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-5{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-5{stroke-width:-1;}#mermaid-svg-2UMTKndBqESDy8Jd .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-6 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-6 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-6 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-6 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-6 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-6{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-6{stroke-width:-4;}#mermaid-svg-2UMTKndBqESDy8Jd .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-7 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-7 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-7 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-7 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-7 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-7{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-7{stroke-width:-7;}#mermaid-svg-2UMTKndBqESDy8Jd .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-8 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-8 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-8 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-8 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-8 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-8{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-8{stroke-width:-10;}#mermaid-svg-2UMTKndBqESDy8Jd .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-9 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-9 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-9 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-9 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-9 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-9{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-9{stroke-width:-13;}#mermaid-svg-2UMTKndBqESDy8Jd .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-10 rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-10 path,#mermaid-svg-2UMTKndBqESDy8Jd .section-10 circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-10 polygon,#mermaid-svg-2UMTKndBqESDy8Jd .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-10 text{fill:black;}#mermaid-svg-2UMTKndBqESDy8Jd .node-icon-10{font-size:40px;color:black;}#mermaid-svg-2UMTKndBqESDy8Jd .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .edge-depth-10{stroke-width:-16;}#mermaid-svg-2UMTKndBqESDy8Jd .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled,#mermaid-svg-2UMTKndBqESDy8Jd .disabled circle,#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:lightgray;}#mermaid-svg-2UMTKndBqESDy8Jd .disabled text{fill:#efefef;}#mermaid-svg-2UMTKndBqESDy8Jd .section-root rect,#mermaid-svg-2UMTKndBqESDy8Jd .section-root path,#mermaid-svg-2UMTKndBqESDy8Jd .section-root circle,#mermaid-svg-2UMTKndBqESDy8Jd .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-2UMTKndBqESDy8Jd .section-root text{fill:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .section-root span{color:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .section-2 span{color:#ffffff;}#mermaid-svg-2UMTKndBqESDy8Jd .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-2UMTKndBqESDy8Jd .edge{fill:none;}#mermaid-svg-2UMTKndBqESDy8Jd .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-2UMTKndBqESDy8Jd :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} CONFIG命名规范
前缀分类
CONFIG_SOC_*
CONFIG_BOARD_*
CONFIG_DRIVER_*
CONFIG_NET_*
CONFIG_BT_*
功能分类
CONFIG_KERNEL
CONFIG_DEBUG
CONFIG_CONSOLE
CONFIG_LOG
CONFIG_SHELL
状态标识
_ENABLED
_DISABLED
_SUPPORTED


三、Kconfig语法详解

3.1 基本语法规则

kconfig 复制代码
# Kconfig文件基本结构

# 1. 注释(以#开头)
# 这是一个注释

# 2. 菜单定义
menu "Kernel Options"
    # 菜单内容
endmenu

# 3. 配置项定义
config CONFIG_DEBUG
    bool "Enable debug mode"
    default n
    help
        This option enables debug mode for the kernel.

# 4. 选择项定义
choice
    prompt "Log Level"
    default LOG_LEVEL_INFO

    config LOG_LEVEL_NONE
        bool "None"
    
    config LOG_LEVEL_ERR
        bool "Error"
    
    config LOG_LEVEL_WRN
        bool "Warning"
    
    config LOG_LEVEL_INFO
        bool "Info"
    
    config LOG_LEVEL_DBG
        bool "Debug"
endchoice

# 5. 依赖关系
config CONFIG_BT
    bool "Bluetooth support"
    depends on SOC_HAS_BLE
    select CONFIG_SERIAL
    help
        Enable Bluetooth support.

# 6. 默认值
config CONFIG_MAIN_STACK_SIZE
    int "Main thread stack size"
    default 1024 if BOARD_QEMU_X86
    default 2048 if BOARD_NRF52840DK
    help
        Stack size for main thread.

3.2 Kconfig关键字

关键字 说明 示例
config 定义配置项 config CONFIG_DEBUG
menu 定义菜单 menu "Kernel Options"
choice 定义选择组 choice LOG_LEVEL
bool 布尔类型 bool "Enable debug"
int 整型 int "Stack size"
string 字符串类型 string "Device name"
default 默认值 default y
depends on 依赖条件 depends on SOC_HAS_BLE
select 自动选择 select CONFIG_SERIAL
help 帮助文本 help Enable feature
if 条件判断 if BOARD_QEMU_X86
range 值范围 range 1024 65536

3.3 Kconfig条件表达式

kconfig 复制代码
# 条件表达式示例

# 基本条件
depends on CONFIG_DEBUG
depends on !CONFIG_DEBUG

# 逻辑与
depends on CONFIG_DEBUG && CONFIG_SERIAL

# 逻辑或
depends on CONFIG_DEBUG || CONFIG_LOG

# 复杂条件
depends on (CONFIG_DEBUG && CONFIG_SERIAL) || CONFIG_LOG

# 字符串匹配
depends on SOC_SERIES_NRF52

# 值比较
depends on CONFIG_MAIN_STACK_SIZE >= 1024

# 存在性检查
depends on SOC_HAS_BLE

四、在代码中使用Kconfig配置

4.1 条件编译

c 复制代码
/* 使用Kconfig配置进行条件编译 */

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

void main(void)
{
    printk("Zephyr application started\n");

#ifdef CONFIG_DEBUG
    printk("Debug mode enabled\n");
    
    // 调试相关代码
    #ifdef CONFIG_DEBUG_THREAD_INFO
        printk("Thread info enabled\n");
    #endif
#endif

#ifdef CONFIG_NETWORKING
    printk("Networking enabled\n");
    
    #ifdef CONFIG_NET_IPV4
        printk("IPv4 support enabled\n");
    #endif
    
    #ifdef CONFIG_NET_IPV6
        printk("IPv6 support enabled\n");
    #endif
#endif

#ifdef CONFIG_BT
    printk("Bluetooth enabled\n");
#endif

#ifdef CONFIG_LOG
    printk("Logging enabled\n");
#endif
}

4.2 获取配置值

c 复制代码
/* 获取Kconfig配置值 */

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

void print_config_info(void)
{
    // 获取整型配置值
    printk("Main stack size: %d bytes\n", CONFIG_MAIN_STACK_SIZE);
    printk("Number of priorities: %d\n", CONFIG_NUM_PREEMPT_PRIORITIES);
    printk("Clock ticks per second: %d\n", CONFIG_SYS_CLOCK_TICKS_PER_SEC);
    
    // 获取字符串配置值
    #ifdef CONFIG_BT_DEVICE_NAME
        printk("Bluetooth device name: %s\n", CONFIG_BT_DEVICE_NAME);
    #endif
    
    // 获取布尔配置值(作为条件判断)
    #if CONFIG_DEBUG
        printk("Debug is enabled\n");
    #else
        printk("Debug is disabled\n");
    #endif
}

4.3 运行时配置检查

c 复制代码
/* 运行时检查配置 */

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

void runtime_config_check(void)
{
    // 使用编译时常量
    static const int stack_size = CONFIG_MAIN_STACK_SIZE;
    
    printk("Static stack size: %d\n", stack_size);
    
    // 条件分支
    if (IS_ENABLED(CONFIG_DEBUG)) {
        printk("Debug mode is enabled at runtime\n");
    }
    
    // 多层条件
    if (IS_ENABLED(CONFIG_NETWORKING)) {
        if (IS_ENABLED(CONFIG_NET_TCP)) {
            printk("TCP support is available\n");
        }
    }
}

五、Kconfig配置工具

bash 复制代码
# 启动menuconfig
west build -b <board> <app> -- menuconfig

# 或使用以下命令
west build -t menuconfig

menuconfig界面说明:
#mermaid-svg-ibPpjKIk8CtDwzXL{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-ibPpjKIk8CtDwzXL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ibPpjKIk8CtDwzXL .error-icon{fill:#552222;}#mermaid-svg-ibPpjKIk8CtDwzXL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ibPpjKIk8CtDwzXL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ibPpjKIk8CtDwzXL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ibPpjKIk8CtDwzXL .marker.cross{stroke:#333333;}#mermaid-svg-ibPpjKIk8CtDwzXL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ibPpjKIk8CtDwzXL p{margin:0;}#mermaid-svg-ibPpjKIk8CtDwzXL .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster-label text{fill:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster-label span{color:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster-label span p{background-color:transparent;}#mermaid-svg-ibPpjKIk8CtDwzXL .label text,#mermaid-svg-ibPpjKIk8CtDwzXL span{fill:#333;color:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL .node rect,#mermaid-svg-ibPpjKIk8CtDwzXL .node circle,#mermaid-svg-ibPpjKIk8CtDwzXL .node ellipse,#mermaid-svg-ibPpjKIk8CtDwzXL .node polygon,#mermaid-svg-ibPpjKIk8CtDwzXL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ibPpjKIk8CtDwzXL .rough-node .label text,#mermaid-svg-ibPpjKIk8CtDwzXL .node .label text,#mermaid-svg-ibPpjKIk8CtDwzXL .image-shape .label,#mermaid-svg-ibPpjKIk8CtDwzXL .icon-shape .label{text-anchor:middle;}#mermaid-svg-ibPpjKIk8CtDwzXL .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ibPpjKIk8CtDwzXL .rough-node .label,#mermaid-svg-ibPpjKIk8CtDwzXL .node .label,#mermaid-svg-ibPpjKIk8CtDwzXL .image-shape .label,#mermaid-svg-ibPpjKIk8CtDwzXL .icon-shape .label{text-align:center;}#mermaid-svg-ibPpjKIk8CtDwzXL .node.clickable{cursor:pointer;}#mermaid-svg-ibPpjKIk8CtDwzXL .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ibPpjKIk8CtDwzXL .arrowheadPath{fill:#333333;}#mermaid-svg-ibPpjKIk8CtDwzXL .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ibPpjKIk8CtDwzXL .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ibPpjKIk8CtDwzXL .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ibPpjKIk8CtDwzXL .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ibPpjKIk8CtDwzXL .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ibPpjKIk8CtDwzXL .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster text{fill:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL .cluster span{color:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL 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-ibPpjKIk8CtDwzXL .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ibPpjKIk8CtDwzXL rect.text{fill:none;stroke-width:0;}#mermaid-svg-ibPpjKIk8CtDwzXL .icon-shape,#mermaid-svg-ibPpjKIk8CtDwzXL .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ibPpjKIk8CtDwzXL .icon-shape p,#mermaid-svg-ibPpjKIk8CtDwzXL .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ibPpjKIk8CtDwzXL .icon-shape .label rect,#mermaid-svg-ibPpjKIk8CtDwzXL .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ibPpjKIk8CtDwzXL .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ibPpjKIk8CtDwzXL .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ibPpjKIk8CtDwzXL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} menuconfig界面
导航
选择
保存退出
上下键: 移动
左右键: 切换菜单
Enter: 进入/选择
y: 启用
n: 禁用
?: 帮助
Esc: 返回
Save: 保存
Exit: 退出

5.2 guiconfig图形界面

bash 复制代码
# 启动guiconfig(需要Qt支持)
west build -b <board> <app> -- guiconfig

# 或
west build -t guiconfig

5.3 查看当前配置

bash 复制代码
# 查看当前配置
west build -b <board> <app> -- -Dprintk-conf

# 查看已启用的配置项
cat build/zephyr/include/generated/autoconf.h

# 搜索特定配置项
grep "CONFIG_DEBUG" build/zephyr/include/generated/autoconf.h

# 查看配置依赖
west config manifest

5.4 配置验证工具

bash 复制代码
# 验证配置文件语法
python -m kconfiglib prj.conf

# 查看配置依赖关系
west build -b <board> <app> -- -Dconfig-dump

六、高级配置技巧

6.1 配置覆盖机制

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

overlay.conf示例:

kconfig 复制代码
# overlay.conf - 开发环境覆盖配置

# 启用调试
CONFIG_DEBUG=y
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_ASSERT=y

# 启用日志
CONFIG_LOG=y
CONFIG_LOG_LEVEL_DBG=y

# 启用Shell
CONFIG_SHELL=y

# 增加栈大小
CONFIG_MAIN_STACK_SIZE=4096

6.2 条件配置

kconfig 复制代码
# prj.conf - 条件配置示例

# 基础配置
CONFIG_KERNEL=y
CONFIG_CONSOLE=y

# 根据目标板配置
CONFIG_BOARD_NRF52840DK_NRF52840=y

# 如果是nRF52系列,启用蓝牙
if CONFIG_SOC_SERIES_NRF52
    CONFIG_BT=y
    CONFIG_BT_DEVICE_NAME="MyDevice"
endif

# 如果是STM32系列,启用特定驱动
if CONFIG_SOC_SERIES_STM32F4
    CONFIG_I2C_STM32=y
    CONFIG_SPI_STM32=y
endif

# 如果启用网络,配置网络参数
if CONFIG_NETWORKING
    CONFIG_NET_IPV4=y
    CONFIG_NET_TCP=y
    CONFIG_NET_UDP=y
    
    # 如果启用蓝牙,配置BLE
    if CONFIG_BT
        CONFIG_BT_PERIPHERAL=y
    endif
endif

6.3 配置模板

kconfig 复制代码
# prj.template.conf - 配置模板

# ===== 基础配置 =====
CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

# ===== 控制台 =====
CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

# ===== 调试选项 =====
# CONFIG_DEBUG=y
# CONFIG_ASSERT=y
# CONFIG_LOG=y

# ===== 驱动选项 =====
# CONFIG_GPIO=y
# CONFIG_I2C=y
# CONFIG_SPI=y
# CONFIG_UART=y

# ===== 网络选项 =====
# CONFIG_NETWORKING=y
# CONFIG_NET_IPV4=y
# CONFIG_NET_IPV6=y

# ===== 蓝牙选项 =====
# CONFIG_BT=y
# CONFIG_BT_DEVICE_NAME="MyDevice"

6.4 多环境配置

复制代码
my_project/
├── prj.conf                 # 基础配置
├── prj_debug.conf           # 调试配置
├── prj_release.conf         # 发布配置
├── prj_test.conf            # 测试配置
└── CMakeLists.txt

CMakeLists.txt配置:

cmake 复制代码
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(my_project)

# 根据构建类型选择配置
if(CONFIG_DEBUG)
    set(CONFIG_FILE "prj_debug.conf")
else()
    set(CONFIG_FILE "prj_release.conf")
endif()

target_sources(app PRIVATE src/main.c)

七、常用配置项参考

7.1 内核配置

配置项 说明 默认值
CONFIG_KERNEL 启用内核 y
CONFIG_PRIVILEGED_MODE 特权模式 y
CONFIG_INIT_STACKS 初始化栈 y
CONFIG_MAIN_STACK_SIZE 主线程栈大小 1024
CONFIG_NUM_COOP_PRIORITIES 协作线程优先级数 16
CONFIG_NUM_PREEMPT_PRIORITIES 抢占线程优先级数 32
CONFIG_SYS_CLOCK_TICKS_PER_SEC 系统时钟频率 100

7.2 调试配置

配置项 说明 默认值
CONFIG_DEBUG 启用调试 n
CONFIG_DEBUG_OPTIMIZATIONS 调试优化 n
CONFIG_DEBUG_THREAD_INFO 线程调试信息 n
CONFIG_ASSERT 启用断言 n
CONFIG_LOG 启用日志 n
CONFIG_LOG_LEVEL_DBG 调试日志级别 n

7.3 控制台配置

配置项 说明 默认值
CONFIG_CONSOLE 启用控制台 y
CONFIG_SERIAL 启用串口 y
CONFIG_UART_CONSOLE UART控制台 y
CONFIG_CONSOLE_INPUT 控制台输入 n
CONFIG_CONSOLE_HANDLER 控制台处理器 y
CONFIG_SHELL 启用Shell n

7.4 驱动配置

配置项 说明 默认值
CONFIG_GPIO GPIO驱动 n
CONFIG_I2C I2C驱动 n
CONFIG_SPI SPI驱动 n
CONFIG_UART UART驱动 n
CONFIG_I2S I2S驱动 n
CONFIG_PWM PWM驱动 n
CONFIG_ADC ADC驱动 n
CONFIG_DAC DAC驱动 n

7.5 网络配置

配置项 说明 默认值
CONFIG_NETWORKING 启用网络 n
CONFIG_NET_IPV4 IPv4支持 n
CONFIG_NET_IPV6 IPv6支持 n
CONFIG_NET_TCP TCP支持 n
CONFIG_NET_UDP UDP支持 n
CONFIG_NET_SOCKETS Socket支持 n

7.6 蓝牙配置

配置项 说明 默认值
CONFIG_BT 启用蓝牙 n
CONFIG_BT_DEVICE_NAME 设备名称 "Zephyr"
CONFIG_BT_PERIPHERAL 外设角色 n
CONFIG_BT_CENTRAL 中心角色 n
CONFIG_BT_OBSERVER 观察者角色 n
CONFIG_BT_BROADCASTER 广播者角色 n

八、常见问题与解决方案

8.1 配置项未找到

问题undefined reference to CONFIG_XXX

解决

bash 复制代码
# 检查配置是否在Kconfig中定义
grep -r "config CONFIG_XXX" zephyr/Kconfig*

# 检查prj.conf是否添加了配置
cat prj.conf | grep CONFIG_XXX

# 检查是否有拼写错误
grep -i "xxx" prj.conf

8.2 配置依赖冲突

问题conflicting configurations

解决

bash 复制代码
# 查看配置依赖关系
west build -b <board> <app> -- -Dconfig-dump

# 检查冲突的配置项
cat build/zephyr/include/generated/autoconf.h | grep -E "CONFIG_A|CONFIG_B"

# 修改prj.conf解决冲突
# 删除冲突的配置项或调整依赖

8.3 配置不生效

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

解决

bash 复制代码
# 清理构建缓存
west build -t pristine

# 重新构建
west build -b <board> <app>

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

8.4 条件编译不生效

问题#ifdef CONFIG_XXX代码未被编译

解决

c 复制代码
// 确保包含正确的头文件
#include <zephyr/kernel.h>

// 检查配置是否正确定义
#ifdef CONFIG_XXX
    // 你的代码
#endif

// 或使用IS_ENABLED宏
if (IS_ENABLED(CONFIG_XXX)) {
    // 你的代码
}

8.5 栈溢出

问题:运行时栈溢出

解决

kconfig 复制代码
# 增加栈大小
CONFIG_MAIN_STACK_SIZE=4096

# 增加线程栈大小
CONFIG_THREAD_STACK_SIZE=2048

# 启用栈初始化和检查
CONFIG_INIT_STACKS=y
CONFIG_STACK_SENTINEL=y

九、最佳实践

9.1 配置管理策略

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

9.2 配置文件组织

复制代码
my_project/
├── prj.conf                 # 基础配置(必须)
├── prj.overlay              # 设备树覆盖(可选)
├── boards/                  # 板级配置(可选)
│   └── myboard.conf
├── configs/                 # 配置变体(可选)
│   ├── debug.conf
│   ├── release.conf
│   └── test.conf
└── CMakeLists.txt

9.3 配置优化建议

场景 建议配置
开发调试 CONFIG_DEBUG=y, CONFIG_LOG=y, CONFIG_SHELL=y
发布版本 CONFIG_DEBUG=n, CONFIG_ASSERT=n, CONFIG_OPTIMIZE_FOR_SIZE=y
内存受限 CONFIG_MAIN_STACK_SIZE=1024, CONFIG_HEAP_MEM_POOL_SIZE=2048
性能优化 CONFIG_DEBUG=n, CONFIG_LOG=n, CONFIG_OPTIMIZE_FOR_PERF=y

9.4 配置验证流程

bash 复制代码
# 1. 编写配置
cat > prj.conf << 'EOF'
CONFIG_KERNEL=y
CONFIG_CONSOLE=y
CONFIG_DEBUG=y
EOF

# 2. 验证配置
west build -b <board> <app> -- -Dconfig-dump

# 3. 编译构建
west build -b <board> <app>

# 4. 测试验证
west build -t run

# 5. 优化调整
# 根据测试结果调整配置

十、Kconfig配置示例

10.1 基础应用配置

kconfig 复制代码
# prj.conf - 基础应用

CONFIG_SOC_SERIES_NRF52=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
CONFIG_I2C=y
CONFIG_SPI=y

10.2 BLE外设应用配置

kconfig 复制代码
# prj.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_CONSOLE=y
CONFIG_UART_CONSOLE=y

CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="MyBLEDevice"
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_GATT_SERVICE=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_NRF_SDH_ENABLED=y

CONFIG_GPIO=y
CONFIG_TIMER=y

10.3 网络应用配置

kconfig 复制代码
# prj.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_UART_CONSOLE=y

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

CONFIG_DNS_RESOLVER=y
CONFIG_MQTT=y

CONFIG_WIFI=y
CONFIG_WIFI_NRF70=y

10.4 QEMU开发配置

kconfig 复制代码
# prj.conf - QEMU开发

CONFIG_SOC_POSIX=y
CONFIG_BOARD_QEMU_X86=y

CONFIG_KERNEL=y
CONFIG_INIT_STACKS=y

CONFIG_CONSOLE=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

CONFIG_DEBUG=y
CONFIG_DEBUG_OPTIMIZATIONS=y
CONFIG_LOG=y
CONFIG_LOG_LEVEL_DBG=y
CONFIG_SHELL=y

CONFIG_TIMER=y
CONFIG_GPIO=y

结束语

通过本文的详细介绍,相信您已经掌握了使用Kconfig配置Zephyr工程的核心知识:

知识点 内容
配置文件 prj.conf是主要配置文件
配置类型 布尔型(=y/n)、整型(=数字)、字符串型(="值")
语法规则 支持条件、依赖、选择组等
代码使用 #ifdef CONFIG_XXXIS_ENABLED()
配置工具 menuconfigguiconfig
最佳实践 分层配置、版本控制、文档化

掌握Kconfig配置系统可以帮助您:

  1. 精准控制功能:按需启用/禁用功能模块
  2. 优化资源占用:根据硬件资源调整配置
  3. 适配多种平台:通过条件配置支持不同硬件
  4. 提高代码可维护性:集中管理配置选项

建议在实际项目中从最小配置开始,逐步添加需要的功能,并使用版本控制管理配置文件。


参考资料

相关推荐
ScilogyHunter5 小时前
Zephyr设备树完全指南
zephyr
ScilogyHunter1 天前
Zephyr项目按需配置完全指南
zephyr
ScilogyHunter1 天前
Zephyr最简工程配置指南
zephyr
ScilogyHunter1 天前
Zephyr主仓库目录结构完全指南
zephyr
ScilogyHunter1 天前
Zephyr工程配置完全指南
zephyr
ScilogyHunter1 天前
Zephyr SDK按需配置完全指南
zephyr
ScilogyHunter1 天前
Zephyr编译生成的build目录完全解析
zephyr
ScilogyHunter1 天前
Zephyr开发中的Manifest文件完全解析
manifest·zephyr·west
ScilogyHunter2 天前
Zephyr概述
zephyr