驱动 11/28

要求:三个按键实现按键中断,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/of_irq.h>
#include<linux/interrupt.h>
/*myirq{
    interrupt-parent = <&gpiof>;
     interrupts = <7 0>, <8 0>, <9 0>;//第一个成员指的是gpio编号,第二个成员表示触发方式,0表示默认属性
};*/
//定义指针指向获取的设备树节点信息空间
 struct device_node *node;
//定义指针指向获取中断的设备树节点信息空间
 struct device_node *nodezd;
 struct gpio_desc * gpiono1;//用于接收gpio编号
 struct gpio_desc * gpiono2;//用于接收gpio编号
 struct gpio_desc * gpiono3;//用于接收gpio编号
 int ret;
 //分配定时器对象
 struct timer_list mytimer;
//定时器的处理函数
unsigned int irqno1; //用于获取软中断号
unsigned int irqno2; //用于获取软中断号
unsigned int irqno3; //用于获取软中断号
int ret;
unsigned int count1 = 0;
unsigned int count2 = 0;
unsigned int count3 = 0;
irqreturn_t irq_handler1(int irqno1,void *dev)
{
    count1++;
    
    if(count1%2==1)
    {
        gpiod_set_value(gpiono1,1);
    }
    else
    {
        gpiod_set_value(gpiono1,0);
    }

    return IRQ_HANDLED;
}
irqreturn_t irq_handler2(int irqno2,void *dev)
{
    count2++;
    
    if(count2%2==1)
    {
        gpiod_set_value(gpiono2,1);
    }
    else
    {
        gpiod_set_value(gpiono2,0);
    }

    return IRQ_HANDLED;
}
irqreturn_t irq_handler3(int irqno3,void *dev)
{
    count3++;
    
    if(count3%2==1)
    {
        gpiod_set_value(gpiono3,1);
    }
    else
    {
        gpiod_set_value(gpiono3,0);
    }

    return IRQ_HANDLED;
}
static int __init mycdev_init(void)
{

    //通过名字获取设备树节点信息
    node=of_find_node_by_name(NULL,"myleds");
    if(node==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");
    //获取并申请gpio编号
    gpiono1=gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("获取gpio1编号失败\n");
        return PTR_ERR(gpiono1);
    }
    printk("获取gpio1编号成功\n");
    //获取并申请gpio编号
    gpiono2=gpiod_get_from_of_node(node,"myled2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono2))
    {
        printk("获取gpio2编号失败\n");
        return PTR_ERR(gpiono2);
    }
    printk("获取gpio2编号成功\n");
    gpiono3=gpiod_get_from_of_node(node,"myled3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono3))
    {
        printk("获取gpio3编号失败\n");
        return PTR_ERR(gpiono3);
    }
    printk("获取gpio3编号成功\n");
      //通过名字获取设备树节点信息
    nodezd=of_find_node_by_name(NULL,"myirq");
    if(node==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功获取到中断的设备树结点\n");
    //根据设备树结点信息获取软中断号
    irqno1=irq_of_parse_and_map(nodezd,2);
    if(irqno1==0)
    {
        printk("获取软中断号失败\n");
        return -EINVAL;
    }
    irqno2=irq_of_parse_and_map(nodezd,0);
    if(irqno2==0)
    {
        printk("获取软中断号失败\n");
        return -EINVAL;
    }
    irqno3=irq_of_parse_and_map(nodezd,1);
    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;
    }
    ret=request_irq(irqno2,irq_handler2,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    ret=request_irq(irqno3,irq_handler3,IRQF_TRIGGER_FALLING,"key3_inte",NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    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);
    //释放申请得到的gpio编号
    gpiod_put(gpiono1);
    gpiod_set_value(gpiono2,0);
    //释放申请得到的gpio编号
    gpiod_put(gpiono2);
     gpiod_set_value(gpiono3,0);
    //释放申请得到的gpio编号
    gpiod_put(gpiono3);

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值