Linux字符设备驱动之中断按键

看了上一篇文章后,相信你们对按键有个基本的了解,在这个驱动之前写的驱动跟51是没有什么区别的,上篇文章中的读按键是一直在进行,特别是对CPU的消耗会非常大,我们一般不采用这种方法,这章我们采用中断实现按键操作。当有按键产生了我们才去读,没有按键产生就休眠。还是直接上代码。


上一篇文章的链接:http://blog.csdn.net/qq_21792169/article/details/48414765


驱动程序:

#include <linux/module.h> /*模块有关的*/
#include <linux/kernel.h>/*内核有关的*/
#include <linux/fs.h>/*文件系统有关的*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>/*linux中断*/
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>  //copy_to_user
#include <mach/regs-gpio.h>/*寄存器设置*/S3C2410_GPF0等的定义
#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义
#include <mach/irqs.h> //IRQ_EINT0等的定义
#include <asm/system.h>


volatile unsigned long *GPFCON=NULL;
volatile unsigned long *GPFDAT=NULL;


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);


/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;


#define DEVICE_NAME  "buttons"


static struct class *buttons_class;

struct pin_desc{
unsigned int pin;
unsigned int key_val;
};

static unsigned char keyval;//jianzhi


/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */


struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};


static irqreturn_t buttons_irq(int irq, void *dev_id)
{
//static int cnt=0;
//printk(KERN_INFO "%d  this is test\n",cnt++);//优先级尽量用上
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;

pinval = s3c2410_gpio_getpin(pindesc->pin);


if (pinval)
{
/* 松开 */
keyval = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
keyval = pindesc->key_val;
}
ev_press = 1;                  /* 表示中断发生了 */
wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
return IRQ_HANDLED;
}


static int third_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT0,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT3,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT4,  buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S5", &pins_desc[3]);
    return 0;
}


ssize_t third_drv_read(struct file *file, 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 0;
}


int third_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT3, &pins_desc[2]);
free_irq(IRQ_EINT4, &pins_desc[3]);
return 0;
}


static struct file_operations three_drv_fops = {
    .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    =  third_drv_open,     
.read =third_drv_read,   
.release =  third_drv_close,  
};


int major;
static int third_drv_init(void)
{
major = register_chrdev(0, DEVICE_NAME, &three_drv_fops);
       buttons_class= class_create(THIS_MODULE, DEVICE_NAME);
device_create(buttons_class, NULL, MKDEV(major, 0), NULL, "buttons"); 
GPFCON = (volatile unsigned long *)ioremap(0x56000050, 16);
GPFDAT= GPFCON + 1;
return 0;
}

static void third_drv_exit(void)
{
unregister_chrdev(major, "third_drv");
device_destroy(buttons_class,MKDEV(major, 0));
class_destroy(buttons_class);
iounmap(GPFCON);
}


module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");


测试程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>


/* thirddrvtest 
  */
int main(int argc, char **argv)
{
int fd;
unsigned char key_val=100;

fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}

while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
//sleep(5);
}
return 0;
}


Makefile:

Makefile其他的部分是不变的,只需要修改文件名字就可以了。

obj-m :=buttons.o
KERNELDIR ?= /home/work/Linux/linux-2.6.28.7
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -f  *o  *.mod.o  *mod.c  *.symvers *.order


上一篇:去年双11破10亿的耐克,和我们分享了2018新零售计划 | 新网商营销大会


下一篇:VS2010中wmain入口函数中使用wprintf输出中文乱码问题