编写测试Hello测试驱动
#include <linux/init.h>
#include <linux/module.h>
static int __init hello_init(void){
printk(KERN_ALERT"Hello,world!\n");
return 0;
}
module_init(hello_init);
static void __exit hello_exit(void){
printk(KERN_ALERT"Goodbye,cruel world\n");
}
module_exit(hello_exit);
MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("A simple Hello world Module");
MODULE_ALIAS("a simplest module");
编写Makefile
rpi3b_path=~/rpi3b
toolchain_path=$(rpi3b_path)/toolchain
toolchain_source=$(toolchain_path)/toolchain-rpi32b
cross_compile=$(toolchain_source)/bin/arm-linux-gnueabihf-
kernel_verber=linux-rpi-4.19.y
kernel_path=$(rpi3b_path)/kernel
kernel_source=$(kernel_path)/$(kernel_verber)
obj-m := hello.o
PWD := $(shell pwd)
KDIR := $(kernel_source)
all :
make -C $(KDIR) M=$(PWD) modules ARCH=arm COMPILE=$(cross_compile)
clean:
rm -rf *.ko *.o *.mod.o *.mod.c *.symvers modul*
编译生成驱动 hello.ko
测试1:动态安装
hello.ko
驱动(sudo insmod hello.ko
),然后使用dmesg
命令打印日志查看
测试2:动态卸载hello.ko
驱动(sudo rmmod hello.ko
),然后使用dmesg
命令打印日志查看
pi@raspberrypi:~ $ sudo insmod hello.ko
pi@raspberrypi:~ $
pi@raspberrypi:~ $ dmesg
......
[104837.097547] rpi_firmware_get_throttled: 12 callbacks suppressed
[104837.097555] Under-voltage detected! (0x00050005)
[104847.497515] rpi_firmware_get_throttled: 12 callbacks suppressed
[104847.497522] Voltage normalised (0x00000000)
[104849.577556] Under-voltage detected! (0x00050005)
[104862.057525] Voltage normalised (0x00000000)
[104864.137577] Under-voltage detected! (0x00050005)
[104874.537545] Voltage normalised (0x00000000)
[104936.084765] Hello,world!
pi@raspberrypi:~ $ sudo rmmod hello.ko
pi@raspberrypi:~ $
pi@raspberrypi:~ $ dmesg
......
[104837.097547] rpi_firmware_get_throttled: 12 callbacks suppressed
[104837.097555] Under-voltage detected! (0x00050005)
[104847.497515] rpi_firmware_get_throttled: 12 callbacks suppressed
[104847.497522] Voltage normalised (0x00000000)
[104849.577556] Under-voltage detected! (0x00050005)
[104862.057525] Voltage normalised (0x00000000)
[104864.137577] Under-voltage detected! (0x00050005)
[104874.537545] Voltage normalised (0x00000000)
[104936.084765] Hello,world!
[105002.237668] Goodbye,cruel world
pi@raspberrypi:~ $