驱动开发--LED设备树

//led.c
#include 
#include 
#include 
#include 
#include 

#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
struct cdev *cdev;
int gpiono1;
int gpiono2;
int gpiono3;
char kbuf[128]={0};
struct device_node *dnode;
struct class *cls;
struct device *dev;
unsigned int major=500;
unsigned int minor=0;
dev_t devno;
int leddev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t leddev_read(struct file *file, char *ubuf, size_t size, loff_t *iof)
{
    int ret;
    //向用户拷贝
    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 leddev_write(struct file *file, const char  *ubuf, size_t size, loff_t *iof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    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;
    }
    return 0;
}
int leddev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
long leddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch(cmd)
    {
        case LED_ON:
            gpio_set_value(gpiono1,1);//开灯
            gpio_set_value(gpiono2,1);//开灯
            gpio_set_value(gpiono3,1);//开灯
            break;
        case LED_OFF:
            gpio_free(gpiono1);//关灯
            gpio_free(gpiono2);//关灯
            gpio_free(gpiono3);//关灯
            break;
    }
    return 0;
}
//定义一个操作方法结构体变量并且初始化
struct file_operations fops={
    .open=leddev_open,
    .read=leddev_read,
    .write=leddev_write,
    .unlocked_ioctl=leddev_ioctl,
    .release=leddev_close,
};
static int __init leddev_init(void)
{
    int ret,i;
    //1.分配对象空间
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("分配字符设备驱动对象失败\n");
        ret=-EFAULT;
        goto OUT1;
    }
    printk("分配对象空间成功\n");
    //2.初始化对象
    cdev_init(cdev,&fops);
    //3.申请设备号
    if(major>0)//静态指定设备号
    {
        ret= register_chrdev_region(MKDEV(major,minor),3,"myleds");
        if(ret)
        {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    }
    else if(major==0)//动态申请设备号
    {
        ret=alloc_chrdev_region(&devno,minor,3,"myleds");
        if(ret)
        {
            printk("动态申请设备号失败\n");
            goto OUT2;
        }
        //获取主设备号和次设备号
        major=MAJOR(devno);
        minor=MINOR(devno);
    }
    printk("申请设备号成功\n");
    
    //4.注册字符设备驱动对象
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    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;
    }
    printk("向上提交目录成功\n");
    
    //向上提交设备节点信息
    for(i=0;i<3;i++)
    {
         dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myleds%d",i);
         if(IS_ERR(dev))
         {
            ret=-PTR_ERR(dev);
            goto OUT5;
         }
    }
    printk("向上提交设备节点成功\n");
    //完成硬件寄存器地址的映射以及初始化

    //解析设备树节点
    dnode=of_find_node_by_name(NULL,"myleds");
    if(dnode==NULL){
        printk("解析设备树节点失败\n");
        return -ENOMEM;
    }
    printk("解析设备树节点成功\n");

    //根据设备树节点解析gpio编号
    gpiono1=of_get_name_gpio(dnode,"led1",0);
    if(gpiono1<0){
        printk("解析设备号led1失败\n");
    }
    gpiono2=of_get_name_gpio(dnode,"led2",0);
    if(gpiono2<0){
        printk("解析设备号led2失败\n");
    }
    gpiono3=of_get_name_gpio(dnode,"led3",0);
    if(gpiono3<0){
        printk("解析设备号led3失败\n");
    }
    
    //申请gpio编号
    int ret1=gpio_request(gpiono1,NULL);
    if(ret1){
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("led1:GPIO编号申请成功\n");
    int ret2=gpio_request(gpiono2,NULL);
    if(ret2){
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("led2:GPIO编号申请成功\n");
    int ret3=gpio_request(gpiono3,NULL);
    if(ret3){
        printk("GPIO编号申请失败\n");
        return -EIO;
    }
    printk("led3:GPIO编号申请成功\n");

    //设置管脚为输出
    gpio_direction_output(gpiono1,0);
    gpio_direction_output(gpiono2,0);
    gpio_direction_output(gpiono3,0);

OUT5:
    //释放已经申请的设备节点信息
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //释放目录空间
    class_destroy(cls);
OUT4:
    //注销字符设备驱动对象
    cdev_del(cdev);
OUT3:
    //释放设备号
    unregister_chrdev_region(MKDEV(major,minor),3);
OUT2:
//释放对象空间
    kfree(cdev);
OUT1:
    return ret;

    return 0;
}
static void __exit leddev_exit(void)
{
    //销毁设备节点
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //释放目录空间
    class_destroy(cls);
    //1.注销字符设备驱动对象
     cdev_del(cdev);
    //2.释放设备号
     unregister_chrdev_region(MKDEV(major,minor),3);
    //3.释放对象空间
     kfree(cdev);
}
module_init(leddev_init);
module_exit(leddev_exit);
MODULE_LICENSE("GPL");
//main.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)

int main(int argc, char const *argv[])
{
    int a;
    char buf[128]={0};
    int fd=open("/dev/leddev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        printf("请输入:1(开灯) 0(关灯)>");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON);//开灯
                break;
            case 0:
                ioctl(fd,LED_OFF);//关灯
                break;
        }
    }
    close(fd);
    return 0;
}

你可能感兴趣的:(嵌入式学习,驱动开发)