一、准备工作
1、《linux设备驱动程序》的hello world模块示例(Page22)
2、linux内核源码目录:BSP/A7/kernel/A7-linux-src-4.14/
3、交叉编译器目录:/usr/local/arm/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin
4、驱动源码目录:BSP/fdriver/first_drv
二、驱动源码
first_drv.c
#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello, world! "); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodby, cruel world "); } module_init(hello_init); module_exit(hello_exit);
三、Makefile
ARCH:=arm PLATFORM?=A7 CROSS_COMPILE:=arm-linux-gnueabihf- export ARCH CROSS_COMPILE PWD=$(shell pwd) KERNEL_SRC=$(PWD)/../../A7/kernel/A7-linux-src-4.14 obj-m:=first_drv.o all: $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules clean: rm -rf *.ko rm -rf *.o
四、调试过程
1、内核没有配置和编译过,在编译驱动时报错:
2、配置命令:
make ARCH=arm m6g2c_defconfig
如果缺少ARCH=arm, 会报错:
3、先编译内核,否则编译驱动还是会提示错误,不清楚为什么?
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
错误如下:
ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/config/auto.conf are missing. Run 'make oldconfig && make prepare' on kernel src to fix it.
4、通过wget把驱动程序first.drv拷贝到demo板上
wget http://192.168.22.70:8080/linux2/BSP/fdriver/first_drv/first_drv.ko
5、加载和卸载驱动程序进行测试,结果如下:
1 [root@xxx ~]# insmod first_drv.ko 2 Hello, world! 3 [root@xxx ~]# rmmod first_drv 4 Goodby, cruel world 5 [root@xxx ~]#