标题:
Linux 驱动程序开发步骤(X86平台)(2)
[打印本页]
作者:
苹果也疯狂
时间:
2015-6-30 21:08
标题:
Linux 驱动程序开发步骤(X86平台)(2)
第十步:根据设备号的设置,在文件系统中建立对应的设备节点
#mknod /dev/test c XXX XX
例子
2:
驱动文件
:
#include<linux/init.h>
#include<linux/module.h>
#include<linux/cdev.h>
#include<linux/fs.h>
#include<linux/kernel.h>
#include<linux/uaccess.h>
#defineDEVICENAME "ccccc"
unsigned intmajor=221;
unsigned intminor=0;
struct cdev *abc;
dev_t dev;
static charbufrh[1024]="read success!";
static int aaaaa_open(structinode *inodep, struct file *filep)
{
printk("read success!\n");
return 0;
}
int aaaaa_release(structinode *inodep, struct file *filep)
{
return 0;
}
static ssize_t aaaaa_read(struct file *filep, char __user *buf, size_t count, loff_t *offset)
{
if(copy_to_user(buf, bufrh, 1))
{
printk("copy_to_user fail!\n");
}
return 0;
}
ssize_t aaaaa_write (structfile *filep, const char __user *buf, size_tcount, loff_t *offse)
{
printk("write!\n");
return 0;
}
static conststruct file_operations fops ={
.owner = THIS_MODULE,
.open = aaaaa_open,
.release = aaaaa_release,
.read = aaaaa_read,
.write = aaaaa_write,
};
static int __initaaaaa_init(void)
{
int a;
dev=MKDEV(major, minor);
a=register_chrdev_region(dev, 1, DEVICENAME);
abc=cdev_alloc();
abc->owner=THIS_MODULE;
cdev_init(abc, &fops);
cdev_add(abc, dev, 1);
return 0;
}
static void__exit aaaaa_cleanup(void)
{
cdev_del(abc);
unregister_chrdev_region(dev, 1);
}
module_init(aaaaa_init);
module_exit(aaaaa_cleanup);
MODULE_LICENSE("GPL");
Makefile
文件
:
obj-m +=firstqd.o(
相应设备文件名
)
KERDIR =/usr/src/linux-headers-2.6.32-24-generic
#KERDIR=/home/linux2.6/linux #arm
骞冲彴
PWD=$(shell pwd)
modules:
$(MAKE) -C $(KERDIR) M=$(PWD) modules
pc:
gcc -o fristqd firstqd.c
arm:
arm-linux-gcc -o fristqd firstqd.c
clean:
rm -rf *.o *~core *.depend *.cmd *.ko *.mod.c*.tmp_versions
测试文件
(test.c):
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
char buf[1024];
char bufw[1024]="writesuccess";
int main()
{
int fd,m,n;
fd=open("/dev/aaa",O_RDWR);
if (fd)
{
m=read(fd,buf,100);
printf("read kernel:%s\n",buf);
n=write(fd,bufw,10);
}
//printf("ni hao");
return 0;
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0