rk3288 阻塞和非阻塞

本文介绍了设备驱动编程中阻塞和非阻塞I/O的工作原理,通过示例展示了如何在应用程序和驱动程序中切换这两种模式。在阻塞模式下,read函数会等待数据可用;而在非阻塞模式下,如果没有数据则会立即返回错误。此外,还提供了一个简单的队列实现用于处理中断事件,并展示了如何在用户空间的测试程序中读取这些事件。
摘要由CSDN通过智能技术生成

适用场景

阻塞:就是等待某件事情发生。比如调用read时,如果没有按键数据则read不会返回,会让进程休眠等待。
其中poll中,如果传入超时事件不是0, 也是会阻塞的。使用poll时,设置超时时间0,没有数据也立即返回。

但是不能让read函数及工作于阻塞方式,又工作于非阻塞方式:
App调用open函数传入O_NONBLOCK,表示使用非阻塞模式,默认是使用阻塞方式。
当open是O_NONBLOCK后,可以使用fcntl修改为阻塞或非阻塞。

应用编程

//open时设置
int fd = open("/dev/xxx", O_RDWR | O_NONBLOCK);	/* 非阻塞方式 */
int fd = open("/dev/xxx", O_RDWR );				/* 阻塞方式 */
//open之后设置
int flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);		/* 非阻塞方式 */
fcntl(fd, F_SETFL, flags & ~O_ONOBLOCK);	/* 阻塞方式 */

驱动编程

在drv_read中,添加判断是否NON_BLOCK

static ssize_t drv_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
{
	if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
		return -EAGAIN;
	
	wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
	//……
}

代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/irqreturn.h>
#include <linux/gpio/consumer.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>

struct gpio_key {
	int gpio;
	struct gpio_desc *gpiod;
	int flag;
	int irq;
};
static struct gpio_key *myBtn_key;
static int button_major = 0;
static struct class *button_class;
static struct fasync_struct *btn_async;

static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);

#define MaxSize 128
struct QNode {
	int Data[MaxSize];
	int rear;
	int front;
};

typedef struct QNode *Queue;

int IsEmpty(Queue Q);
void AddQ(Queue PtrQ, int item);
int DeleteQ(Queue PtrQ);

int IsEmpty(Queue Q)
{
	return (Q->rear == Q->front);   //1:empty 0:not empty
}


void AddQ(Queue PtrQ, int item)
{
    if((PtrQ->rear+1)%MaxSize == PtrQ->front) {
		printk("%s,Queue full\n", __FUNCTION__);
        return;
    }
    PtrQ->rear = (PtrQ->rear+1)%MaxSize;
    PtrQ->Data[PtrQ->rear] = item;
} 


int DeleteQ(Queue PtrQ)
{
    if(PtrQ->front == PtrQ->rear) {
		printk("%s,Queue empty\n", __FUNCTION__);
        return -1;
    } else {
        PtrQ->front = (PtrQ->front+1)%MaxSize;
        return PtrQ->Data[PtrQ->front];
    }
}

static Queue irqBuff;
static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
	int val;

	if(file->f_flags & O_NONBLOCK) {
		if(IsEmpty(irqBuff)) {
			return -EAGAIN;
		} else {
			val = DeleteQ(irqBuff);
			err = copy_to_user(buf, &val, 4);
			if(err != 4) {
				return -1;
			}
			return 4;
		}
	}

	wait_event_interruptible(gpio_key_wait, !IsEmpty(irqBuff));
	val = DeleteQ(irqBuff);
	err = copy_to_user(buf, &val, 4);
	if(err != 4) {
		return -1;
	}
	return 4;
}

static unsigned int button_poll(struct file *fp, poll_table * wait)
{
	printk("%s,button poll\n", __FUNCTION__);
	poll_wait(fp, &gpio_key_wait, wait);
	return IsEmpty(irqBuff) ? 0 : POLLIN | POLLRDNORM;
}

int button_fasync(int fd, struct file *file, int on)
{
	if(fasync_helper(fd, file, on, &btn_async) >= 0)
		return 0;
	else 
		return -EIO;
}

static struct file_operations button_ops = {
	.owner = THIS_MODULE,
	.read  = button_read, 
	.poll  = button_poll,
	.fasync = button_fasync,
};

