LED灯的驱动,GPIO子系统,添加按键的中断处理

1.应用程序发送指令控制LED亮灭

2.按键1 按下,led1电位反转 按键2按下,led2电位反转 按键3 按下,led3电位反转

驱动程序:

#include 
#include 
#include
#include
#include
#include 
#include 

unsigned int major;
struct class *cls;
struct device *dev;

struct device_node *dev_led;
struct device_node *dev_key;

unsigned int irqno1;
unsigned int irqno2;
unsigned int irqno3;

struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;

//功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)

//中断处理函数
irqreturn_t my_handler1(int irq, void *dev) //key1
{
    //灯状态取反
    gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1));
    return IRQ_HANDLED;
}

irqreturn_t my_handler2(int irq, void *dev) //key2
{
    //灯状态取反
    gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
    return IRQ_HANDLED;
}

irqreturn_t my_handler3(int irq, void *dev) //key3
{
    //灯状态取反
    gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
    return IRQ_HANDLED;
}

// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd)
    {
    case LED_ON:
        switch (arg)
        {
        case 1:                           // LED1
            gpiod_set_value(gpiono1, 1); // LED1开灯
            break;
        case 2:                           // LED2
            gpiod_set_value(gpiono2, 1); // LED2开灯
            break;
        case 3:                          // LED3
            gpiod_set_value(gpiono3, 1); // LED3开灯
            break;
        }

        break;
    case LED_OFF:
        switch (arg)
        {
        case 1:
            gpiod_set_value(gpiono1, 0);
            break;
        case 2:
            gpiod_set_value(gpiono2, 0);
            break;
        case 3:
            gpiod_set_value(gpiono3, 0);
            break;
        }
    }
    return 0;
}

int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

//定义操作方法结构体变量并赋值
struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};


static int __init mycdev_init(void)
{
    int ret;
    int i;
    //***************字符设备***************//
    // 字符设备驱动注册
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);

    //向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");

    //向上提交设备节点信息
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");

    //*************LED**************//
    // 根据设备树节点的路径解析设备树信息
    dev_led = of_find_node_by_path("/leds");
    if (dev_led == NULL)
    {
        printk("解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("解析设备树节点成功\n");

    // 根据解析得到的设备树节点指针解析出LED1的gpio编号
    gpiono1 = gpiod_get_from_of_node(dev_led, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono1))
    {
        printk("解析gpio编号失败\n");
        return -PTR_ERR(gpiono1);
    }
    gpiono2 = gpiod_get_from_of_node(dev_led, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("解析gpio编号失败\n");
        return -PTR_ERR(gpiono2);
    }
    gpiono3 = gpiod_get_from_of_node(dev_led, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("解析gpio编号失败\n");
        return -PTR_ERR(gpiono3);
    }
    //*************KEY**************//
    // 根据设备树节点的路径解析设备树信息
    dev_key = of_find_node_by_path("/myirq");
    if (dev_key == NULL)
    {
        printk("解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("解析设备树节点成功\n");

    //*****KEY1*******//
    //根据设备树节点,解析软中断号
    irqno1 = irq_of_parse_and_map(dev_key, 0); //按键1索引号为 0
    if(!irqno1)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("解析软中断号成功 irqno1=%d\n", irqno1);
    //注册中断
    ret = request_irq(irqno1, my_handler1, IRQF_TRIGGER_FALLING, "key1", NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return -EFAULT;
    }
    printk("KEY1注册中断成功\n");

    //*****KEY2*******//
    //根据设备树节点,解析软中断号
    irqno2 = irq_of_parse_and_map(dev_key, 1); //按键2索引号为 1
    if(!irqno2)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("解析软中断号成功 irqno2=%d\n", irqno2);
    //注册中断
    ret = request_irq(irqno2, my_handler2, IRQF_TRIGGER_FALLING, "key2", NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return -EFAULT;
    }
    printk("KEY2注册中断成功\n");

    //*****KEY3*******//
    //根据设备树节点,解析软中断号
    irqno3 = irq_of_parse_and_map(dev_key, 2); //按键3索引号为 2
    if(!irqno3)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("解析软中断号成功 irqno3=%d\n", irqno3);
    //注册中断
    ret = request_irq(irqno3, my_handler3, IRQF_TRIGGER_FALLING, "key3", NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return -EFAULT;
    }
    printk("KEY3注册中断成功\n");

    return 0;
}

static void __exit mycdev_exit(void)
{
    //销毁节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }

    //销毁目录信息
    class_destroy(cls);

    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");

    //注销中断
    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);

    // 释放gpio编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);

}

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

应用层程序:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//功能码
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)

int main(int argc, char const *argv[])
{
    int a, b;

    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        printf("请输入指令\n");
        printf("0(关灯) 1(开灯)\n");

        printf("请输入:");
        scanf("%d", &a);

        printf("请输入要控制的灯 1(LED1) 2(LED2) 3(LED3):");
        scanf("%d", &b);

        switch (a)
        {
        case 1:
            ioctl(fd, LED_ON, b); //开灯
            break;
        case 0:
            ioctl(fd, LED_OFF, b); //关灯
            break;
        default:
            printf("输入错误\n");
            break;

        }
    }

    close(fd);

    return 0;
}

你可能感兴趣的:(驱动开发,单片机,驱动开发,c语言)