按键驱动程序设计笔记

来源:互联网 发布:淘宝手机端主图大小 编辑:程序博客网 时间:2024/05/18 04:00

/*本来友善的程序是检测上升沿和下降沿的,当按下按键,输出 KEY? down当弹下按键,输出 KEY? up 现在修改成只检测下降沿按下的*/#include <linux/module.h>/*模块有关的*/#include <linux/kernel.h>/*内核有关的*/#include <linux/fs.h>/*文件系统有关的*/#include <linux/init.h>/*init*/#include <linux/delay.h> /*delay*/#include <linux/poll.h>/*poll*/#include <linux/irq.h>/*中断*/#include <asm/irq.h>/*中断*/#include <linux/interrupt.h>/*linux中断*/#include <asm/uaccess.h>/*uaccess*/#include <mach/regs-gpio.h> /*寄存器设置*/#include <mach/hardware.h>/*hardware*/#include <linux/platform_device.h>#include <linux/cdev.h>#include <linux/miscdevice.h>#include <linux/sched.h>#include <linux/gpio.h>#define DEVICE_NAME "button"struct button_irq_desc { int irq;/*中断号*/ int pin;/*中断控制的寄存器*/ int pin_setting; /*中断的引脚*/ int number;/*按键编号*/ char *name;/*按键名称*/};static struct button_irq_desc button_irqs [] = { {IRQ_EINT8 , S3C2410_GPG(0) , S3C2410_GPG0_EINT8 , 0, "KEY0"}, {IRQ_EINT11, S3C2410_GPG(3) , S3C2410_GPG3_EINT11 , 1, "KEY1"}, {IRQ_EINT13, S3C2410_GPG(5) , S3C2410_GPG5_EINT13 , 2, "KEY2"}, {IRQ_EINT14, S3C2410_GPG(6) , S3C2410_GPG6_EINT14 , 3, "KEY3"}, {IRQ_EINT15, S3C2410_GPG(7) , S3C2410_GPG7_EINT15 , 4, "KEY4"}, {IRQ_EINT19, S3C2410_GPG(11), S3C2410_GPG11_EINT19, 5, "KEY5"},};/*存放按键操作结果*/static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};/*创建一个等待队列*/static DECLARE_WAIT_QUEUE_HEAD(button_waitq);/*按键标志位*/static volatile int ev_press = 0;static irqreturn_t buttons_interrupt(int irq, void *dev_id){/*这里注意*/ struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id; if ('0'== key_values[button_irqs->number]) { // Changed key_values[button_irqs->number] = '1'; }else if('1' == key_values[button_irqs->number]){key_values[button_irqs->number] = '0';}ev_press = 1; wake_up_interruptible(&button_waitq);//有数据可读,唤醒读进程 return IRQ_RETVAL(IRQ_HANDLED);}static int s3c24xx_buttons_open(struct inode *inode, struct file *file){ int i; int err = 0; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0){continue;}/*int request_irq(unsigned int irq, void (*handler)(int, void*, struct pt_regs *), unsigned long flags, const char *devname, void *dev_id)返回0表示成功,或者返回一个错误码参数:irq:中断号handler:中断处理函数flags:与中断管理有关的各种选项devname:设备名dev_id:共享中断时使用不同中断号注册同一中断处理函数buttons_interruptIRQ_TYPE_EDGE_BOTH (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)上升沿或者下降沿产生中断(void *)&button_irqs[i] 把结构体数组变量地址给void *dev_id*/ err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_RISING, button_irqs[i].name, (void *)&button_irqs[i]); if (err) break; } if (err) { i--; for (; i >= 0; i--) { if (button_irqs[i].irq < 0) {continue; } disable_irq(button_irqs[i].irq); free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return -EBUSY; } ev_press = 1;//按下 return 0;}static int s3c24xx_buttons_close(struct inode *inode, struct file *file){ int i; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) { continue;}free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return 0;}/*read函数读取key_values数组的结果*//*由它读取键盘输入的结果*//*实质上就是读取key_values数组的值*//*它完成了键盘作为输入设备的核心功能*//*数组是否可读,要根据标志位ev_press来判断*//*如果数组可读,则读取数据到用户buffer中*//*如果数组不可读,则读进程进入等待队列,等待到数组可读为止*//*等待队列机制,是中断管理中常用到的机制*//*因为有些进程经常需要等待某一事件的发生*/static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){ unsigned long err; if (!ev_press) {//没按下时 /*看看用户有没有要求不阻塞*/if (filp->f_flags & O_NONBLOCK)return -EAGAIN;else/*阻塞在button_waitq 等待队列上*//*此时没数据可读,让读进程阻塞掉*/wait_event_interruptible(button_waitq, ev_press);//ev_press=1时,返回 } //这时是ev_press = 1 ev_press = 0;/*把键值读到用户*/ err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count)); return err ? -EFAULT : min(sizeof(key_values), count);}static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait){ unsigned int mask = 0; poll_wait(file, &button_waitq, wait); if (ev_press)//为1的时候按键按下 mask |= POLLIN | POLLRDNORM; return mask;}static struct file_operations dev_fops = { .owner = THIS_MODULE, .open = s3c24xx_buttons_open, .release = s3c24xx_buttons_close, .read = s3c24xx_buttons_read, .poll = s3c24xx_buttons_poll,};static struct miscdevice misc = {.minor = MISC_DYNAMIC_MINOR,//动态次设备号.name = DEVICE_NAME,.fops = &dev_fops,//关联文件操作};static int __init dev_init(void){int ret;/*混杂设备注册*/ret = misc_register(&misc);printk (DEVICE_NAME"\tinitialized\n");return ret;}static void __exit dev_exit(void){misc_deregister(&misc);}module_init(dev_init);module_exit(dev_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Bai");

 

 测试程序:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/select.h>#include <sys/time.h>#include <errno.h> int main(void){      int buttons_fd;      char buttons[6] = {'0', '0', '0', '0', '0', '0'};      buttons_fd = open("/dev/button", 0);      if (buttons_fd < 0) {             perror("open device buttons");             exit(1);      }      for (;;) {             char current_buttons[6];             int i;             if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {                    perror("read buttons:");                    exit(1);             }             for (i = 0; i < sizeof buttons / sizeof buttons[0]; i++) {                    if (buttons[i] != current_buttons[i]) {                           buttons[i] = current_buttons[i];                           printf("The %d key is pressed!\n",i+1);                    }             }                          }      close(buttons_fd);      return 0;}


 

1.中断处理函数

static irqreturn_t buttons_interrupt(int irq, void *dev_id);

    每次按键触发中断,进入中断处理函数,进行相关操作。

 

    中断资源弥足珍贵,最好选择在打开设备即需要使用时注册,而不是装载模块时,若退出模块时请切记释放中断资源。

 

2.工作队列

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

    创建了一个等待队列,每当有一按键按下,激活队列中一个等待的任务。

poll_wait(file, &button_waitq, wait);
    监测进程队列button_waitq里的进程,如果ev_press置1,就跳出等待。 

wait_event_interruptible(button_waitq, ev_press);
    等待,当ev_press为1时,跳出。  

wake_up_interruptible(&button_waitq);   

    数组可读,唤醒休眠的进程。   

当加进udelay(20000);消抖动时,出现错误,没网上,有空再搞搞

insmod button.ko

button: Unknown symbol __bad_udelay

insmod: cannot insert 'button.ko': unknown symbol in module or invalid paramete

 

	
				
		
原创粉丝点击