内核开发

Copyright © Quectel Wireless Solutions Co., Ltd. 2026. All rights reserved.


Debian系统中集成开发内核模块需要的内核头文件,同时提供编译内核模块需要的工具链,用户可以在设备中直接开发和构建新的模块。

环境准备

Quectel Pi M2 内核版本为 6.1.118-rt36,系统提供对应的 Linux Kernel Headers,路径如下:

/usr/src/linux-headers-6.1-rockchip

安装内核模块编译所需工具:

apt update 
apt install -y make gcc

创建 Hello World 内核模块工程目录:

mkdir -p /home/pi/helloworld
cd /home/pi/helloworld

编写源码

helloworld.c文件

helloworld.c文件内容:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Quectel");
MODULE_DESCRIPTION("A simple Hello World kernel module");
MODULE_VERSION("0.1");

static int __init helloworld_init(void)
{
    printk(KERN_INFO "Hello World!\n");
    return 0;
}

static void __exit helloworld_exit(void)
{
    printk(KERN_INFO "Goodbye!\n");
}

module_init(helloworld_init);
module_exit(helloworld_exit);

Makefile 文件

Makefile 文件内容:

obj-m := helloworld.o

KERNELDIR ?= /usr/src/linux-headers-6.1-rockchip
PWD := $(shell pwd)

all:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

编译构建

进入工程目录:

cd /home/pi/helloworld

执行编译:

make

编译完成后,目录下生成内核模块文件:

helloworld.ko

运行测试

加载模块:insmod helloworld.ko

检查模块:lsmod | grep helloworld

检查运行日志:dmesg | grep “Hello World”

卸载模块:rmmod helloworld

检查卸载日志:dmesg | grep “Goodbye”