欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码

程序员文章站 2022-07-14 16:28:17
...

一 drv_open函数解析

配置某个引脚为外部触发中断源:
(1)引脚标号-void *dev_id
(2)敏感沿-unsigned long irqflags
(3)中断标号-unsigned int irq;
(4)中断类型-irq_handler_t handler
(5)设备名称-const char *devname
在linux内核中以上参数通过request_irq函数设置。

[1]request_irq函数调用

request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);

[2]request_irq函数内部处理:调用setup_irq函数对于中断进行初始化。
其中,
irq= (3)中断标号-unsigned int irq = IRQ_EINT0
action->handler = (4)中断发生后调用的函数-irq_handler_t handler = buttons_irq
action->flags = (2)敏感沿-unsigned long irqflags = IRQT_BOTHEDGE
action->name = (5)设备名称-const char *devname = “S2” 随意取值即可
action->next = NULL;
action->dev_id = (1)引脚标号-void *dev_id = &pins_desc[0] 随意取值即可,目的是确定卸载哪一个驱动

int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)
{
	...
	retval = setup_irq(irq, action);
	...
}

[3]setup_irq函数内部处理:使用set_type函数处理

int setup_irq(unsigned int irq, struct irqaction *new)
{
	...
	if (desc->chip && desc->chip->set_type)
		desc->chip->set_type(irq,new->flags & IRQF_TRIGGER_MASK);
	...
}

其中,desc->chip在哪里定义呢?
arch\arm\plat-s3c24xx\irq.c\void __init s3c24xx_init_irq(void),由于[1]中定义了IRQ_EINT0,所以desc->chip对应s3c_irq_eint0t4。

void __init s3c24xx_init_irq(void)
{
	...
	/* external interrupts */
	
	for (irqno = IRQ_EINT0; irqno <= IRQ_EINT3; irqno++) {
		irqdbf("registering irq %d (ext int)\n", irqno);
		set_irq_chip(irqno, &s3c_irq_eint0t4);
		set_irq_handler(irqno, handle_edge_irq);
		set_irq_flags(irqno, IRQF_VALID);
	}
	
	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);
	}
	...
}

查看s3c_irq_eint0t4定义:

static struct irq_chip s3c_irq_eint0t4 = {
	.name		= "s3c-ext0",
	.ack		= s3c_irq_ack,
	.mask		= s3c_irq_mask,
	.unmask		= s3c_irq_unmask,
	.set_wake	= s3c_irq_wake,
	.set_type	= s3c_irqext_type,
};

[4]set_type函数=s3c_irqext_type函数
前面的调用代码为:

new->flags=action->flags =	(2)敏感沿-unsigned long irqflags=IRQT_BOTHEDGE
desc->chip->set_type(irq,new->flags & IRQF_TRIGGER_MASK);

函数的定义为:

s3c_irqext_type(unsigned int irq, unsigned int type)
{
	...
	if ((irq >= IRQ_EINT0) && (irq <= IRQ_EINT3))
	{
		gpcon_reg = S3C2410_GPFCON;
		extint_reg = S3C24XX_EXTINT0;
		gpcon_offset = (irq - IRQ_EINT0) * 2;
		extint_offset = (irq - IRQ_EINT0) * 4;
	}
	...
	/* Set the external interrupt to pointed trigger type */
	switch (type)
	{
		case IRQT_NOEDGE:
			printk(KERN_WARNING "No edge setting!\n");
			break;

		case IRQT_RISING:
			newvalue = S3C2410_EXTINT_RISEEDGE;
			break;

		case IRQT_FALLING:
			newvalue = S3C2410_EXTINT_FALLEDGE;
			break;
		//与前面request_irq函数调用一致
		case IRQT_BOTHEDGE:
			newvalue = S3C2410_EXTINT_BOTHEDGE;
			break;

		case IRQT_LOW:
			newvalue = S3C2410_EXTINT_LOWLEV;
			break;

		case IRQT_HIGH:
			newvalue = S3C2410_EXTINT_HILEV;
			break;

		default:
			printk(KERN_ERR "No such irq type %d", type);
			return -1;
	}
}

二 drv_close函数解析

函数目的:释放中断源

  1. free_irq函数调用
irq=(3)中断标号-unsigned int irq = IRQ_EINT0
dev_id = (1)引脚标号-void *dev_id = &pins_desc[0]

free_irq(IRQ_EINT0, &pins_desc[0]);

free_irq函数定义:

void free_irq(unsigned int irq, void *dev_id)

三 buttons_irq中断服务函数解析-唤醒函数

  1. 函数声明格式:static irqreturn_t buttons_irq(int irq, void *dev_id)
    注意:这里可以看到dev_id作用:判断中断源
  2. 返回值:return IRQ_RETVAL(IRQ_HANDLED);
  3. 唤醒函数:wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */

四 drv_read函数解析-休眠函数:

  1. 休眠函数调用
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
  1. 休眠函数原型
#define wait_event_interruptible(wq, condition)				\
({									\
	int __ret = 0;							\
	if (!(condition))						\								//condition数值为0的时候休眠
		__wait_event_interruptible(wq, condition, __ret);	\
	__ret;								\
})
  1. 参数声明

[1]ev_press参数:

/* 中断事件标志, 中断服务程序将它置1:表示唤醒
third_drv_read将它清0:表示休眠 */
static volatile int ev_press = 0;

[2]button_waitq参数:
表示挂在队列里的本进程

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

五 驱动测试1

app程序执行read函数的时候:如果未发生中断函数则执行休眠进程;
如果发生中断则唤醒进程

  1. 开发板加载驱动后如何调用drv_open函数?
    exec 5</dev/buttons含义:打开设备/dev/buttons,定位到5去

    说明:下图所示shell进程编号为770
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
    /proc/770/fd:fd — 这是个目录,包含当前进程打开的每一个文件的文件描述符(file descriptor),这些文件描述符是指向实际文件的一个符号链接;shell进程打开的驱动/dev/buttons
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
    执行效果:
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码

  2. 调用close函数?
    执行:exec 5<&-
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码

六 驱动测试2

  1. 执行测试程序(后台执行):./app &
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
    从图中可知:驱动程序和测试程序均在后台执行。其中,测试程序处于睡眠状态(S)
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
    查看各个进程占用的CPU资源:top命令(ctrl+c退出)
    韦东山uboot_内核_根文件系统学习笔记5.8-第005课_字符设备驱动_第008节_第008节_字符设备驱动程序之中断方式的按键驱动_编写代码
相关标签: uboot学习