hello world 示范实例_第1页
hello world 示范实例_第2页
hello world 示范实例_第3页
hello world 示范实例_第4页
hello world 示范实例_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、本文是参考了网上多篇帖子而写的算不上什么原创。唯一值得欣慰的只不过在本机上实现罢了。因为毕竟失败了几次。也因为本人是初学驱动编程 很多简单的问题在我来说是相当的困难的。望有识之士不要笑话。最后,希望本文能给刚学驱动而还没开头的人提供一些帮助。刚看 OREILLY 写的LINUX 设备驱动程序时。作者一再强调在编写驱动程序时必须 建立内核树。所谓内核树,我的理解和网上资料说的一致就是内核源码的一种逻辑形式。那怎么建立呢?为此上网“翻云覆雨”起来而结果却是“惨败而归“。为此托了一天又4个小时(当然包括吃饭睡觉的时间),连个简单的 hello wrold 都没实现。(书中p22页最简单也最没用的驱动

2、事列)不过功夫不负有心人。在今天终于弄明白了怎么回事。下面就请让我慢慢道来吧。先查看自己OS使用的内核版本shanashana:$ uname -r2.6.22-14-generic /* 这是我显示的结果 */如果安装系统时,自动安装了源码。在 /usr/src 目录下有对应的使用的版本目录。例如下(我是自己下的)shanashana:/usr/src$ lslinux-headers-2.6.22-14linux-headers-2.6.22-14-genericlinux-source-2.6.22 /*这个就是解压后的源码目录 */linux-source-2.6.22.tar.bz2

3、 /* 这是我下的源码 包 */shanashana:/usr/src$如果没有源码。(一般ubuntu 都没有吧)查看一下可一下载的源码包(切记不要使用超级用户使用此命令否则会提示没有此命令)shanashana:/usr/src$ apt-cache search linux-sourcelinux-source - Linux kernel source with Ubuntu patchesxen-source-2.6.16 - Linux kernel source for version 2.6.17 with Ubuntu patcheslinux-source-2.6.22 -

4、 Linux kernel source for version 2.6.22 with Ubuntu patchesshanashana:/usr/src$我选择了 linux-source-2.6.22 - Linux kernel source for version 2.6.22 with Ubuntu patches 这个然后 install 之shanashana:/usr/src$ sudo apt-get install linux-source-2.6.22下载完成后,在/usr/src下,文件名为:linux-source-2.6.22.tar.bz2,是一个压缩包,解压缩

5、既可以得到整个内核的源代码:注意 已经切换到超级用户模式rootshana:/usr/src#tar jxvf linux-source-2.6.20.tar.bz2解压后生成一个新的目录/usr/src/linux-source-2.6.22,所有的源代码都在该目录下。进入该目录开始配置内核 选择最快的原版的配置(默认)方式 (我是如此)rootshana:/usr/src/linux-source-2.6.22# make oldconfig当然你也可以使用 自己喜欢的配置方式 如 menuconfig , xconfig(必须有GTK环境吧)。反正不用剪裁什么,所以不管那种方式能配置它就

6、行了。完成后,开始make 吧 这儿比较久 一般有1一个小时吧。(保证空间足够 我编译完成后 使用了1.8G) 我分区时分给/目录30G的空间,我没遇到这问题。倒是我朋友遇到了。shanashana:/usr/src/linux-source-2.6.22$ makeshanashana:/usr/src/linux-source-2.6.22$ make bzImage当然,第一个make也可以不执行,直接make bzImage。执行结束后,可以看到在当前目录下生成了一个新的文件: vmlinux, 其属性为-rwxr-xr-x。然后 :rootshana:/usr/src/linux-s

7、ource-2.6.22#make modules /* 编译 模块 */rootshana:/usr/src/linux-source-2.6.22#make modules_install /* 安装 模块 */执行结束之后,会在/lib/modules下生成新的目录/lib/modules/2.6.22-14-generic/。 在随后的编译模块文件时,要用到这个路径下的build目录。至此,内核编译完成。可以重启一下系统。至此 内核树就建立啦 原来不是很难.写一个 最简单 最没用的驱动吧我在 /home/shana/linux_q/ 目录下创建2个文本文件 hello.c Makefi

8、le/hello.c#include #include MODULE_LICENSE(Dual BSD/GPL);static int hello_init(void)printk(KERN_ALERT Hello, worldn);return 0;static void hello_exit(void)printk(KERN_ALERTGoodbye, cruel worldn);module_init(hello_init);module_exit(hello_exit);程序我就不解释了Makefile 文件obj-m := hello.oKERNELDIR := /lib/modul

