linux设备驱动(8)-中断

什么是中断?

       首先回顾stm32裸机中断编程
       中断过程:CPU执行主程序,当中断源产生中断事件,CPU响应中断请求,转向执行中断服务程序(处理中断事件),中断服务执行成功后,转到后来主程序的断点继续执行。
       中断源:GPIO口,定时器,ADC,或者软件指令。
       中断事件:包括但不仅限于:引脚电平变化,计数溢出,转换完成,SWI。
       中断服务程序。处理中断事件,要快进快出。

申请中断线

#include <linux/interrupt.h>
static inline int 
__must_check request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
														const char *name, void *dev)

作用:申请中断线,注册中断服务
参数解释
       irq:要申请的硬件中断号
       handler:向系统注册的中断处理函数,是一个回调函数,中断发生时,系统调用这个函数,dev_id参数将被传递给它
       flags:定义了中断线的行为

		#define IRQF_TRIGGER_NONE	0x00000000
		#define IRQF_TRIGGER_RISING	0x00000001		// 上升沿
		#define IRQF_TRIGGER_FALLING	0x00000002  // 下降沿
		#define IRQF_TRIGGER_HIGH	0x00000004		// 高电平
		#define IRQF_TRIGGER_LOW	0x00000008		// 低电平

       name:设置中断名称,通常是设备驱动程序的名称,在cat /proc/interrupts中可以看到此名称。
       dev:在中断共享时会用到,一般设置为这个设备的设备结构体或者NULL
       返回0表示成功,返回-INVAL表示中断号无效或处理函数指针为NULL,返回-EBUSY表示中断已经被占用且不能共享

       示例:
       request_irq(IRQ_GPIO_A_START + 28, key2_isr, IRQF_TRIGGER_FALLING, “key2_gpioa28”, NULL);

释放中断线

/**
 *	free_irq - free an interrupt allocated with request_irq
 *	@irq: Interrupt line to free
 *	@dev_id: Device identity to free
 *	
 *	Remove an interrupt handler. The handler is removed and if the
 *	interrupt line is no longer in use by any driver it is disabled.
 *	On a shared IRQ the caller must ensure the interrupt is disabled
 *	on the card it drives before calling this function. The function
 *	does not return until any executing interrupts for this IRQ
 *	have completed.
 * 
 *	This function must not be called from interrupt context.
 */
void free_irq(unsigned int irq, void *dev_id);
irq : 需要释放的中断
dev_id : 将要释放设备的ID

       示例:
       free_irq(IRQ_GPIO_A_START + 28, NULL);

中断服务程序类型

//中断服务程序类型
typedef irqreturn_t (*irq_handler_t)(int, void *);

/**
 * enum irqreturn
 * @IRQ_NONE		interrupt was not from this device
 * @IRQ_HANDLED		interrupt was handled by this device
 * @IRQ_WAKE_THREAD	handler requests to wake the handler thread
 */
enum irqreturn {
	IRQ_NONE		= (0 << 0),
	IRQ_HANDLED		= (1 << 0),
	IRQ_WAKE_THREAD		= (1 << 1),
};
typedef enum irqreturn irqreturn_t;

示例:
irqreturn_t keys_isr(int irq, void *dev)
{
       return IRQ_HANDLED;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值