Linux内核驱动Kconfig的编写方法

前言

内核配置的目的是裁剪C代码,C代码编译一般由Makefile管理
一个菜单有三个相关内容:
1、 菜单;-----------Kconfig文件
2、 Makefile;------根据 .config抽取文件
3、 C代码。--------通过Makefile文件进行关联

源码目录下一般都有两个文件Kconfig和Makefile,所以我们添加菜单的时候就需要去修改这两个文件,我们可以通过make menuconfig来生成图形化界面去操作他

Kconfig语法

他有点像makefile的编写,直接来看源码吧

#menuconfig,config
#if/endif
#bool, tristate, hex, int, string
#prompt, comment,depends on, choice/endchoice
#select
#memu/endmenu
#range numberleft numberright
menuconfig UPLOOK_LESSON //定义主菜单
		bool "linxu kerniel driver lesson about Kconfig" //主菜单为bool类型
		---help--- //帮助
		it is a simple examle for Kconfig
	
if UPLOOK_LESSON //进入子菜单

config TINY4412_LED //添加选项
		bool "about led driver"
		---help---
		there is 4 led on tiny4412

endif

他的语法类似于

menuconfig 加入到总的Kconfig的名字
		类型  "选项名字名字"

同时我们还需要去修改他的Makefile,将这个文件的路径加进去
在这里插入图片描述

下来我们执行make menuconfig,将这两个选项打开,做完这些后我们可以看见,在总目录下的.Kconfig中,都变成了y
在这里插入图片描述

Kconfig的关键字

在这里插入图片描述

我们在上面定于这些关键字都是声明的下来就来说明一下这些关键字

  • bool 二选一,是否使用,使用为
  • tristate:三态选项(n、m、y)
  • hex:用户需要输入一个16进制数
  • int:用户需要输入一个整形(只能0~9数字组合其他非法
  • string: 用户需要输入一个字符串

下来是其他的选项关键字

  • comment 添加注释
  • select 选中当前的就必须选select的那一个
  • depend on 只有他依赖的为真,才可以去选择他
  • choice/endchoice 在两个关键字的范围内只能选一个
  • menu/endmenu 在两个菜单间添加一个菜单
  • range 添加范围
  • prompt 打印提示

具体的测试代码在下方

#menuconfig,config
#if/endif
#bool, tristate, hex, int, string
#prompt, comment,depends on, choice/endchoice
#select
#memu/endmenu
#range numberleft numberright
menuconfig UPLOOK_LESSON
		bool "linxu kerniel driver lesson about Kconfig"
		---help---
		it is a simple examle for Kconfig
	
if UPLOOK_LESSON
comment "welcome to my driver"

config TINY4412_LED
		bool "about led driver"
		---help---
		there is 4 led on tiny4412
config TINY4412_LED_4
		int "about 4 led driver"
		range 1 4
		---help---
		nothing
menu "all of TS && io"

config TINY4412_ts
		tristate "about TS driver"
		---help---
		there is 4 led on tiny4412
config TINY4412_io
		string "about io driver"
		---help---
		there is 4 led on tiny4412
choice
		prompt "please choice ont diver"
config TINY4412_input
		bool "input io driver"
		---help---
		there is 4 led on tiny4412
config TINY4412_out
		bool "output io driver"
		---help---
		there is 4 led on tiny4412
endchoice
endmenu
endif

显示的界面为在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值