Linux中断的沿触发和电平触发

Linux中的中断处理分为沿触发和电平触发两种方式。沿触发中断在信号边沿发生时被记录并需立即响应,处理过程中可能出现新中断需处理。电平触发则在中断信号保持活跃期间持续有效,处理时会立即屏蔽中断,直到退出处理程序。两种处理方式与中断特性相匹配,确保中断处理的正确性和效率。
摘要由CSDN通过智能技术生成

Linux中断的沿触发和电平触发

 

初始化中断向量表时,有一个重要的操作就是set_irq_handler。这个函数参数一般为handle_edge_irq或者handle_level_irq。对于边缘中断,使用handle_edge_irq作参数;对于电平中断,使用handle_level_irq作为中断。那这两个函数有什么区别呢?

1. handle_edge_irq

kernel源代码中的注释为:

c代码

/**
* handle_edge_irq - edge type IRQ handler
* @irq: the interrupt number
* @desc: the interrupt description structure for this irq
*
* Interrupt occures on the falling and/or rising edge of a hardware
* signal. The occurence is latched into the irq controller hardware
* and must be acked in order to be reenabled. After the ack another
* interrupt can happen on the same source even before the first one
* is handled by the assosiacted event handler. If this happens it
* might be necessary to disable (mask) the interrupt depending on the
* controller hardware. This requires to reenable the interrupt inside
* of the loop which handles the interrupts which have arrived while
* the handler was running. If all pending interrupts are handled, the
* loop is left.
*/

下面翻译为中文:

中断发生在硬件信号的上升沿/下降沿。中断事件保存在中断控制器中并且必须被立刻响应以重新打开中断。响应之后,即使第一个中断还没有被相关的中断函数处理完,另一个中断也可能在同一个中断源发生。如果这种情况发生的话就有必要关闭中断。这样,在中断处理函数运行时发生的中断就会被标记为阻塞,在中断处理循环处理完这些阻塞的中断之后,就有必要在循环内部重新打开中断。如果全部的阻塞中断处理完毕,就可以离开中断处理循环。

2. handle_level_irq

c代码

/**
* handle_level_irq - Level type irq handler
* @irq: the interrupt number
* @desc: the interrupt description structure for this irq
*
* Level type interrupts are active as long as the hardware line has
* the active level. This may require to mask the interrupt and unmask
* it after the associated handler has acknowledged the device, so the
* interrupt line is back to inactive.
*/

一旦硬件线路达到了激活电平,电平类型的中断就会被触发。这可能需要屏蔽中断,并且在相关的中断处理程序响应了设备之后重新打开中断,这样中断线才能恢复反激活状态。

对比上述两个函数可见,对于电平中断,一进入中断处理程序就会立刻屏蔽中断,直到退出中断的时候才会unmask中断;而对于边缘中断,除非在处理当前中断时有一个新的中断被触发否则不会屏蔽中断。这两个处理函数的特点刚好和这两种中断的特点一致:对于电平中断,只要中断脚在激活电平电平,就会不断地触发中断;而边缘中断只在上升沿或者下降沿被触发。

下面是linux 2.6.32版本kernel的代码。

//觉得很奇怪,既然电平中断一开始就mask了中断,
//
退出的时候才unmask,怎么会与同类型的其他中断冲突呢?

c代码


void
handle_level_irq
(unsigned int irq, struct irq_desc *desc)
{
struct irqaction *action;
irqreturn_t action_ret
; 

spin_lock
(

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的示例程序,可以让GPIO引脚在高低电平间交替触发中断: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #define GPIO_BASE_ADDR 0x3F200000 // 树莓派GPIO控制器的物理地址 #define BLOCK_SIZE 4096 // GPIO控制器寄存器的偏移地址 #define GPFSEL0 0x00 #define GPFSEL1 0x04 #define GPSET0 0x1c #define GPCLR0 0x28 #define GPLEV0 0x34 #define GPEDS0 0x40 #define GPREN0 0x4c #define GPFEN0 0x58 #define GPHEN0 0x64 #define GPLEN0 0x70 #define GPAREN0 0x7c #define GPAFEN0 0x88 volatile unsigned int *gpio; // 用于存储GPIO控制器寄存器的指针 // 初始化GPIO控制器 void gpio_init() { int fd = open("/dev/mem", O_RDWR | O_SYNC); if (fd < 0) { perror("open /dev/mem failed"); exit(-1); } gpio = (unsigned int *)mmap(NULL, BLOCK_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE_ADDR); if (gpio == MAP_FAILED) { perror("mmap failed"); exit(-1); } close(fd); } // 设置GPIO引脚为输入模式 void gpio_input(unsigned int pin) { unsigned int reg = pin / 10; unsigned int shift = (pin % 10) * 3; gpio[reg] &= ~(7 << shift); } // 设置GPIO引脚为输出模式 void gpio_output(unsigned int pin) { unsigned int reg = pin / 10; unsigned int shift = (pin % 10) * 3; gpio[reg] |= (1 << shift); } // 设置GPIO引脚为高电平 void gpio_high(unsigned int pin) { gpio[GPSET0 / 4] = 1 << pin; } // 设置GPIO引脚为低电平 void gpio_low(unsigned int pin) { gpio[GPCLR0 / 4] = 1 << pin; } // 读取GPIO引脚的电平值 int gpio_read(unsigned int pin) { return (gpio[GPLEV0 / 4] >> pin) & 1; } // 设置GPIO引脚触发中断的方式 void gpio_set_edge(unsigned int pin, unsigned int edge) { unsigned int reg = pin / 32; unsigned int shift = (pin % 32); gpio[reg == 0 ? GPREN0 / 4 : GPHEN0 / 4] = edge << shift; } // 清除GPIO引脚触发中断的标志位 void gpio_clear_event(unsigned int pin) { gpio[GPEDS0 / 4] = 1 << pin; } int main() { gpio_init(); gpio_input(17); // 设置GPIO17为输入模式 gpio_output(18); // 设置GPIO18为输出模式 gpio_set_edge(17, 0b10); // 设置GPIO17为上升沿触发中断 while (1) { if (gpio_read(17)) { gpio_high(18); // 如果GPIO17的电平为高,则设置GPIO18为高电平 } else { gpio_low(18); // 如果GPIO17的电平为低,则设置GPIO18为低电平 } gpio_clear_event(17); // 清除GPIO17的中断标志位 usleep(100000); // 等待100毫秒 } return 0; } ``` 在上面的程序中,我们使用了树莓派GPIO控制器的物理地址和寄存器偏移地址来控制GPIO引脚的输入输出和中断触发方式。具体来说,我们使用了 `mmap()` 函数将GPIO控制器的物理地址映射到用户空间,通过访问对应的寄存器地址来操作GPIO引脚。在 `main()` 函数中,我们不断地读取GPIO17的电平值,如果它为高电平,则设置GPIO18为高电平;如果它为低电平,则设置GPIO18为低电平。同时,我们还设置了GPIO17为上升沿触发中断的方式,并在每次循环结束时清除GPIO17的中断标志位。这样,当GPIO17的电平从低电平变为高电平时,就会触发中断,从而执行相应的中断处理函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值