文章目录
前言
在Android系统开发中,经常需要编译自己的驱动到内核。
以helloworld为例,分为以下步骤:
- 添加驱动文件
- 关联上下级驱动编译文件
- 重新编译内核
一、添加驱动文件
- 在/kernel/drivers/char/目录下,新建helloworld目录
- 在helloworld目录下新建helloworld.c文件:
#include <linux/module.h>
#include <linux/kernel.h>
static int helloworld_init(void)
{
printk("helloworld_init\r\n");
return 0;
}
static void helloworld_exit(void)
{
printk("helloworld_exit\r\n");
}
module_init(helloworld_init);
module_exit(helloworld_exit);
- 在helloworld目录下新建Kconfig文件:
config helloworld
bool "hellworld support"
default y
help
helloworld Kconfig
- 在helloworld目录下新建Makefile文件
obj-$(CONFIG_helloworld) += helloworld.o
- 编译内核kernel,观察.config文件,会出现:
二、关联上下级驱动编译文件
- 在char目录下Kconfig文件里添加:
source "drivers/char/helloworld/Kconfig"
- 在char目录下Makefile文件里添加:
obj-$(CONFIG_helloworld) += helloworld.o
三、重新编译内核
make distclean
make ARCH=arm64 rockchip_defconfig -j8
make ARCH=arm64 rk3399-xxxx.img -j8