platform总线驱动实现

a.应用程序通过阻塞的io模型来读取number变量的值

b.number是内核驱动中的一个变量

c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0

d.在按下按键的时候需要同时将led1的状态取反

e.驱动中需要编写字符设备驱动

f.驱动中需要自动创建设备节点

g.这个驱动需要的所有设备信息放在设备树的同一个节点中

pdev.c

#include 
#include 
#include
 struct resource res[]=
    {
        [0]={
             .start=0x12345678,
             .end=0x12345678+49,
             .flags= IORESOURCE_MEM,     
        },    
        [1]={
            .start=71,
            .end=71,
            .flags= IORESOURCE_IRQ,       
        },
    };
    void pdev_release(struct device *dev)
    {
        printk("%s:%d\n",__FILE__,__LINE__);
    }
    struct platform_device pdev={
         .name="aaa0",
         .id= PLATFORM_DEVID_AUTO,
         .dev={
             .release=pdev_release,         
         }, 
         .resource=res,
         .num_resources=ARRAY_SIZE(res), 
     };
static int __init mycdev_init(void)
{
    //注册对象
    platform_device_register(&pdev);
    return 0;
}
static void __exit mycdev_exit(void)
{
    //注销
    platform_device_unregister(&pdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

pdrv.c 

#include 
#include 
#include
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include
#include 
#include 
#include 
#include 
struct cdev *cdev;
struct class* cls;
struct device* dev;
dev_t dev1;
unsigned int major;
unsigned int minor;
struct resource *res;
int irqno;
//定义一个指向设备节点的指针
struct device_node *node;
struct property *pr;//属性结构体指针
struct gpio_desc *desc;
unsigned char number;//定义数组用于存放和用户之间拷贝的数据
//定义队列头
wait_queue_head_t head;
unsigned int condition=0;//判断是否有数据准备好的标识变量

//对应的是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(file->f_flags&O_NONBLOCK)
    {
        //非阻塞
        return -EINVAL;
    }
    else{
        //阻塞
        ret=wait_event_interruptible(head,condition);
        if(ret)
        {
            printk("接收阻塞休眠\n");
            return ret;
        }
    }
    //把父进程拷贝到内核的数据再拷贝给子进程
    if(size>sizeof(char))
    size=sizeof(char);
    ret=copy_to_user(ubuf,&number,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    //condition=0;
    condition=0;
     
       return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    return 0;
}
struct file_operations fops=
{
    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
};
//中断处理函数
irqreturn_t irq_handler(int irq, void *dev)
{
    gpiod_set_value(desc,!gpiod_get_value(desc));
    if(number=='0')
    {
        number='1';
        condition=1;
        wake_up_interruptible(&head);
 
    }else
    {
        number='0';
        condition=1;
        wake_up_interruptible(&head);
    }
    return IRQ_HANDLED;
}
int pdrv_probe(struct platform_device *pdr)
 {
    int ret;
       printk("%s:%d\n",__FILE__,__LINE__);
        res=platform_get_resource(pdr,IORESOURCE_MEM,0);
       if(res==NULL)
       {
            printk("获取资源失败\n");
            return -ENODATA;
       }
       irqno=platform_get_irq(pdr,0);
       if(irqno<0)
       {
             printk("获取资源失败\n");
             return irqno;
       }
     printk("%#llx    %d\n",res->start,irqno);
       
        //获取GPIO编号
     desc=gpiod_get_from_of_node(pdr->dev.of_node,"led1",0,GPIOD_OUT_HIGH,NULL);
     if(IS_ERR(desc))
     {
      printk("获取GPIO编号失败\n");
      return PTR_ERR(desc);
     }
     //注册软中断号
     ret=request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"key2——inte",NULL);
    if(ret)
    {
        printk("注册中断号失败\n");
        return ret;
    }
    printk("注册中断号成功\n");
 
    condition=0;
    init_waitqueue_head(&head);
     return 0;
 }
 int pdrv_remove(struct platform_device *pdr)
 {
        free_irq(irqno,NULL);
        gpiod_set_value(desc,0);
        gpiod_put(desc);
       printk("%s:%d\n",__FILE__,__LINE__);
       return 0;
 }
 
  //定义compatible表
 struct of_device_id oftable[]={
      {.compatible="hqyj,hello2"},
      {},
 };
MODULE_DEVICE_TABLE(of,oftable);
//定义并初始化对象
    struct platform_driver pdrv ={
      .probe=pdrv_probe,
      .remove=pdrv_remove,
      .driver={
          .name= "test1",
          .of_match_table= oftable,//设备树匹配     
      },
 };

static int __init mycdev_init(void)
{
    int ret,i;
    //注册对象
    platform_driver_register(&pdrv);
    //注册设备驱动
    //1,分配设备驱动对象
    cdev=cdev_alloc();
    if(NULL==cdev)
    {
        printk("申请驱动对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
        //return -ENOMEM;
    }
    //2,初始化对象
    cdev_init(cdev,&fops);
    //3,设备号的申请
    ret=alloc_chrdev_region(&dev1,0,3,"LED");
    if(ret != 0)
    {
        printk("申请设备号失败\n");
        goto ERR2;
    }
    major = MAJOR(dev1);
    minor = MINOR(dev1);
    printk("动态设备号申请成功\n");
    //4,注册字符设备驱动
    ret=cdev_add(cdev,dev1,3);
    if(ret !=0)
    {
        printk("注册设备驱动失败\n");
        goto ERR3;
    }
    printk("注册字符设备驱动成功:%d\n",dev1);
 
    //5,自动创建设备节点
    //创建目录
    cls=class_create(THIS_MODULE,"LED");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto ERR4;
    }
    printk("向上提交目录成功\n");
    //创建设备节点  3次
    for(i=0;i<3;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
        if(IS_ERR(dev))
        {
            printk("向上提交节点失败\n");
            ret = PTR_ERR(dev);
            goto ERR5;
        }
    }
    printk("向上提交节点成功\n");
    platform_driver_register(&pdrv);
    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(dev1,3);
    ERR2:
    kfree(cdev);
    ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    int i;
    //注销
    platform_driver_unregister(&pdrv);
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    
    //销毁目录
    class_destroy(cls);
 
    //2,注销字符设备驱动
    cdev_del(cdev);
    //3,释放设备号
    unregister_chrdev_region(dev1,3);
    //4,释放动态申请的空间
    kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用层main.c 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "myled.h"
#include 
#include 
#include 
#include 
#include  
int main(int argc, char const *argv[])
{
    int fd;
    char number;
 
    fd=open("/dev/myled1",O_RDWR);
    if(fd<0)
    {
        printf("打开platform总线失败\n");
        exit(-1);  
    }
    
    while(1)
    {
        int ret;
        ret=read(fd,&number,sizeof(char));
        if(0==ret)
        {
            printf("读取失败\n");
            break;
        }
        printf("number=%c\n",number);
    }
 
    close(fd); 
    return 0;   
}

你可能感兴趣的:(linux,驱动开发)