驱动程序之_1_字符设备_7_非阻塞
阻塞操作
是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。
被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。
非阻塞操作
进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。
驱动程序中:
如果是以非阻塞方式打开文件,在读函数中,判断是否按下按键,若有按键则返回键值,否则直接退回应用程序;而如果以阻塞方式打开(前面关于按键中断的文章就是这种情况),则进程会进入休眠,等待按键事件触发
应用程序中:
1、以非阻塞方式打开设备文件
fd = open("/dev/key_irq_drv",O_RDWR | O_NONBLOCK);
2、休眠时间设定为2s,2s秒循环一次代码
3、一次循环只进行一次读操作,所以即使2s内按下多次按键,也只会按2s的周期返回一次键值
附上驱动程序和测试用的应用程序代码
驱动程序
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/irq.h>
static struct class *my_irq_class;
static struct class_device *my_irq_class_device;
static unsigned char key_val;
static DECLARE_MUTEX(button_lock);
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc key_pins_desc[4]={
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
static irqreturn_t buttons_irq(int irq,void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval) //松开
{
key_val = 0x80 | pindesc->key_val;
}
else
{
key_val = pindesc->key_val;
}
ev_press = 1;
wake_up_interruptible(&button_waitq);
return IRQ_HANDLED;
}
static int my_irq_drv_open(struct inode *inode, struct file *file)
{
if(file->f_flags & O_NONBLOCK)
{
if(down_trylock(&button_lock))
return -EBUSY;
}
else
{
down(&button_lock);
}
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S2",&key_pins_desc[0]);
request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",&key_pins_desc[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",&key_pins_desc[2]);
request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",&key_pins_desc[3]);
return 0;
}
int my_irq_drv_close(struct inode *inode,struct file *file)
{
up(&button_lock);
free_irq(IRQ_EINT0,&key_pins_desc[0]);
free_irq(IRQ_EINT2,&key_pins_desc[1]);
free_irq(IRQ_EINT11,&key_pins_desc[2]);
free_irq(IRQ_EINT19,&key_pins_desc[3]);
return 0;
}
ssize_t my_irq_drv_read(struct file *file, char __user *buf, size_t size, loff_t * ppos)
{
if(size != 1)
return -EINVAL;
if(file->f_flags & O_NONBLOCK)
{
if(!ev_press)
return -EAGAIN;
}
else
{
wait_event_interruptible(button_waitq,ev_press);
}
ev_press = 0;
copy_to_user(buf,&key_val,1);
return 0;
}
static struct file_operations irq_drv_fops = {
.owner = THIS_MODULE,
.open = my_irq_drv_open,
.read = my_irq_drv_read,
.release = my_irq_drv_close,
};
static int major;
static int my_irq_drv_init(void)
{
major = register_chrdev(0,"my_irq_drv",&irq_drv_fops);
my_irq_class = class_create(THIS_MODULE,"my_irq_drv");
my_irq_class_device = class_device_create(my_irq_class,NULL,MKDEV(major,0),NULL,"key_irq_drv");
return 0;
}
static void my_irq_drv_exit(void)
{
unregister_chrdev(major,"my_irq_drv");
class_device_unregister(my_irq_class_device);
class_destroy(my_irq_class);
}
module_init(my_irq_drv_init);
module_exit(my_irq_drv_exit);
MODULE_LICENSE("GPL");
应用程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc,char *args[])
{
unsigned char key_val;
int fd;
int ret;
fd = open("/dev/key_irq_drv",O_RDWR | O_NONBLOCK);
if(fd < 0)
{
printf("can't open\r\n");
return 0;
}
printf("open successfully\r\n");
while(1)
{
ret = read(fd,&key_val,1);
printf("key_val : 0x%x , ret : %d\r\n",key_val,ret);
sleep(2);
}
return 0;
}