字符设备驱动开发:
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>