你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux设备驱动工程师之路——platform按键驱动

[日期:2011-10-03] 来源:blog.csdn.net/ayangke  作者:衡阳师范学院08电2 Y-Kee [字体:大 中 小]

二、驱动代码

  该驱动实现能够读取按键按下的键值,比如说如果是第一个键按下读取的键值就为1。

  platform平台设备

  1. #include <linux/device.h>  
  2. #include <linux/string.h>  
  3. #include <linux/platform_device.h>  
  4. #include <linux/module.h>  
  5. #include <linux/kernel.h>  
  6. #include <linux/fs.h>  
  7. #include <linux/init.h>  
  8. #include <linux/delay.h>  
  9. #include <linux/poll.h>  
  10. #include <linux/irq.h>  
  11. #include <asm/irq.h>  
  12. #include <linux/interrupt.h>  
  13. #include <asm/uaccess.h>  
  14. #include <mach/regs-gpio.h>  
  15. #include <mach/hardware.h>  
  16. #include <linux/cdev.h>  
  17. #include <linux/miscdevice.h>  
  18. #include <linux/sched.h>  
  19. #include <linux/gpio.h>  
  20.   
  21. static struct resource key_resource[]=  
  22. {     
  23.     [0] = {  
  24.         .start = IRQ_EINT8,  
  25.         .end = IRQ_EINT8,  
  26.         .flags = IORESOURCE_IRQ,  
  27.     },  
  28.     [1] = {  
  29.         .start = IRQ_EINT11,  
  30.         .end = IRQ_EINT11,  
  31.         .flags = IORESOURCE_IRQ,  
  32.     },  
  33.     [2]= {  
  34.         .start = IRQ_EINT13,  
  35.         .end = IRQ_EINT13,  
  36.         .flags = IORESOURCE_IRQ,  
  37.     },  
  38.     [3] = {  
  39.         .start = IRQ_EINT14,  
  40.         .end = IRQ_EINT14,  
  41.         .flags = IORESOURCE_IRQ,  
  42.     },  
  43.     [4] = {  
  44.         .start = IRQ_EINT15,  
  45.         .end = IRQ_EINT15,  
  46.         .flags = IORESOURCE_IRQ,  
  47.     },  
  48.     [5] = {  
  49.         .start = IRQ_EINT19,  
  50.         .end = IRQ_EINT19,  
  51.         .flags = IORESOURCE_IRQ,  
  52.     },  
  53. };  
  54.   
  55. struct platform_device *my_buttons_dev;  
  56.   
  57. static int __init platform_dev_init(void)  
  58. {  
  59.     int ret;  
  60.       
  61.     my_buttons_dev = platform_device_alloc("my_buttons", -1);  
  62.       
  63.     platform_device_add_resources(my_buttons_dev,key_resource,6);//添加资源一定要用该函数,不能使用对platform_device->resource幅值  
  64.                                                                 //否则会导致platform_device_unregister调用失败,内核异常。  
  65.       
  66.     ret = platform_device_add(my_buttons_dev);  
  67.       
  68.     if(ret)  
  69.         platform_device_put(my_buttons_dev);  
  70.       
  71.     return ret;  
  72. }  
  73.   
  74. static void __exit platform_dev_exit(void)  
  75. {  
  76.     platform_device_unregister(my_buttons_dev);  
  77. }  
  78.   
  79. module_init(platform_dev_init);  
  80. module_exit(platform_dev_exit);  
  81.   
  82. MODULE_AUTHOR("Y-Kee");  
  83. MODULE_LICENSE("GPL");   

