设为首页 加入收藏

TOP

S3C6410中断方式查询按键值驱动(一)
2014-11-24 03:19:56 来源: 作者: 【 】 浏览:1
Tags:S3C6410 中断 方式 查询 按键 驱动

个人觉得中断的驱动还是蛮简单的,因为许多函数系统已经给你封装好了,比如中断的注册、获取按键值等等,但是如果想要彻底了解函数的工作过程还是很困难的。
以下是代码
驱动程序irq_botton_drive.c



#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


//#include
//#include

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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux进程间通信 下一篇Linux 驱动 Printk 在终端没有输出

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言中如何将结构体 (2025-12-24 22:20:09)
·纯C语言结构体成员变 (2025-12-24 22:20:06)
·C语言中,指针函数和 (2025-12-24 22:20:03)
·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)