上面是我的微信和QQ群,欢迎新朋友的加入。
进入linux源码目录
增加驱动
hello.c
#include <linux/module.h> //所有模块都需要的头文件
#include <linux/init.h> // init&exit相关宏
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("hello world module");
static int __init hello_init(void)
{
printk(KERN_WARNING "hello world.\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_WARNING "hello exit!\n");
}
module_init(hello_init);
module_exit(hello_exit);
makefile
obj-m +=hello.o
#generate the path
CURRENT_PATH:=$(shell pwd)
#the absolute path
LINUX_KERNEL_PATH:=/home/jun/qemu/linux-5.12.9
#complie object
all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
Kconfig
menu "HELLO TEST Driver "
comment "HELLO TEST Driver Config"
config HELLO
tristate "hello module test"
default m
help
This is the hello test driver
endmenu
修改上层文件
修改kconfig
source "drivers/hello/Kconfig"
修改makefile
obj-$(CONFIG_HELLO) += hello/
编译
//这里直接使用make是因为我最开始改了顶层makefile
make menuconfig
进去选择好驱动
make
make modules
make dtbs
ls arch/arm/boot/dts/vexpress*
ls arch/arm/boot/
开始编译
测试
把生成的驱动文件拷贝到文件系统里面