static irqreturn_t myBtn_irq_request(int irq, void *dev_id)
{
	struct gpio_key *gpio_key = dev_id;
	int val;
	val = gpiod_get_value(gpio_key->gpiod);

	printk(KERN_WARNING"key %d %d\n", gpio_key->gpio, val);
	val = (myBtn_key->gpio << 8)|val;
	AddQ(irqBuff, val);
	wake_up_interruptible(&gpio_key_wait);
	kill_fasync(&btn_async, SIGIO, POLLIN);

	return IRQ_HANDLED;
}

static int my_button_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	int count;
	enum of_gpio_flags flag;
	int i, err;

	count = of_gpio_count(node);
	if(!count) {
		printk("%s,there isn't any gpio availiable\n", __FUNCTION__);
		return -1;
	}
	
	myBtn_key = (struct gpio_key*)kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
	if(!myBtn_key) {
		printk("%s,kzalloc malloc failed\n", __FUNCTION__);
		return -1;
	}


	for(i=0;i<count;i++) {
		myBtn_key[i].gpio = of_get_gpio_flags(node, i, &flag);
		if(myBtn_key[i].gpio < 0) {
			printk("%s, of_get_gpio_flags failed\n", __FUNCTION__);
			return -1;
		}
		myBtn_key[i].gpiod = gpio_to_desc(myBtn_key[i].gpio);
		myBtn_key[i].flag  = flag & OF_GPIO_ACTIVE_LOW;
		myBtn_key[i].irq   = gpio_to_irq(myBtn_key[i].gpio);
		err = request_irq(myBtn_key[i].irq, myBtn_irq_request, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"myBtn_key", &myBtn_key[i]);
	}

    button_major = register_chrdev(0, "mybutton", &button_ops);
    if (button_major < 0) {
        printk(KERN_ERR "button : couldn't get a major number.\n");
        return -1;
    }

    button_class = class_create(THIS_MODULE, "button_class");
    if(IS_ERR(button_class)) {
        printk(KERN_ERR "button class: create failed\n");
		unregister_chrdev(button_major, "mybutton");
        return -1;
    }


	device_create(button_class, NULL, MKDEV(button_major, 0), NULL, "mybutton%d", 0);

	return 0;
}

static int my_button_remove(struct platform_device *pdev)
{
	struct device_node *node= pdev->dev.of_node;
	int count;
	int i;

	device_destroy(button_class, MKDEV(button_major, 0));
	class_destroy(button_class);
	unregister_chrdev(button_major, "mybutton");

	count = of_gpio_count(node);
	for(i=0;i<count;i++) {
		free_irq(myBtn_key[i].irq, &myBtn_key[i]);
	}

	kfree(myBtn_key);
	return 0;
}

static struct of_device_id mybuttons[] = {
	{ .compatible = "mybtn,btn_drv" },
	{ },
};

static struct platform_driver my_button_driver = {
	.probe  = my_button_probe,
	.remove = my_button_remove,
	.driver = {
		.name = "button_dirver",
		.of_match_table = mybuttons,
	},
};

static int gpio_button_init(void)
{
	int err;
	irqBuff = (Queue)kzalloc(sizeof(struct QNode), GFP_KERNEL);
	err = platform_driver_register(&my_button_driver);
	printk(KERN_WARNING"my button dirver init\n");
	return 0;
}

static void gpio_button_exit(void)
{
	platform_driver_unregister(&my_button_driver);
	kfree(irqBuff);
	printk(KERN_WARNING"my button dirver exit\n");
}

module_init(gpio_button_init);
module_exit(gpio_button_exit);
MODULE_LICENSE("GPL");

button_test.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>

static int fd;
static void sig_func(int sig)
{
	int val;
	read(fd, &val, 4);
	printf("get button: 0x%x\n", val);
}


int main(int argc, char *argv[])
{
	int val;
	int flags;
	int i;

	if(argc != 2) {
		printf("Usage: %d <dev>\n", argv[0]);
		return -1;
	}
	signal(SIGIO, sig_func);

	fd = open(argv[1], O_RDWR|O_NONBLOCK);
	if(fd<0) {
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	for(i=0;i<10;i++) {
		if(read(fd, &val, 4) == 4)
			printf("get button: 0x%x\n", val);
		else
			printf("get button: -1\n");
	}

	flags = fcntl(fd, F_GETFL);
	fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);

	while(1) {
		if(read(fd, &val, 4) == 4)
			printf("get button: 0x%x\n", val);
		else
			printf("while get button: -1\n");
	}

	close(fd);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

习惯就好zz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值