在編譯第一個驅(qū)動之前,需要把基本的環(huán)境準(zhǔn)備好,可以參照這兩篇文章:
https://wlink.blog.csdn.net/article/details/128590747
https://wlink.blog.csdn.net/article/details/128591216
我們之前寫過一個基于ubuntu最基本的字符設(shè)備驅(qū)動,參照文章:
https://wlink.blog.csdn.net/article/details/128505550
所以本節(jié)我們直接來用一下第一個最簡單的字符設(shè)備驅(qū)動來驗證一下我們的環(huán)境準(zhǔn)備的是否正常
我們直接上代碼:
hello_driver.c
#include <linux/module.h>
static int __init hello_driver_init(void)
{
printk("hello_driver_init\r\n");
return 0;
}
static void __exit hello_driver_cleanup(void)
{
printk("hello_driver_cleanup\r\n");
}
module_init(hello_driver_init);
module_exit(hello_driver_cleanup);
MODULE_LICENSE("GPL");
Makefile
KERNELDIR := /home/zhongjun/project/board/yuanzi/imx6ull/nfs/kernel
CURRENT_PATH := $(shell pwd)
obj-m := hello_driver.o
build: kernel_modules
kernel_modules:
$(MAKE) -C $(KERNELDIR) $(KBUILD_CFLAGS) M=$(CURRENT_PATH) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
其中KERNELDIR是我們編譯過Kernel的路徑
insmod hello_driver.ko
其中hello_driver_init就是我們printk打印的log
我們也可以通過dmesg來查看log
lsmod
rmmod