20230625

pdrv.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>


struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
char kbuf[128]="";
int major;

struct class *cls;
struct device *dev;

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* lof)
{
    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 failed\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* lof)
{
    int ret;
    if(size>sizeof(kbuf))
        size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user failed\n");
        return -EIO;    //拷贝失败,返回错误码
    }
    //LED1
    if(kbuf[0]=='1')
    {
        //开灯
        gpiod_set_value(gpiono,1);
    }
    else if(kbuf[0]=='0')
    {
        //关灯
        gpiod_set_value(gpiono,0);
    }
     //LED2
    if(kbuf[0]=='2')
    {
        //开灯
        gpiod_set_value(gpiono1,1);
    }
    else if(kbuf[0]=='3')
    {
        //关灯
        gpiod_set_value(gpiono1,0);
    }
         //LED3
    if(kbuf[0]=='4')
    {
        //开灯
        gpiod_set_value(gpiono2,1);
    }
    else if(kbuf[0]=='5')
    {
        //关灯
        gpiod_set_value(gpiono2,0);
    }
    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,
};
//定义相关成员函数
//probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev) //当和设备匹配成功后执行probe
{
    //进行字符设备驱动的注册
    major=register_chrdev(0,"mycdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败!\n");
        return major;
    }
    printk("字符设备驱动注册成功,major=%d\n",major);
    cls=class_create(THIS_MODULE,"mycdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败!\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功!\n");
    //向上提交设备节点信息
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"mycdev");
    if(IS_ERR(dev))
    {
        printk("向上提交设备节点失败!\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功!\n");
    //解析MEM类型的资源
    res=platform_get_resource(pdev,IORESOURCE_MEM,0);
    if(NULL==res)
    {
        printk("解析资源失败!%d\n",__LINE__);
        return -ENOMEM;
    }
    //解析中断类型的资源
    irqno=platform_get_irq(pdev,0);
    if(irqno<0)
    {
        printk("解析资源失败!\n");
        return -ENOMEM;
    }
    printk("mem:%x irq:%d\n",res->start,irqno);

    //解析出GPIO编号并申请
    gpiono=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("GPIO信息解析失败!\n");
        return -PTR_ERR(gpiono);
    }
    gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("GPIO信息解析失败!\n");
        return -PTR_ERR(gpiono);
    }
    gpiono2=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("GPIO信息解析失败!\n");
        return -PTR_ERR(gpiono);
    }
    printk("GPIO信息解析成功!\n");
   
    //设置gpio的方向为输出
    gpiod_direction_output(gpiono,0);
    gpiod_direction_output(gpiono1,0);
    gpiod_direction_output(gpiono2,0);
    return 0;
}
//remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev) //当设备和驱动分离时执行remove
{
    //灭灯
    gpiod_set_value(gpiono,0);
    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    //释放GPIO编号
    gpiod_put(gpiono);
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    //字符设备驱动的注销
    unregister_chrdev(major,"mycdev");
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

//在驱动中创建一个用于设备数匹配的表
struct of_device_id oftable[]={
    {.compatible="hqyj,myplatform0",},
    {.compatible="hqyj,myplatform1",},
    {.compatible="hqyj,myplatform2",},
    {},     //让match函数有一个结束的条件
};

struct platform_device_id idtable[]={
    {"aaaaa",0},
    {"bbbbb",1},
    {"ccccc",2},
    {},
};
//分配对象并初始化
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="aaaaa",
        .of_match_table=oftable,    //指定设备树匹配表的首地址
    },
};
//一键注册宏,代替入口出口
module_platform_driver(pdrv);
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>

int main(int argc,char const *argv[])
{
    char buf[128]="";
    int fd=open("/dev/mycdev",O_RDWR);//为了调用驱动中的mycdev_open
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        
        printf("请输入LED1、LED2、LED3控制码: 1(led1灯亮) 0(led1灯灭) 2(led2灯亮) 3(led2灯灭) 4(led3灯亮) 5(led3灯灭)>>>");
        fgets(buf,sizeof(buf),stdin);//从终端读取字符串
        buf[strlen(buf)-1]='\0';
        write(fd,buf,sizeof(buf));
        
    }

  
    close(fd);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值