10.20 驱动 day9

任务:通过platform总线驱动实现

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

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

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

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

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

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

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

硬件信息设置pdev.c:

#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>

//资源结构体设置
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 <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>

#define CNAME "mycdev"
struct resource *res;
struct gpio_desc *desc;
int count=1;
int major,minor;
struct class *cls;//句柄
struct device *dev;
struct cdev *cdev;
char kbuf[128]={};
unsigned int irqno;//接收软中断号
struct device_node *node;
int condition=0;
wait_queue_head_t head;
int number=0;
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 mycdev_open(struct inode *inode, struct file *file)
{
	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(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,&number,sizeof(number));
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    //condition=0;
    condition=0;
    return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    return 0;
}

//close()
int mycdev_close(struct inode *inode, struct file *file)
{
	return 0;
}

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("%#x   %d\n",res->start,irqno);
    //获取GPIO编号
    desc=gpiod_get_from_of_node(pdr->dev.of_node,"led2",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)
{
	printk("%s:%d\n",__FILE__,__LINE__);
	return 0;
}

struct file_operations fops=
{
    .open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
    .release=mycdev_close,
};

//定义compatible表
struct of_device_id oftable[]={
    {.compatible="ljh,platform"},
    {},
};

//定义并初始化对象
struct platform_driver pdrv ={
	.probe=pdrv_probe,
	.remove=pdrv_remove,
	.driver={
		.name= "test",
        .of_match_table=oftable,//设备树匹配 
	},
};

static int __init mycdev_init(void)
{
    int ret;
    //分配对象
    dev_t devno;
     cdev=cdev_alloc();
    if(cdev==NULL)
    {
    printk("cdev alloc memory err\n");
    ret = -ENOMEM;
    goto ERR1;
    }
    printk("对象分配成功\n");
    //对象的初始化
    cdev_init(cdev,&fops);
    //设备号的申请
     ret=alloc_chrdev_region(&devno,minor,count,"my_led");
        if(ret)
    {
        printk("动态申请设备号失败\n");
        goto ERR2;
    }
        major = MAJOR(devno);
        minor = MINOR(devno);
        printk("动态申请设备号成功\n");
  
    //注册字符设备驱动
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("字符设备驱动注册失败\n");
        goto ERR3;
    }
    printk("注册字符设备驱动成功\n");
    //向上提交节点目录
     cls=class_create(THIS_MODULE,"LED");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //创建设备节点
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myled");
    if(IS_ERR(dev))
    {
        printk("创建节点失败\n");
        return PTR_ERR(dev);
    }
    printk("创建节点成功\n");
    //注册对象
    platform_driver_register(&pdrv);
    return 0;
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit mycdev_exit(void)
{
    //1.销毁设备节点
    device_destroy(cls,MKDEV(major,0));
 
    class_destroy(cls);
    //2.注销字符设备驱动
    cdev_del(cdev);
    //3.释放设备号
    unregister_chrdev_region(MKDEV(major,minor),1);
    //4.释放动态申请的空间
    kfree(cdev);
    //5.释放GPIO编号
    gpiod_direction_output(desc,0);
    gpiod_put(desc);
    //6.注销中断
    free_irq(irqno,NULL);
    //7.注销
    platform_driver_unregister(&pdrv);
  

}
module_init(mycdev_init);
module_exit(mycdev_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>
int main(int argc, char const *argv[])
{
    int number;
    int fd = open("/dev/myled", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("打开设备文件成功\n");
    //从终端读取
    while (1)
    {
        read(fd, &number, sizeof(number));
        printf("number:%d\n",number);
    }
    close(fd);
    return 0;
}

实验现象:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值