I/O模型之多路复用epoll(在中断基础上------jz2440)

epoll的使用(现在常用的是epoll)

功能:创建epoll的实例

int epoll_create(int size);

参数:

      @size  :无效了

       返回值:成功返回epoll实例的文件描述符,失败-1;

 

//功能:向epoll实例中添加文件描述符,或者从中删除文件描述符

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);

参数:

        @ epfd :epoll实例的文件描述符

        @ op   :EPOLL_CTL_ADD  添加文件描述符

                    EPOLL_CTL_MOD  更正对文件描述符监听的事件

                    EPOLL_CTL_DEL  删除文件描述符

        @ fd   :想要添加的文件描述符

        @event :epoll_event

         typedef union epoll_data {

               int          fd;

         } epoll_data_t;

         struct epoll_event {

               uint32_t     events;    //监听的事件的类型EPOLLIN读/EPOLLOUT写

               epoll_data_t data;      //fd

           };

EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭);

EPOLLOUT:表示对应的文件描述符可以写;

EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);

EPOLLERR:表示对应的文件描述符发生错误;

EPOLLHUP:表示对应的文件描述符被挂断;

EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。

EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里

        返回值:成功返回0,失败返回-1

 

 

//功能:阻塞等待文件描述符对应驱动的数据是否准备好    //死等    

int epoll_wait(int epfd, struct epoll_event *events,int maxevents, int timeout);

参数:    

          @epfd        :e poll实例的文件描述符

          @events     :被返回的epoll_event

          @maxevents  :监听文件描述符的最大值

          @timeout    :超时(-1表示或略超时时间)

          返回值:>0  :返回文件描述符的个数

                       =0  :超时

                        -1  :失败

注意:支持管道,FIFO,套接字,POSIX消息队列,终端,设备等,但是就是不支持普通文件或目录的

#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 <sys/epoll.h>
#include<stdio.h>
#include <sys/time.h>
#include<poll.h>
char buf[100]={0};
int  main(int argc,char  *argv[])
{

       int fd,epfd;
      int ret;
      struct epoll_event   event;
     struct epoll_event    revent[3];
       unsigned int key_val;
       epfd=epoll_create(10);//创建epoll
       if(epfd==-1)
        {
                perror("epoll_create\n");
       }
       fd=open("/dev/led1", O_RDWR);//后续的I/O操作都是非阻塞的方式 
       
       event.data.fd=fd;
       event.events=EPOLLIN;//读事件
       epoll_ctl(epfd,  EPOLL_CTL_ADD, fd, &event);//把事件添加到集合中
 
	while (1)
	{     
	       ret=epoll_wait( epfd, revent,3,-1);//死等
	       if (ret > 0)
		{
                        if(revent[0].events & EPOLLIN)
		       {
                               read(fd, &key_val, 1);
			     printf("key_val = 0x%x\n", key_val);
			}
		}
		else
		{
		        perror("poll\n");
		}
	}
	          
    return 0;
}
  

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值