1 Makefile
Makefile文件编写如下:
ifeq ($(KERNELRELEASE),)
KERNEL_DIR=/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi
PWD=$(shell pwd)
modules:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
arm-linux-gnueabihf-gcc -o test test.c
modules_install:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules_install
clean:
rm -rf *.ko *.o .tmp_versions *.mod.c modules.order Module.symvers .*.cmd
else
obj-m:=test.o
endif
2 *.c文件
建立一个test.文件,编写内容如下:
#include "linux/init.h"
#include <linux/kernel.h>
#include <linux/module.h>
static int __init empty_init(void)
{
printk("---driver init---\r\n");
return 0;
}
static void __exit empty_exit(void)
{
printk("---dirver exit---\r\n");
}
module_init(empty_init);
module_exit(empty_exit);
MODULE_DESCRIPTION("Driver for empty");
MODULE_AUTHOR("wit_yuan");
MODULE_LICENSE("GPL");
3 获取ko文件
3.1 编译程序
wityuan@ubuntu:~/Desktop/A20_driver$ make
make -C /home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi M=/home/wityuan/Desktop/A20_driver modules
make[1]: Entering directory `/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi'
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.
WARNING: Symbol version dump /home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi/Module.symvers
is missing; modules will have no dependencies and modversions.
Building modules, stage 2.
/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi/scripts/Makefile.modpost:42: include/config/auto.conf: No such file or directory
make[2]: *** No rule to make target `include/config/auto.conf'. Stop.
make[1]: *** [modules] Error 2
make[1]: Leaving directory `/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi'
make: *** [modules] Error 2
这个问题主要原因在编译驱动的时候,认为内核没有编译。所以是编译环境导致。
在marsboard sdk路径下,进入linux-sunxi目录下,做这样的操作:
$ cd linux-sunxi
$ cp marsboard_kernel_defconfig arch/arm/configs/
$ vi Makefile
...
#ARCH ?= $(SUBARCH)
ARCH = arm
#CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)
CROSS_COMPILE = arm-linux-gnueabihf-
$ make marsboard_kernel_defconfig
$ make uImage
之后,在编写的驱动路径下,执行
$ make
...
make -C /home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi M=/home/wityuan/Desktop/A20_driver modules
make[1]: Entering directory `/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi'
CC [M] /home/wityuan/Desktop/A20_driver/test.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/wityuan/Desktop/A20_driver/test.mod.o
LD [M] /home/wityuan/Desktop/A20_driver/test.ko
make[1]: Leaving directory `/home/wityuan/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi'
arm-linux-gnueabihf-gcc -o test test.c
test.c:2:24: fatal error: linux/init.h: No such file or directory
compilation terminated.
make: *** [modules] Error 1
从上图看,已经报错了,但是,其实还是能生成.ko文件的,如下图所示:
wityuan@ubuntu:~/Desktop/A20_driver$ ls
Makefile modules.order Module.symvers test.c test.ko test.mod.c test.mod.o test.o
wityuan@ubuntu:~/Desktop/A20_driver$
我们在后面将驱动做的差不多的时候,再回过头来解决这个问题。
3.2 安装驱动程序
将上面生成的test.ko通过tftp下载到开发板上,然后再开发板上进行安装,如下所示:
root@marsboard:~#
root@marsboard:~# insmod test.ko
---driver init---
root@marsboard:~# rmmod test
---dirver exit---
root@marsboard:~#
到这个步骤为止,就可以判定我们的驱动程序编写成功了。
4 问题解决
4.1 linux/init.h不能找到
该篇留下了一个问题,也就是不能找到linux/init.h文件,其实搜寻文件,还是能找到该文件的:
wityuan@ubuntu:~/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi$ find -name "init.h"
./include/linux/init.h
./include/config/mmc/paranoid/sd/init.h
./include/config/debug/memory/init.h
./arch/x86/include/asm/init.h
./arch/avr32/mach-at32ap/include/mach/init.h
./arch/mips/powertv/init.h
./arch/um/include/shared/init.h
./drivers/video/sis/init.h
./drivers/staging/keucr/init.h
./drivers/net/wireless/wl12xx/init.h
./drivers/net/wireless/wl1251/init.h
wityuan@ubuntu:~/Downloads/MarsBoard-A20-Linux-SDK-V1.2/linux-sunxi$
d第一个文件便是,解决方法,待定。。。