文章目录
文章更新版本 | 时间 | 更新说明 | 修订人 |
---|---|---|---|
V0.1 | 发布于2020-05-01 | 初始版本 | libo |
V0.2 | 修改于2020-05-08 | 添加0/9/10/11/12 | libo |
V0.3 | 修改于2020-09-09 | 修改8 | libo |
0. kconfig 格式
- kconfig 使用缩进进行语法分析
- ESP-IDF 建议将 TAB 修改为 4 个连续空格
- 每一级之间需要保持 4 个空格的缩进
- 每一行结束,不能加空格
- 慎用反斜杠
ESP-IDF 附带了
kconfig
文件格式检查工具check_kconfigs.py
,详见:Format checker
1. menu 嵌套
- 设置 menu 可见性
- 设置嵌套目录,支持多级嵌套
menu "ESP32S2-specific"
visible if IDF_TARGET_ESP32S2 # 设置可见性
menu "Cache config" #嵌套目录
#
#
#
endmenu
endmenu
2. 条件式默认值
- 使用
default
可以设置配置项的默认值 - 使用
default () if ()
设置不同条件下的默认值 if
后可以添加操作符如if !IDF_ENV_FPGA
config ESP32S2_DEFAULT_CPU_FREQ_MHZ
int
default 40 if IDF_ENV_FPGA
default 80 if ESP32S2_DEFAULT_CPU_FREQ_80
default 160 if ESP32S2_DEFAULT_CPU_FREQ_160
default 240 if ESP32S2_DEFAULT_CPU_FREQ_240
使用条件默认之后,一般不再要求用户自行输入,因此不在类型后面加提示符,也不使用prompt
添加提示,但是并不是不可以,如以下例子:
config ESP32S2_ULP_COPROC_RESERVE_MEM
int
prompt "RTC slow memory reserved for coprocessor" if ESP32S2_ULP_COPROC_ENABLED
default 512 if ESP32S2_ULP_COPROC_ENABLED
range 32 8192 if ESP32S2_ULP_COPROC_ENABLED
default 0 if !ESP32S2_ULP_COPROC_ENABLED
range 0 0 if !ESP32S2_ULP_COPROC_ENABLED
help
Bytes of memory to reserve for ULP coprocessor firmware & data.
Data is reserved at the beginning of RTC slow memory.
3. 条件式范围
- 使用
range () () if ()
设置条件式范围
config ESP32S2_ULP_COPROC_RESERVE_MEM
int
prompt "RTC slow memory reserved for coprocessor" if ESP32S2_ULP_COPROC_ENABLED
default 512 if ESP32S2_ULP_COPROC_ENABLED
range 32 8192 if ESP32S2_ULP_COPROC_ENABLED
default 0 if !ESP32S2_ULP_COPROC_ENABLED
range 0 0 if !ESP32S2_ULP_COPROC_ENABLED
help
Bytes of memory to reserve for ULP coprocessor firmware & data.
Data is reserved at the beginning of RTC slow memory.
4. 条件式提示
- 可以直接在类型后面添加
"string"
,添加配置项的界面提示 - 不添加提示,默认不再界面进行显示,不与用户直接交互
- 也可通过
prompt
添加提示,相比直接在类型后面添加"string"
,该方式更加灵活。可以通过prompt "string" if ()
实现条件提示
例:只在 ESP32S2_ULP_COPROC_ENABLED
使能时添加界面提示,否则使用默认值
config ESP32S2_ULP_COPROC_RESERVE_MEM
int
prompt "RTC slow memory reserved for coprocessor" if ESP32S2_ULP_COPROC_ENABLED
default 512 if ESP32S2_ULP_COPROC_ENABLED
range 32 8192 if ESP32S2_ULP_COPROC_ENABLED
default 0 if !ESP32S2_ULP_COPROC_ENABLED
range 0 0 if !ESP32S2_ULP_COPROC_ENABLED
help
Bytes of memory to reserve for ULP coprocessor firmware & data.
Data is reserved at the beginning of RTC slow memory.
5. 多选一选项
- 使用
choice ...endchoice
可以配置多选一选项 - 使用
default
设置选择默认值 - 使用
prompt
设置选择提示
选项的变量类型只能是 bool
和 tristate
,为了实现其他类型的选择,可以使用以下方式:
choice ESP32S2_DEFAULT_CPU_FREQ_MHZ
prompt "CPU frequency"
default ESP32S2_DEFAULT_CPU_FREQ_160 if !IDF_ENV_FPGA
default ESP32S2_DEFAULT_CPU_FREQ_FPGA if IDF_ENV_FPGA
help
CPU frequency to be set on application startup.
config ESP32S2_DEFAULT_CPU_FREQ_FPGA
depends on IDF_ENV_FPGA
bool "FPGA"
config ESP32S2_DEFAULT_CPU_FREQ_80
bool "80 MHz"
config ESP32S2_DEFAULT_CPU_FREQ_160
bool "160 MHz"
config ESP32S2_DEFAULT_CPU_FREQ_240
bool "240 MHz"
endchoice
config ESP32S2_DEFAULT_CPU_FREQ_MHZ
int
default 40 if IDF_ENV_FPGA
default 80 if ESP32S2_DEFAULT_CPU_FREQ_80
default 160 if ESP32S2_DEFAULT_CPU_FREQ_160
default 240 if ESP32S2_DEFAULT_CPU_FREQ_240
6. 选项依赖关系
A depends on B
只有 B 选中,才能对 A 进行操作A select B
只要 A 选中,就会同时选中 Bdepends on
为依赖关系,select
为反向依赖- 也可以
depends on !B
,只有B不选中,才能对 A 进行操作
ESP32S2_TRAX
反向依赖ESP32S2_MEMMAP_TRACEMEM
,当ESP32S2_TRAX
使能时,ESP32S2_MEMMAP_TRACEMEM
也同时被使能。
config ESP32S2_MEMMAP_TRACEMEM
bool
default "n"
config ESP32S2_TRAX
bool "Use TRAX tracing feature"
default "n"
select ESP32S2_MEMMAP_TRACEMEM
help
The ESP32S2 contains a feature which allows you to trace the execution path the processor
has taken through the program. This is stored in a chunk of 32K (16K for single-processor)
of memory that can't be used for general purposes anymore. Disable this if you do not know
what this is.
7. 为菜单添加注释
menu "motors config"
comment "config gpio for motors"
...
endmenu
8. 引用其他 Kconfig
- 相当于包含文档,将其在添加位置处展开
source "path/to/kconfig" #绝对路径
rsource "path/to/kconfig" #相对路径
9. kconfig 配置项类型
tristate
和string
是配置项的基本类型,其他类型是对其进行的包装,包括:
- bool:n;y (0;1)
- tristate:n;m;y (0;1;2)(不编译;编译成模块;编译到内核)
- string:例如 “esp32”
- hex:例如 0x1000
- int:例如 115200
10. 使用 sdkconfig.defaults 设置默认值
sdkconfig.defaults
格式与sdkconfig
完全一致,最简单的生成默认文件的方法是,将sdkconfig
重命名为sdkconfig.defaults
,然后将不需要默认值的选项删除。
sdkconfig.defaults
的优先级大于kconfig
中的选项default
,只有在sdkconfig.defaults
中未定义的选项,才会使用kconfig
中的选项default
。详见:Using sdkconfig.defaults
11. 使用 sdkconfig.defaults.TARGET 设置默认值
添加完sdkconfig.defaults
中的默认设置以后,编译系统会从sdkconfig.defaults.TARGET_NAME
检索与硬件相关的默认配置,其中TARGET_NAME
目前可以是esp32
esp32s2
。
详见:Target-dependent sdkconfig defaults
12. 使用 confgen.py 实现版本过渡
在 ESP-IDF 的迭代更新中,部分配置选项被丢弃或者被重命名,IDF 提供了自动检查工具confgen.py
,可以对老版本的 kconfig
实现自动检查和替换。详见:Backward Compatibility of Kconfig Options