驱动开发----platform总线控制LED灯

通过终端输入,串口控制LED灯的亮灭,要求使用platform总线实现

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>
#include <linux/timer.h>
struct class *cls;
struct device *dev;
int i;
struct resource *res;
unsigned int irqno;
unsigned int major;
struct gpio_desc *gpiono[3];
char kbuf[128]={0};
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)
{
    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 mycdev_write(struct file *file, const char  *ubuf, size_t size, loff_t *lof)
{
    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;
    }
    switch(kbuf[0])
    {
        case '1':
            if(kbuf[1]=='1')
                gpiod_set_value(gpiono[0],1);
            else
                gpiod_set_value(gpiono[0],0);
            break;
        case '2':
            if(kbuf[1]=='1')
                gpiod_set_value(gpiono[1],1);
            else
                gpiod_set_value(gpiono[1],0);
            break;
        case '3':
            if(kbuf[1]=='1')
                gpiod_set_value(gpiono[2],1);
            else
                gpiod_set_value(gpiono[2],0);
            break;
        default:
            printk("输入错误\n");
            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,
};
//定义相关成员函数
//probe函数用于匹配设备成功后执行
 int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
 {
    //字符设备驱动注册
    major=register_chrdev(0,"mycdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功%d\n",major);
    //解析MEM类型的资源
    res=platform_get_resource(pdev,IORESOURCE_MEM,0);
    if(res==NULL)
    {
        printk("解析资源失败%d\n",__LINE__);
        return -ENOMEM;
    }
    //解析中断类型的资源
    irqno=platform_get_irq(pdev,0);
    if(irqno<0)
    {
        printk("解析资源失败%d\n",__LINE__);
        return -ENOMEM;
    }
     printk("mem:%x  irq:%d\n",res->start,irqno);
     //解析出GPIO编号并申请
    gpiono[0]=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono[0]))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono);
    }
    gpiono[1]=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono[1]))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono);
    }
    gpiono[2]=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono[2]))
    {
        printk("GPIO信息解析失败\n");
        return -PTR_ERR(gpiono);
    }
    printk("GPIO信息解析成功\n");
     return 0; 
 }
 //remove用于和设备分离时执行
  int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
  {
     for(i=0;i<3;i++)
    { 
        //灭灯
        gpiod_set_value(gpiono[i],0);
        //释放GPIO编号
        gpiod_put(gpiono[i]);
    }
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      return 0;  
  }
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
    {.compatible="hqyj,myplatform",},
    {.compatible="hqyj,myplatform1",},
    {.compatible="hqyj,myplatform2",},
    {},
};
  //分配对象并初始化
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="aaaaa", 
        .of_match_table=oftable,  //指定设备树匹配表的首地址 
    },

};
//一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

led.c

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


int main()
{
    int fd;
    char buf[32]={0};
    fd=open("/dev/mycdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        //从终端读取
        printf("请输入两个字符\n");
        printf("第一个字符:1(LED1) 2(LED2) 3(LED3)\n");
        printf("第二个字符:0(关灯) 1(开灯)\n");
        printf("请输入>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        //向设备文件中写
        write(fd,buf,sizeof(buf));
    }
    close(fd);
    return 0;
}

my.sh

#!/bin/bash 
make clean    
make arch=arm modname=pdrv
cp pdrv.ko ~/nfs/rootfs/  
arm-linux-gnueabihf-gcc led.c
cp a.out ~/nfs/rootfs/ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值