STM32MP157A 基于GPIO子系统实现灯的控制

应用层代码如下:

command.h

#ifndef __COMMAND_H__
#define __COMMAND_H__



#define LED1_ON _IOW('a',1,int)
#define LED1_OFF _IOW('a',0,int)
#define LED2_ON _IOW('b',1,int)
#define LED2_OFF _IOW('b',0,int)
#define LED3_ON _IOW('c',1,int)
#define LED3_OFF _IOW('c',0,int)


#endif

test.c

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

char buf[128] = {0};

int main(int argc, char const *argv[])
{
    int fd0 = open("/dev/gpiochrdev0",O_RDWR);
    if (fd0 < 0)
    {
        printf("LED1 open failed\n");
        return -1;
    }

    int fd1 = open("/dev/gpiochrdev1",O_RDWR);
    if (fd1 < 0)
    {
        printf("LED2 failed\n");
        return -1;
    }

    int fd2 = open("/dev/gpiochrdev2",O_RDWR);
    if (fd2 < 0)
    {
        printf("LED3 failed\n");
        return -1;
    }

    int a,b;
    while(1)
    {
        printf("请输入要实现的功能:1(开灯) 0(关灯)>\n");
        fscanf(stdin, "%d", &a);
        if(1==a) //开灯
        {
            printf("请输入要操作的灯: 1(LED1),2(LED2),3(LED3)>");
            scanf("%d", &b);
            if(1==b)
            {
                ioctl(fd0,LED1_ON,&b);
            }
            if(2==b)
            {
                ioctl(fd1,LED2_ON,&b);
            }
            if(3==b)
            {
                ioctl(fd2,LED3_ON,&b);
            }
            
        } else if(0==a) //关灯
        {
            printf("请输入要操作的灯: 1(LED1),2(LED2),3(LED3)>");
            scanf("%d", &b);
            if(1==b)
            {
                ioctl(fd0,LED1_OFF,&b);
            }
            if(2==b)
            {
                ioctl(fd1,LED2_OFF,&b);
            }
            if(3==b)
            {
                ioctl(fd2,LED3_OFF,&b);
            }
        }
    }




    return 0;
}

驱动层代码如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include "command.h"

struct device_node *dnode;
struct gpio_desc *desc; //led1
struct gpio_desc *desc0;//led2
struct gpio_desc *desc1;//led3

unsigned int minor = 0;
unsigned int major = 0;
struct cdev *cdev;
dev_t devno;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;
int i = 0;

int led_init(void);


long gpiochrdev_ioctl (struct file *file, unsigned int cmd, unsigned long args)
{
    switch (cmd)
    {
        case LED1_ON:
            gpiod_set_value(desc, 1);
            break;
        case LED1_OFF:
            gpiod_set_value(desc, 0);
            break;
        case LED2_ON:
            gpiod_set_value(desc0, 1);
            break;
        case LED2_OFF:
            gpiod_set_value(desc0, 0);
            break;
        case LED3_ON:
            gpiod_set_value(desc1, 1);
            break;
        case LED3_OFF:
            gpiod_set_value(desc1, 0);
            break;
        default:
            break;
    }

    return 0;
}



int gpiochrdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t gpiochrdev_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
     unsigned long ret;
    //向用户空间读取拷贝
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
ssize_t gpiochrdev_write (struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
     unsigned long ret;
    //从用户空间读取数据
    if(size>sizeof(kbuf))//用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)//拷贝失败
    {
        printk("copy_to_user filed\n");
        return ret;
    }
    return 0;
}
int gpiochrdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}




const struct file_operations fops = 
{
    .open = gpiochrdev_open,
    .read = gpiochrdev_read,
    .write =gpiochrdev_write,
    .release = gpiochrdev_close,
    .unlocked_ioctl = gpiochrdev_ioctl,
};

