您的位置:首页 > 其它

韦东山驱动视频笔记——2.字符设备驱动程序之中断方式的按键驱动程序

2013-08-08 11:17 555 查看
linux内核版本:linux-2.6.30.4

目的:当有按键按下和松开时,在终端输出不同的键值

原理:假设我们现在已经加载(insmod)了third_drv.ko,当我们进入main函数后,可以打开设备节点"/dev/buttons",然后进入while循环,接着系统调用read,内核经过一系列的处理,进入到我们的驱动程序的read函数(third_drv_read),这时候如果没有按键按下,应用程序就会停在wait_event_interruptible处休眠,程序就不会向下面跑了,假如在某个时间点我们按下了按键,就会触发中断,调用中断处理函数(buttons_irq),经过读取按键的引脚的电平高低来判断是按下还是松开,然后给key_val赋不同的值,最后唤醒等待队列,然后重新调用wait_event_interruptible,此时ev_press = 1,就不会再休眠,所以可以继续执行,就把按键值返回了用户空间,所以我们的应用程序中的key_val得到了按键值。

#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 <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>

static struct class *third_drv_class;
static struct class_device  *third_drv_class_dev;

static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;
static int major;

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

static unsigned char key_val;

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

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pin = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pin->pin);

if (pinval)
{
//松开
key_val = 0x80 | pin->key_val;
}
else
{
//按下
key_val = pin->key_val;
}

wake_up_interruptible(&button_waitq);
ev_press = 1;

return IRQ_HANDLED;
}

static int third_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S1", &pins_desc[0]);
request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[1]);
request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[2]);
request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[3]);
return 0;
}

static ssize_t third_drv_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
if (count != 1)
return -EINVAL;
/*如果没有按键动作,休眠*/
wait_event_interruptible(button_waitq, ev_press);

/*如果有按键动作发生,返回键值*/
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return 1;
}

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

static struct file_operations third_fops = {
.owner = THIS_MODULE,
.open = third_drv_open,
.read = third_drv_read,
.release  = third_drv_release,
};

int third_init()
{
int ret;
major = register_chrdev(0, "third_drv", &third_fops);
third_drv_class = class_create(THIS_MODULE, "third_drv");
device_create(third_drv_class, NULL, MKDEV(major, 0), NULL, "buttons");

gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;

return 0;
}
static void third_exit()
{
unregister_chrdev(major, "third_drv");
device_destroy(third_drv_class, MKDEV(major, 0));
class_destroy(third_drv_class);
iounmap(gpfcon);
}

module_init(third_init);
module_exit(third_exit);

MODULE_LICENSE("GPL");


测试程序:

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

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

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);

}

return 0;
}


运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