S3C2400 第一个字符设备驱动开发日志

字符设备驱动开发:

first_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
//#include <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>


static int first_drv_open(struct inode * inode, struct file * file)
{
        printk("first_drv_open by liuzewen\n");
        return 0 ;
}

static ssize_t first_drv_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
{
        printk("first_drv_write by liuzewen\n");
        return 0;
}

static struct file_operations first_drv_fops={

        .owner = THIS_MODULE,
        .open   =  first_drv_open,
        .write   =  first_drv_write,
};

int first_drv_init(void)
{
        register_chrdev(111,"first_drv",&first_drv_fops);
        return 0;
}

void first_drv_exit(void)
{
        unregister_chrdev(111,"first_drv");
}

module_init(first_drv_init);
module_exit(first_drv_exit);

Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
        make -C $(KERN_DIR) M=`pwd` modules

clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order

obj-m   += first_drv.o

 

报错: 

/work/drivers_and_test/first_drv.c:9:32: asm/arch/regs-gpio.h: No such file or directory
/work/drivers_and_test/first_drv.c:32: error: syntax error before "int"
/work/drivers_and_test/first_drv.c: In function `__inittest':
/work/drivers_and_test/first_drv.c:43: error: `first_drv_init' undeclared (first use in this                                                                                                    function)
/work/drivers_and_test/first_drv.c:43: error: (Each undeclared identifier is reported only o                                                                                                   nce
/work/drivers_and_test/first_drv.c:43: error: for each function it appears in.)
/work/drivers_and_test/first_drv.c: At top level:
/work/drivers_and_test/first_drv.c:44: warning: type defaults to `int' in declaration of `mo                                                                                                   dules_exit'
/work/drivers_and_test/first_drv.c:44: warning: parameter names (without types) in function                                                                                                    declaration
/work/drivers_and_test/first_drv.c:44: warning: data definition has no type or storage class
/work/drivers_and_test/first_drv.c:25: warning: 'first_drv_fops' defined but not used
scripts/Makefile.build:208: recipe for target '/work/drivers_and_test/first_drv.o' failed
make[2]: *** [/work/drivers_and_test/first_drv.o] Error 1
Makefile:1204: recipe for target '_module_/work/drivers_and_test' failed
make[1]: *** [_module_/work/drivers_and_test] Error 2
make[1]: Leaving directory '/work/system/linux-2.6.22.6'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2

原来的驱动文件为:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>


static int first_drv_open(struct inode * inode, struct file * file)
{
	printk("first_drv_open by liuzewen\n");
	return 0 ;
}

static ssize_t first_drv_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
{
	printk("first_drv_write by liuzewen\n");
	return 0;
}

static struct file_operations first_drv_fops={

	.owner = THIS_MODULE,
	.open   =  first_drv_open,
	.write   =  first_drv_write,
}

int first_drv_init(void)
{
	register_chrdev(111,"first_drv",&first_drv_fops);
	return 0;
}

int first_drv_exit(void)
{
	unregister_chrdev(111,"first_drv";
	return 0;
}

module_init(first_drv_init);
modules_exit(first_drv_exit);

错误原因:

1.结构体那里没加“;”

2.引用那里有找不到的文件:

#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

3.modules_exit  

多了个s

修改:

修改上面的文件后

clean  清楚之前编译的结果

然后make 编译

制作yaff2文件系统镜像 烧写到板子中

然后

insmod 加载 报错如下:

# ls
bin           first_drv.ko  linuxrc       sbin
dev           hello.txt     lost+found    sys
etc           lib           proc          usr
# insmod first_drv.ko
first_drv: version magic '2.6.22.6 mod_unload ARMv5 ' should be '2.6.22.6 mod_unload ARMv4 '
insmod: cannot insert 'first_drv.ko': Invalid module format (-1): Exec format error

搜集的资料:

https://www.xuebuyuan.com/3254272.html

https://blog.csdn.net/Longyu_wlz/article/details/103292251

https://blog.csdn.net/hbcbgcx/article/details/88194321

https://blog.csdn.net/diandianyangyi/article/details/8533976

 

根据资料显示,应该是板子的内核版本和make编译时的调用的内核版本不一致导致的

 

解决办法见下一篇文章

字符设备驱动遇到的内核版本不匹问题

 

参考修改:

https://www.cnblogs.com/lifexy/p/8431012.html

 

4.1首先直接修改Makefile

将以前的内核位置改为KERN_DIR = /work/system/linux-3.4.2

4.2然后直接make,根据以下错误信息来修改

复制代码

first_drv.c:7:32: error: asm/arch/regs-gpio.h: No such file or directory

first_drv.c:8:26: error: asm/hardware.h: No such file or directory

first_drv.c: In function 'first_drv_write':

first_drv.c:65: warning: ignoring return value of 'copy_from_user', declared with attribute warn_unused_result

first_drv.c: In function 'first_drv_init':

first_drv.c:152: error: implicit declaration of function 'class_create'

first_drv.c:152: warning: assignment makes pointer from integer without a cast

first_drv.c:155: error: implicit declaration of function 'class_device_create'

first_drv.c:155: warning: assignment makes pointer from integer without a cast

first_drv.c:160: warning: assignment makes pointer from integer without a cast

first_drv.c: In function 'first_drv_exit':

first_drv.c:170: error: implicit declaration of function 'class_destroy'

first_drv.c:172: error: implicit declaration of function 'class_device_unregister'

复制代码

 根据上面错误信息,来修改led文件first_drv.c

1)去掉第7~8行:

//#include <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z文的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值