字符设备驱动-中断方式操控按键

xiaoxiao2021-02-28  91

Linux中断处理体系结构

框架总结:

我们从内核启动第二阶段可知,进行了中断处理体系结构的初始化.

框架总结以外部中断EIN4~EINT23为例。

<0> 初始化中断处理体系结构init_IRQ(arch/arm/kernel/irq.c)

void __init init_IRQ(void) { int irq; for (irq = 0; irq < NR_IRQS; irq++) irq_desc[irq].status |= IRQ_NOREQUEST | IRQ_NOPROBE; #ifdef CONFIG_SMP bad_irq_desc.affinity = CPU_MASK_ALL; bad_irq_desc.cpu = smp_processor_id(); #endif init_arch_irq(); }

然后通过init_arch_irq()调用特定架构相关的中断初始化函数。在这里这个函数就是s3c24xx_init_irq(arch/arm/plat-s3c24xx/irq.c),在后续移植Linux内核会深入由来。

/* external interrupts */ for (irqno = IRQ_EINT4; irqno <= IRQ_EINT23; irqno++) { irqdbf("registering irq %d (extended s3c irq)\n", irqno); set_irq_chip(irqno, &s3c_irqext_chip); set_irq_handler(irqno, handle_edge_irq); set_irq_flags(irqno, IRQF_VALID);

<1>发生中断,cpu执行异常向量vector_irq代码

<2>在vector_irq里面,最终调用中断处理的总入口函数asm_do_IRQ

<3> asm_do_IRQ根据中断号irq=(EINT4~EINT23)调用irq_desc[irq].handle_irq=(handle_edge_irq)

<4> handle_edge_irq使用chip成员中的函数=&s3c_irqext_chip来设置硬件

<5> handle_edge_irq逐个调用用户在irq_desc[irq].action连表中注册的处理函数

<6> 用户注册中断服务程序

int request_irq(unsigned int irq, irq_handler_t handler, unsigned long irqflags, const char *devname, void *dev_id)

Ⅰ.分配一个irqaction结构

struct irqaction *action; action->handler = handler; action->flags = irqflags; cpus_clear(action->mask); action->name = devname; action->next = NULL; action->dev_id = dev_id;

Ⅱ.把这个结构放入irq_desc[irq].action连表中

setup_irq(irq, action);

Ⅲ.设置中断触发方式和引脚状态

desc->chip->set_type(irq, new->flags & IRQF_TRIGGER_MASK

设置引脚功能为外部中断,由irqflags参数设置中断触发方式

Ⅳ.使能中断

desc->chip->startup(irq)/enable(irq)

<七> 卸载中断服务程序

void free_irq(unsigned int irq, void *dev_id)

Ⅰ.出链

由中断号irq定位action链表,再使用dev_id在action链表中找到要卸载的表项。

desc->chip->release(irq, dev_id)

Ⅱ.关闭中断

desc->chip->shutdown(irq)/disable(irq)

中断方式操作按键

根据上面的描述,我们只需要通过request_irq自己注册中断服务程序。

int request_irq(unsigned int irq, irq_handler_t handler, unsigned long irqflags, const char *devname, void *dev_id)

驱动程序:third_drv.c

#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/irq.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> static struct class *thirddrv_class; static struct class_device *thirddrv_class_dev; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); /* 中断事件标志, 中断服务程序将它置1,s3c24xx_third_read将它清0 */ static volatile int ev_press = 0; volatile unsigned long *gpfcon; volatile unsigned long *gpfdat; volatile unsigned long *gpgcon; volatile unsigned long *gpgdat; struct pin_desc{ unsigned int pin; unsigned int key_val; }; /* 键值: 按下时,0x01、0x02、0x03 */ /* 键值: 松开时,0x81、0x82、0x83 */ static unsigned char key_val; struct pin_desc pin_desc[3] = { {S3C2410_GPF0,0X01}, {S3C2410_GPF2,0X02}, {S3C2410_GPG3,0X03}, }; static irqreturn_t buttons_irq(int irq, void *dev_id) { struct pin_desc *pindesc = (struct pin_desc *)dev_id; unsigned int pinval; pinval = s3c2410_gpio_getpin(pindesc->pin); if(pinval) { /* 松开 */ key_val = 0x80 | (pindesc->key_val); *gpfdat |= ((1<<4) | (1<<5) | (1<<6)); } else { /* 按下 */ key_val = pindesc->key_val; *gpfdat &= ~((1<<4) | (1<<5) | (1<<6)); } printk("key_val = 0x%x\n",key_val); ev_press = 1; /* 表示中断发生了 */ wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ return IRQ_RETVAL(IRQ_HANDLED); } static int third_drv_open(struct inode *inode,struct file *file) { /* 配置GPF0,2、GPG3为中断引脚 */ request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]); request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]); request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]); /* 配置GPF4、5、6为输入引脚 */ *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2)); *gpfcon |= ((1<<4*2) | (1<<5*2) | (1<<6*2)); 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; /* 如果无按键动作发生,则进行休眠状态 */ /* 如果ev_press等于0,休眠 */ wait_event_interruptible(button_waitq,ev_press); /* 如果有按键动作发生,则返回按键的值 */ copy_to_user(buf,&key_val,1); ev_press = 0; return 1; } static int third_drv_close (struct inode *inode, struct file *file) { free_irq(IRQ_EINT0, &pin_desc[0]); free_irq(IRQ_EINT2, &pin_desc[1]); free_irq(IRQ_EINT11, &pin_desc[2]); 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) { major = register_chrdev(0,"third_drv",&third_drv_fops); thirddrv_class = class_create(THIS_MODULE,"thirddrv"); thirddrv_class_dev = class_device_create(thirddrv_class,NULL,MKDEV(major,0),NULL,"buttons"); gpfcon = (volatile unsigned long *)ioremap(0x56000050,16); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060,16); gpgdat = gpgcon + 1; return 0; } static int third_drv_exit(void) { unregister_chrdev(major,"third_drv"); class_device_unregister(thirddrv_class_dev); class_destroy(thirddrv_class); iounmap(gpfcon); iounmap(gpgcon); return 0; } module_init(third_drv_init); module_exit(third_drv_exit); MODULE_LICENSE("GPL");

Makefile

KERN_DIR = /work/system/linux-2.6.22.6 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m += third_drv.o

驱动测试程序:thirddrvtest.c

#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; 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; }

测试:

make arm-linux-gcc -o thirddrvtest thirddrvtest.c cp thirddrvtest third_drv.ko /work/nfs_root/czg insmod third_drv.ko

不使用驱动测试程序来测试

cat /proc/interrupts

exec 5</dev/buttons //挂载到5下,会直接调用驱动程序 cat /proc/interrupts //按下按键 exec 5<&- //取消挂载

使用驱动测试程序测试

./thirddrvtest & cat /proc/interrupts //按下按键 top

转载请注明原文地址: https://www.6miu.com/read-53511.html

最新回复(0)