驱动DAY8

复制代码
#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>
/*       myled
    {
        led1-gpio=<&gpioe 10 0>;
        led2-gpio=<&gpiof 10 0>;
        led3-gpio=<&gpioe 8 0>;
    };
*/
struct device_node *dnode;
struct gpio_desc *gpiono;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
struct timer_list mytimer;
struct timer_list mytimer2;
void mytimer_function(struct timer_list *timer)
{
    gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
    gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
    gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
    mod_timer(timer, jiffies + HZ);
}

void mytimer_function2(struct timer_list *timer2)
{
    printk("hello world\n");
    mod_timer(timer2, jiffies + HZ*5);
}

static int __init mycdev_init(void)
{
    // 解析设备树节点信息
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }

    // 获取LED1 GPIO编号
    gpiono = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono);
    }
    // 灯亮
    //gpiod_set_value(gpiono, 1);

    // 获取LED2 GPIO编号
    gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono2);
    }
    // 灯亮
    //gpiod_set_value(gpiono2, 1);

    // 获取LED3 GPIO编号
    gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono3);
    }
    // 灯亮
    //gpiod_set_value(gpiono3, 1);
    timer_setup(&mytimer, mytimer_function, 0);
    mytimer.expires = jiffies + HZ;
    add_timer(&mytimer);

    timer_setup(&mytimer2, mytimer_function2, 0);
    mytimer2.expires = jiffies + 5*HZ;
    add_timer(&mytimer2);
    return 0;
}
static void __exit mycdev_exit(void)
{
    del_timer(&mytimer);
    del_timer(&mytimer2);
    gpiod_set_value(gpiono, 0);
    // 释放GPIO编号
    gpiod_put(gpiono);
    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");
相关推荐
阿里云大数据AI技术20 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
orion572 天前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站2 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户120487221612 天前
Linux驱动编译与加载
linux·嵌入式
火车叼位2 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房3 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站3 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维