驱动开发作业3——GPIO子系统

作业1:在内核模块中启用定时器,定时1s,让LED1以1s为周期实现流水灯

myled.c(驱动文件)
cpp 复制代码
/*
 * Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
 * @Author: Huijie Xia
 * @Date: 2023-06-29 08:56:26
 * @LastEditTime: 2023-07-11 15:36:16
 * @FilePath: /B_Drive/day8/01_myled/myled.c
 * @version:
 * @Description: GPIO子系统
 */
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>

struct device_node* dnode;
struct timer_list mytimer; /* 定义计时器对象 */
unsigned int gpiono;

void mytimer_func(struct timer_list* timer)
{
    if (gpio_get_value(gpiono) == 0) {
        /* 设置管脚输出高电平,点亮LED */
        gpio_set_value(gpiono, 1);
    } else {
        /* 设置管脚输出低电平,熄灭LED */
        gpio_set_value(gpiono, 0);
    }
    mod_timer(&mytimer, jiffies + HZ);
}

/**
 * @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
 * demo_init:驱动入口的名字
 * __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
 * @return {*}
 */
static int __init demo_init(void)
{
    int ret;
    /* 1.1 通过路径获取节点 */
    dnode = of_find_node_by_path("/myleds");
    if (dnode == NULL) {
        printk("get device node error! __%d__\n", __LINE__);
        return -ENXIO;
    }
    printk("get device node successful! __%d__\n", __LINE__);

    /* 1.2 根据设备树节点解析LED1的GPIO编号 */
    gpiono = of_get_named_gpio(dnode, "led1-gpios", 0);
    if (gpiono < 0) {
        printk("analysis GPIO number error! __%d__\n", __LINE__);
        return gpiono;
    }
    printk("analysis GPIO number successful! GPIO number = %d! __%d__\n", gpiono, __LINE__);

    /* 1.3 申请gpio编号 */
    ret = gpio_request(gpiono, NULL);
    if (ret < 0) {
        printk("request GPIO number error! __%d__\n", __LINE__);
        return ret;
    }
    printk("request GPIO number successful! __%d__\n", __LINE__);

    /* 1.4 设置LED1对应的GPIO管脚为输出模式 */
    ret = gpio_direction_output(gpiono, 0);
    if (ret < 0) {
        printk("set GPIO output error! __%d__\n", __LINE__);
        return ret;
    }
    printk("set GPIO output successful! __%d__\n", __LINE__);

    /* 2.1 初始化定时器对象 */
    mytimer.expires = jiffies + HZ; /* 设置1s定时 */
    timer_setup(&mytimer, mytimer_func, 0);

    /* 2.2 将定时器对象注册进入内核并启动 */
    add_timer(&mytimer);

    return 0;
}

/**
 * @description: 出口:在卸载驱动的时候出口函数执行,做资源释放的工作
 * demo_exit:驱动出口的名字
 * __exit:它是给编译器使用的,告诉编译器将这个函数放在.exit.text段中
 * @return {*}
 */
static void __exit demo_exit(void)
{
    /* 1. 设置管脚输出低点平,熄灭LED */
    gpio_set_value(gpiono, 0);

    /* 2. 释放gpio编号 */
    gpio_free(gpiono);

    /* 3. 注销定时器 */
    del_timer(&mytimer);
}

/* 告诉内核入口地址 */
module_init(demo_init);

/* 告诉内核出口地址 */
module_exit(demo_exit);

/* 许可证:遵从GPL协议 */
MODULE_LICENSE("GPL");

作业2:基于GPIO子系统完成LED灯驱动的注册,并利用应用程序测试

chrdevled.c(驱动文件)

cpp 复制代码
/*
 * Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
 * @Author: Huijie Xia
 * @Date: 2023-06-29 08:56:26
 * @LastEditTime: 2023-07-11 17:02:51
 * @FilePath: /B_Drive/day8/02_chrdevled/chrdevled.c
 * @version:
 * @Description: GPIO子系统
 */
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>