9、es/2.6.20/buildPWD := $(shell pwd)modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install如果以上你都完成了在 make 时出现这样的错误shanashana:/linux_驱动开发$ makemake: 没有什么可以做的为 modules。原因很简单 你肯定是从我这直接复制的吧呵呵,Makefile格式错误啦解决办法就是 你把如 $(MAKE) -C $(KERNELDIR) M=$(PWD)

10、 modules_install 移动到行首 然后按Tab 键自动对齐这时里边的变量都变成绿色了然后在 make 吧shanashana:/linux_驱动开发$ makemake -C /lib/modules/2.6.22-14-generic/build M=/home/shana/linux_驱动开发 modulesmake1: Entering directory /usr/src/linux-headers-2.6.22-14-genericCC M /home/shana/linux_驱动开发/hello.oBuilding modules, stage 2.MODPOST 1

11、modulesCC /home/shana/linux_驱动开发/hello.mod.oLD M /home/shana/linux_驱动开发/hello.komake1: Leaving directory /usr/src/linux-headers-2.6.22-14-genericshanashana:/linux_驱动开发$shanashana:/linux_驱动开发$ ls -l总用量 124-rw-r-r- 1 shana shana 303 2008-03-16 10:43 hello.c-rw-r-r- 1 shana shana 49039 2008-03-16 12:11

12、 hello.ko-rw-r-r- 1 shana shana 687 2008-03-16 12:11 hello.mod.c-rw-r-r- 1 shana shana 25840 2008-03-16 12:11 hello.mod.o-rw-r-r- 1 shana shana 24360 2008-03-16 12:11 hello.o-rw-r-r- 1 shana shana 8344 2008-03-16 09:17 linux_qudong_qu.txt-rw-r-r- 1 shana shana 266 2008-03-16 12:09 Makefile-rw-r-r- 1

13、 shana shana 0 2008-03-16 12:11 Module.symversshanashana:/linux_驱动开发$然后加载模块 (超级用户)rootshana:/home/shana/linux_驱动开发# insmod ./hello.ko按照书上的例子 会在终端显示 hello , world 但是运行后什么都没有出现 (原因不解)rootshana:/home/shana/linux_驱动开发# insmod ./hello.korootshana:/home/shana/linux_驱动开发#查看加载模块rootshana:/home/shana/linux_驱

14、动开发# lsmodModule Size Used byhello 2560 0已经加载上咯删除模块rootshana:/home/shana/linux_驱动开发# rmmod hellorootshana:/home/shana/linux_驱动开发#那程序的输出在那呢?书中说明 如果不出现在终端 则会写进 syslog 文件中shanashana:/linux_驱动开发$ cat /var/log/syslog |grep worldMar 16 12:14:53 shana kernel: 5937.529297 Hello, worldMar 16 12:16:05 shana k

15、ernel: 6009.439036 Goodbye, cruel worldshanashana:/linux_驱动开发$至此 全部工作都完成了。下面为这个模块提供些简单的参数,之后可以这样加载模块:insmod ./hello.ko howmany=10 whom=Tczf输出:hello,Tczf hello,Tczf hello,Tczf hello,Tczf .(共10个)修改下hello.c就可以咯,如下:#include #include #include MODULE_LICENSE(Dual BSD/GPL);static char *whom = world;static

16、int howmany = 1;module_param(hownamy,int,S_IRUGO);module_param(whom,charp,S_IRUGO);static int hello_init(void) int i; for(i = 0; i howmany; i+) printk(KERN_ALERT Hello, %sn,whom); return 0;static void hello_exit(void)printk(KERN_ALERTGoodbye, cruel worldn);module_init(hello_init);module_exit(hello_e

17、xit);重新安装的时候别忘了先rmmod hello阿,不然会出错的好了!一切OK!运行环境:linux-2.6.12编译环境:arm-linux-gcc运行平台:AT91RM9200一、编写模块程序testmodule.c#include linux/init.h#include linux/kernel.h#include linux/module.hstatic int hello_initstatic void hello_exitmodule_init;module_exit;MODULE_LICENSE;二、编写Makefileobj-m := testmodule.oKDIR := /src/linux-2.6.12PWD := $default: $ -C $ SUBDIRS=$ modules#注重:$前面要空一个TabKDIR 为内核的路径,这个内核要与AT91RM9200运行的内核先同(编译器也要相同的,要不运行不了)。三、编译在linux下执行:make CC=/s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论