驱动开发 作业 day7 9/18

基于GPIO子系统实现led灯点亮

head.h

#ifndef __HEAD_H__
#define __HEAD_H__ 

//构建LED开关的功能码,不添加ioctl第三个参数
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)

#endif 

test.c

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


int main(int argc, char const *argv[])
{
    int a;
    char buf[128] = {0};
    int fd = open("/dev/myled1", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        // 从终端读取
        printf("请输入对LED灯的控制:1(开灯)0(关灯)>");
        scanf("%d", &a);
        switch (a)
        {
        case 1:
            ioctl(fd, LED_ON); // 开灯
            break;
        case 0:
            ioctl(fd, LED_OFF); // 关灯
        }
    }

    close(fd);

    return 0;
}

mychrdev.c

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

struct cdev *cdev;
char kbuf[128] = {0};
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
module_param(major, uint, 0664); // 方便在命令行传递major的值
struct class *cls;
struct device *dev;
struct device_node *dnode;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;

// 封装操作方法
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: // 控制LED1
        switch (cmd)
        {
        case LED_ON:
            // 开灯
            gpiod_set_value(gpiono1, 1);
            break;
        case LED_OFF:
            // 关灯
            gpiod_set_value(gpiono1, 0);
            break;
        }
        break;
    case 1: // 控制LED2
        switch (cmd)
        {
        case LED_ON:
            // 开灯
            gpiod_set_value(gpiono2, 1);
            break;
        case LED_OFF:
            // 关灯
            gpiod_set_value(gpiono2, 0);
            break;
        }
        break;
    case 2: // 控制LED3
        switch (cmd)
        {
        case LED_ON:
            // 开灯
            gpiod_set_value(gpiono3, 1);
            break;
        case LED_OFF:
            // 关灯
            gpiod_set_value(gpiono3, 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 ret;
    // 字符设备注册
    // 1.申请驱动对象
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请对象空间失败!\n");
        ret = -EFAULT;
        goto out1;
    }
    printk("申请对象成功!\n");
    // 2.初始化驱动对象
    cdev_init(cdev, &fops);
    printk("初始化对象成功!\n");

    // 3.申请主设备号和一定数量设备资源
    if (major > 0) // 静态指定设备号
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
        if (ret)
        {
            printk("静态申请设备号失败!\n");
            goto out2;
        }
    }
    else if (major == 0) // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, 0, 3, "myled");
        if (ret)
        {
            printk("动态申请设备号失败!\n");
            goto out2;
        }
        major = MAJOR(devno); // 获取主设备号
        minor = MINOR(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, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败!\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功!\n");

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

    // 从内核获取设备节点
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析GPIO信息成功\n");

    // 对设备进行相关章节初始化  GPIO章节
    // 根据设备节点解析出gpio对象,并向内核申请
    gpiono1 = gpiod_get_from_of_node(dnode, "led-gpios", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono1))
    {
        printk("申请gpio失败\n");
    }
    printk("申请成功\n");

    gpiono2 = gpiod_get_from_of_node(dnode, "led-gpios", 1, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("申请gpio失败\n");
    }
    printk("申请成功\n");

    gpiono3 = gpiod_get_from_of_node(dnode, "led-gpios", 2, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("申请gpio失败\n");
    }
    printk("申请成功\n");

    /*
        // 获取GPIO信息
        gpiono1 = of_get_named_gpio(dnode, "led-gpios", 0);
        if (gpiono1 < 0)
        {
            printk("获取引脚编号1失败\n");
        }
        printk("获取引脚编号1成功\n");

        gpiono2 = of_get_named_gpio(dnode, "led-gpios", 1);
        if (gpiono2 < 0)
        {
            printk("获取引脚编号2失败\n");
        }
        printk("获取引脚编号2成功\n");

        gpiono3 = of_get_named_gpio(dnode, "led-gpios", 2);
        if (gpiono3 < 0)
        {
            printk("获取引脚编号3失败\n");
        }
        printk("获取引脚编号3成功\n");

        // 申请gpio编号
        ret = gpio_request(gpiono1, NULL);
        if (ret)
        {
            printk("申请gpio编号失败\n");
            return -1;
        }
        printk("申请gpio编号成功\n");

        ret = gpio_request(gpiono2, NULL);
        if (ret)
        {
            printk("申请gpio编号失败\n");
            return -1;
        }
        printk("申请gpio编号成功\n");

        ret = gpio_request(gpiono3, NULL);
        if (ret)
        {
            printk("申请gpio编号失败\n");
            return -1;
        }
        printk("申请gpio编号成功\n");
        // 设置为输出
        gpio_direction_output(gpiono1, 0);
        gpio_direction_output(gpiono2, 0);
        gpio_direction_output(gpiono3, 0);
    */
    return 0;
out5:
    for (--i; i > -1; 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++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁设备类目录
    class_destroy(cls);
    // 注销驱动
    cdev_del(cdev);
    // 注销设备号
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 释放cdev对象
    kfree(cdev);

    // 熄灭led灯
    gpiod_set_value(gpiono1, 0);
    gpiod_set_value(gpiono2, 0);
    gpiod_set_value(gpiono2, 0);
    // 释放gpio对象
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
}

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

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