Writing LED driver based on GPIO subsystem

1. Application

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

int main(int argc, char const *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        // 从终端读取
        printf("请输入两个字符\n");
        printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");
        printf("第二个字符:0(关灯) 1(开灯)\n");
        printf("请输入>");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        // 向设备文件中写
        write(fd, buf, sizeof(buf));
    }

    close(fd);

    return 0;
}

2. Driver code

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

struct device_node *dnode;
struct gpio_desc *gpiono1,*gpiono2,*gpiono3;
char kbuf[128] = {0};
int major;
struct timer_list mytimer;

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    unsigned long ret;
    // 向用户空间读取拷贝
    if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret) // 拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    unsigned long ret;
    // 从用户空间读取数据
    if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size = sizeof(kbuf);
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret) // 拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    switch(kbuf[0]){
        case '1'://LED1
            if(kbuf[1]=='0')//关灯
                gpiod_set_value(gpiono1, 0);
            else//开灯
                gpiod_set_value(gpiono1, 1);
            break;
        case '2'://LED2
            if(kbuf[1]=='0')//关灯
                gpiod_set_value(gpiono2, 0);
            else//开灯
                gpiod_set_value(gpiono2, 1);
            break;
        case '3'://LED3
            if(kbuf[1]=='0')//关灯
                gpiod_set_value(gpiono3, 0);
            else//开灯
                gpiod_set_value(gpiono3, 1);
            break;
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

//定时器处理函数
 void mytimer_function(struct timer_list *timer)
 {
    printk("hello world!\n");
    //再次启用定时器
    mod_timer(timer,jiffies+5*HZ);
 }

// 定义操作方法结构体变量并赋值
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    // 字符设备驱动注册
    major = register_chrdev(0, "mychrdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("主设备号:%d\n", major);
    // 解析设备树节点信息
    dnode = of_find_node_by_path("/myled");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    // 获取LED1 GPIO编号
    gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono1))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono1);
    }
    // 获取LED2 GPIO编号
    gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono2))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono2);
    }
    // 获取LED3 GPIO编号
    gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono3))
    {
        printk("申请gpio信息失败\n");
        return -PTR_ERR(gpiono3);
    }

     //初始化定时器对象
    timer_setup(&mytimer,mytimer_function,0);
    mytimer.expires=jiffies+5*HZ;//定时1s;
    //注册定时器
    add_timer(&mytimer);
    return 0;
}
static void __exit mycdev_exit(void)
{
    gpiod_set_value(gpiono1, 0);
    gpiod_set_value(gpiono2, 0);
    gpiod_set_value(gpiono3, 0);
    // 释放GPIO编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");

    //注销定时器
    del_timer(&mytimer);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

Guess you like

Origin blog.csdn.net/weixin_53478812/article/details/134107647