歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> S3C6410中斷方式查詢按鍵值驅動

S3C6410中斷方式查詢按鍵值驅動

日期:2017/3/1 9:56:03   编辑:Linux編程

個人覺得中斷的驅動還是蠻簡單的,因為許多函數系統已經給你封裝好了,比如中斷的注冊、獲取按鍵值等等,但是如果想要徹底了解函數的工作過程還是很困難的。
以下是代碼
驅動程序irq_botton_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 <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>

static struct class *thirddrv_class;
static struct class_device*thirddrv_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 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);
return IRQ_HANDLED;
}


static int third_drv_open(struct inode *inode, struct file *file)
{
//printk("third_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 third_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 third_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 third_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)
{
//printk(KERN_ERR "third_drv_init\n");
major = register_chrdev(0, "thirdt_drv", &third_drv_fops); // 注冊, 告訴內核

thirddrv_class = class_create(THIS_MODULE, "thirddrv");

thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "bottons"); /* /dev/bottons */


return 0;
}

static void third_drv_exit(void)
{
//printk(KERN_ERR "third_drv_exit\n");
unregister_chrdev(major, "thirdt_drv"); // 卸載

device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);

}

module_init(third_drv_init);
module_exit(third_drv_exit);


MODULE_LICENSE("GPL");

測試程序 irq_botton_test.c

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

/* irq_botton test
*
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
fd = open("/dev/bottons", 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);
}

return 0;
}

測試



該測試程序 只占了0.0%的CPU 相比查詢方式節省下了極大的系統資源

Copyright © Linux教程網 All Rights Reserved