驱动day9

思维导图

在这里插入图片描述

练习

在这里插入图片描述
设备树

 homework_230828{
                compatible = "hqyj,homework_230828";
                interrupt-parent = <&gpiof>;
                interrupts = <9 0>, <7 0>, <8 0>;
                led-gpios = <&gpioe 10 0>, <&gpiof 10 0>, <&gpioe 8 0>;
        };

应用程序

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *agrv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mycdev0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    printf("打开设备文件成功\n");

    while (1)
    {
        read(fd, buf, sizeof(buf));
        printf("%s\n", buf);
    }
    close(fd);
    return 0;
}

驱动

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>

/*
   homework_230828{
                compatible = "hqyj,homework_230828";
                interrupt-parent = <&gpiof>;
                interrupts = <9 0>, <7 0>, <8 0>;
                led-gpios = <&gpioe 10 0>, <&gpiof 10 0>, <&gpioe 8 0>;
        };
};
*/
char kbuf[128] = {0};
unsigned int number = 0;
struct device_node *dnode;
struct gpio_desc *gpionum1;
struct gpio_desc *gpionum2;
struct gpio_desc *gpionum3;
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;
unsigned int key_irq[3]; // 用于存放软终端号的数组
unsigned int condition = 0;
// 定义等待队列头
wait_queue_head_t wq_head;

// 封装操作方法
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    wait_event_interruptible(wq_head, condition);
    int ret = copy_to_user(ubuf, kbuf, size);
    if (ret)
    {
        printk("copy_to_user failed\n");
        return ret;
    }
    condition = 0; // 表示下次的数据没有准备好

    return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
    .read = mycdev_read,
};

// 定义中断处理函数
irqreturn_t key_handler(int irq, void *dev)
{
    // 根据dev数值的不同来区分不同的按键
    int which = (int)dev;
    switch (which)
    {
    case 0: // key1
        printk("key1 interrupt!\n");
        number = !gpiod_get_value(gpionum1);
        sprintf(kbuf, "led1_number = %d", number);
        gpiod_set_value(gpionum1, !gpiod_get_value(gpionum1)); // 实现LED1状态取反
        break;
    case 1: // key2
        printk("key2 interrupt!\n");
        number = !gpiod_get_value(gpionum2);
        sprintf(kbuf, "led2_number = %d", number);
        gpiod_set_value(gpionum2, !gpiod_get_value(gpionum2)); // 实现LED2状态取反

        break;
    case 2: // key3
        printk("key3 interrupt!\n");
        number = !gpiod_get_value(gpionum3);
        sprintf(kbuf, "led3_number = %d", number);
        gpiod_set_value(gpionum3, !gpiod_get_value(gpionum3)); // 实现LED3状态取反
        break;
    }
    condition = 1;
    wake_up_interruptible(&wq_head); // 唤醒休眠的进程

    return IRQ_HANDLED;
}

static int __init mycdev_init(void)
{

    // 解析按键的设备树节点
    dnode = of_find_compatible_node(NULL, NULL, "hqyj,homework_230828");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");

    // 根据按键的设备树节点解析按键的软中断号
    int i;
    for (i = 0; i < 3; i++)
    {
        key_irq[i] = irq_of_parse_and_map(dnode, i);
        if (!key_irq[i])
        {
            printk("解析按键%d的软中断号失败\n", i + 1);
            return -1;
        }
        printk("解析按键%d的软中断号成功%d\n", i + 1, key_irq[i]);

        // 注册中断
        int ret = request_irq(key_irq[i], key_handler, IRQF_TRIGGER_FALLING, "key", (void *)i);
        if (ret < 0)
        {
            printk("注册中断%d失败\n", i + 1);
            return -1;
        }
        printk("注册中断%d成功\n", i + 1);
    }

    // 1.分配字符设备驱动对象
    int ret;
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("申请字符设备驱动对象成功\n");

    // 2.初始化字符设备驱动对象
    cdev_init(cdev, &fops);

    // 3.申请设备号
    if (major > 0) // 静态指定
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "mycdev");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    }
    else
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
        if (ret)
        {
            printk("动态指定设备号失败\n");
            goto OUT2;
        }
        minor = MINOR(devno); // 根据设备号获取次设备号
        major = MAJOR(devno); // 根据设备号获取主设备号
    }
    printk("申请设备号成功\n");

    // 4.注册字符设备驱动对象
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");

    // 5.向上提交设备目录
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录成功\n");

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

    // 解析LED设备树节点
    dnode = of_find_node_by_path("/homework_230828");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");

    // 获取GPIO_desc对象并且设置输出为低电平
    gpionum1 = gpiod_get_from_of_node(dnode, "led-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (gpionum1 == NULL)
    {
        printk("gpio1信息解析失败\n");
        return -PTR_ERR(gpionum1);
    }
    printk("解析gpio1信息成功\n");
    gpionum2 = gpiod_get_from_of_node(dnode, "led-gpios", 1, GPIOD_OUT_LOW, NULL);
    if (gpionum2 == NULL)
    {
        printk("gpio2信息解析失败\n");
        return -PTR_ERR(gpionum2);
    }
    printk("解析gpio2信息成功\n");
    gpionum3 = gpiod_get_from_of_node(dnode, "led-gpios", 2, GPIOD_OUT_LOW, NULL);
    if (gpionum3 == NULL)
    {
        printk("gpio3信息解析失败\n");
        return -PTR_ERR(gpionum3);
    }
    printk("解析gpio3信息成功\n");

    // 初始化等待队列头
    init_waitqueue_head(&wq_head);

    return 0;
OUT5:
    for (--i; i >= 0; i--)
        device_destroy(cls, MKDEV(major, i)); // 释放提交成功的设备信息
    class_destroy(cls);                       // 销毁目录
OUT4:
    cdev_del(cdev);
OUT3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
OUT2:
    kfree(cdev);
OUT1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    int i;
    // 注销中断
    for (i = 0; i < 3; i++)
    {
        free_irq(key_irq[i], (void *)i);
        printk("注销中断%d成功\n", i + 1);
    }

    // 销毁设备节点信息
    for (i = 0; i < 3; i++)
        device_destroy(cls, MKDEV(major, i));
    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动对象
    cdev_del(cdev);
    // 释放设备号
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 释放对象空间
    kfree(cdev);

    // 灭灯
    gpiod_set_value(gpionum1, 0);
    gpiod_set_value(gpionum2, 0);
    gpiod_set_value(gpionum3, 0);
    gpiod_put(gpionum1);
    gpiod_put(gpionum2);
    gpiod_put(gpionum3);
}
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、付费专栏及课程。

余额充值