platform平台驱动

  1. //platform driver  
  2. #include <linux/module.h>  
  3. #include <linux/types.h>  
  4. #include <linux/miscdevice.h>  
  5. #include <linux/fs.h>  
  6. #include <linux/init.h>  
  7. #include <linux/platform_device.h>  
  8. #include <linux/interrupt.h>  
  9. #include <linux/clk.h>  
  10. #include <linux/uaccess.h>  
  11. #include <linux/io.h>  
  12. #include <mach/map.h>  
  13. #include <mach/regs-gpio.h>  
  14. #include <linux/poll.h>  
  15. #include <linux/irq.h>  
  16. #include <asm/unistd.h>  
  17. #include <linux/device.h>  
  18.   
  19.   
  20. static int buttons_irq[6];  
  21.   
  22. struct irq_des  
  23. {  
  24.     int *buttons_irq;  
  25.     char *name[6];  
  26. };  
  27.   
  28.   
  29. struct irq_des button_irqs = {   
  30.     .buttons_irq = buttons_irq,  
  31.     .name = {"KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5"},  
  32. };  
  33.   
  34. static volatile int key_values;  
  35.   
  36.   
  37. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  
  38.   
  39.   
  40. static volatile int ev_press = 0;  
  41.   
  42.   
  43. static irqreturn_t buttons_interrupt(int irq, void *dev_id)  
  44. {  
  45.     int i;  
  46.     for(i=0; i<6; i++){  
  47.         if(irq == buttons_irq[i]){  
  48.             key_values = i;  
  49.             ev_press = 1;  
  50.             wake_up_interruptible(&button_waitq);     
  51.         }  
  52.     }  
  53.       
  54.     return IRQ_RETVAL(IRQ_HANDLED);  
  55. }  
  56.   
  57.   
  58. static int s3c24xx_buttons_open(struct inode *inode, struct file *file)  
  59. {  
  60.     int i;  
  61.     int err = 0;  
  62.       
  63.     for (i = 0; i < 6; i++) {  
  64.         err = request_irq(button_irqs.buttons_irq[i], buttons_interrupt, IRQ_TYPE_EDGE_BOTH,   
  65.                           button_irqs.name[i], (void *)&button_irqs.buttons_irq[i]);  
  66.         if (err)  
  67.             break;  
  68.     }  
  69.   
  70.     if (err) {  
  71.         i--;  
  72.         for (; i >= 0; i--) {  
  73.         if (button_irqs.buttons_irq[i] < 0) {  
  74.         continue;  
  75.         }  
  76.         disable_irq(button_irqs.buttons_irq[i]);  
  77.             free_irq(button_irqs.buttons_irq[i], (void *)&button_irqs.buttons_irq[i]);  
  78.         }  
  79.         return -EBUSY;  
  80.     }  
  81.       
  82.     return 0;  
  83. }  
  84.   
  85.   
  86. static int s3c24xx_buttons_close(struct inode *inode, struct file *file)  
  87. {  
  88.     int i;  
  89.       
  90.     for (i = 0; i < 6; i++) {  
  91.     free_irq(button_irqs.buttons_irq[i], (void *)&button_irqs.buttons_irq[i]);  
  92.     }  
  93.   
  94.     return 0;  
  95. }  
  96.   
  97.   
  98. static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)  
  99. {  
  100.     unsigned long err;  
  101.   
  102.     if (!ev_press) {  
  103.     if (filp->f_flags & O_NONBLOCK)  
  104.         return -EAGAIN;  
  105.     else  
  106.         wait_event_interruptible(button_waitq, ev_press);  
  107.     }  
  108.       
  109.     ev_press = 0;  
  110.   
  111.     err = copy_to_user(buff, (const void *)&key_values, min(sizeof(key_values), count));  
  112.   
  113.     return err ? -EFAULT : min(sizeof(key_values), count);  
  114. }  
  115.   
  116. static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)  
  117. {  
  118.     unsigned int mask = 0;  
  119.     poll_wait(file, &button_waitq, wait);  
  120.     if (ev_press)  
  121.         mask |= POLLIN | POLLRDNORM;  
  122.     return mask;  
  123. }  
  124.   
  125.   
  126. static struct file_operations dev_fops = {  
  127.     .owner   =   THIS_MODULE,  
  128.     .open    =   s3c24xx_buttons_open,  
  129.     .release =   s3c24xx_buttons_close,   
  130.     .read    =   s3c24xx_buttons_read,  
  131.     .poll    =   s3c24xx_buttons_poll,  
  132. };  
  133.   
  134. static struct miscdevice misc = {  
  135.     .minor = MISC_DYNAMIC_MINOR,  
  136.     .name = "my_buttons",  
  137.     .fops = &dev_fops,  
  138. };  
  139.   
  140.   
  141. static int my_plat_probe(struct platform_device *dev)  
  142. {  
  143.     int ret,i;  
  144.     struct resource *plat_resource;  
  145.     struct platform_device *pdev = dev;  
  146.       
  147.     printk("my platform dirver find my platfrom device.\n");  
  148.   
  149.     for(i=0; i<6; i++){  
  150.         plat_resource = platform_get_resource(pdev,IORESOURCE_IRQ,i);  
  151.         if(plat_resource == NULL)  
  152.             return -ENOENT;   
  153.         buttons_irq[i] = plat_resource->start;  
  154.     }  
  155.   
  156.     ret = misc_register(&misc);  
  157.     if(ret)  
  158.         return ret;  
  159.   
  160.       
  161.     return 0;  
  162. }  
  163.   
  164. static int my_plat_remove(struct platform_device *dev)  
  165. {  
  166.     printk("my platfrom device has removed.\n");  
  167.     misc_deregister(&misc);  
  168.     return 0;  
  169. }  
  170.   
  171. struct platform_driver my_buttons_drv = {   
  172.     .probe = my_plat_probe,  
  173.     .remove = my_plat_remove,  
  174.     .driver = {   
  175.         .owner = THIS_MODULE,  
  176.         .name = "my_buttons",  
  177.     },  
  178. };  
  179.   
  180. static int __init platform_drv_init(void)  
  181. {  
  182.     int ret;  
  183.   
  184.     ret = platform_driver_register(&my_buttons_drv);  
  185.       
  186.     return ret;  
  187. }  
  188.   
  189. static void __exit platform_drv_exit(void)  
  190. {  
  191.     platform_driver_unregister(&my_buttons_drv);  
  192. }  
  193.   
  194. module_init(platform_drv_init);  
  195. module_exit(platform_drv_exit);  
  196.   
  197. MODULE_AUTHOR("Y-Kee");  
  198. MODULE_LICENSE("GPL");  

