6410poll机制

转:http://blog.csdn.net/longshan_2009/article/details/9981975

poll机制也是一种查询的机制,它可以等待一个时间段,比如按键,如果在这个时间段之后,按键没有按下的话,poll机制就会让应用程序处于休眠状态。

我们可以在指定的时间内查询是否有按键按下。poll机制所耗费的资源也比较低。超时时间又应用程序poll函数决定。

关于poll机制的具体分析可以参考韦东山分析文档

示例如下:

[cpp]  view plain copy
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <asm/uaccess.h>  
  6. #include <linux/device.h>  
  7. #include <asm/io.h>  
  8. #include <linux/interrupt.h>  
  9. #include <mach/regs-gpio.h>  
  10. #include <linux/gpio.h>  
  11. #include <linux/poll.h>  
  12.   
  13.   
  14. struct class *buttons_class;  
  15. struct device *buttons_dev;  
  16.   
  17.   
  18. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//定义休眠队列  
  19. static volatile int ev_press = 0; //初始值为0,处于休眠状态  
  20.   
  21. static unsigned char key_val;  
  22.   
  23. struct pin_desc{  
  24.     unsigned int pin;  
  25.     unsigned int key_val;  
  26. };  
  27.   
  28. struct pin_desc pins_desc[4] = {  
  29.     {S3C64XX_GPN(0),0x01},  
  30.     {S3C64XX_GPN(1),0x02},  
  31.     {S3C64XX_GPN(2),0x03},  
  32.     {S3C64XX_GPN(3),0x04},  
  33. };  
  34.   
  35.   
  36.   
  37. static irqreturn_t button_irq_handle(int irq, void *devid)  
  38. {  
  39.     printk("irq = %d\n",irq);  
  40.     struct pin_desc * pindesc = (struct pin_desc *)devid;  
  41.     unsigned int pinval;  
  42.   
  43.     pinval = gpio_get_value(pindesc->pin);  
  44.   
  45.     if (pinval)  
  46.     {  
  47.         key_val = 0x80 | pindesc->key_val;  
  48.         printk("songkai\n");  
  49.     }  
  50.     else  
  51.     {  
  52.         key_val = pindesc->key_val;  
  53.         printk("anxia\n");  
  54.     }  
  55.   
  56.     ev_press = 1;   //唤醒                
  57.     wake_up_interruptible(&button_waitq);    
  58.     return IRQ_HANDLED;  
  59. }  
  60.   
  61.   
  62. static int buttons_drv_open(struct inode *inode, struct file *file)  
  63. {  
  64.     printk("buttons_drv_open \n");  
  65.   
  66.     request_irq(IRQ_EINT(0),button_irq_handle,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "PAD1", &pins_desc[0]);  
  67.     request_irq(IRQ_EINT(1),button_irq_handle,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "PAD2", &pins_desc[1]);  
  68.     request_irq(IRQ_EINT(2),button_irq_handle,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "PAD3", &pins_desc[2]);  
  69.     request_irq(IRQ_EINT(3),button_irq_handle,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "PAD4", &pins_desc[3]);  
  70.   
  71.     return 0;  
  72. }  
  73.   
  74. static ssize_t buttons_drv_read( struct file *file,  
  75.               char __user *buffer,  
  76.               size_t len,  
  77.               loff_t *offset )  
  78. {  
  79.     if (len != 1)  
  80.         return -EINVAL;  
  81.       
  82.     wait_event_interruptible(button_waitq, ev_press);//进入睡眠  
  83.   
  84.     ev_press = 0;  
  85.     copy_to_user(buffer, &key_val, 1);  
  86.   
  87.   
  88.     return 1;  
  89. }  
  90.   
  91. int buttons_drv_release(struct inode *inode, struct file *file)  
  92. {  
  93.     free_irq(IRQ_EINT(0),&pins_desc[0]);  
  94.     free_irq(IRQ_EINT(1),&pins_desc[1]);  
  95.     free_irq(IRQ_EINT(2),&pins_desc[2]);  
  96.     free_irq(IRQ_EINT(3),&pins_desc[3]);  
  97.   
  98.     return 0;  
  99. }  
  100.   
  101. static unsigned int buttons_drv_poll(struct file *file, poll_table *wait)  
  102. {  
  103.     unsigned int mask = 0;  
  104.   
  105.     poll_wait(file, &button_waitq, wait);//到这里还没有进入休眠  
  106.   
  107.     if (ev_press)  
  108.         mask |= POLLIN | POLLWRNORM;//mask是返回应用程序poll的返回值,mask一定要和应用程序中的events相对应,不然还会进行超时  
  109.   
  110.     return mask;  
  111. }  
  112.   
  113.   
  114. static struct file_operations buttons_fops = {  
  115.     .owner   =   THIS_MODULE,      
  116.     .open    =   buttons_drv_open,  
  117.     .read    =   buttons_drv_read,  
  118.     .release =   buttons_drv_release,  
  119.     .poll    =   buttons_drv_poll,  
  120. };  
  121.   
  122.   
  123. int major;  
  124. static int __init buttons_drv_init(void)  
  125. {  
  126.     printk("buttons_drv_init \n");  
  127.     major = register_chrdev(0,"buttons",&buttons_fops);  
  128.     buttons_class = class_create(THIS_MODULE,"buttons");  
  129.     buttons_dev = device_create(buttons_class,NULL,MKDEV(major,0),NULL,"buttons");  
  130.       
  131.     return 0;  
  132. }  
  133.   
  134. static void __exit buttons_drv_exit(void)  
  135. {  
  136.     device_destroy(buttons_class,MKDEV(major,0));  
  137.     class_destroy(buttons_class);  
  138.     unregister_chrdev(major,"buttons");  
  139. }  
  140.   
  141. module_init(buttons_drv_init);  
  142. module_exit(buttons_drv_exit);  
  143.   
  144. MODULE_LICENSE("GPL");   

应用测试程序:

[cpp]  view plain copy
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <stdio.h>  
  5. #include <poll.h>  
  6.   
  7.   
  8. /* thirddrvtest  
  9.  
  10.   */  
  11. int main(int argc, char **argv)  
  12. {  
  13.     int fd;  
  14.     unsigned char key_val;  
  15.     int ret;  
  16.     static int time = 5000;  
  17.       
  18.     struct pollfd fds[1];  
  19.       
  20.     fd = open("/dev/buttons", O_RDWR);  
  21.     if (fd < 0)  
  22.     {  
  23.         printf("can't open!\n");  
  24.     }  
  25.   
  26.     fds[0].fd = fd;  
  27.     fds[0].events = POLLIN;  
  28.       
  29.       
  30.     while (1)  
  31.     {         
  32.         ret = poll(fds,1,time);//poll函数的使用可以man poll来查看  
  33.         if(ret == 0){  
  34.             printf("Time out !!!\n");  
  35.         }else{  
  36.             read(fd, &key_val, 1);  
  37.             printf("key_val = 0x%x\n", key_val);  
  38.         }  
  39.     }  
  40.     return 0;  
  41. }  

使用poll函数也可以对CPU资源的使用很低,如下所示:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值