驱动程序分为在ubuntu上运行和在ARM开发板上运行两种,我们分别来进行测试
1.源码
empty#include <linux init.h=''>
#include
static int hello_init(void)
{
printk(KERN_EMERG 'Enter Hello abc World!n');
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG 'Exit hello world!n');
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR('RFIDUNION');
MODULE_LICENSE('GPL v2');
MODULE_DESCRIPTION('A simple driver');
MODULE_ALIAS('a simple test module');
empty
2.电脑上的Makefile
ifneq ($(KERNELRELEASE),)
module-objs := helloworld.o
obj-m := helloworld.o
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
(MAKE)−C(����)−�(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -rf *.o *~core.depend.*.cmd *.ko *.mod.c.tmp versions
KERNELRELEASE 内核顶层目录Makefile的一个变量。
KERNELDIR ?= /lib/modules/(shelluname−r)/build内核源码树目录。该Makefile共读取两次,在输入Makefile时,(�ℎ��������−�)/�����内核源码树目录。该��������共读取两次,在输入��������时,(KERNELDIR) 第一次读取KERNELRELEASE并没有被定义,然后就开始读取内核源码的目录,开始定义KERNELRELEASE,然后到当前模块的目录里面,M=$(PWD) 进入该Makefile时KERNELRELEAS已经被定义了,读取要编译的模块,然后再返回到modules
编译完成,产生.KO文件
3.ARM开发板上的Makefile
ifneq ($(KERNELRELEASE),)
obj-m := helloworld.o
else
KDIR := /qemu_arm/linux-kernel/linux-3.16.39/
all:
make -C (KDIR)M=(����)�=(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif
KDIR := /qemu_arm/linux-kernel/linux-3.16.39/
KDIR 指定开发板内核所在目录。
CROSS_COMPILE=arm-none-linux-gnueabi- 指定编译器
4.Makefile注意事项
Makefile的拼写不能出错,不是makefile,Makefile用要用Tab键
常见的错误:
提示:missing separator 在这一行要用Tab键开头
5.在电脑上测试驱动
将源码和Makefile两个文件放在同一个目录中,然后用make命令编译(注意此处的Makefile是用第2步中的Makefile)
book@book-virtual-machine:/qemu_arm/driver$ make
make -C /lib/modules/4.8.0-34-generic/build M=/qemu_arm/driver modules
make[1]: Entering directory '/usr/src/linux-headers-4.8.0-34-generic'
CC [M] /qemu_arm/driver/helloworld.o
Building modules, stage 2.
MODPOST 1 modules
CC /qemu_arm/driver/helloworld.mod.o
LD [M] /qemu_arm/driver/helloworld.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.8.0-34-generic'
book@book-virtual-machine:/qemu_arm/driver$
编译完成后用file命令查看下是否是在PC机上运行的模块,不要和ARM开发板上运行的搞混了。
book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko
helloworld.ko: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), BuildID[sha1]=53c0e5e95e8d1c4683f92a7da49c23a5c4d205a8, not stripped
book@book-virtual-machine:/qemu_arm/driver$
看到80386即是在电脑上使用。
加载模块
卸载模块
注意在控制台中无法开到printk打印的信息,原因不知。
可以用下面的命令来查看
dmesg | tail -8
6.在Qemu上模拟ARM开发板测试
将源码和Makefile两个文件放在同一个目录中,然后用make命令编译(注意此处的Makefile是用第3步中的Makefile)
book@book-virtual-machine:/qemu_arm/driver$ make
make -C /qemu_arm/linux-kernel/linux-3.16.39/ M=/qemu_arm/driver modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
make[1]: Entering directory '/qemu_arm/linux-kernel/linux-3.16.39'
CC [M] /qemu_arm/driver/helloworld.o
Building modules, stage 2.
MODPOST 1 modules
CC /qemu_arm/driver/helloworld.mod.o
LD [M] /qemu_arm/driver/helloworld.ko
make[1]: Leaving directory '/qemu_arm/linux-kernel/linux-3.16.39'
book@book-virtual-machine:/qemu_arm/driver$
编译完成后用file命令查看下是否是在ARM开发板上运行的模块,不要和PC机上运行的搞混了。
book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko
helloworld.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a8f903d9a908203d02cbdac6b23a9d258c21e783, not stripped
book@book-virtual-machine:/qemu_arm/driver$
看到ARM即是用在开发板上的。
将编译好的helloworld.ko文件拷到根文件系统中,然后用Qemu模拟运行,
qemu-system-arm -M vexpress-a9 -m 512M -kernel /qemu_arm/linux-kernel/linux-3.16.39/arch/arm/boot/zImage -nographic -append 'root=/dev/mmcblk0 console=ttyAMA0' -sd /qemu_arm/root_system/a9rootfs.ext3
启动完成后在加载模块显示如下:
/ # insmod helloworld.ko
Enter Hello World!
/ #
参考文章:
http://blog.chinaunix.net/uid-24219701-id-3035689.html 简单的驱动makefile解析
上一篇:Ubuntu下安装arm-linux-gnueabi-xxx编译器
下一篇:Linux 2440 LCD 控制器
- 热门资源推荐
- 热门放大器推荐
- LDK120M12R 1.2V低压降稳压器典型应用(可调版)电路
- EVB-UTC2000-DFP、EVK-UTC2000 评估套件基于 UTC2000 USB 3.0/2.0 Type-A,即插即用 Type-C DFP
- LTC3219 演示板、250mA 通用九通道 LED 驱动器
- DC919A-D,LTC2204 CMOS 输出演示板,直流输入,40Msps 16 位 ADC,DC
- AD8618ARZ-REEL 二阶低通滤波器运算放大器的典型应用电路
- 使用 NXP Semiconductors 的 PCA9511A 的参考设计
- LT3091MPDE 恒流恒压实验室电源的典型应用
- ADR392B 4.096 Vout 微功率、低噪声精密电压基准的典型应用
- LT3470HDDB 5V 降压转换器的典型应用电路
- 使用 Diodes Incorporated 的 AZ7042ZTR-E1 的参考设计

非常经典的关于LLC的杨波博士论文
MB47082PS

XC6406PP60DL






京公网安备 11010802033920号