测试代码

  1. /*  
  2.  *      Buttons Example for Matrix V  
  3.  *  
  4.  *      Copyright (C) 2004 capbily - friendly-arm  
  5.  *  capbily@hotmail.com  
  6.  */  
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <unistd.h>  
  10. #include <sys/ioctl.h>  
  11. #include <sys/types.h>  
  12. #include <sys/stat.h>  
  13. #include <fcntl.h>  
  14. #include <sys/select.h>  
  15. #include <sys/time.h>  
  16. #include <errno.h>  
  17.   
  18. int main(void)  
  19. {  
  20.     int buttons_fd;  
  21.     int key_value;  
  22.   
  23.     buttons_fd = open("/dev/buttons", 0);  
  24.     if (buttons_fd < 0) {  
  25.         perror("open device buttons");  
  26.         exit(1);  
  27.     }  
  28.   
  29.     for (;;) {  
  30.         fd_set rds;  
  31.         int ret;  
  32.   
  33.         FD_ZERO(&rds);  
  34.         FD_SET(buttons_fd, &rds);  
  35.   
  36.         ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);  
  37.         if (ret < 0) {  
  38.             perror("select");  
  39.             exit(1);  
  40.         }  
  41.         if (ret == 0) {  
  42.             printf("Timeout.\n");  
  43.         } else if (FD_ISSET(buttons_fd, &rds)) {  
  44.             int ret = read(buttons_fd, &key_value, sizeof key_value);  
  45.             if (ret != sizeof key_value) {  
  46.                 if (errno != EAGAIN)  
  47.                     perror("read buttons\n");  
  48.                 continue;  
  49.             } else {  
  50.                 printf("buttons_value: %d\n", key_value+1);  
  51.             }  
  52.                   
  53.         }  
  54.     }  
  55.   
  56.     close(buttons_fd);  
  57.     return 0;  
  58. }  

测试结果:

运行测试程序后按下第二个键,中断上打印了多次按键的键值,产生原因是因为按键抖动。导致按一下按键,产生多次中断。
linux
相关资讯       Linux驱动 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款