static int __init gpiochrdev_init(void)
{
    //初始化LED灯
    led_init();
    
    int ret;
    //1.分配字符设备驱动对象空间
    cdev = cdev_alloc();
    if(NULL == cdev)
    {
        printk("申请字符设备驱动对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("申请字符设备驱动对象空间成功\n");

    //2.初始化字符设备驱动对象
    cdev_init(cdev,&fops);
    printk("初始化字符设备驱动对象成功\n");

    //3.动态申请设备号
    ret = alloc_chrdev_region(&devno,0, 3,"gpiochrdev");
    if(ret)
    {
        printk("动态申请设备号失败\n");
        ret = -EBUSY;
        goto ERR2;
    }
    printk("动态申请设备号成功\n");
    major = MAJOR(devno);
    minor = MINOR(devno);

    //4.注册
    ret = cdev_add(cdev, MKDEV(major,minor), 3);
    if(ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto ERR3;
    }
    printk("注册字符设备驱动对象成功\n");

    //5.向上提交目录
    cls = class_create(THIS_MODULE, "gpiochrdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = PTR_ERR(cls);
        goto ERR4;
    }
    printk("向上提交目录成功\n");

    //6.创建设备节点
    for(i = 0; i < 3; i++)
    {
        dev =  device_create(cls, NULL,\
			     MKDEV(major,i), NULL, "gpiochrdev%d", i);
        if(IS_ERR(dev))
        {
            printk("创建设备节点gpiochrdev%d失败\n",i);
            ret = PTR_ERR(dev);
            goto ERR5;
        }
        printk("创建设备节点gpiochrdev%d成功\n",i);
    }
    

    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),3);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit gpiochrdev_exit(void)
{


    //灭灯
    gpiod_set_value(desc, 0);
    gpiod_set_value(desc0, 0);
    gpiod_set_value(desc1, 0);
    //释放gpio
    gpiod_put(desc);
    gpiod_put(desc0);
    gpiod_put(desc1);

    for(--i; i>=0;i--) 
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
    cdev_del(cdev);
    unregister_chrdev_region(MKDEV(major,minor),3);
    kfree(cdev);

}


int led_init(void)
{
    //解析myled设备树节点
    dnode = of_find_node_by_name(NULL,"myleds");
    if(NULL == dnode)
    {
        printk("解析设备树节点失败\n");
        return -1;
    }
    printk("解析设备树节点成功\n");
    
    desc = gpiod_get_from_of_node(dnode,"myled1",0,GPIOD_OUT_LOW,NULL) ;
    if(IS_ERR(desc))
    {
        printk("解析LED0对象失败\n");
        return PTR_ERR(desc);
    }
    printk("解析LED0对象成功\n");


    desc0 = gpiod_get_from_of_node(dnode,"myled2",0,GPIOD_OUT_LOW,NULL) ;
    if(IS_ERR(desc0))
    {
        printk("解析LED1对象失败\n");
        return PTR_ERR(desc0);
    }
    printk("解析LED1对象成功\n");


    desc1 = gpiod_get_from_of_node(dnode,"myled3",0,GPIOD_OUT_LOW,NULL) ;
    if(IS_ERR(desc1))
    {
        printk("解析LED2对象失败\n");
        return PTR_ERR(desc1);
    }
    printk("解析LED2对象成功\n");

    if( gpiod_direction_output(desc,0))
    {
        printk("设置LED0输出失败\n");
        return -1;
    }
     printk("设置LED0输出成功\n");

     if( gpiod_direction_output(desc0,0))
    {
        printk("设置LED1输出失败\n");
        return -1;
    }
     printk("设置LED1输出成功\n");

     if( gpiod_direction_output(desc1,0))
    {
        printk("设置LED2输出失败\n");
        return -1;
    }
     printk("设置LED2输出成功\n");

     return 0;
}
module_init(gpiochrdev_init);
module_exit(gpiochrdev_exit);
MODULE_LICENSE("GPL");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值