Linux中断的使用方法

本文详细介绍了Linux内核中request_irq和request_threaded_irq的中断注册过程,强调了request_threaded_irq的异步处理机制,以及如何正确使用中断类型和线程上下文处理,特别是对于边沿触发与电平触发的建议。
摘要由CSDN通过智能技术生成

内核版本号:5.4.91

  • 内核中断处理中断注册函数request_irq

        要使用中断处理流程,需要通过函数request_irq进行中断注册,才能使用。原型在include\linux\interrupt.h。参数的含义和request_threaded_irq一致,见request_threaded_irq解析。

static inline int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
	    const char *name, void *dev)
{
	return request_threaded_irq(irq, handler, NULL, flags, name, dev);
}
  •  内核中断处理中断注册函数request_irq原型

        request_threaded_irq的实现在\kernel\irq\ manage.c。该函数功能是分配一个中断线(allocate an interrupt line):分配中断资源和使能中断线、IRQ handling。

/**
 *	request_threaded_irq - allocate an interrupt line
 *	@irq: Interrupt line to allocate
 *	@handler: Function to be called when the IRQ occurs.
 *		  Primary handler for threaded interrupts
 *		  If NULL and thread_fn != NULL the default
 *		  primary handler is installed
 *	@thread_fn: Function called from the irq handler thread
 *		    If NULL, no irq thread is created
 *	@irqflags: Interrupt type flags
 *	@devname: An ascii name for the claiming device
 *	@dev_id: A cookie passed back to the handler function
 *
 *	This call allocates interrupt resources and enables the
 *	interrupt line and IRQ handling. From the point this
 *	call is made your handler function may be invoked. Since
 *	your handler function must clear any interrupt the board
 *	raises, you must take care both to initialise your hardware
 *	and to set up the interrupt handler in the right order.
 *
 *	If you want to set up a threaded irq handler for your device
 *	then you need to supply @handler and @thread_fn. @handler is
 *	still called in hard interrupt context and has to check
 *	whether the interrupt originates from the device. If yes it
 *	needs to disable the interrupt on the device and return
 *	IRQ_WAKE_THREAD which will wake up the handler thread and run
 *	@thread_fn. This split handler design is necessary to support
 *	shared interrupts.
 *
 *	Dev_id must be globally unique. Normally the address of the
 *	device data structure is used as the cookie. Since the handler
 *	receives this value it makes sense to use it.
 *
 *	If your interrupt is shared you must pass a non NULL dev_id
 *	as this is required when freeing the interrupt.
 *
 *	Flags:
 *
 *	IRQF_SHARED		Interrupt is shared
 *	IRQF_TRIGGER_*		Specify active edge(s) or level
 *
 */
