主题 : 请教:设备驱动程序该如何编译 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 6476
精华: 0
发帖: 20
金钱: 195 两
威望: 96 点
综合积分: 40 分
注册时间: 2009-06-03
最后登录: 2011-04-14
楼主  发表于: 2009-08-30 18:43

 请教:设备驱动程序该如何编译

我现在用的是mini2440自带的linux系统,pc上是手册上说得fedora9
我现在想学驱动程序的编写
参考别人的hello-world例子如下
/*****************************************************************************************************************
                                                           hello.c
****************************************************************************************************************/
#include<linux/init.h>
#include<linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT"Hello,sunchao!\n");
    return 0;
}

static void hello_exit(void)
{
   printk(KERN_ALERT"Goodbye,sunchao!\n");
}

module_init(hello_init);
module_exit(hello_exit);

/*******************************************************************************************************
                                                                  Makefile
******************************************************************************************************/
KERNELDIR =/opt/FriendlyARM/mini2440/linux-2.6.29
PWD:=$(shell pwd)
#INSTALLDIR=
CROSS_COMPILE =/usr/local/arm/4.3.2/bin/arm-linux
CC   =$(CROSS_COMPILE)gcc
obj-m:=hello.o
modules:
         $(MAKE)  -C   $(KERNELDIR)    M=$(PWD)    modules
.PHONY:modules


我在pc上编译,输入 make modules
出现如下错误
[root@localhost hello-world]# make modules
make  -C   /opt/FriendlyARM/mini2440/linux-2.6.29    M=/opt/FriendlyARM/mini2440/modules/hello-world    modules
make[1]: Entering directory `/opt/FriendlyARM/mini2440/linux-2.6.29'

  ERROR: Kernel configuration is invalid.
         include/linux/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /opt/FriendlyARM/mini2440/linux-2.6.29/Module.symvers
           is missing; modules will have no dependencies and modversions.

make[2]: *** No rule to make target `/opt/FriendlyARM/mini2440/modules/hello-world/hello-world.c', needed by `/opt/FriendlyARM/mini2440/modules/hello-world/hello-world.o'.  Stop.
make[1]: *** [_module_/opt/FriendlyARM/mini2440/modules/hello-world] Error 2
make[1]: Leaving directory `/opt/FriendlyARM/mini2440/linux-2.6.29'
make: *** [modules] Error 2
[root@localhost hello-world]#
请帮忙,谢谢










*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-08-31 11:34
编译模块之前,先执行下面的命令
$cd /opt/FriendlyARM/mini2440/linux-2.6.29
(如果你用的是NEC 3.5寸屏,否则用对应的内核配置文件替换下面的config_N35)
$cp -a config_N35 .config
$make oldconfig && make prepare
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
2楼  发表于: 2009-08-31 11:36
记住,编译内核模块需要
1) 内核源代码树(至少包含头文件)
2)内核源代码树已经配置过,并且和开发板上跑的内核镜像配置一致
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 6476
精华: 0
发帖: 20
金钱: 195 两
威望: 96 点
综合积分: 40 分
注册时间: 2009-06-03
最后登录: 2011-04-14
3楼  发表于: 2009-08-31 15:52
谢谢版主,我照你说的,现在还有错误如下

[root@localhost hello-world]# make modules
make  -C   /opt/FriendlyARM/mini2440/linux-2.6.29    M=/opt/FriendlyARM/mini2440/modules/hello-world    modules
make[1]: Entering directory `/opt/FriendlyARM/mini2440/linux-2.6.29'

  WARNING: Symbol version dump /opt/FriendlyARM/mini2440/linux-2.6.29/Module.symvers
           is missing; modules will have no dependencies and modversions.

make[2]: *** No rule to make target `/opt/FriendlyARM/mini2440/modules/hello-world/hello-world.c', needed by `/opt/FriendlyARM/mini2440/modules/hello-world/hello-world.o'.  Stop.
make[1]: *** [_module_/opt/FriendlyARM/mini2440/modules/hello-world] Error 2
make[1]: Leaving directory `/opt/FriendlyARM/mini2440/linux-2.6.29'
make: *** [modules] Error 2
[root@localhost hello-world]#


麻烦了
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2009-08-31 15:58
看起来你的Makefile要找hello-world.c,而事实上却没有?
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 6476
精华: 0
发帖: 20
金钱: 195 两
威望: 96 点
综合积分: 40 分
注册时间: 2009-06-03
最后登录: 2011-04-14
5楼  发表于: 2009-08-31 19:45
确实是这样,我原来的.c文件是hello.c,改成hello-world.c又有错误如下

[root@localhost hello-world]# make modules
make  -C   /opt/FriendlyARM/mini2440/linux-2.6.29    M=/opt/FriendlyARM/mini2440/modules/hello-world    modules
make[1]: Entering directory `/opt/FriendlyARM/mini2440/linux-2.6.29'

  WARNING: Symbol version dump /opt/FriendlyARM/mini2440/linux-2.6.29/Module.symvers
           is missing; modules will have no dependencies and modversions.

  CC [M]  /opt/FriendlyARM/mini2440/modules/hello-world/hello-world.o
  Building modules, stage 2.
  MODPOST 1 modules
/bin/sh: scripts/mod/modpost: No such file or directory
make[2]: *** [__modpost] Error 127
make[1]: *** [modules] Error 2
make[1]: Leaving directory `/opt/FriendlyARM/mini2440/linux-2.6.29'
make: *** [modules] Error 2

  
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2009-09-01 10:40
$cd /opt/FriendlyARM/mini2440/linux-2.6.29
$make modules_prepare
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 6476
精华: 0
发帖: 20
金钱: 195 两
威望: 96 点
综合积分: 40 分
注册时间: 2009-06-03
最后登录: 2011-04-14
7楼  发表于: 2009-09-01 14:27
谢谢版主,现在可以生成.ko文件,下载到板子的/lib/modules/2.6.29-FriendlyARM里,云行insmod hello-world.ko
终端里没有输出hello,sunchao!
运行lsmod,终端显示hello_world 996 0 -live  0xbf000000
运行rmmod hello-world.ko,终端显示
rmmod:chdir(2.6.29.4-FriendlyARM):No such file or directory
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
8楼  发表于: 2009-09-01 14:54
要看内核的打印输出,最好的办法是dmesg命令;
rmod的问题,搜索论坛的老贴
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 6476
精华: 0
发帖: 20
金钱: 195 两
威望: 96 点
综合积分: 40 分
注册时间: 2009-06-03
最后登录: 2011-04-14
9楼  发表于: 2009-09-01 19:33
我现在在/lib/modules/下新建文件夹2.6.29.4-FriendlyARM,把hello-world.ko拷贝到该文件夹下,然后在该文件夹下加载和卸载驱动都没有任何信息输出,也不报任何错误,但事实是模块是加载了,但却卸载不掉,程序就是我的上述程序不变,请版主帮忙看看吧,谢谢