platform是Linux内核抽象出来的软件代码,用于设备与驱动的连接,设备与驱动通过总线进行匹配;匹配成功后会执行驱动中的probe函数,在probe函数中可以获取到设备的信息;
设备与驱动的信息分别通过链表进行管理。
编写驱动和设备端的代码----搭建platform框架
设备与硬件有三种匹配方式:名字匹配,id_table匹配,设备树匹配
驱动代码
#include
#include
#include
struct resource *res;
int irqno;//定义一个probe函数int pdrv_probe(struct platform_device *pdev){//获取MEM类型的资源res=platform_get_resource(pdev,IORESOURCE_MEM,0);if(res==NULL){printk("获取MEM资源失败\n");return ENODATA;}//获取中断类型的资源irqno=platform_get_irq(pdev,0);if(irqno<0){printk("获取中断资源失败\n");return ENODATA;}printk("addr:%#llx ,irqno:%d\n",res->start,irqno);return 0;}int pdrv_remove(struct platform_device *pdev){printk("%s:%d\n",__func__,__LINE__);return 0;}//热插拔宏,可以在插入硬件时,自动安装驱动MODULE_DEVICE_TABLE(platform,idtable);//对象初始化struct platform_driver pdrv={.probe=pdrv_probe,.remove=pdrv_remove, .driver={.name="aaaaa", //按名字匹配,只能匹配一个设备 }, };module_platform_driver(pdrv); //一键注册驱动
MODULE_LICENSE("GPL");
设备代码
#include
#include
#include //对设备信息进行填充struct resource res[]={[0]={.start=0x12345678,.end=0x12345678+49,.flags= IORESOURCE_MEM, },[1]={.start=71,.end=71,.flags= IORESOURCE_IRQ, },};//定义一个relese函数
void pdev_release(struct device *dev)
{printk("%s:%d\n",__func__,__LINE__);
}//给对象赋值struct platform_device pdev={.name="aaaaa",.id=PLATFORM_DEVID_AUTO,.dev={.release=pdev_release, },.resource=res,.num_resources=ARRAY_SIZE(res), };static int __init mycdev_init(void)
{//注册devicereturn platform_device_register(&pdev);
}
static void __exit mycdev_exit(void)
{//注销deviceplatform_device_unregister(&pdev);}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
#include
#include
#include
#include
struct resource *res;
int irqno;//定义一个probe函数int pdrv_probe(struct platform_device *pdev){//获取MEM类型的资源res=platform_get_resource(pdev,IORESOURCE_MEM,0);if(res==NULL){printk("获取MEM资源失败\n");return ENODATA;}//获取中断类型的资源irqno=platform_get_irq(pdev,0);if(irqno<0){printk("获取中断资源失败\n");return ENODATA;}printk("addr:%#llx ,irqno:%d\n",res->start,irqno);return 0;}int pdrv_remove(struct platform_device *pdev){printk("%s:%d\n",__func__,__LINE__);return 0;}//id_table创建struct platform_device_id idtable[]={{"hi1",0},{"hi2",1},{"hi3",2},{},//防止越界};//热插拔宏,可以在插入硬件时,自动安装驱动MODULE_DEVICE_TABLE(platform,idtable);//对象初始化struct platform_driver pdrv={.probe=pdrv_probe,.remove=pdrv_remove, .driver={.name="aaaaa", //按名字匹配,只能匹配一个设备 }, .id_table=idtable, //设置名字表匹配,可以匹配多个硬件设备};module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
1、编写自己的设备树节点---编译设备树,开发板上电加载
2、驱动对象中,匹配方式-----设备树匹配
3、构建comptible表
struct of_device_id oftable[]={
{.compatible="hqyj,platform",},
};设备初始化结构体中,添加设备树匹配
.of_match_table=oftable,
驱动编译后拷贝到~/nfs/rootfs
#include
#include
#include
#include
#include
#include
struct resource *res;
int irqno;
struct gpio_desc *gpiono; //存放gpio编号//定义一个probe函数int pdrv_probe(struct platform_device *pdev){//获取MEM类型的资源res=platform_get_resource(pdev,IORESOURCE_MEM,0);if(res==NULL){printk("获取MEM资源失败\n");return ENODATA;}//获取中断类型的资源irqno=platform_get_irq(pdev,0);if(irqno<0){printk("获取中断资源失败\n");return ENODATA;}printk("addr:%#x ,irqno:%d\n",res->start,irqno);//解析设备树节点信息gpiono=gpiod_get_from_of_node(pdev->dev.of_node,"myled1",0,GPIOD_OUT_HIGH,NULL);if(IS_ERR(gpiono)){printk("获取gpio编号失败\n");return PTR_ERR(gpiono);}printk("点亮led1\n");gpiod_set_value(gpiono,1);return 0;}int pdrv_remove(struct platform_device *pdev){gpiod_set_value(gpiono,0);gpiod_put(gpiono);return 0;}struct of_device_id oftable[]={{.compatible="hqyj,platform",},{},
};//热插拔宏,可以在插入硬件时,自动安装驱动MODULE_DEVICE_TABLE(of,oftable);//对象初始化struct platform_driver pdrv={.probe=pdrv_probe,.remove=pdrv_remove, .driver={.name="aaaaa", //按名字匹配,只能匹配一个设备.of_match_table=oftable,//设备树匹配 }, };module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
现象:
如何让设备安装时,驱动自动加载,就像插入u盘,系统会自动识别硬件安装驱动
MODULE_DEVICE_TABLE(platform,idtable);添加此行即可
下一篇: 真的不容易初二作文500字【精彩6篇】