ubuntu下使用qemu模拟ARM(六)------驱动程序

发布者:TranquilDreamer最新更新时间:2024-11-08 来源: cnblogs关键字:ubuntu  qemu  模拟ARM  驱动程序 手机看文章 扫描二维码
随时随地手机看文章

驱动程序分为在ubuntu上运行和在ARM开发板上运行两种,我们分别来进行测试

1.源码

 

  1. empty#include <linux init.h=''>  

  2. #include   

  3.   

  4.   

  5. static int hello_init(void)  

  6. {  

  7. printk(KERN_EMERG 'Enter Hello abc World!n');  

  8. return 0;  

  9. }  

  10.   

  11. static void hello_exit(void)  

  12. {  

  13. printk(KERN_EMERG 'Exit hello world!n');  

  14.   

  15. }  

  16.   

  17. module_init(hello_init);  

  18. module_exit(hello_exit);  

  19. MODULE_AUTHOR('RFIDUNION');  

  20. MODULE_LICENSE('GPL v2');  

  21. MODULE_DESCRIPTION('A simple driver');  

  22. MODULE_ALIAS('a simple test module');  

  23.     empty  


2.电脑上的Makefile

  1. ifneq ($(KERNELRELEASE),)  

  2. module-objs := helloworld.o  

  3. obj-m   := helloworld.o  

  4. else  

  5. KERNELDIR := /lib/modules/$(shell uname -r)/build  

  6. PWD  := $(shell pwd)  

  7. modules:  

  8.     (MAKE)−C(����)−�(KERNELDIR) M=$(PWD) modules  

  9. endif  

  10.    

  11. clean:  

  12.     rm -rf *.o *~core.depend.*.cmd *.ko *.mod.c.tmp versions  

  13.    


KERNELRELEASE 内核顶层目录Makefile的一个变量。
KERNELDIR ?= /lib/modules/(shelluname−r)/build内核源码树目录。该Makefile共读取两次,在输入Makefile时,(�ℎ��������−�)/�����内核源码树目录。该��������共读取两次,在输入��������时,(KERNELDIR) 第一次读取KERNELRELEASE并没有被定义,然后就开始读取内核源码的目录,开始定义KERNELRELEASE,然后到当前模块的目录里面,M=$(PWD) 进入该Makefile时KERNELRELEAS已经被定义了,读取要编译的模块,然后再返回到modules
编译完成,产生.KO文件

 

3.ARM开发板上的Makefile

 

  1. ifneq ($(KERNELRELEASE),)  

  2.    

  3. obj-m := helloworld.o  

  4.    

  5. else  

  6. KDIR := /qemu_arm/linux-kernel/linux-3.16.39/  

  7. all:  

  8.     make -C (KDIR)M=(����)�=(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-  

  9. clean:  

  10.     rm -f *.ko *.o *.mod.o *.mod.c *.symvers  

  11.    

  12. 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)

 

  1. book@book-virtual-machine:/qemu_arm/driver$ make  

  2. make -C /lib/modules/4.8.0-34-generic/build M=/qemu_arm/driver modules  

  3. make[1]: Entering directory '/usr/src/linux-headers-4.8.0-34-generic'  

  4.   CC [M]  /qemu_arm/driver/helloworld.o  

  5.   Building modules, stage 2.  

  6.   MODPOST 1 modules  

  7.   CC      /qemu_arm/driver/helloworld.mod.o  

  8.   LD [M]  /qemu_arm/driver/helloworld.ko  

  9. make[1]: Leaving directory '/usr/src/linux-headers-4.8.0-34-generic'  

  10. book@book-virtual-machine:/qemu_arm/driver$   


编译完成后用file命令查看下是否是在PC机上运行的模块,不要和ARM开发板上运行的搞混了。

  1. book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko   

  2. helloworld.ko: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), BuildID[sha1]=53c0e5e95e8d1c4683f92a7da49c23a5c4d205a8, not stripped  

  3. book@book-virtual-machine:/qemu_arm/driver$   


看到80386即是在电脑上使用。

 

加载模块

卸载模块

注意在控制台中无法开到printk打印的信息,原因不知。

可以用下面的命令来查看

dmesg | tail -8

6.在Qemu上模拟ARM开发板测试

将源码和Makefile两个文件放在同一个目录中,然后用make命令编译(注意此处的Makefile是用第3步中的Makefile)

 

  1. book@book-virtual-machine:/qemu_arm/driver$ make  

  2. make -C /qemu_arm/linux-kernel/linux-3.16.39/ M=/qemu_arm/driver modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-  

  3. make[1]: Entering directory '/qemu_arm/linux-kernel/linux-3.16.39'  

  4.   CC [M]  /qemu_arm/driver/helloworld.o  

  5.   Building modules, stage 2.  

  6.   MODPOST 1 modules  

  7.   CC      /qemu_arm/driver/helloworld.mod.o  

  8.   LD [M]  /qemu_arm/driver/helloworld.ko  

  9. make[1]: Leaving directory '/qemu_arm/linux-kernel/linux-3.16.39'  

  10. book@book-virtual-machine:/qemu_arm/driver$   


编译完成后用file命令查看下是否是在ARM开发板上运行的模块,不要和PC机上运行的搞混了。

  1. book@book-virtual-machine:/qemu_arm/driver$ file helloworld.ko   

  2. helloworld.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a8f903d9a908203d02cbdac6b23a9d258c21e783, not stripped  

  3. 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
启动完成后在加载模块显示如下:

 

  1. / # insmod helloworld.ko   

  2. Enter Hello  World!  

  3. / #   


参考文章:
http://blog.chinaunix.net/uid-24219701-id-3035689.html   简单的驱动makefile解析 


关键字:ubuntu  qemu  模拟ARM  驱动程序 引用地址:ubuntu下使用qemu模拟ARM(六)------驱动程序

上一篇:Ubuntu下安装arm-linux-gnueabi-xxx编译器
下一篇:Linux 2440 LCD 控制器

小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved