通过GPIO子系统编写LED驱动,应用程序控制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 OUT1;
    }
    //初始化设备驱动对象
    cdev_init(cdev,&fops);
    //动态申请设备号
    ret = alloc_chrdev_region(&devon,minor,1,"myled");
    if(ret)
    {
        printk("动态申请设备号失败\n");
        goto OUT2;
    }
    major = MAJOR(devon);
    minor = MINOR(devon);
    //将驱动注册进内核
    ret = cdev_add(cdev,MKDEV(major,minor),1);
    if(ret)
    {
        printk("字符设备驱动注册失败\n");
        goto OUT3;
    }
    printk("字符设备驱动对象注册成功\n");
    //自动创建设备节点
    //向上提交目录
    cls = class_create(THIS_MODULE,"myled");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = PTR_ERR(cls);
        goto OUT4;
    }
    //向上提交设备节点
    dev = device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev0");
    if(IS_ERR(dev))
    {
        printk("向上提高目录失败\n");
        ret = PTR_ERR(dev);
        goto OUT5;
    }
    printk("向上提交设备节点成功\n");

    //解析设备树节点
    node=of_find_node_by_path("/myleds");
    if(node==NULL)
    {
        printk("设备树节点信息解析失败\n");
        return -ENOENT;
    }
    printk("设备树节点解析成功\n");
     //根据设备树节点解析出gpio编号并申请
    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 OUT6;
    }
    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 OUT7;
    }
        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 OUT8;
    }
    
    return 0;

OUT8:
    //先灭灯
    gpiod_set_value(gpiono2,0);
    gpiod_put(gpiono2);
OUT7:
    //先灭灯
    gpiod_set_value(gpiono1,0);
    gpiod_put(gpiono1);
OUT6:
    //先灭灯
    gpiod_set_value(gpiono,0);
    gpiod_put(gpiono);    
OUT5:
    device_destroy(cls,MKDEV(major,0));    
OUT4:
    cdev_del(cdev);
OUT3:
    unregister_chrdev_region(MKDEV(major,minor),1);
OUT2:
    kfree(cdev);
OUT1:
    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");

test.c

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值