int request_threaded_irq(unsigned int irq, irq_handler_t handler,
			 irq_handler_t thread_fn, unsigned long irqflags,
			 const char *devname, void *dev_id)
{
	struct irqaction *action;
	struct irq_desc *desc;
	int retval;

	if (irq == IRQ_NOTCONNECTED)
		return -ENOTCONN;

	/*
	 * Sanity-check: shared interrupts must pass in a real dev-ID,
	 * otherwise we'll have trouble later trying to figure out
	 * which interrupt is which (messes up the interrupt freeing
	 * logic etc).
	 *
	 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
	 * it cannot be set along with IRQF_NO_SUSPEND.
	 */
	if (((irqflags & IRQF_SHARED) && !dev_id) ||
	    (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
	    ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
		return -EINVAL;

	desc = irq_to_desc(irq);
	if (!desc)
		return -EINVAL;

	if (!irq_settings_can_request(desc) ||
	    WARN_ON(irq_settings_is_per_cpu_devid(desc)))
		return -EINVAL;

	if (!handler) {
		if (!thread_fn)
			return -EINVAL;
		handler = irq_default_primary_handler;
	}

	action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
	if (!action)
		return -ENOMEM;

	action->handler = handler;
	action->thread_fn = thread_fn;
	action->flags = irqflags;
	action->name = devname;
	action->dev_id = dev_id;

	retval = irq_chip_pm_get(&desc->irq_data);
	if (retval < 0) {
		kfree(action);
		return retval;
	}

	retval = __setup_irq(irq, desc, action);

	if (retval) {
		irq_chip_pm_put(&desc->irq_data);
		kfree(action->secondary);
		kfree(action);
	}

#ifdef CONFIG_DEBUG_SHIRQ_FIXME
	if (!retval && (irqflags & IRQF_SHARED)) {
		/*
		 * It's a shared IRQ -- the driver ought to be prepared for it
		 * to happen immediately, so let's make sure....
		 * We disable the irq to make sure that a 'real' IRQ doesn't
		 * run in parallel with our fake.
		 */
		unsigned long flags;

		disable_irq(irq);
		local_irq_save(flags);

		handler(irq, dev_id);

		local_irq_restore(flags);
		enable_irq(irq);
	}
#endif
	return retval;
}
EXPORT_SYMBOL(request_threaded_irq);

        从调用这个函数开始,你注册的中断处理函数就能被调用到。需要谨慎处理单板的上电流程和该函数的调用顺序。在进入处理函数后,你必须先清掉或禁掉板上所有的中断。(这个在实际中并没有完全遵照实现)。

  •  request_irq和request_threaded_irq差异

        request_irq的本质实现是request_threaded_irq。这两个函数的区别在于:request_irq是完全的顶半部处理,处于中断上下文,只能处理对时间要求较高且较短的任务,不能有sleep,io操作等系列严格要求。hanle处理完成时,返回值是IRQ_HANDLED。其余耗时操作需在下半部完成。要通过softirq或workqueue、tasklet完成。tasklet的执行仍在中断上下文中,workqueue在进程上下文中执行。

        request_threaded_irq可在注册时添加个thread irq handle。该接口是在Linux kernel 2.6.30 之后新加的irq handler API。这里推荐的使用方法是:handle和thread_fn都进行注册。hanle在硬件的中断上下文执行,主要完成中断信息的确认,如果是需要的中断,需要禁中断,并返回IRQ_WAKE_THREAD,这个返回值要注意和request_irq不一样。返回后会唤醒处理线程并执行thread_fn。这里的thread_fn的执行已不在中断上下文中,在进程的上下文中,可以通过thread的接口调整优先级。同时可以做sleep,io等操作,对于共享型中断,可减少中断上下文的处理时间。

        尽量使用request_threaded_irq来替代request_irq。

  • 中断函数参数解析

        参数解析:

unsigned int irq:Interrupt line to allocate(硬件分配的CPU的中断信号线)

irq_handler_t handler:硬件中断上下文处理函数

irq_handler_t thread_fn:线程上下文处理函数

unsigned long irqflags: 中断类型标志: IRQF_SHARED        Interrupt is shared
                                         RQF_TRIGGER_*        Specify active edge(s) or level


const char *devname:中断名

void *dev_id

  • request_threaded_irq使用示例

static irqreturn_t boardctrl_gpio_isr_task(int irq, void *devid)
{
    printk("Is Task!\n");

   board_epld_read(WORK_EPLD, WORKEPLD_MS_INT_STATUS, &wVal);
    if(wVal & WORKEPLD_MS_INT_BIT_ALL)
    {
         INT_DISABLE(WORKEPLD_INT_MASK, WORKEPLD_MS_INT_STATUS);
    }
    return IRQ_WAKE_THREAD;;
}

static irqreturn_t boardctrl_gpio_isr(int irq, void *devid)
{
    
}

LOCAL int __init testmode_init(void)
{
    sdwRetVal = request_threaded_irq(0, boardctrl_gpio_isr_task, handler, IRQ_TYPE_EDGE_FALLING, "GPIO", NULL);
}

        在实际实现中,如果在第二个参数:中断上下文处理函数中,没有做关中断或清中断操作,上述操作放在thread进行实现。而在第四个参数中:flags,设置的是电平触发,会产生中断多次上报的情况。我们可以通过cat /proc/intrerrups进行确认。多次产生这中断的原因,就在于中断上下文的优先级最高,在中断上下文中,执行非常快,然后退出,此时到中断线程被调度和执行,中间存在时间差。而如果这时中断电平还存在的话,就会继续触发中断流程,直到电平改变。

        所以在使用request_threaded_irq接口时,建议使用边沿触发。

        如果用电平触发,需要尽可能在中断函数即上下文中关中断或清中断。

        

  • 21
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值