struct cdev* cdev;
struct class* cls;
struct device* dev;
struct device_node* dnode;
unsigned int gpiono[3];

/* !!!注意:静态指定时,主设备号major必须小于等于511!!! */
unsigned int major = 0;
unsigned int minor = 0;
const int count = 3;

#define CNAME "chrdevled"
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)

char* propname[3] = { "led1-gpios", "led2-gpios", "led3-gpios" };

int mycdev_open(struct inode* inode, struct file* file)
{
    /* 获取打开文件的次设备号 */
    unsigned int led_minor = MINOR(inode->i_rdev);
    /* 将获取的次设备号放入file结构体中private_data成员 */
    file->private_data = (void*)led_minor;

    // printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t mycdev_read(struct file* file, char __user* ubuf, size_t size, loff_t* offs)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf, size_t size, loff_t* offs)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(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)
{
    unsigned int led_minor = (int)file->private_data;
    // printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    switch (led_minor) {
    case 0: /* 控制LED1 */
        switch (cmd) {
        case LED_ON:
            /* 设置管脚输出高点平,点亮LED */
            gpio_set_value(gpiono[0], 1);
            break;

        case LED_OFF:
            /* 设置管脚输出低点平,熄灭LED */
            gpio_set_value(gpiono[0], 0);
            break;

        default:
            break;
        }
        break;

    case 1: /* 控制LED2 */
        switch (cmd) {
        case LED_ON:
            /* 设置管脚输出高点平,点亮LED */
            gpio_set_value(gpiono[1], 1);
            break;

        case LED_OFF:
            /* 设置管脚输出低点平,熄灭LED */
            gpio_set_value(gpiono[1], 0);
            break;

        default:
            break;
        }
        break;

    case 2: /* 控制LED3 */
        switch (cmd) {
        case LED_ON:
            /* 设置管脚输出高点平,点亮LED */
            gpio_set_value(gpiono[2], 1);
            break;

        case LED_OFF:
            /* 设置管脚输出低点平,熄灭LED */
            gpio_set_value(gpiono[2], 0);
        default:
            break;
        }
        break;

    default:
        break;
    }
    return 0;
}

struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

/**
 * @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
 * demo_init:驱动入口的名字
 * __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
 * @return {*}
 */
