I/O模型之多路複用select(在中斷基礎上------jz2440)

 int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout); 

 功 能:select用於監測是哪個或哪些文件描述符產生件; 

            參數:nfds:         監測的最大文件描述個數

                      readfds:     讀事件集合; //讀

                      writefds:     寫事件集合;  //NULL表示不關心

                      exceptfds:   異常事件集合(帶外集合);  //一般爲NULL

                      timeout:      超時設置.  NULL:一直阻塞,直到有文件描述符就緒或出錯

                                            時間值爲0:僅僅檢測文件描述符集的狀態,然後立即返回

                                            時間值不爲0:在指定時間內,如果沒有事件發生,則超時返回

                      struct timeval {

               long    tv_sec;           /* seconds */秒

               long    tv_usec;        /* microseconds */微妙

           };

   select返回值:  <0  出錯

                            >0  表示有事件產生;

                            ==0 表示超時時間已到

注意:當select()函數退出後,集合表示有數據的集合

         當select()函數退出前,集合表示描述符的集合

void FD_CLR(int fd, fd_set *set);//把fd從集合中清除

int  FD_ISSET(int fd, fd_set *set);//判斷fd是否在在集合中

void FD_SET(int fd, fd_set *set);//把描述符插入到集合中

void FD_ZERO(fd_set *set);//對集合清零

if( FD_ISSET(int fd, fd_set *set))//判斷fd是否在集合中

{  }

#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> 
#include <linux/poll.h>
struct    led_dev_struct{
     int  major;
     struct class *dev_class;
     struct device *device_class;
     wait_queue_head_t wq; //定義等待隊列頭
};

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;
/* 中斷事件標誌, 中斷服務程序將它置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(&(key_drv->wq));   /* 喚醒休眠的進程 */
	   return IRQ_HANDLED;//表示中斷完成
	   
}
static   int  key_open(struct inode *inode, struct file *file)
{
            //申請中斷
           int ret;
           ret=request_irq(IRQ_EINT0, key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key1",NULL);
	   if(ret)
	   {  
	            return  -EINVAL;
	   }
	  ret=request_irq(IRQ_EINT2, key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key2",NULL);
	   if(ret)
	   {  
	            return  -EINVAL;
	   }
	  ret=request_irq(IRQ_EINT11,key_interrupt,IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,"key11",NULL);
	    if(ret)
	   {  
	            return  -EINVAL;
	   }
           return 0;
		
}
static  ssize_t  key_read(struct file *file, char __user *buffer, size_t size, loff_t *ppos)
{      
           printk("%d\n",key_val);
           /* 如果沒有按鍵動作, 休眠 */
	   wait_event_interruptible(key_drv->wq, ev_press);
	   copy_to_user(buffer,&key_val,sizeof(key_val));
	   printk("rrrrrrrrrrrrrrrrrrrrrr\n");
	   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;
}
//poll機制

static unsigned  key_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;
	poll_wait(file, &key_drv->wq, wait); // 不會立即休眠

	if (ev_press)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}



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


//入口函數
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");
		ret=-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;	
    }
     init_waitqueue_head(&(key_drv->wq));//初始化等待隊列頭
    //進行映射把物理地址變成虛擬地址
       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");
	return  ret;

}
//出口函數
static  void __exit  key_exit(void)
{    
       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>
#include <poll.h>
#include<stdio.h>
 #include <sys/time.h>
      

int  main(int argc,char  *argv[])
{

       int fd,ret;
       int max=0;
       unsigned int key_val;
       fd_set readfd;
       char buf[100]={0};
       fd=open("/dev/led1", O_RDWR);//後續的I/O操作都是非阻塞的方式
       if(fd<0)
       {

 
            perror("open\n");
	   return -1;
       }
      
	while (1)
	{     
	       FD_ZERO(&readfd);//把集合清除
	       FD_SET(0,&readfd);//把描述符號插入到集合中
	       FD_SET(fd, &readfd);   //把描述符號插入到集合中
	       max=fd;
		
	       ret= select(max+1, &readfd, NULL,NULL,NULL);//表示死等
	       if (ret > 0)
		{
                        if( FD_ISSET(fd,&readfd))//判斷fd是否在集合中
                        {
      
                                read(fd, &key_val, 1);
			      printf("key_val = 0x%x\n", key_val);
		      }
	               if( FD_ISSET(0,&readfd))//判斷fd是否在集合中
                        {
      
                                  memset(buf,0,sizeof(buf));
                                  fgets(buf,sizeof(buf),stdin);
			        printf("%s\n",buf);
		       }
			
			
		}
		else
		{
		        perror("poll\n");
		}
	}
	          
    return 0;
}
  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章