驱动开发 day7

gpio子系统编写LED驱动

head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 

#define LED_ON _IO('l', 1)  //开灯
#define LED_OFF _IO('l', 0) //关灯

#endif 

text.c

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

int main(int argc, char const *argv[])
{
    int a, b, fd;
    int result;
    char buf[128] = {0};

    while (1)
    {
        printf("请选择要实现的功能\n");
        printf("0(关灯) 1(开灯) 2(退出)>");
        scanf("%d", &a);

        if (a == 2)
        {
            break; // 用户选择退出
        }

        // 清空输入缓冲区
        while ((getchar()) != '\n');

        if (a != 0 && a != 1)
        {
            printf("无效的选择,请重新输入。\n");
            continue; // 用户输入无效,重新循环
        }

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

        if (b < 1 || b > 3)
        {
            printf("无效的选择,请重新输入。\n");
            continue; // 用户输入无效,重新循环
        }

        // 清空输入缓冲区
        while ((getchar()) != '\n');

        // 定义设备文件路径
        char devicePath[20];
        sprintf(devicePath, "/dev/mychrdev%d", b - 1);

        // 打开设备文件
        fd = open(devicePath, O_RDWR);
        if (fd < 0)
        {
            perror("打开设备文件失败");
            exit(-1);
        }

        // 控制LED
        if (a == 1)
            result = ioctl(fd, LED_ON);
        else if (a == 0)
            result = ioctl(fd, LED_OFF);

        if (result == -1)
            perror("ioctl error");

        close(fd);
    }

    return 0;
}

demo.c

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

int major;
char kbuf[128] = {0};
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
struct device_node *dnode;
struct gpio_desc *gpio_leds[3]; // 三个LED的GPIO描述符

int mycdev_open(struct inode *inode, struct file *file)
{
    int min = MINOR(inode->i_rdev);
    file->private_data = (void *)min;
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int min = (int)file->private_data;

    // 进行功能控制
    switch (min)
    {
    case 0:
        switch (cmd)
        {
        case LED_ON: // 开灯
            gpiod_set_value(gpio_leds[0], 1);
            break;
        case LED_OFF: // 关灯
            gpiod_set_value(gpio_leds[0], 0);
            break;
        }
        break;

    case 1:
        switch (cmd)
        {
        case LED_ON: // 开灯
            gpiod_set_value(gpio_leds[1], 1);
            break;
        case LED_OFF: // 关灯
            gpiod_set_value(gpio_leds[1], 0);
            break;
        }
        break;

    case 2:
        switch (cmd)
        {
        case LED_ON: // 开灯
            gpiod_set_value(gpio_leds[2], 1);
            break;
        case LED_OFF: // 关灯
            gpiod_set_value(gpio_leds[2], 0);
            break;
        }
        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 i;
    // 字符设备驱动注册
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mychrdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    // 解析led灯的设备树节点
    dnode = of_find_node_by_path("/myleds");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");

    for (i = 0; i < 3; i++)
    {
        char nodename[20];

        // 向上提交设备节点信息,为三盏灯创建三个设备文件
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mychrdev%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            return -PTR_ERR(dev);
        }

        sprintf(nodename, "led%d", i + 1);
        // 根据设备树节点解析每个LED的GPIO结构体并向内核注册
        gpio_leds[i] = gpiod_get_from_of_node(dnode, nodename, 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(gpio_leds[i]))
        {
            printk("申请gpio失败\n");
            return -PTR_ERR(gpio_leds[i]);
        }
    }

    return 0;
}
static void __exit mycdev_exit(void)
{
    int i;

    for (i = 0; i < 3; i++)
    {
        // 销毁设备节点信息
        device_destroy(cls, MKDEV(major, i));
        // 灭灯
        gpiod_set_value(gpio_leds[i], 0);
        // 注销GPIO信息
        gpiod_put(gpio_leds[i]);
    }

    // 销毁目录信息
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

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