10.18 利用GPIO子系统编写驱动实现LED灯的点亮

GPIO子系统编写驱动实现LED灯的点亮

头文件myleds.h

#ifndef __MYLED_H__
#define __MYLED_H__

    enum LED
    {
        LED1=1,
        LED2,
        LED3
    };
#define LedON _IOW('a',1,int)
#define LedOFF _IOW('a',0,int)
#endif

驱动程序myleds.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/of.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
#include<linux/cdev.h>
#include<linux/ioctl.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/io.h>
#include "myleds.h"
/*myleds{
     led1=<&gpioe 10 0>; //&gpioe表示引用的是gpioe控制器 //10表示gpioe10
     //0表示默认状态
     led2=<&gpiof 10 0>; 
     led3=<&gpioe 8 0>; 
 };    */
 
//定义一个指向设备节点的指针
struct device_node *node;
struct property *pr;//属性结构体指针
int gpiono1,gpiono2,gpiono3;
int minor=0;//次设备号从0开始
#if 0
unsigned int major = 0;//动态申请
#else
unsigned int major = 500;//静态指定设备号
#endif
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
struct cdev *cdev;
struct class *cls;
struct device *dev;

const int count=3;//指定设备节点的个数为3

//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
      
      printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      return 0;
}
//read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    
    //size参数是用户期待读到的字节长度
    int ret;
    //把父进程拷贝到内核的数据再拷贝给子进程
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }

     
       return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
       return size;
}
long ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret, which;
    switch (cmd)
    {
    case LedON:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("copy failed\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            //点亮led1
            gpio_set_value(gpiono1,1);
            break;
        case LED2:
            gpio_set_value(gpiono2,1);
            break;
        case LED3:
            gpio_set_value(gpiono3,1);
            break;
        }
        break;
    case LedOFF:
        ret = copy_from_user(&which, (void *)arg, sizeof(int));
        if (ret)
        {
            printk("copy failed\n");
            return -EIO;
        }
        switch (which)
        {
        case LED1:
            gpio_set_value(gpiono1,0);
            break;
        case LED2:
            gpio_set_value(gpiono2,0);
            break;
        case LED3:
            gpio_set_value(gpiono3,0);
            break;
        }
        break;
    default:
        printk("cmd error\n");
        break;
    }
    return 0;
}
//close()
 
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,
    .unlocked_ioctl = ioctl,
    .release=mycdev_close,
};
static int __init myleds_init(void)
{

    int ret,i;
    //分配对象
   dev_t devno;
    cdev=cdev_alloc();
    if(cdev==NULL)
    {
        printk("cdev alloc memory err\n");
        
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("alloc success\n");
    //对象的初始化
    cdev_init(cdev,&fops);
    //设备号的申请
 
    if(major==0)//动态申请
    {
        ret=alloc_chrdev_region(&devno,minor,count,"my_led");
        if(ret)
        {
            printk("dev_no failed\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
        printk("dev_no success\n");
    }
    else
    {
        ret=register_chrdev_region(MKDEV(major,minor),count,"my_led");
        if(ret)
        {
            printk("dev_no failed\n");
            goto ERR2;
        }
          printk("dev_no success\n");
    }
    //注册字符设备驱动
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("register failed\n");
        goto ERR3;
    }
    printk("register  success\n");
   
     //自动创建设备节点
      cls = class_create(THIS_MODULE,"led");
     if(IS_ERR(cls))
     {
        printk("dir failed\n");
        ret=PTR_ERR(cls);
        goto ERR4;
     }
      printk("mkdir success\n");
    //向上提交节点信息
    for(i=0;i<3;i++)
    {
      dev = device_create(cls,NULL,MKDEV(major,i),NULL,"my_led%d",i);
        if(IS_ERR(dev))
         {
          printk("jiedain failed\n");
          ret = PTR_ERR(dev);
          goto ERR5;
         }
    }

    
    //根据设备节点路径获取设备节点信息
    node=of_find_node_by_path("/myleds");
    if(node==NULL)
    {
        printk("get jiedian failed\n");
        return ENODATA;
    }
    printk("get jiedian success\n");
   //获取gpio编号
   gpiono1=of_get_named_gpio(node,"led1",0);
   gpiono2=of_get_named_gpio(node,"led2",0);
   gpiono3=of_get_named_gpio(node,"led3",0);
   if(gpiono1 < 0)
   {
    printk("get led1_no failed\n");
    return gpiono1;
   }
   printk("get led1_no success\n");
   if(gpiono2 < 0)
   {
    printk("get led2_no failed\n");
    return gpiono2;
   }
   printk("get led2_no success\n");
   if(gpiono3 < 0)
   {
    printk("get led3_no failed\n");
    return gpiono3;
   }
   printk("get led3_no success\n");
   //申请gpio编号使用权
   ret=gpio_request(gpiono1,NULL);
   if(ret)
   {
        printk("get gpiono1_no failed\n");
        return ret;
   }
   ret=gpio_request(gpiono2,NULL);
   if(ret)
   {
        printk("get gpiono2_no failed\n");
        return ret;
   }
   ret=gpio_request(gpiono3,NULL);
   if(ret)
   {
        printk("get gpiono3_no failed\n");
        return ret;
   }
   //设置输出方式
   gpio_direction_output(gpiono1,0);
   gpio_direction_output(gpiono2,0);
   gpio_direction_output(gpiono3,0);
   //点亮led1
   //gpio_set_value(gpiono1,1);
   
    return 0;
    ERR5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit myleds_exit(void)
{
     //1.销毁设备节点
    int i;
    for(i=0;i<count;i++)
    {
         device_destroy(cls,MKDEV(major,i));
    }
     class_destroy(cls);
    //2.注销字符设备驱动
    cdev_del(cdev);
    //3.释放设备号
    unregister_chrdev_region(MKDEV(major,minor),count);
    //4.释放动态申请的空间
    kfree(cdev);
    //释放设备号
    
    gpio_free(gpiono1);
    gpio_free(gpiono2);
    gpio_free(gpiono3);
 
}
module_init(myleds_init);
module_exit(myleds_exit);
MODULE_LICENSE("GPL");

应用程序test.c


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

int main(int argc, char const *argv[])
{
    char buf[128] = {};
    int which;
    int fd1, fd2, fd3;
    fd1 = open("/dev/my_led0", O_RDWR);
    if (fd1 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led0 success\n");
    fd2 = open("/dev/my_led1", O_RDWR);
    if (fd2 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led1 success\n");
    fd3 = open("/dev/my_led2", O_RDWR);
    if (fd3 < 0)
    {
        printf("open failed\n");
        exit(-1);
    }
    printf("open my_led2 success\n");
    while (1)
    {
        printf("please input cmd:0:LED OFF 1:LED ON\n");
        fgets(buf, sizeof(buf), stdin);

        buf[strlen(buf) - 1] = '\0';
        if (buf[0] == '1')
        {
            printf("please choose led:1->LED1  2->LED2 3->LED3\n");
            scanf("%d", &which);
            getchar();
            if (which == 1)
            {
                ioctl(fd1, LedON, &which);
            }
            if (which == 2)
            {
                ioctl(fd2, LedON, &which);
            }
            if (which == 3)
            {
                ioctl(fd3, LedON, &which);
            }
            // ioctl(fd,LedON,&which);
        }
        else
        {
            printf("please choose led:1->LED1  2->LED2 3->LED3\n");
            scanf("%d", &which);
            getchar();
            if (which == 1)
            {
                ioctl(fd1, LedOFF, &which);
            }
            if (which == 2)
            {
                ioctl(fd2, LedOFF, &which);
            }
            if (which == 3)
            {
                ioctl(fd3, LedOFF, &which);
            }
        }
    }
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名社畜L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值