通过platform总线驱动框架编写LED灯的驱动

通过platform总线驱动框架编写LED灯的驱动,编写应用程序测试
在这里插入图片描述
pdrv.c


#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/uaccess.h>
#include <linux/io.h>

int major; // 保存申请的主设备号
char kbuf[128]={0};
//主设备号
int major;
 
//用于上传目录和设备节点信息
struct class *cls;
struct device *device;
 
// led设备号
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;
 // 封装常用的操作方法
int mychrdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mychrdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     unsigned int ret;
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user err\n");
        return -ret;
    }
    return 0;
}
ssize_t mychrdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{
    //ubuf就是应用程序中write函数第二个参数传过来的值
    //size就是应用程序中write函数第三个参数传过来的值
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    unsigned int ret;
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user err\n");
        return -ret;
    }
    if(kbuf[0]=='0')//关灯
    {
        gpiod_set_value(gpiono1, 0);
        gpiod_set_value(gpiono2, 0);
        gpiod_set_value(gpiono3, 0);
    }
    else if(kbuf[0]=='1')//开led1灯
    {
        gpiod_set_value(gpiono1, 1);
    }
    else if(kbuf[0]=='2')//开led2灯
    {
        gpiod_set_value(gpiono2, 1);
    }
    else if(kbuf[0]=='3')//开led3灯
    {
        gpiod_set_value(gpiono3, 1);
    }
    return 0;
}
int mychrdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
struct file_operations fops = {
    .open=mychrdev_open,
    .release=mychrdev_close,
    .read=mychrdev_read,
    .write=mychrdev_write,
};
// 封装probe函数,当设备和驱动匹配成功之后执行
int pdrv_probe(struct platform_device *dev)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
 
    // 字符设备驱动注册
    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);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    int i; // 向上提交三次设备节点信息
    for (i = 0; i < 3; i++)
    {
        device = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }
 
    printk("向上提交设备节点成功\n");
 
    // 解析LED1的gpio编号
    gpiono1 = gpiod_get_from_of_node(dev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono1 == NULL)
    {
        printk("解析led1对应gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析led1对应gpio编号成功\n");
    // 解析LED1的gpio编号
    gpiono2 = gpiod_get_from_of_node(dev->dev.of_node, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono2 == NULL)
    {
        printk("解析led2对应gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析led2对应gpio编号成功\n");
    // 解析LED1的gpio编号
    gpiono3 = gpiod_get_from_of_node(dev->dev.of_node, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    if (gpiono3 == NULL)
    {
        printk("解析led3对应gpio编号失败\n");
        return -ENXIO;
    }
    printk("解析led3对应gpio编号成功\n");
 
    return 0;
}
// 封装remove函数,用于驱动和设备卸载时执行
int pdrv_remove(struct platform_device *dev)
{
    // 销毁设备节点信息
    device_destroy(cls, MKDEV(major, 0));
 
    // 销毁设备节点信息
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
 
    // 释放gpio编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
 
    // 销毁目录
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
 
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
// 构建用于匹配的设备树表
struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {/* end node */}, // 防止数组越界
};
// 分配驱动对象并初始化
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "bbbbb",
        .of_match_table = oftable, // 设置设备树匹配
    },
 
};
// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

test.c

#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] = {};
    int fd = open("/dev/mychrdev", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        return -1;
    }
    while (1)
    {
        printf("请输入控制码:1(led1开灯) 2(led2开灯) 3(led3开灯) 0(关灯)>");
        fgets(buf, sizeof(buf), stdin); // 从终端输入一个数据
        buf[strlen(buf) - 1] = '\0';
        write(fd, buf, sizeof(buf));
    }
    close(fd);
    return 0;
}

在设备树文件中添加设备树节点信息
stm32mp157a-fsmp1a.dts
在这里插入图片描述

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值