三个按键实现按键中断,key1-> led1 key2->led2 key3->led3 按键按一下灯亮,再按一下灯灭

时间:2025-03-07 14:26:27
#include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/of_gpio.h> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/> #include <linux/of_irq.h> #include <linux/> #define CNAME "myled" struct device_node *node; int ret; struct gpio_desc *gpiono1; struct gpio_desc *gpiono2; struct gpio_desc *gpiono3; //接受软中断号 unsigned int irqno1,irqno2,irqno3; //中断处理函数 irqreturn_t irq_handler1(int irqno,void *dev) { gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1)); return IRQ_HANDLED; } irqreturn_t irq_handler2(int irqno,void *dev) { gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2)); return IRQ_HANDLED; } irqreturn_t irq_handler3(int irqno,void *dev) { gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3)); return IRQ_HANDLED; } // open函数 int mycdev_open(struct inode *inode, struct file *file) { printk("%s:%s:%d\n", __FILE__, __func__, __LINE__); return 0; } // close函数 int mycdev_close(struct inode *inode, struct file *file) { printk("%s:%s:%d\n", __FILE__, __func__, __LINE__); return 0; } //操作方法结构体初始化 const struct file_operations fops = { .open = mycdev_open, // .unlocked_ioctl = ioctl, .release = mycdev_close, }; //入口函数 static int __init mycdev_init(void) { //通过名字获取设备树节点信息 node = of_find_node_by_name(NULL, "myled"); if (node == NULL) { printk("通过名字解析设备树节点失败\n"); return -EFAULT; } printk("成功解析到设备树节点\n"); //获取并申请gpio编号 gpiono1 = gpiod_get_from_of_node(node, "myled1", 0, GPIOD_OUT_LOW, NULL); gpiono2 = gpiod_get_from_of_node(node, "myled2", 0, GPIOD_OUT_LOW, NULL); gpiono3 = gpiod_get_from_of_node(node, "myled3", 0, GPIOD_OUT_LOW, NULL); printk("获取gpio编号成功\n"); //通过名字获取设备树节点信息 node = of_find_node_by_name(NULL, "myirq"); if (node == NULL) { printk("通过名字解析设备树节点失败\n"); return -EFAULT; } printk("成功解析到设备树节点\n"); //获取软中断号1 irqno1 = irq_of_parse_and_map(node,2); if(irqno1 == 0) { printk("获取软中断号失败\n"); return EINVAL; } //终端注册进内核 ret = request_irq(irqno1,irq_handler1,IRQF_TRIGGER_FALLING,"key1",NULL); if(ret) { printk("注册软中断号失败\n"); return EINVAL; } //获取软中断号2 irqno2 = irq_of_parse_and_map(node,0); if(irqno2 == 0) { printk("获取软中断号失败\n"); return EINVAL; } //终端注册进内核 ret = request_irq(irqno2,irq_handler2,IRQF_TRIGGER_FALLING,"key2",NULL); if(ret) { printk("注册软中断号失败\n"); return EINVAL; } printk("注册软中断成功\n"); //获取软中断号3 irqno3 = irq_of_parse_and_map(node,1); if(irqno3 == 0) { printk("获取软中断号失败\n"); return EINVAL; } printk("获取软中断成功\n"); //终端注册进内核 ret = request_irq(irqno3,irq_handler3,IRQF_TRIGGER_FALLING,"key3",NULL); if(ret) { printk("注册软中断号失败\n"); return EINVAL; } printk("注册软中断成功\n"); return 0; } //出口函数 static void __exit mycdev_exit(void) { free_irq(irqno1,NULL); free_irq(irqno2,NULL); free_irq(irqno3,NULL); // 6.释放申请的gpio编号 gpiod_put(gpiono1); gpiod_put(gpiono2); gpiod_put(gpiono3); } module_init(mycdev_init); module_exit(mycdev_exit); // GPL协议 MODULE_LICENSE("GPL");