.基于GPIO子系统实现led灯驱动的编写,应用层控制三个灯的亮灭

驱动

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/poll.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>

/*myleds{
    led1=<&gpioe 10 0>;//&gpioe表示引用gpioe控制器,10表示使用gpioe10,0表示gpio按照默认状态设置
    led2=<&gpiof 10 0>;
    led3=<&gpioe 8 0>; 
 };*/
struct cdev *cdev;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;
unsigned int major = 0;
struct device_node *node;
struct gpio_desc *gpiono,*gpiono1,*gpiono2;
int minor = 0;
dev_t devon;
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 *off)
{
    int ret; 
    //判断kbuf的大小,如果<size,把size的数值修改为kbuf的大小
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;//拷贝失败返回错误码
    }
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t  mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *off)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;//拷贝失败返回错误码
    }
    switch(kbuf[0])
    {
        case '1':
            gpiod_set_value(gpiono,1);
            break;
        case '2':
            gpiod_set_value(gpiono,0);
            break;
        case '3':
            gpiod_set_value(gpiono1,1);
            break;
        case '4':
            gpiod_set_value(gpiono1,0);
            break;
        case '5':
            gpiod_set_value(gpiono2,1);
            break;
        case '6':
            gpiod_set_value(gpiono2,0);
            break;
        default:
            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,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
};

static int __init mycdev_init(void)
{
    int ret;
    //设备驱动申请空间
    cdev=cdev_alloc();
    if(cdev == NULL)
    {
        printk("设备驱动申请空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    //初始化设备驱动对象
    cdev_init(cdev,&fops);
    //动态申请设备号
    ret = alloc_chrdev_region(&devon,minor,1,"myled");
    if(ret)
    {
        printk("动态申请设备号失败\n");
        goto ERR2;
    }
    major = MAJOR(devon);
    minor = MINOR(devon);
    //将驱动注册进内核
    ret = cdev_add(cdev,MKDEV(major,minor),1);
    if(ret)
    {
        printk("字符设备驱动注册失败\n");
        goto ERR3;
    }
    printk("字符设备驱动对象注册成功\n");
    //自动创建设备节点
    //向上提交目录
    cls = class_create(THIS_MODULE,"myled");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = PTR_ERR(cls);
        goto ERR4;
    }
    //向上提交设备节点
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev0");
    if(IS_ERR(dev))
    {
        printk("向上提高目录失败\n");
        ret = PTR_ERR(dev);
        goto ERR5;
    }
    printk("向上提交设备节点成功\n");

    //解析设备树节点
    node=of_find_node_by_path("/myleds");
    if(node==NULL)
    {
        printk("设备树节点信息解析失败\n");
        return -ENOENT;
    }
    printk("设备树节点解析成功\n");
    gpiono = gpiod_get_from_of_node(node,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取并注册gpio失败\n");
        ret = PTR_ERR(gpiono);
        goto ERR6;
    }
    gpiono1 = gpiod_get_from_of_node(node,"led2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取并注册gpio失败\n");
        ret = PTR_ERR(gpiono);
        goto ERR7;
    }
        gpiono2 = gpiod_get_from_of_node(node,"led3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取并注册gpio失败\n");
        ret = PTR_ERR(gpiono);
        goto ERR8;
    }
    
    return 0;

ERR8:
    //先灭灯
    gpiod_set_value(gpiono2,0);
    gpiod_put(gpiono2);
ERR7:
    //先灭灯
    gpiod_set_value(gpiono1,0);
    gpiod_put(gpiono1);
ERR6:
    //先灭灯
    gpiod_set_value(gpiono,0);
    gpiod_put(gpiono);    
ERR5:
    device_destroy(cls,MKDEV(major,0));    
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),1);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //先灭灯,释放gpio编号
    gpiod_set_value(gpiono2,0);
    gpiod_put(gpiono2);
    gpiod_set_value(gpiono1,0);
    gpiod_put(gpiono1);
    gpiod_set_value(gpiono,0);
    gpiod_put(gpiono);
    
    //销毁设备节点
    device_destroy(cls,MKDEV(major,0));
    //销毁目录
    class_destroy(cls);
    //注销字符设备驱动
    cdev_del(cdev);
    //释放设备号
     unregister_chrdev_region(MKDEV(major,minor),3);
     //释放驱动对象空间
     kfree(cdev);
}

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

程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int fd;
char buf[128] = {0};

int main(int argc, const char *argv[])
{
    fd = open("/dev/mycdev0",O_RDWR);
    if(fd < 0)
    {
        printf("设备打开失败\n");
        exit(-1);
    }
    int i = 0;
    while(1)
    {
        if(i==6)
        {
            i = 0;
        }
        buf[0] = '1' + i;
        write(fd,buf,sizeof(buf));
        sleep(1);
        i++;
    }    
    close(fd);

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值