在 Armv8 架构上,如果你在请求中断(request_irq)时收到了意外中断,可能是因为中断控制器没有正确配置或中断处理程序没有正确注册。
以下是一个简单的代码示例,展示了如何正确请求中断并处理中断:
#include
#include
#include
#include
static irqreturn_t my_interrupt_handler(int irq, void *dev_id)
{
// 处理中断的逻辑
printk(KERN_INFO "Interrupt handled\n");
return IRQ_HANDLED;
}
static int __init my_module_init(void)
{
int irq_number;
// 请求中断
irq_number = request_irq(IRQ_NUM, my_interrupt_handler, IRQF_SHARED, "my_interrupt", NULL);
if (irq_number < 0) {
printk(KERN_ERR "Failed to request IRQ: %d\n", irq_number);
return irq_number;
}
printk(KERN_INFO "IRQ requested successfully\n");
return 0;
}
static void __exit my_module_exit(void)
{
// 释放中断
free_irq(IRQ_NUM, NULL);
printk(KERN_INFO "IRQ freed\n");
}
module_init(my_module_init);
module_exit(my_module_exit);
请确保将 IRQ_NUM
替换为适当的中断号,并按照自己的需求修改中断处理程序。
此代码示例中,我们首先在 my_module_init
函数中请求中断,并在 my_interrupt_handler
函数中处理中断。如果请求中断失败,将输出错误消息。
在 my_module_exit
函数中,我们释放了之前请求的中断。
请注意,此示例仅为演示目的,并非完整的可编译代码。您需要根据自己的需求进行适当的修改和调整。