三个按键实现按键中断,key1-》led1 key2->led2 key3->led3
按键按一下灯亮,再按一下灯灭
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
/* myirq{
interrupt-parent=<&gpiof>;
interrupts=<7 0>,<8 0>,<9 0>;
};
*/
//定义指向获取的设备树节点信息空间
struct device_node *node1;
struct device_node *node2;
struct gpio_desc * gpiono1;
struct gpio_desc * gpiono2;
struct gpio_desc * gpiono3;
int ret;
unsigned int irqno1;//用于接收软中断
unsigned int irqno2;
unsigned int irqno3;
unsigned int count=0;//用于计数,中断发生一次+1
//定义工作队列底半部对象
struct work_struct work1;
struct work_struct work2;
struct work_struct work3;
//工作队列底半部处理函数
void work_func1(struct work_struct *work)
{
gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
}
void work_func2(struct work_struct *work)
{
gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
}
void work_func3(struct work_struct *work)
{
gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
}
//中断处理函数顶半部
irqreturn_t irq_handler1(int irqno,void *dev)
{
count++;
printk("key interrupt......%d..\n",count);
//开启中断底半部
schedule_work(&work1);
return IRQ_HANDLED;
}
irqreturn_t irq_handler2(int irqno,void *dev)
{
count++;
printk("key interrupt......%d..\n",count);
//开启中断底半部
schedule_work(&work2);
return IRQ_HANDLED;
}
irqreturn_t irq_handler3(int irqno,void *dev)
{
count++;
printk("key interrupt......%d..\n",count);
//开启中断底半部
schedule_work(&work3);
return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{
//对象初始化
INIT_WORK(&work1,work_func1);
INIT_WORK(&work2,work_func2);
INIT_WORK(&work3,work_func3);
//通过名字获取设备树节点信息
node1=of_find_node_by_name(NULL,"myirq");
if(node1==NULL)
{
printk("通过名字解析设备树节点失败\n");
return -EFAULT;
}
printk("成功解析设备树节点\n");
//通过名字获取设备树节点信息
node2=of_find_node_by_name(NULL,"myleds");
if(node2==NULL)
{
printk("通过名字解析设备树节点失败\n");
return -EFAULT;
}
printk("成功解析设备树节点\n");
//获取申请gpio编号
gpiono1=gpiod_get_from_of_node(node2,"myled1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono1))
{
printk("获取gpio编号失败\n");
return PTR_ERR(gpiono1);
}
gpiono2=gpiod_get_from_of_node(node2,"myled2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono2))
{
printk("获取gpio编号失败\n");
return PTR_ERR(gpiono2);
}
gpiono3=gpiod_get_from_of_node(node2,"myled3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono3))
{
printk("获取gpio编号失败\n");
return PTR_ERR(gpiono3);
}
printk("获取gpio编号成功\n");
//获取软中断号
irqno1=irq_of_parse_and_map(node1,1);
if(irqno1==0)
{
printk("获取软中断号失败\n");
return EINVAL;
}
irqno2=irq_of_parse_and_map(node1,0);
if(irqno2==0)
{
printk("获取软中断号失败\n");
return EINVAL;
}
irqno3=irq_of_parse_and_map(node1,2);
if(irqno3==0)
{
printk("获取软中断号失败\n");
return EINVAL;
}
printk("获取软中断号成功\n");
//将中断注册进内核
ret=request_irq(irqno1,irq_handler1,IRQF_TRIGGER_FALLING,"key1_inte",NULL);
if(ret)
{
printk("注册中断失败\n");
return ret;
}
request_irq(irqno2,irq_handler2,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
request_irq(irqno3,irq_handler3,IRQF_TRIGGER_FALLING,"key3_inte",NULL);
printk("注册中断成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//中断注销
free_irq(irqno1,NULL);
free_irq(irqno2,NULL);
free_irq(irqno3,NULL);
gpiod_set_value(gpiono1,0);
gpiod_set_value(gpiono2,0);
gpiod_set_value(gpiono3,0);
//释放
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");