I/O模型之阻塞(在中断基础上------jz2440)

1 阻塞:  在应用层调用read函数的时候,如果硬件中的数据没有准备好,此时进程会进入休眠状态,当硬件的数据准备好的时候会给驱动发送中断。驱动收到中断之后,唤醒休眠的进程。这个被唤醒的进程在driver_read读取硬件的数据,并把数据  返回到用户空间。(模型中断)

可以使用队列,把整个进程进入到队列中,使进程进入阻塞的状态,当有数据产生的时候,产生中断,唤醒在队列中的进程,从而读取数据.

队列:

        wait_queue_head_t wq  //定义等待队列头

        init_waitqueue_head(&wq)//初始化等待队列头

        wait_event(wq, condition)  //不可中断的等待态

        wait_event_interruptible(wq, condition)//可中断的等待态(当产生中断信号就可以唤醒)

        参数:

        @wq         :等待对列头

        @condition :如果条件为假表示可休眠,如果为真表示不休眠

        返回值:成功返回0,失败返回错误码

        wake_up(&wq)

        wake_up_interruptible(&wq)

        唤醒休眠

        condition = 1;

        分析:wake_up_interruptible()唤醒后,wait_event_interruptible(wq, condition)宏,自身再检查“condition”这个条件以决定是返回还是继续休眠,真(1)则返回,假(0)则继续睡眠,不过这个程序中若有中断程序的话,中断来了,还是会继续执行中断函数的。只有当执行wake_up_interruptible()并且condition条件成立时才会把程序从队列中唤醒

==========================================================    

       user

        fd = open("hello",O_RDWR); //阻塞的方式打开文件

        read(fd,buf,sizeof(buf));

    -----------------------------------------------------

        kernel:

        fops:driver_read()

        {

             //2.阻塞方式打开的

             那进入休眠状态(等待唤醒)

            3.读取数据拷贝数据

        }

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include<linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/interrupt.h> 
struct    led_dev_struct{
     int  major;
     struct class *dev_class;
     struct device *device_class;
};

struct     led_dev_struct   *key_drv;

#define  GPFx    0x56000050
#define  GPGx   0x56000060
volatile  unsigned   int  *gpfcon;
volatile  unsigned  int   *gpfdat;

volatile  unsigned   int  *gpgcon;
volatile  unsigned  int   *gpgdat; 
static unsigned     int     key_val;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//队列
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;

static   irqreturn_t   key_interrupt(int irq, void *dev_id)
{

	  if(irq==IRQ_EINT0)
	  {
                 printk("key1-----------\n");
		key_val=*gpfdat;
	 	key_val=(key_val&(1<<0))?0:1;
	  }
	  if(irq==IRQ_EINT2)
	  {
                printk("key2------------\n");
		key_val=*gpfdat;
	 	key_val=(key_val&(1<<2))?0:1;	   
	  }
	  if(irq==IRQ_EINT11)
	  {
	       printk("key3-----------\n");
	      key_val=*gpgdat;
	      key_val=(key_val&(1<<3))?0:1;
	  }
	    ev_press = 1;                  /* 表示中断发生了 */
            wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
	   return IRQ_HANDLED;//表示中断完成
	   
}
static   int  key_open(struct inode *inode, struct file *file)
{
            //申请中断
           request_irq(IRQ_EINT0, key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key1",NULL);
	  request_irq(IRQ_EINT2, key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key2",NULL);
	  request_irq(IRQ_EINT11,key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key11",NULL);
           return 0;
		
}
static  ssize_t  key_read(struct file *file, char __user *buffer, size_t size, loff_t *ppos)
{  
      
          /* 如果没有按键动作, 休眠 */
	   wait_event_interruptible(button_waitq, ev_press);
	    copy_to_user(buffer,&key_val,sizeof(key_val));	
	  ev_press = 0;
          return sizeof(key_val);
}


static  ssize_t  key_write(struct file *file, const char __user *buffer, size_t size,loff_t *ppos)
{
	   
         printk("xxxxxxxxxxxxxx\n");
	 return 0;
}

struct file_operations key_fops={
            .open=key_open,   
            .read= key_read,
            .write=key_write,
};  


//入口函数
static int __init key_init(void)
{
       int  ret;
      //给机构体申请空间
      key_drv=kmalloc(sizeof(struct  led_dev_struct), GFP_KERNEL);
       //申请设备号
      key_drv->major=register_chrdev(0, "key_led", &key_fops);
      if ( key_drv->major < 0) { 
		printk("register_chrdev  error\n");
		return -EINVAL;
	}  
      //创建类
     key_drv->dev_class=class_create(THIS_MODULE, "leds");
     if (IS_ERR( key_drv->dev_class)) {
	   ret= PTR_ERR( key_drv->dev_class);
	   goto  flag1;
	}
     //创建设备
    key_drv->device_class=device_create(key_drv->dev_class, NULL,MKDEV( key_drv->major,0), "led1");
    if (IS_ERR( key_drv->device_class)){
	    ret=PTR_ERR( key_drv->device_class);
	  goto flag2;	
    }
    //进行映射把物理地址变成虚拟地址
       gpfcon= ioremap(GPFx, 16);    
       gpfdat=gpfcon+1;
	  
      gpgcon=ioremap(GPGx,12);
      gpgdat=gpgcon+1;
     
   
   return 0;
flag2: 
	class_destroy( key_drv->dev_class);
flag1:
	unregister_chrdev(0, "key_led");

}


//出口函数
static  void __exit  key_exit()
{    

       
       iounmap( gpgcon);
       iounmap( gpfcon);
       device_destroy(key_drv->dev_class, MKDEV( key_drv->major,0));    
       class_destroy(key_drv->dev_class);
       unregister_chrdev(0, "key_led");
       kfree(key_drv);
       free_irq(IRQ_EINT0,NULL);
       free_irq(IRQ_EINT2,NULL);
       free_irq(IRQ_EINT11,NULL);
}

//入口函数和出口函数的修饰
module_init(key_init);
module_exit(key_exit);

MODULE_LICENSE("GPL");

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int  main(int argc,char  *argv[])
{

      int fd;
       unsigned int key_val;
	int cnt = 0;
	
      fd=open("/dev/led1", O_RDWR);
      if(fd<0)
      {

 
            perror("open\n");
	   return -1;
      }
       while (1)
     {
		read(fd, &key_val, sizeof(key_val));
		if (!key_val )
		{
			printf("%04d key pressed: %d d\n", cnt++, 0);
		}
		else
		{
                         printf("%04d key pressed: %d d\n", cnt++, 1);
		}
	}
    return 0;
}
  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值