异步通知的按键驱动程序

来源:互联网 发布:matlab mac 破解 编辑:程序博客网 时间:2024/05/17 05:18
 所谓异步就是驱动程序去主动通知应用程序去执行,在以前写的按键驱动程序里面都是应用程序主动去读取按键值,而异步通知的程序应用程序一直在休眠,直到有按键按下的时候驱动程序给应用程序发信号告诉应用程序去读取按键值。

   下面是驱动程序signal_button_drive_.c   其实也只是在上一个按键中断程序上加了一些东西

#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <asm/uaccess.h>#include <asm/irq.h>#include <asm/io.h>#include <mach/gpio.h>#include <linux/device.h>#include <linux/interrupt.h>#include <linux/wait.h>#include <linux/sched.h>#include <linux/poll.h>//#include <asm/arch/regs-gpio.h>//#include <asm/hardware.h>static struct fasync_struct * button_async;static struct class *signaldrv_class;static struct class_device*signaldrv_class_dev;static unsigned char keyval;static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press = 0;struct pin_desc{unsigned int pin;unsigned int key_val;};/* 按键值按下时0x01,0x02,0x03,0x04,0x05,0x06松开时                0x81,0x82,0x83,0x84,0x85,0x86*/struct pin_desc pins_desc[6] = {{S3C64XX_GPN(0), 0x01},{S3C64XX_GPN(1), 0x02},{S3C64XX_GPN(2), 0x03},{S3C64XX_GPN(3), 0x04},{S3C64XX_GPN(4), 0x05},{S3C64XX_GPN(5), 0x06},};static intsignal_drv_fasync(int fd, struct file *filep, int mode){printk(KERN_ERR"call signal_drv_fasync function\n");return fasync_helper(fd, filep, mode, &button_async);}static irqreturn_t botton_irq(int irq, void *dev_id){struct pin_desc * pindes = (struct pin_desc *)dev_id;unsigned int pinval;pinval = gpio_get_value(pindes->pin);if (pinval){/* 松开的 */keyval = 0x80 | pindes->key_val;}else {/* 按下的 */keyval = pindes->key_val;}//printk(KERN_ERR "irq = %d\n", irq);ev_press = 1;wake_up_interruptible(&button_waitq);kill_fasync(&button_async, SIGIO, POLL_IN);return IRQ_HANDLED;}static int signal_drv_poll(struct file *file, poll_table *wait){unsigned int mask = 0;poll_wait(file, &button_waitq, wait);if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;}static int signal_drv_open(struct inode *inode, struct file *file){//printk("signal_drv_open\n");//request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char * name,void * dev)request_irq(IRQ_EINT(0), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s2", &pins_desc[0]);request_irq(IRQ_EINT(1), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s3", &pins_desc[1]);request_irq(IRQ_EINT(2), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s4", &pins_desc[2]);request_irq(IRQ_EINT(3), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s5", &pins_desc[3]);request_irq(IRQ_EINT(4), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s6", &pins_desc[4]);request_irq(IRQ_EINT(5), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s7", &pins_desc[5]);return 0;}static ssize_t signal_drv_read(struct file *file, const char __user *buf, size_t size, loff_t * ppos){if(size != 1)return -EINVAL;/* 如果没有按键动作发生就休眠 */wait_event_interruptible(button_waitq, ev_press);/* 如果有按键动作发生就返回 *//* 传递给用户 */copy_to_user(buf, &keyval, 1);ev_press = 0;return 1;}int signal_drv_close(struct inode *inode, struct file *file){free_irq(IRQ_EINT(0), &pins_desc[0]);free_irq(IRQ_EINT(1), &pins_desc[1]);free_irq(IRQ_EINT(2), &pins_desc[2]);free_irq(IRQ_EINT(3), &pins_desc[3]);free_irq(IRQ_EINT(4), &pins_desc[4]);free_irq(IRQ_EINT(5), &pins_desc[5]);return 0;}static struct file_operations signal_drv_fops = {    .owner   =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */    .open    =   signal_drv_open,     .read = signal_drv_read, .release =   signal_drv_close,.poll    =   signal_drv_poll,.fasync  =   signal_drv_fasync,};int major;static int signal_drv_init(void){//printk(KERN_ERR "signal_drv_init\n");major = register_chrdev(0, "signalt_drv", &signal_drv_fops); // 注册, 告诉内核signaldrv_class = class_create(THIS_MODULE, "signaldrv");signaldrv_class_dev = device_create(signaldrv_class, NULL, MKDEV(major, 0), NULL, "bottons"); /* /dev/bottons */return 0;}static void signal_drv_exit(void){//printk(KERN_ERR "signal_drv_exit\n");unregister_chrdev(major, "signalt_drv"); // 卸载device_unregister(signaldrv_class_dev);class_destroy(signaldrv_class);}module_init(signal_drv_init);module_exit(signal_drv_exit);MODULE_LICENSE("GPL"); 

下面是测试程序 signal_botton_test.c  主程序一直在休眠

 #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <poll.h>#include <signal.h>#include <sys/types.h>#include <unistd.h>/* poll_irq_botton test  *   */int fd;void my_signal_fun(int signum){unsigned char key_val;read(fd, &key_val, 1);printf("key_val = 0x%x\n", key_val);}int main(int argc, char **argv){int Oflags;signal(SIGIO, my_signal_fun); fd = open("/dev/bottons", O_RDWR);if (fd < 0){printf("can't open!\n");}fcntl(fd, F_SETOWN, getpid());Oflags = fcntl(fd, F_GETFL);fcntl(fd, F_SETFL, Oflags | FASYNC);while (1){sleep(1000);}return 0;}