Linux驱动程序:外部中断
实验平台
内核版本:Linux-4.19.5
开发板:SAMSUNG JZ2440
主要函数
1.中断注册函数
函数原型
int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
const char *name, void *dev_id)
函数参数解析
irq:中断编号,在\arch\arm\mach-s3c24xx\include\mach\irqs.h头文件中定义了各个中断编号的宏定义
handler:中断执行函数,就是发生中断的时候会自动跳转执行的函数
flags:中断类型,在include\linux\interrupt.h头文件中定义了各个中断类型的宏定义
name:中断的名称
dev_id:传参,中断执行函数有两个传入参数一个是中断号irq,另一个就是dev_id指针
函数功能
注册一个中断,并设置对应的引脚
2.中断释放函数
函数原型
void *free_irq(unsigned int irq, void *dev_id)
函数参数解析
irq:中断编号,free_irq函数通过传入的irq,dev_id来释放中断,填写和request_irq中相同的参数,就可以释放对应的中断。
dev_id:同上。
函数功能
释放一个中断
3.中断执行函数
函数原型
irqreturn_t (*irq_handler_t)(int irq, void *dev_id)
函数参数解析
irq:中断编号,触发该中断的中断编号
dev_id:注册中断时候request_irq,设置的dev_id指针的值
函数功能
中断执行函数,每次中断触发都会执行该函数
实验程序
/* 驱动程序 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.h>
int major; //主设备号
static struct class *buttons_class;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val; //返回给用户的键值
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //休眠队列
static volatile int ev_press = 0; //条件变量
const int t_s3c2440_devid[4] = {
1, 2, 3, 4}; //键值数组
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
int i_pinselect =