• 华清远见上海中心22071班


    #include

    #include

    #include

    #include

    #includegpio.h>

    #include

    #include

    #include

    #include

    /*myirq{

        interrupt-parent = <&gpiof>;

         interrupts = <7 0>, <8 0>, <9 0>;//第一个成员指的是gpio编号,第二个成员表示触发方式,0表示默认属性

    };*/

    //定义指针指向获取的设备树节点信息空间

     struct device_node *node;

     struct device_node *node1;

     struct gpio_desc * gpiono1;//用于接收gpio1编号

     struct gpio_desc * gpiono2;//用于接收gpio2编号

     struct gpio_desc * gpiono3;//用于接收gpio3编号

     int ret;

     unsigned int irqno_1;//用于接收软中断号

     unsigned int irqno_2;//用于接收软中断号

     unsigned int irqno_3;//用于接收软中断号

     //unsigned int count=0;//用于计数,中断发生一次+1

    //中断处理函数

    irqreturn_t irq_handler1(int irqno_1,void *dev)

    {

        gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));

        //printk("key2 interrupt....%d...\n",count);

        return IRQ_HANDLED;

    }

    irqreturn_t irq_handler2(int irqno_2,void *dev)

    {

        gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));

       

        return IRQ_HANDLED;

    }

    irqreturn_t irq_handler3(int irqno_3,void *dev)

    {

        gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));

       

        return IRQ_HANDLED;

    }

    static int __init mycdev_init(void)

    {

            //通过名字获取设备树节点信息

        node1=of_find_node_by_name(NULL,"myleds");

        if(node1==NULL)

        {

            printk("通过名字解析设备树节点失败\n");

            return -EFAULT;

        }

        printk("成功解析到灯的设备树节点\n");

        //获取并申请gpio编号

        gpiono1=gpiod_get_from_of_node(node1,"myled1",0,GPIOD_OUT_LOW,NULL);

        if(IS_ERR(gpiono1))

        {

            printk("获取gpio1编号失败\n");

            return PTR_ERR(gpiono1);

        }

        printk("获取gpio1编号成功\n");

         gpiono2=gpiod_get_from_of_node(node1,"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(node1,"myled3",0,GPIOD_OUT_LOW,NULL);

        if(IS_ERR(gpiono3))

        {

            printk("获取gpio3编号失败\n");

            return PTR_ERR(gpiono3);

        }

        printk("获取gpio3编号成功\n");

        //通过名字获取设备树节点信息

        node=of_find_node_by_name(NULL,"myirq");

        if(node==NULL)

        {

            printk("通过名字解析设备树节点失败\n");

            return -EFAULT;

        }

        printk("成功解析到按键中断设备树节点\n");

        //根据设备树节点信息获取软中断号

          irqno_1=irq_of_parse_and_map(node,0);

        if(irqno_1==0)

        {

            printk("获取软中断1号失败\n");

            return EINVAL;

        }

        printk("获取软中断1号成功\n");

        irqno_2=irq_of_parse_and_map(node,1);

        if(irqno_2==0)

        {

            printk("获取软中断2号失败\n");

            return EINVAL;

        }

        printk("获取软中断2号成功\n");

          irqno_3=irq_of_parse_and_map(node,2);

        if(irqno_3==0)

        {

            printk("获取软中断3号失败\n");

            return EINVAL;

        }

        printk("获取软中断3号成功\n");

        //将中断注册进内核

        ret=request_irq(irqno_1,irq_handler1,IRQF_TRIGGER_FALLING,"key1_inte",NULL);

        if(ret)

        {

            printk("注册中断1失败\n");

            return ret;

        }

        printk("注册中断1成功\n");

         ret=request_irq(irqno_2,irq_handler2,IRQF_TRIGGER_FALLING,"key2_inte",NULL);

        if(ret)

        {

            printk("注册中断2失败\n");

            return ret;

        }

        printk("注册中断2成功\n");

         ret=request_irq(irqno_3,irq_handler3,IRQF_TRIGGER_FALLING,"key3_inte",NULL);

        if(ret)

        {

            printk("注册中断3失败\n");

            return ret;

        }

        printk("注册中断3成功\n");

       

       return 0;

    }

    static void __exit mycdev_exit(void)

    {

        //卸载驱动前熄灭灯

        gpiod_set_value(gpiono1,0);

        gpiod_set_value(gpiono2,0);

        gpiod_set_value(gpiono3,0);

        //释放申请得到的gpio编号

        gpiod_put(gpiono1);

        gpiod_put(gpiono2);

        gpiod_put(gpiono3);

       //中断的注销

       free_irq(irqno_1,NULL);

       free_irq(irqno_2,NULL);

       free_irq(irqno_3,NULL);

    }

    module_init(mycdev_init);

    module_exit(mycdev_exit);

    MODULE_LICENSE("GPL");

  • 相关阅读:
    【机器学习】K-Means聚类的执行过程?优缺点?有哪些改进的模型?
    Burp Suite--Get acquainted for the first time
    “看片”神器没了,又将有谁突出重围?
    [kubernetes]二进制部署k8s集群-基于containerd
    Java 命令行工具
    Mathematica中的常用基本操作
    android 获取SD卡路径
    如何利用WhatsApp群发功能减轻团队工作量
    【附源码】计算机毕业设计SSM网上购物系统
    用OC预览bundle中的API和属性
  • 原文地址:https://blog.csdn.net/qq_60135456/article/details/128085206