RK3568驱动模块编译进内核

一、创建文件

  首先在drivers/char目录下创建hello文件夹,然后在hello文件夹下创建hello.c 文件、Kconfig和Makefile文件。
  hello.c 文件内容如下

#include <linux/module.h>
#include <linux/kernel.h> 
static int __init helloworld_init(void)        
{
    
    
	printk(KERN_EMERG "helloworld_init\r\n");
	return 0;
}
static void __exit helloworld_exit(void)    
{
    
    
	printk(KERN_EMERG "helloworld_exit\r\n");
}

module_init(helloworld_init);    
module_exit(helloworld_exit); 

  Kconfig文件内容如下

config HELLO
tristate "hello world" help
hello h

  Makefile文件内容如下

obj-$(CONFIG_HELLO)+=helloworld.o

二、修改已有的文件

  需要修改上一级目录的 Kconfig 文件和 Makefile 文件,也就是 driver/char 目录
  Makefile添加如下图所示内容。

obj-y += hello

在这里插入图片描述
  Kconfig 添加如下图所示内容

source "drivers/char/hello/Kconfig"

在这里插入图片描述

三、配置编译

  打开 menuconfig 图形化配置工具,在配置界面选择 helloworld 驱动。把驱动编译进Linux 内核,用 * 来表示,所以配置选项改为*。如果想要将驱动编译为模块,则用 M 来表示,配置选项改为 M

3.1、编译进Linux 内核

  在menuconfig 图形化配置工具里面选择*,把驱动编译进Linux 内核。然后保存配置将信息保存到.config 文件。然后输入以下命令编译:

make savedefconfig
cp defconfig arch/arm64/configs/rockchip_linux_defconfig
cd ../
./build.sh kerne

  编译成功之后,进入到 drivers/char/hello 目录下,可以看到会生成对应的.o 文件。就说明已经成功将驱动编译进内核。
  将编译好的内核镜像烧写到开发板上后,在开发板系统启动的时候可以成功看到加载helloworld 驱动。

3.2、编译成模块

  如果在图形化配置界面中选择的 M,也就是编译成驱动模块,则生成 helloworld.ko 文件。

猜你喜欢

转载自blog.csdn.net/xxxx123041/article/details/133908515