Linux驱动的GPIO中断编程主要有以下几个步骤:
1、 通过GPIO号获取软件中断号 (中断编程不需要设置GPIO输入输出,当然申请GPIO,设置输入也没问题)
int gpio_to_irq(unsigned int gpio)
参数 | 含义 |
---|---|
gpio | GPIO引脚编号 |
2、 注册中断处理函数,设置中断触发方式(上升沿、下降沿等)
int request_irq(unsigned int irq, irq_handler_t handler,
unsigned long flags, const char *name, void *dev)
参数 | 含义 |
---|---|
irq | 软件中断号(通过gpio_to_irq获取) |
handler | 中断处理函数 |
flags | 中断触发方式 |
name | 自定义中断名字 |
dev | 自定义参数 |
3、 驱动出口函数中 注销中断
void free_irq(unsigned int irq, void *dev_id)
参数 | 含义 |
---|---|
irq | 软件中断号(通过gpio_to_irq获取) |
dev_id | 注册中断函数时传入的 dev 参数 |
4、 定义中断处理函数
形如
static irqreturn_t gpio_irq_handle(int irq, void *dev_id)
{
printk("this is irq_handle, irq = %d\n", irq);
return IRQ_HANDLED;
}
主要注意参数和返回值
参数:int irq, void *dev_id
返回值 : irqreturn_t 类型(见下图),一般选择 IRQ_HANDLED,因为既然已经进入中断函数了,那就是已经执行了
5、 GPIO中断应用 - SR501人体红外感应
PS:左边的一定要逆时针转到底,这样反应会灵敏些;跳线帽接到H上,即可重复模式,有人一直高电平,无人一直低电平
5V供电,输出引脚我接到了正点原子IMX6ULL阿尔法开发板的GPIO1_IO2上
SR501驱动代码
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/capability.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <asm/mach-types.h>
#include <asm/uaccess.h>
#include <asm/therm.h>
#include <linux/string.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqreturn.h>
#include <linux/module.h>
static int major;
static unsigned char buff[100];
static struct class *sr501_class;
/*sr501 args*/
//unsigned int sr501_gpio = 129; //对照原理图 蜂鸣器 GPIO5_IO1
//unsigned int key_gpio = 19; //对照原理图 按键 GPIO1_IO18
//unsigned int led_gpio = 4; //对照原理图 led GPIO1_IO3
unsigned int sr501_gpio = 2; //对照原理图 sr501 GPIO1_IO2
int sr501_irq; //存储中断号
static irqreturn_t sr501_handle(int irqs, void *dev_id)
{
int value;
/*读取sr501的值*/
value = gpio_get_value(sr501_gpio);
printk("this is sr501_handle, irq = %d\n",irqs);
printk("read sr501 = %d, %s\n", value, value == 1 ? "people" : "people go out");
return IRQ_HANDLED;
}
static int sr501_open (struct inode *node, struct file *filp)
{
printk("sr501_open\n");
printk("%s %s %d\n",__FILE__, __FUNCTION__, __LINE__);
return 0;
}
static ssize_t sr501_read (struct file *filp, char *buf, size_t size, loff_t *offset)
{
int err;
printk("sr501_drv_read\n");
/*read sr501_gpio value*/
buff[0] = gpio_get_value(sr501_gpio);
err = copy_to_user(buf, buff, 1);
//返回什么不重要,也可以直接返回gpio的值,但是最好还是按照驱动程序模式来写,使用copy_to_user
return buff[0];
}
static int sr501_release (struct inode *node, struct file *filp)
{
printk("sr501_release\n");
return 0;
}
/*1.定义 file_operations 结构体*/
static const struct file_operations sr501_fops = {
.owner = THIS_MODULE,
.read = sr501_read,
.open = sr501_open,
.release = sr501_release,
};
/*3.入口函数*/
static int sr501_init(void)
{
int err;
/************* 1.注册设备,返回设备号 ************/
major = register_chrdev(0,"sr501_drv",&sr501_fops);
/*2.在内核中创建设备*/
sr501_class = class_create(THIS_MODULE, "sr501_class");
if (IS_ERR(sr501_class)) {
printk("sr501 class create failed!\n");
}
/*3.在/dev下面创建设备节点*/
device_create(sr501_class, NULL, MKDEV(major, 0), NULL, "sr501_drv");
/************* 4.sr501引脚申请中断 ************/
sr501_irq = gpio_to_irq(sr501_gpio);
printk("request irq = %d\n", sr501_irq);
/*上升沿和下降沿都触发,上升沿-有人,下降沿-人走了*/
err = request_irq(sr501_irq, sr501_handle, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
"sr501_irq", NULL); //
if(err != 0){
printk("request irq error!\n");
}
return 0;
}
/*4.退出函数*/
static int sr501_exit(void)
{
//卸载设备
unregister_chrdev(major,"sr501_fops");
//销毁设备
device_destroy(sr501_class, MKDEV(major, 0));
//删除设备类
class_destroy(sr501_class);
/*释放sr501_gpio引脚中断*/
free_irq(sr501_irq, NULL);
printk("sr501_exit\n");
return 0;
}
module_init(sr501_init);
module_exit(sr501_exit);
MODULE_LICENSE("GPL");
查看中断是否注册成功:
cat /proc/interrupts
开启内核输出
echo "7 4 1 7" > /proc/sys/kernel/printk
实验现象:
由于在中断函数中输出信息了,所以检测到有人无人时,中断会自动打印出信息
[root@100ask:/mnt/05.sr501_driver]# [ 146.755858] read sr501 = 1, people
[ 146.759329] read sr501 = 1, people
[ 146.762828] read sr501 = 1, people
[ 146.766355] read sr501 = 0, people go out
[ 146.778352] read sr501 = 1, people
[ 146.785841] read sr501 = 1, people
[ 146.789365] read sr501 = 1, people
[ 146.800944] read sr501 = 1, people
[ 146.804542] read sr501 = 0, people go out
[ 146.808594] read sr501 = 1, people
[ 147.141370] read sr501 = 1, people
[ 147.145108] read sr501 = 1, people
[ 147.150737] read sr501 = 1, people
[ 147.154439] read sr501 = 1, people
[ 147.157925] read sr501 = 0, people go out
再次查看中断发生次数