static int __init mychrdev_init(void)
{
    int ret, i;
    dev_t devno;

    /* 1.1 分配字符设备驱动对象空间 cdev_alloc */
    cdev = cdev_alloc();
    if (cdev == NULL) {
        printk("cdev alloc memory error  line : %d\n", __LINE__);
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("cdev alloc memory successful  line : %d\n", __LINE__);

    /* 1.2 字符设备驱动对象部分初始化 cdev_init */
    cdev_init(cdev, &fops);

    /* 1.3 申请设备号 */
    if (major > 0) {
        /* 静态申请设备号 register_chrdev_region */
        ret = register_chrdev_region(MKDEV(major, minor), count, CNAME);
        if (ret) {
            printk("static:request device number error  line : %d\n", __LINE__);
            goto ERR2;
        }
        printk("static:request device number successful  line : %d\n", __LINE__);
    } else {
        /* 动态申请设备号 alloc_chrdev_region */
        ret = alloc_chrdev_region(&devno, 0, count, CNAME);
        if (ret) {
            printk("dynamic:request device number error  line : %d\n", __LINE__);
            goto ERR2;
        }
        printk("dynamic:request device number successful  line : %d\n", __LINE__);
        major = MAJOR(devno);
        minor = MINOR(devno);
    }

    /* 1.4 注册字符设备驱动对象 cdev_add */
    ret = cdev_add(cdev, MKDEV(major, minor), count);
    if (ret) {
        printk("register char device driver error  line : %d\n", __LINE__);
        goto ERR3;
    }
    printk("register char device driver successful  line : %d\n", __LINE__);

    /* 2.1 向上提交目录信息 class_create */
    cls = class_create(THIS_MODULE, CNAME);
    if (IS_ERR(cls)) {
        printk("submit directory error  line : %d\n", __LINE__);
        goto ERR4;
    }
    printk("submit directory successful  line : %d\n", __LINE__);

    /* 2.2 向上提交设备节点信息 */
    for (i = 0; i < count; i++) {
        dev = device_create(cls, NULL, MKDEV(major, minor + i), NULL, "chrdevled%d", i);
        if (IS_ERR(dev)) {
            printk("submit device node infomation error  line : %d\n", __LINE__);
            goto ERR5;
        }
    }

    /* 3.1 通过路径获取节点 */
    dnode = of_find_node_by_path("/myleds");
    if (dnode == NULL) {
        printk("get device node error! __%d__\n", __LINE__);
        return -ENXIO;
    }
    printk("get device node successful! __%d__\n", __LINE__);

    for (i = 0; i < ARRAY_SIZE(propname); i++) {
        /* 3.2 根据设备树节点解析LED的GPIO编号 */
        gpiono[i] = of_get_named_gpio(dnode, propname[i], 0);
        if (gpiono[i] < 0) {
            printk("analysis %s GPIO number error! __%d__\n", propname[i], __LINE__);
            return gpiono[i];
        }
        printk("analysis %s GPIO number successful! GPIO number = %d! __%d__\n", propname[i], gpiono[i], __LINE__);

        /* 3.3 申请gpio编号 */
        ret = gpio_request(gpiono[i], NULL);
        if (ret < 0) {
            printk("request %s GPIO number error! __%d__\n", propname[i], __LINE__);
            return ret;
        }
        printk("request %s GPIO number successful! __%d__\n", propname[i], __LINE__);

        /* 3.4 设置LED对应的GPIO管脚为输出模式 */
        ret = gpio_direction_output(gpiono[i], 0);
        if (ret < 0) {
            printk("set %s GPIO output error! __%d__\n", propname[i], __LINE__);
            return ret;
        }
        printk("set %s GPIO output successful! __%d__\n", propname[i], __LINE__);
    }

    return 0;

ERR5:
    for (--i; i >= 0; i++) {
        device_destroy(cls, MKDEV(major, minor + i));
    }
    class_destroy(cls); /* 销毁目录 */

ERR4:
    cdev_del(cdev); /* 注销字符设备驱动对象 */

ERR3:
    unregister_chrdev_region(MKDEV(major, minor), count); /* 销毁设备号 */

ERR2:
    kfree(cdev); /* 释放对象空间 */

ERR1:
    return ret;
}

/**
 * @description: 出口:在卸载驱动的时候出口函数执行,做资源释放的工作
 * demo_exit:驱动出口的名字
 * __exit:它是给编译器使用的,告诉编译器将这个函数放在.exit.text段中
 * @return {*}
 */
static void __exit mychrdev_exit(void)
{
    int i;

    /* 1. 销毁提交设备节点信息 */
    for (i = 0; i < 3; i++) {
        device_destroy(cls, MKDEV(major, minor + i));
    }

    /* 2. 销毁目录 */
    class_destroy(cls);

    /* 3. 注册字符设备驱动对象 */
    cdev_del(cdev);

    /* 4. 销毁设备号 */
    unregister_chrdev_region(MKDEV(major, minor), count);

    /* 5. 释放对象空间 */
    kfree(cdev);

    /* 6. 设置管脚输出低点平,熄灭LED */
    for (i = 0; i < ARRAY_SIZE(propname); i++) {
        gpio_set_value(gpiono[i], 0);
    }

    /* 7. 释放gpio编号 */
    for (i = 0; i < ARRAY_SIZE(propname); i++) {
        gpio_free(gpiono[i]);
    }
}

/* 告诉内核入口地址 */
module_init(mychrdev_init);

/* 告诉内核出口地址 */
module_exit(mychrdev_exit);

/* 许可证:遵从GPL协议 */
MODULE_LICENSE("GPL");
test.c(测试文件)
cpp 复制代码
/*
 * Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
 * @Author: Huijie Xia
 * @Date: 2023-07-03 14:44:33
 * @LastEditTime: 2023-07-11 16:50:01
 * @FilePath: /B_Drive/day8/02_chrdevled/test.c
 * @version:
 * @Description: 驱动控制硬件的测试文件
 */

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)

