04_Linux下把驱动编译进内核

Linux下把驱动编译进内核

需求:

把前面课程学习的helloworld驱动编译到内核。

例子:

source"drivers/redled/Kconfig"
config LED_4412
	tristate "Led Support for GPIO Led"
	depends on LEDS CLASS
	help
	This option enable support for led

说明:

1.source “drivers/redled/Kconfig”

他会包含drivers/redled/这个路径下的驱动文件,方便我们对菜单进行管理

2.config LED__4412

配置选项的名称,CONFIG_LED_4412

3.tristate 表示的驱动的状态,把驱动编译成模块,把驱动编译到内核,不编译

与之对应的还有bool 分别是编译到内核,不编译

“Led Support for GPIO Led” make menuconfig显示的名字

A depends on B

表示只有在选择B的时候才可以选择A

4.比如我想直接去掉LED相关的去掉,我们直接改.config文件可以吗?

可以,但是不推荐。如果有依赖的话,直接修改.config是不成功的。

5.select

​ 反向依赖,该选项被选中时,后面的定义也会被选中。

help

This option enable support for led

帮助信息

实例:

sudo apt-get install lzop
sudo apt-get install u-boot-tools

注意这两个依赖,没有会导致编译报错,打开板子不打印helloworld

进入内核源码目录

image-20240418135508010

进入drivers/char/中, "cd drivers/char/"

image-20240418135615431

创建hello文件夹,并进入

image-20240418135701497

拷贝helloworld.c到hello文件夹,创建Kconfig文件

image-20240418135734254

编写Kconfig

config HELLO
    tristate "hello world"
    help
    hello hello

创建Makefile

image-20240418140209623

编写Makefile

# $(CONFIG_HELLO):根据我们选择的驱动的状态去改变,选择编译到内核就是y(obj-y),选择编译到模块就是m(obj-m)
# CONFIG_HELLO 名称是由Kconfig中的HELLO,前面+CONFIG_得到
obj-$(CONFIG_HELLO)+=helloworld.o

返回上一级目录

image-20240418140559798

修改Makefile

image-20240418150930758

修改Kconfig

image-20240418142407285

返回源码根目录

"export ARCH=arm"

输入"make menuconfig"

image-20240418142602621

image-20240418142704171

image-20240418142731400

按空格变成* --> 编译进内核

image-20240418142827246

保存退出

进入.config,查看hello

image-20240418143043212

查看create.sh

image-20240418143331816

编译时使用的是arch/arm/configs/imx_v7_defconfig这个文件来编译的,不是使用我们修改make menuconfig生成的.config文件

使用"make distclean"清除所有的编译文件

image-20240418143702303

使用"cp arch/arm/configs/imx_v7_defconfig .config"将imx_v7_defconfig 复制成.config

意思是菜单上都是特色菜,但需要加一道菜

image-20240418143839400

使用"make menuconfig"再将hello world编译进内核

image-20240418144013588

保存退出

进入arch/arm/configs/ 将imx_v7_defconfig改成imx_v7_defconfig_nohello

"cd arch/arm/configs/"

"mv imx_v7_defconfig imx_v7_defconfig_nohello"

将我们内核根目录使用make menuconfig生成的.config 复制到arch/arm/configs/下,并改成imx_v7_defconfig

"cp /home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga/.config imx_v7_defconfig"

image-20240418144615056

返回内核源码根目录, 运行脚本 "./create.sh"

image-20240418150744832

查看drivers/char/hello/下有helloworld.o文件,说明编译成功

image-20240418151337975

将arch/arm/boot下的zImage文件烧录到开发板上

image-20240418151450748

posted @ 2024-04-25 22:33  爱吃冰激凌的黄某某  阅读(64)  评论(0编辑  收藏  举报