tq210 按键驱动

来源:互联网 发布:网络言论自由作文800字 编辑:程序博客网 时间:2024/04/30 03:49

经过前面的配置,S5PV210开发已经可以成功进入Linux控制台了,那么,有了这个环境就可以开始学习Linux驱动的编写和测试了。学习Linux设备驱动,通常是从字符设备驱动开始。我写的第一个驱动程序是Led的,其实也就是熟悉下字符设备驱动的基本结构,本文以中断方式的按键驱动为例,简单的介绍下字符设备驱动程序。

一 按键驱动程序的简单实现

下面是基于中断和消息的按键驱动程序,其工作原理是:当应用程序读取键值时,会调用按键驱动程序的read函数,而我们实现的read函数检测完读取长度后没有直接读取键值而是等待按键消息,如果没有按键,程序会进入休眠状态,这样可以节省大量的CPU,而当我们按键时硬件会产生中断,程序自动进入中断处理函数,在中断处理函数中,驱动程序读取键值存入全局变量并激活read函数中等待的消息,应用程序被迅速唤醒并通过read函数读取键值,如此,完成了获取键值的工作。下面是源码,比较简单,也就不多说了。

源码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <linux/types.h>  
  2. #include <linux/module.h>  
  3. #include <linux/cdev.h>  
  4. #include <linux/fs.h>  
  5. #include <linux/device.h>  
  6. #include <linux/gpio.h>  
  7. #include <linux/irq.h>  
  8. #include <linux/interrupt.h>  
  9. #include <linux/sched.h>   
  10. #include <linux/wait.h>  
  11. #include <linux/uaccess.h>  
  12.   
  13. static dev_t devno;  
  14. static struct cdev cdev;  
  15. static struct class* buttons_class;  
  16. static struct device* buttons_device;  
  17.   
  18. static wait_queue_head_t button_waitq;  
  19.   
  20. static volatile int pressed = 0;  
  21. static unsigned char key_val;  
  22.   
  23. struct key_desc{  
  24.     unsigned int  pin;  
  25.     unsigned char value;  
  26. };  
  27.   
  28. static struct key_desc key_descs[8] = {  
  29.     [0] = {  
  30.         .pin = S5PV210_GPH0(0),  
  31.         .value = 0x00,  
  32.     },  
  33.   
  34.     [1] = {  
  35.         .pin = S5PV210_GPH0(1),  
  36.         .value = 0x01,  
  37.     },  
  38.   
  39.     [2] = {  
  40.         .pin = S5PV210_GPH0(2),  
  41.         .value = 0x02,  
  42.     },  
  43.   
  44.     [3] = {  
  45.         .pin = S5PV210_GPH0(3),  
  46.         .value = 0x03,  
  47.     },  
  48.   
  49.     [4] = {  
  50.         .pin = S5PV210_GPH0(4),  
  51.         .value = 0x04,  
  52.     },  
  53.   
  54.     [5] = {  
  55.         .pin = S5PV210_GPH0(5),  
  56.         .value = 0x05,  
  57.     },  
  58.   
  59.     [6] = {  
  60.         .pin = S5PV210_GPH2(6),  
  61.         .value = 0x06,  
  62.     },  
  63.   
  64.     [7] = {  
  65.         .pin = S5PV210_GPH2(7),  
  66.         .value = 0x07,  
  67.     },  
  68. };  
  69.   
  70. static irqreturn_t buttons_irq(int irq, void *dev_id){  
  71.     volatile struct key_desc *key = (volatile struct key_desc *)dev_id;  
  72.   
  73.     if(gpio_get_value(key->pin)){  
  74.         key_val = key->value|0x80;  
  75.     }  
  76.     else{  
  77.         key_val = key->value;  
  78.     }  
  79.   
  80.     pressed = 1;  
  81.     wake_up_interruptible(&button_waitq);  
  82.   
  83.     return IRQ_RETVAL(IRQ_HANDLED);  
  84. }  
  85.   
  86. static int buttons_open(struct inode *inode, struct file *file){  
  87.     int ret;  
  88.   
  89.     ret = request_irq(IRQ_EINT(0),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key1", &key_descs[0]);  
  90.     if(ret)  
  91.         return ret;  
  92.     ret = request_irq(IRQ_EINT(1),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key2", &key_descs[1]);  
  93.     if(ret)  
  94.         return ret;  
  95.     ret = request_irq(IRQ_EINT(2),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key3", &key_descs[2]);  
  96.     if(ret)  
  97.         return ret;  
  98.     ret = request_irq(IRQ_EINT(3),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key4", &key_descs[3]);  
  99.     if(ret)  
  100.         return ret;  
  101.     ret = request_irq(IRQ_EINT(4),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key5", &key_descs[4]);  
  102.     if(ret)  
  103.         return ret;  
  104.     ret = request_irq(IRQ_EINT(5),   buttons_irq, IRQ_TYPE_EDGE_BOTH, "key6", &key_descs[5]);  
  105.     if(ret)  
  106.         return ret;  
  107.     ret = request_irq(IRQ_EINT(22),  buttons_irq, IRQ_TYPE_EDGE_BOTH, "key7", &key_descs[6]);  
  108.     if(ret)  
  109.         return ret;  
  110.     ret = request_irq(IRQ_EINT(23),  buttons_irq, IRQ_TYPE_EDGE_BOTH, "key8", &key_descs[7]);  
  111.     if(ret)  
  112.         return ret;  
  113.     return 0;  
  114. }  
  115.   
  116. static ssize_t buttons_read(struct file * file, char __user *data, size_t count, loff_t *loff){  
  117.     if(count != 1){  
  118.         printk(KERN_ERR "The driver can only give one key value once!\n");  
  119.         return -ENOMEM;  
  120.     }  
  121.   
  122.     wait_event_interruptible(button_waitq, pressed);  
  123.     pressed = 0;  
  124.   
  125.     if(copy_to_user(data, &key_val, 1)){  
  126.         printk(KERN_ERR "The driver can not copy the data to user area!\n");  
  127.         return -ENOMEM;  
  128.     }  
  129.       
  130.     return 0;  
  131. }  
  132.   
  133. static int buttons_close(struct inode *inode, struct file *file){  
  134.     free_irq(IRQ_EINT(0),  &key_descs[0]);  
  135.     free_irq(IRQ_EINT(1),  &key_descs[1]);    
  136.     free_irq(IRQ_EINT(2),  &key_descs[2]);  
  137.     free_irq(IRQ_EINT(3),  &key_descs[3]);  
  138.     free_irq(IRQ_EINT(4),  &key_descs[4]);  
  139.     free_irq(IRQ_EINT(5),  &key_descs[5]);  
  140.     free_irq(IRQ_EINT(22), &key_descs[6]);  
  141.     free_irq(IRQ_EINT(23), &key_descs[7]);  
  142.     return 0;  
  143. }  
  144.   
  145. struct file_operations buttons_ops = {  
  146.     .open    = buttons_open,  
  147.     .read    = buttons_read,  
  148.     .release = buttons_close,  
  149. };  
  150.   
  151. int buttons_init(void){  
  152.     int ret;  
  153.   
  154.     cdev_init(&cdev, &buttons_ops);  
  155.     cdev.owner = THIS_MODULE;  
  156.   
  157.     ret = alloc_chrdev_region(&devno, 0, 1, "buttons");  
  158.     if(ret){  
  159.         printk(KERN_ERR "alloc char device region faild!\n");  
  160.         return ret;  
  161.     }  
  162.   
  163.     ret = cdev_add(&cdev, devno, 1);  
  164.     if(ret){  
  165.         printk(KERN_ERR "add char device faild!\n");  
  166.         goto add_error;  
  167.     }  
  168.   
  169.     buttons_class = class_create(THIS_MODULE, "buttonsdrv");  
  170.     if(IS_ERR(buttons_class)){  
  171.         printk(KERN_ERR "create class error!\n");  
  172.         goto class_error;  
  173.     }  
  174.   
  175.     buttons_device = device_create(buttons_class, NULL, devno, NULL, "buttons");  
  176.     if(IS_ERR(buttons_device)){  
  177.         printk(KERN_ERR "create buttons device error!\n");  
  178.         goto device_error;  
  179.     }  
  180.   
  181.     init_waitqueue_head(&button_waitq);  
  182.   
  183.     return 0;  
  184.   
  185. device_error:  
  186.     class_destroy(buttons_class);  
  187. class_error:  
  188.     cdev_del(&cdev);  
  189. add_error:  
  190.     unregister_chrdev_region(devno,1);  
  191.   
  192.     return -ENODEV;  
  193. }  
  194.   
  195. void buttons_exit(void){  
  196.     device_destroy(buttons_class, devno);  
  197.     class_destroy(buttons_class);  
  198.     cdev_del(&cdev);  
  199.     unregister_chrdev_region(devno, 1);  
  200. }  
  201.   
  202. module_init(buttons_init);  
  203. module_exit(buttons_exit);  
  204. MODULE_LICENSE("GPL");  

测试程序代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3.   
  4. int main(){  
  5.     int fd = open("/dev/buttons", O_RDWR);  
  6.     if(fd < 0){  
  7.         printf("open error");;  
  8.         return 0;  
  9.     }  
  10.   
  11.     unsigned char key;  
  12.     while(1){  
  13.         read(fd, &key, 1);  
  14.         printf("The key = %x\n", key);  
  15.     }  
  16.   
  17.     close(fd);  
  18. }  

相比轮询方式的按键驱动程序,中断方式编写的按键驱动程序可以很大程度上节省CPU资源,因此,推荐使用中断方式。

二 支持POLL机制

上面这种方式实现的按键驱动程序有个弊端,如果我们不按键,应用程序将会永远阻塞在这里,幸运的是,linux内核提供了poll机制,可以设置超时等待时间,如果在这个时间内读取到键值则正常返回,反之则超时退出。使内核支持poll非常简单,为file_operations的poll成员提供poll处理函数即可。

使内核支持poll还需要以下几步:

添加poll头文件

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <linux/poll.h>  

编写poll处理函数:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static unsigned buttons_poll(struct file *file, poll_table *wait){  
  2.     unsigned int mask = 0;  
  3.     poll_wait(file, &button_waitq, wait);  
  4.   
  5.     if (pressed)  
  6.         mask |= POLLIN | POLLRDNORM;  
  7.   
  8.     return mask;  
  9. }  
将poll处理函数添加给file_operations:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. .poll    = buttons_poll,  
这样,驱动程序就支持poll机制了。下面是poll方式的测试程序:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <stdio.h>  
  5. #include <poll.h>  
  6.   
  7. int main(int argc, char **argv){  
  8.     int fd;  
  9.     unsigned char key_val;  
  10.     int ret;  
  11.   
  12.     struct pollfd fds[1];  
  13.       
  14.     fd = open("/dev/buttons", O_RDWR);  
  15.     if (fd < 0){  
  16.         printf("can't open!\n");  
  17.     }  
  18.   
  19.     fds[0].fd     = fd;  
  20.     fds[0].events = POLLIN;  
  21.     while (1){  
  22.         ret = poll(fds, 1, 5000);  
  23.         if (ret == 0){  
  24.             printf("time out\n");  
  25.         }  
  26.         else{  
  27.             read(fd, &key_val, 1);  
  28.             printf("key_val = 0x%x\n", key_val);  
  29.         }  
  30.     }  
  31.       
  32.     return 0;  
  33. }  

这样,应用程序可以限制时间,如果在一定时间内读取不到键值就可以做特殊处理,这种思想在网络通信中应用广泛。

三 支持异步机制

很多情况下,我们的程序在等待按键期间需要处理其它任务而不是在这里空等,这时,就需要采用异步模式了。所谓异步模式,实际上是采用消息机制(以本文的按键程序为例),即当驱动程序检测到按键后发送消息给应用程序,应用程序接收到消息后再去读取键值。与前面的两种模式相比,最大的不同在于异步方式是驱动告诉应用程序来读而不是应用程序主动去读。添加异步支持更加简单,首先是为file_operations注册fasync函数,函数内容如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static int buttons_fasync(int fd, struct file * file, int on){  
  2.     return fasync_helper(fd, file, on,  &button_async);  
  3. }  
然后再buttons_read函数中添加一行代码,修改后的代码如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. static ssize_t buttons_read(struct file * file, char __user *data, size_t count, loff_t *loff){  
  2.     if(count != 1){  
  3.         printk(KERN_ERR "The driver can only give one key value once!\n");  
  4.         return -ENOMEM;  
  5.     }  
  6.   
  7.     wait_event_interruptible(button_waitq, pressed);  
  8.     pressed = 0;  
  9.   
  10.     if(copy_to_user(data, &key_val, 1)){  
  11.         printk(KERN_ERR "The driver can not copy the data to user area!\n");  
  12.         return -ENOMEM;  
  13.     }  
  14.   
  15.     return 0;  
  16. }  
这样,驱动程序就支持异步获取键值了,为了测试效果,测试程序也需要修改,代码如下:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <fcntl.h>  
  2. #include <stdio.h>  
  3. #include <poll.h>  
  4. #include <signal.h>  
  5. #include <sys/types.h>  
  6. #include <unistd.h>  
  7. #include <fcntl.h>  
  8.   
  9.   
  10. /* sixthdrvtest  
  11.  */  
  12. int fd;   
  13.   
  14. void my_signal_fun(int signum)  
  15. {  
  16.     unsigned char key_val;  
  17.     read(fd, &key_val, 1);   
  18.     printf("key_val: 0x%x\n", key_val);  
  19. }  
  20.   
  21. int main(int argc, char **argv)  
  22. {  
  23.     unsigned char key_val;  
  24.     int ret;  
  25.     int Oflags;  
  26.   
  27.     signal(SIGIO, my_signal_fun);  
  28.   
  29.     fd = open("/dev/buttons", O_RDWR | O_NONBLOCK);  
  30.     if (fd < 0){   
  31.         printf("can't open!\n");  
  32.         return -1;   
  33.     }     
  34.   
  35.     fcntl(fd, F_SETOWN, getpid());  
  36.     Oflags = fcntl(fd, F_GETFL);  
  37.     fcntl(fd, F_SETFL, Oflags | FASYNC);  
  38.   
  39.   
  40.     int rest;  
  41.     while (1){  
  42.         printf("Hello\n");  
  43.         while(rest = sleep(50)){  
  44.             sleep(rest);  
  45.         }  
  46.     }  
  47.   
  48.     return 0;  
  49. }  
这里需要注意的是,应用程序接收到消息会打断sleep,比如执行sleep(5)之后程序接收到了一个消息,这时,应用程序就被唤醒了,虽然是去执行的消息处理函数。如果程序接收到消息时仅睡眠了2秒,那么sleep被中断时会返回5-2=3,所以代码中采用while循环方式进行sleep,这样,即使接收到了消息也能完整的休眠5秒,当然,sleep函数本身是不够精确的,不过相差无几。

到这里,这个驱动程序基本上就算可以了,当然,还有对阻塞和非阻塞的支持,同步与互斥的支持,而阻塞与非阻塞无非是加上个逻辑判断,同步与互斥根应用程序的同步控制也差不多,无非就是信号量或者原子操作,这里就不多说了,如果有朋友需要这些内容可以留言讨论。

0 0
原创粉丝点击