#define ERR_MSG(msg)                              \
    do {                                          \
        fprintf(stderr, "line : %d\n", __LINE__); \
        perror(msg);                              \
    } while (0)

char buf[128] = { 0 };
int main(int argc, const char* argv[])
{
    int ledfd; /* 文件描述符 */
    int hardware_which; /* 硬件设备选择 */
    int choice; /* 设备选择 */
    int whichled; /* LED选择 */

    while (1) {
        printf("请选择要控制的LED\n");
        printf("1(LED1) 2(LED2) 3(LED3) > ");
        scanf("%d", &whichled);
        switch (whichled) {
        case 1:
            if ((ledfd = open("/dev/chrdevled0", O_RDWR)) == -1)
                ERR_MSG("chrdevled0 open error");
            while (1) {
                printf("请选择要实现的功能\n");
                printf("0(关灯) 1(开灯) 2(退出) > ");
                scanf("%d", &choice);
                if (choice == 0) {
                    ioctl(ledfd, LED_OFF);
                } else if (choice == 1) {
                    ioctl(ledfd, LED_ON);
                } else {
                    break;
                }
            }
            close(ledfd);
            break;

        case 2:
            if ((ledfd = open("/dev/chrdevled1", O_RDWR)) == -1)
                ERR_MSG("chrdevled1 open error");
            while (1) {
                printf("请选择要实现的功能\n");
                printf("0(关灯) 1(开灯) 2(退出) > ");
                scanf("%d", &choice);
                if (choice == 0) {
                    ioctl(ledfd, LED_OFF);
                } else if (choice == 1) {
                    ioctl(ledfd, LED_ON);
                } else {
                    break;
                }
            }
            close(ledfd);
            break;

        case 3:
            if ((ledfd = open("/dev/chrdevled2", O_RDWR)) == -1)
                ERR_MSG("chrdevled2 open error");
            while (1) {
                printf("请选择要实现的功能\n");
                printf("0(关灯) 1(开灯) 2(退出) > ");
                scanf("%d", &choice);
                if (choice == 0) {
                    ioctl(ledfd, LED_OFF);
                } else if (choice == 1) {
                    ioctl(ledfd, LED_ON);
                } else {
                    break;
                }
            }
            close(ledfd);
            break;

        default:
            break;
        }
    }
    return 0;
}
相关推荐
hhhhhhh_hhhhhh_13 小时前
rk3568制冷项目驱动开发流程汇总(只适用于部分模块CIF DVP等,自用)
驱动开发
icy、泡芙16 小时前
T527-----音频调试
linux·驱动开发·音视频
7yewh1 天前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
疯狂飙车的蜗牛1 天前
从零玩转CanMV-K230(4)-小核Linux驱动开发参考
linux·运维·驱动开发
嵌入式进阶行者2 天前
【驱动开发初级】内核模块静态和动态添加功能的步骤
驱动开发
逝灮2 天前
【蓝桥杯——物联网设计与开发】拓展模块3 - 温度传感器模块
驱动开发·stm32·单片机·嵌入式硬件·物联网·蓝桥杯·温度传感器
__NULL__USER3 天前
petalinux-adi ---添加AD9361驱动(二)
linux·驱动开发
7yewh4 天前
嵌入式驱动RK3566 HDMI eDP MIPI 背光 屏幕选型与调试提升篇-eDP屏
linux·arm开发·驱动开发·嵌入式硬件·嵌入式linux·rk·edp
少年、潜行5 天前
树莓派3B+驱动开发(8)- i2c控制PCF8591
驱动开发·树莓派·3b+
千千道5 天前
深入理解 Linux 内核启动流程
linux·arm开发·驱动开发