韦东山驱动视频笔记——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得到了按键值。
测试程序:
运行效果:
目的:当有按键按下和松开时,在终端输出不同的键值
原理:假设我们现在已经加载(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; }
运行效果:
相关文章推荐
- 韦东山驱动视频笔记——1.字符设备驱动程序之查询方式的按键驱动程序
- 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制
- 跟着韦东山老师学字符设备驱动之查询方式的按键驱动程序分析
- 字符设备驱动笔记——中断方式按键驱动之代码(六)
- 韦东山驱动视频笔记——4.字符设备驱动程序之异步通知
- 字符设备驱动笔记——中断方式按键驱动之linux异常处理结构(四)
- 第12课第4.1节 字符设备驱动程序之中断方式的按键驱动_Linux异常处理结构
- 第12课第4.2节 字符设备驱动程序之中断方式的按键驱动_Linux中断处理结构
- 第12课第4.3节 字符设备驱动程序之中断方式的按键驱动_编写代码
- 字符设备驱动笔记——中断方式按键驱动之linux中断处理结构(五)
- 字符设备驱动程序按键驱动---中断方式
- 字符设备驱动程序之中断方式的按键驱动
- Linux设备驱动开发基础---字符设备驱动程序开发之基于中断的按键驱动
- 07-S3C2440驱动学习(一)嵌入式linux字符设备驱动-查询+中断+引入poll机制的按键驱动程序
- 字符设备驱动--中断方式下的按键驱动
- 字符设备驱动之按键处理一(查询方式的按键驱动程序)
- 字符设备驱动学习笔记----查询方式取得按键值
- 字符设备驱动-中断方式操控按键
- 字符设备驱动之按键处理二(中断处理的按键驱动程序)
- 字符设备驱动笔记——查询方式按键驱动(三)