Linux驱动开发五:中断方式获取按键值
2014-10-24 22:56
351 查看
序:
当按下中断后,发生中断,发生以下事情
1.CPU进入异常模式,并跳转到vector_irq + stubs_offset处
2.因为在用户模式发生中断,所以跳转到__irq_usr指示的地址处
3.最终跳转到函数asm_do_IRQ处
4.在asm_do_IRQ按中断号查找到对应中断的irq_desc描述符,然后进入中断处理入口handle_irq
5.接着调用generic_handle_irq_desc
6.desc->handle_irq(irq,desc),而这个是在中断架构初始化的时候设置的
7.接着使能中断,调用用户注册的中断处理例程链表
8.处理完成后,恢复现场
代码:
1、third_drv.c
2、Makefile
3、测试程序
4、执行结果
key_vals = 0x1
key_vals = 0x81
key_vals = 0x2
key_vals = 0x2
key_vals = 0x82
key_vals = 0x3
key_vals = 0x3
key_vals = 0x83
key_vals = 0x4
key_vals = 0x4
key_vals = 0x4
key_vals = 0x84
key_vals = 0x4
key_vals = 0x84
当按下中断后,发生中断,发生以下事情
1.CPU进入异常模式,并跳转到vector_irq + stubs_offset处
2.因为在用户模式发生中断,所以跳转到__irq_usr指示的地址处
3.最终跳转到函数asm_do_IRQ处
4.在asm_do_IRQ按中断号查找到对应中断的irq_desc描述符,然后进入中断处理入口handle_irq
5.接着调用generic_handle_irq_desc
6.desc->handle_irq(irq,desc),而这个是在中断架构初始化的时候设置的
7.接着使能中断,调用用户注册的中断处理例程链表
8.处理完成后,恢复现场
代码:
1、third_drv.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 <linux/device.h> #include <linux/irq.h> #include <linux/interrupt.h> //#include <mach/regs-gpio.h> //#include <mach/hardware.h> #include <mach/gpio.h> #include <mach/hardware.h> #include <linux/sched.h> int major; const char *major_name = "third_drv"; const char *minor_name = "button"; static struct class *thirddrv_class; static struct class_device *thirddrv_class_dev; volatile unsigned long *gpncon = NULL; volatile unsigned long *gpndat = NULL; //声明一个按键的等待队列 static DECLARE_WAIT_QUEUE_HEAD(button_waitq); /* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */ static volatile int ev_press = 0; struct pin_desc{ unsigned int pin; unsigned int key_val; }; /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ static unsigned char key_val; /* * K1,K2,K3,K4对应GPN0,GPGN1,GPN2,GPN3 */ /* struct pin_desc pins_desc[4] = { {S3C64XX_GPN0_EINT0, 0x01}, {S3C64XX_GPN1_EINT1, 0x02}, {S3C64XX_GPN2_EINT2, 0x03}, {S3C64XX_GPN3_EINT3, 0x04}, }; */ struct pin_desc pins_desc[4] = { {0, 0x01}, {1, 0x02}, {2, 0x03}, {3, 0x04}, }; /* * 确定按键值 *中断处理程序并置标志位为1,唤醒等待队列上等待的进程 */ static irqreturn_t button_irq(int irq,void *dev_id) { struct pin_desc * pindesc = (struct pin_desc *)dev_id; unsigned int pinval; // pinval = ioread32(S3C64XX_GPNDAT); pinval = gpio_get_value(S3C64XX_GPN(pindesc->pin));/* 读取中断引脚的状态 */ /* 确定按键值 */ if (pinval) { /* 松开 */ key_val = 0x80 | pindesc->key_val; } else { /* 按下 */ key_val = pindesc->key_val; } ev_press = 1; 4000 /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ return IRQ_RETVAL(IRQ_HANDLED); } /* open函数 */ static int third_drv_open(struct inode *inode,struct file *file) { /* 配置GPN0、1、2、3为输入(配置为0) */ // *gpncon &=~((0x3<<(0*2)) | (0x3<<(1*2)) | (0x3<<(2*2)) | (0x3<<(3*2)));//清零寄存器 request_irq(IRQ_EINT(0),button_irq,IRQ_TYPE_EDGE_BOTH,"K1",&pins_desc[0]); request_irq(IRQ_EINT(1),button_irq,IRQ_TYPE_EDGE_BOTH,"K2",&pins_desc[1]); request_irq(IRQ_EINT(2),button_irq,IRQ_TYPE_EDGE_BOTH,"K3",&pins_desc[2]); request_irq(IRQ_EINT(3),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",&pins_desc[3]); // request_irq(IRQ_EINT(4),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",1); return 0; } /* read函数 */ static 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, &key_val, 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]); 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, }; /* 入口函数 */ static int third_drv_init(void) { major = register_chrdev(0,major_name,&third_drv_fops); if(major < 0) { printk("Can't register major number\n"); return major; } /* 自动生成设备节点机制,获取系统消息,cat /proc/class会发现 first_drv */ thirddrv_class = class_create(THIS_MODULE, major_name); /* register your own device in sysfs, and this will cause udev to create corresponding device node */ thirddrv_class_dev = device_create( thirddrv_class, NULL, MKDEV(major, 0),NULL, minor_name ); /* 地址映射 */ gpncon = (volatile unsigned long *)ioremap(0x7F008830,16); gpndat = gpncon + 1;//指针加1 return 0; } /* 出口函数 */ static void third_drv_exit(void) { unregister_chrdev(major,major_name); device_unregister(thirddrv_class_dev); class_destroy(thirddrv_class); iounmap(gpncon); } module_init(third_drv_init); module_exit(third_drv_exit); MODULE_LICENSE("GPL");
2、Makefile
obj-m := third_drv.o KER_DIR := /work/work/linux/ all: $(MAKE) -C $(KER_DIR) M=`pwd` modules # cp first_drv.ko /work/tftpboot/ cp third_drv.ko /work/nfsroot/ echo $^ clean: make -C $(KER_DIR) M=`pwd` modules clean
3、测试程序
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> /*test */ int main(int argc,char *argv[]) { int fd; int cnt = 0; char key_vals; fd = open("/dev/button",O_RDWR); if(fd < 0) { printf("can't opent /dev/button\n"); return 0; } while(1) { read(fd,&key_vals,1); printf("key_vals = 0x%x\n",key_vals); } return 0; }
4、执行结果
key_vals = 0x1
key_vals = 0x81
key_vals = 0x2
key_vals = 0x2
key_vals = 0x82
key_vals = 0x3
key_vals = 0x3
key_vals = 0x83
key_vals = 0x4
key_vals = 0x4
key_vals = 0x4
key_vals = 0x84
key_vals = 0x4
key_vals = 0x84
相关文章推荐
- Linux无需开发底层驱动,从应用层获取GPIO中断
- linux驱动之中断方式获取键值
- linux驱动之中断方式获取键值
- ARM-Linux驱动--ADC驱动(中断方式)
- Linux内核驱动开发之KGDB原理介绍及kgdboe方式配置
- Linux设备驱动开发-linux驱动中的阻塞访问方式
- linux驱动开发中断例程学习
- Linux驱动开发二:外部中断分析
- ARM-Linux驱动--ADC驱动(中断方式)
- linux驱动之key中断方式读键值
- Linux驱动开发十:按键中断之输入子系统
- Linux设备驱动开发-linux驱动中的非阻塞访问方式
- linux驱动开发学习--对中断和内核定时器的学习笔记
- 字符设备驱动笔记——中断方式按键驱动之linux中断处理结构(五)
- Linux驱动开发九:按键中断+定时去抖
- Linux设备驱动开发详解--笔记10--中断与时钟
- Linux内核驱动开发之KGDB单步调试内核(kgdboc方式)
- Linux驱动开发之五---按键驱动(查询方式)(Tiny6410)
- linux驱动开发--中断:tasklet实现中断底半部
- arm-linux --ADC驱动(中断方式)