以阻塞方式对IO文件进行读取(test.c读取,test2.c发送数据)
实验结果
执行test.c生成的pro1可执行文件,光标显示处于阻塞状态
执行test2.c生成的pro2可执行文件,test.c处打印 hello dhl
三级标题test.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
memset(buf,0,sizeof(buf));//清空
read(fd,buf,sizeof(buf)); //读取数据
printf("buf:%s\n",buf);
}
close(fd);
return 0;
}
三级标题test2.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
char buf[128] = "hello dhl";
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
write(fd,buf,sizeof(buf)); //写数据.模拟传感器获取到数据
close(fd);
return 0;
}
三级标题myled0.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/slab.h>
#include<linux/io.h>
#include<linux/uaccess.h>
struct cdev *cdev; //字符设备空间首地址
unsigned int major=500; //静态申请设备号
unsigned int minor=0;//次设备号的起始值
dev_t devno; //动态申请设备号
struct class *cls; //接收注册结构体的地址
struct device *dev; //设备号
char kbuf[128] = {0}; //硬件数据
//定义等待队头
wait_queue_head_t wq_head; //等待队列
unsigned int condition = 0; //硬件准备信号
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
if(sizeof(kbuf) < size)
size = sizeof(kbuf);
wait_event_interruptible(wq_head, condition); //进程切换为休眠状态
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy_to_user failed\n");
return -EIO;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
condition = 0; //下次事件还没准备好
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
if(sizeof(kbuf) < size)//判断用户空间的需求是否能被驱动满足,满足不了就给能给的最好的
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
condition = 1; //表示硬件数据准备就绪
wake_up_interruptible(&wq_head); //唤醒休眠的进程
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
static int __init mycdev_init(void) //寄存器地址映射和初始化
{
int ret; //ret返回错误码
//初始化等待队列头
init_waitqueue_head(&wq_head);
/*
// 映射物理地址
vir_gpioe = ioremap(GPIOE, sizeof(gpio_t));
if (vir_gpioe == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
vir_rcc = ioremap(RCC, 4);
if (vir_rcc == NULL)
{
printk("RCC寄存器映射失败\n");
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (vir_gpiof == NULL)
{
printk("MODER寄存器映射失败\n");
return -EFAULT;
}
printk("寄存器映射成功\n");
*/
//1.分配字符设备驱动对象空间 cdev_alloc
cdev=cdev_alloc(); //字符设备空间首地址
if(cdev==NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret=-EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
//2.字符设备驱动对象部分初始化 cdev_init
cdev_init(cdev,&fops);
//3.申请设备号 register_chrdev_region/alloc_chrdev_region
if(major>0)//静态申请设备号
{
ret=register_chrdev_region(MKDEV(major,minor),1,"myled0"); //设备号需要是组合出来的,次设备数量,设备文件名
if(ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else//动态申请设备号
{
ret=alloc_chrdev_region(&devno,minor,1,"myled0"); //动态申请设备号,次设备号,设备数量,文件名
if(ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major=MAJOR(devno); //根据设备号得到主设备号
minor=MINOR(devno); //根据设备号得到次设备号
}
printk("申请设备号成功\n");
//4.注册字符设备驱动对象 cdev_add()
ret=cdev_add(cdev,MKDEV(major,minor),1); //字符设备,设备号,设备数量
if(ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录
cls=class_create(THIS_MODULE,"myled0"); //指向自身的指针,文件名
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret=-PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myled0"); //创建设备节点
if(IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret=-PTR_ERR(dev);
goto out5;
}
printk("向上提交设备节点信息成功\n");
/*
(*vir_rcc) |= (0x3 << 4);
// 寄存器初始化led1
vir_gpioe->moder &= (~(0x3 << 20));
vir_gpioe->moder |= (0x1 << 20);
vir_gpioe->odr &= (~(0x1 << 10));
printk("LED1硬件寄存器初始化成功\n");
*/
return 0;
out5:
//销毁上面提交的设备信息
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
out4:
cdev_del(cdev);
out3:
unregister_chrdev_region(MKDEV(major,minor),1);
out2:
kfree(cdev);
out1:
return ret;
}
static void __exit mycdev_exit(void)
{
//1.销毁设备信息 device_destroy
device_destroy(cls,MKDEV(major,0));
//2.销毁目录 class_destroy
class_destroy(cls);
//3.注销对象 cdev_del()
cdev_del(cdev);
//4.释放设备号 unregister_chrdev_region()
unregister_chrdev_region(MKDEV(major,minor),1);
//5.释放对象空间 kfree()
kfree(cdev);
/*
// 取消寄存器地址映射
iounmap(vir_gpioe);
iounmap(vir_rcc);
iounmap(vir_gpiof);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁字符设备驱动
class_destroy(cls);
// 字符设备驱动的注销
unregister_chrdev(major, "led0");
*/
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");