驱动开发day8

编写LED灯的驱动,使用GPIO子系统,里面添加按键的中断处理

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

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

驱动程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
struct device_node *dev_key;
unsigned int irqno_key1;
unsigned int irqno_key2;
unsigned int irqno_key3;

struct device_node *dev_led;
struct gpio_desc *gpiono_led1;
struct gpio_desc *gpiono_led2;
struct gpio_desc *gpiono_led3;

int major;
struct class *cls;
struct device *dev;
// 中断处理函数
irqreturn_t myirq_handler_key1(int irq, void *dev)
{
    gpiod_set_value(gpiono_led1,!gpiod_get_value(gpiono_led1));
    return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key2(int irq, void *dev)
{
    gpiod_set_value(gpiono_led2,!gpiod_get_value(gpiono_led2));
    return IRQ_HANDLED;
}
irqreturn_t myirq_handler_key3(int irq, void *dev)
{
    gpiod_set_value(gpiono_led3,!gpiod_get_value(gpiono_led3));
    return IRQ_HANDLED;
}
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(gpiono_led1,1);; // LED1开灯
            break;
        case 2:                           // LED2
            gpiod_set_value(gpiono_led2,1); // LED2开灯
            break;
        case 3:                          // LED3
            gpiod_set_value(gpiono_led3,1); // LED3开灯
            break;
        }
 
        break;
    case LED_OFF:
        switch (arg)
        {
        case 1:
            gpiod_set_value(gpiono_led1,0);
            break;
        case 2:
            gpiod_set_value(gpiono_led2,0);
            break;
        case 3:
            gpiod_set_value(gpiono_led3,0);
            break;
        }
 
        break;
    }
    return 0;
}
struct file_operations fops = {
 
    
    .unlocked_ioctl = mycdev_ioctl,
   
};
static int __init mycdev_init(void)
{
    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");

    int ret;
    // 解析按键的设备树节点
    dev_key = of_find_node_by_path("/myirq");
    if (dev_key == NULL)
    {
        printk("解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("解析设备树节点成功\n");
    // 根据设备树节点解析出软中断号
    irqno_key1 = irq_of_parse_and_map(dev_key, 0); // 按键1索引号为0
    if (!irqno_key1)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("key1解析软中断号成功 irqno=%d\n", irqno_key1);
    irqno_key2 = irq_of_parse_and_map(dev_key, 1); // 按键1索引号为0
    if (!irqno_key2)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("key2解析软中断号成功 irqno=%d\n", irqno_key2);
    irqno_key3 = irq_of_parse_and_map(dev_key, 2); // 按键1索引号为0
    if (!irqno_key3)
    {
        printk("解析软中断号失败\n");
        return -ENXIO;
    }
    printk("key3解析软中断号成功 irqno=%d\n", irqno_key3);
    // 注册中断
    ret = request_irq(irqno_key1, myirq_handler_key1, IRQF_TRIGGER_FALLING, "key1", NULL);
    if (ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    printk("key1注册中断成功\n");
    ret = request_irq(irqno_key2, myirq_handler_key2, IRQF_TRIGGER_FALLING, "key2", NULL);
    if (ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    printk("key2注册中断成功\n");
    ret = request_irq(irqno_key3, myirq_handler_key3, IRQF_TRIGGER_FALLING, "key3", NULL);
    if (ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    printk("key3注册中断成功\n");


   // 根据设备树节点的路径解析设备树信息
    dev_led = of_find_node_by_path("/leds");
    if (dev_led == NULL)
    {
        printk("解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("解析设备树节点成功\n");
    // 申请gpio_desc对象并设置输出为低电平
    gpiono_led1 = gpiod_get_from_of_node(dev_led, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono_led1))
    {
        printk("申请gpio对象失败\n");
        return -PTR_ERR(gpiono_led1);
    }
    printk("申请gpio_led1对象成功\n");
        gpiono_led2 = gpiod_get_from_of_node(dev_led, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono_led2))
    {
        printk("申请gpio对象失败\n");
        return -PTR_ERR(gpiono_led2);
    }
    printk("申请gpio_led1对象成功\n");
        gpiono_led3 = gpiod_get_from_of_node(dev_led, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono_led3))
    {
        printk("申请gpio对象失败\n");
        return -PTR_ERR(gpiono_led3);
    }
    printk("申请gpio_led1对象成功\n");

    return 0;
}
static void __exit mycdev_exit(void)
{
    // 注销中断
    free_irq(irqno_key1, NULL);
    free_irq(irqno_key2, NULL);
    free_irq(irqno_key3, NULL);

    
    // 灭灯
    gpiod_set_value(gpiono_led1, 0);
    // 释放gpio编号
    gpiod_put(gpiono_led1);
       // 灭灯
    gpiod_set_value(gpiono_led2, 0);
    // 释放gpio编号
    gpiod_put(gpiono_led2);
       // 灭灯
    gpiod_set_value(gpiono_led3, 0);
    // 释放gpio编号
    gpiod_put(gpiono_led3);

        int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用程序

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

int main(int argc, char const *argv[])
{
    int a, b;
    char buf[128] = {0};
    int fd0 = open("/dev/mycdev0", O_RDWR);
    if (fd0 < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    int fd1 = open("/dev/mycdev1", O_RDWR);
    if (fd1 < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    int fd2 = open("/dev/mycdev2", O_RDWR);
    if (fd2 < 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 (b)
        {
        case 1:
            switch (a)
            {
            case 1:
                ioctl(fd0, LED_ON); // 开灯
                break;
            case 0:
                ioctl(fd0, LED_OFF);
                break;
            }
            break;
        case 2:
            switch (a)
            {
            case 1:
                ioctl(fd1, LED_ON); // 开灯
                break;
            case 0:
                ioctl(fd1, LED_OFF);
                break;
            }
            break;
        case 3:
            switch (a)
            {
            case 1:
                ioctl(fd2, LED_ON); // 开灯
                break;
            case 0:
                ioctl(fd2, LED_OFF);
                break;
            }
            break;
        }
    }

    close(fd0);
    close(fd1);
    close(fd2);

    return 0;
}

你可能感兴趣的:(驱动开发)