简体   繁体   English

如何使用request_threaded_irq以便在线程处理程序工作时调用中断处理程序?

[英]How to use request_threaded_irq so that the interrupt handler is called while the threaded handler works?

I am trying to write a simple interrupt handler for a GPIO in the linux kernel. 我正在尝试为linux内核中的GPIO编写一个简单的中断处理程序。 I use request_threaded_irq to get an interrupt context handler, and a threaded handler. 我使用request_threaded_irq来获取中断上下文处理程序和线程处理程序。

My problem is that the work done by the threaded handler has a big impact on the timing of the calls to the interrupt handler. 我的问题是线程处理程序完成的工作对调用中断处理程序的时间有很大影响。

The code to setup the interrupt is: 设置中断的代码是:

gpio_request(93, "test")
gpio_direction_input(93);
gpio_request(33, "mirror");
gpio_direction_output(33, 1);

request_threaded_irq(gpio_to_irq(93),
        interrupt_handler,
        threaded_interrupt_handler,
        IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_TIMER,
        "test", NULL);

Here, I am simply requesting gpio 93 to fire an interrupt on rising and falling edges. 在这里,我只是要求gpio 93在上升沿和下降沿触发中断。 I also request gpio 33 to use as a mirror, see below. 我还要求gpio 33用作镜子,见下文。

(In my setup, I put a clock source on gpio 93). (在我的设置中,我在gpio 93上放了一个时钟源)。

The code of the interrupt handler is this: 中断处理程序的代码是这样的:

static irqreturn_t interrupt_handler(int irq, void *data)
{
    int state = gpio_get_value(93);
    gpio_set_value(33, state);
    return IRQ_WAKE_THREAD;
}

Here, the interrupt handler is simply mirroring gpio 93 input value as an output value for gpio 33. This allows me to monitor the effective rate of the calls to interrupt_handler . 这里,中断处理程序只是将gpio 93输入值镜像为gpio 33的输出值。这使我可以监视对interrupt_handler的调用的有效速率。

Lastly, the threaded handler: 最后,线程处理程序:

static irqreturn_t threaded_interrupt_handler(int irq, void *data)
{
    /* doing msleep here is apparently problematic... */
    msleep(1);
    return IRQ_HANDLED;
}

In the threaded interrupt handler, calling msleep (or actually performing work) is problematic: by looking with a scope at the gpio output 33, I can see that the interrupt_handler callback rate changes drastically when the threaded_interrupt_handler sleeps or perform too much work. 在线程中断处理程序中,调用msleep (或实际执行工作)是有问题的:通过在gpio输出33处查看范围,我可以看到,当threaded_interrupt_handler睡眠或执行太多工作时, interrupt_handler回调率会发生巨大变化。

How can I setup/use request_threaded_irq() so that the interrupt handler is always called "on-time" even if the threaded handler as some big work to do? 如何设置/使用request_threaded_irq()以便中断处理程序始终被称为“按时”,即使线程处理程序作为一些大工作要做?

I eventually understand what was happening. 我终于明白发生了什么。 According to this answer on SO , the interrupt is masked while processing the interrupt and the threaded interrupt handler. 根据SO上的这个答案 ,在处理中断线程中断处理程序时屏蔽中断。

So it seems I misunderstood the point of request_threaded_irq : it should really be used so that the interrupt handling is in a task scheduled by the scheduler. 所以我似乎误解了request_threaded_irq的要点:应该使用它,以便中断处理在调度程序安排的任务中。

For my needs, what I really wanted was simply a wait_queue. 根据我的需要,我真正想要的只是一个wait_queue。 I remove the threaded interrupt handler for my code and changed the interrupt handler to something like: 我删除了代码的线程中断处理程序,并将中断处理程序更改为:

static wait_queue_head_t wq;

static irqreturn_t interrupt_handler(int irq, void *data)
{
    int state = gpio_get_value(93);
    gpio_set_value(33, state);
    wake_up_interruptible(&wq);
    return IRQ_HANDLED;
}

Now, the interrupt handler is called with a correct timing! 现在,以正确的时序调用中断处理程序!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM