#include
#include
#include //对设备信息进行填充
struct resource res[]={[0]={.start=0x12345678,.end=0x12345678+40,.flags=IORESOURCE_MEM,},[1]={.start=71,.end=71,.flags=IORESOURCE_IRQ,},
};
//定义一个release函数
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 demo_init(void)
{return platform_device_register(&pdev);
}
static void __exit demo_exit(void)
{platform_device_unregister(&pdev);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
#include
#include
#include int pdir_probe(struct platform_device *pdev)
{printk("%s:%d\n",__func__,__LINE__);return 0;
}
int pdir_remove(struct platform_device *pdev)
{printk("%s:%d\n",__func__,__LINE__);return 0;
}
struct platform_driver pdir={.probe=pdir_probe,.remove=pdir_remove,.driver={.name="aaaaa",//通过设备名字匹配},
};
static int __init demo_init(void)
{platform_driver_register(&pdir);return 0;
}
static void __exit demo_exit(void)
{platform_driver_unregister(&pdir);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
匹配成功

设备文件代码>>>
在给对象赋值时,将name改为名字列表中的名字即可
#include
#include
#include //对设备信息进行填充
struct resource res[]={[0]={.start=0x12345678,.end=0x12345678+40,.flags=IORESOURCE_MEM,},[1]={.start=71,.end=71,.flags=IORESOURCE_IRQ,},
};
//定义一个release函数
void pdev_release(struct device *dev)
{printk("%s:%d\n",__func__,__LINE__);
}
//给对象赋值
struct platform_device pdev={.name="hello1", //选择名字列表中的名字.id=PLATFORM_DEVID_AUTO,.dev={.release=pdev_release,},.resource=res,.num_resources=ARRAY_SIZE(res),
};
static int __init demo_init(void)
{return platform_device_register(&pdev);
}
static void __exit demo_exit(void)
{platform_device_unregister(&pdev);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
驱动文件代码>>>
构建名字表
#include
#include
#include
#include
#include
struct resource *res;
int irqno;int pdir_probe(struct platform_device *pdev)
{//获取MEM类型的资源res = platform_get_resource(pdev,IORESOURCE_MEM,0);if(res==NULL){printk("get MEM resource error\n");return -ENODATA;}irqno = platform_get_irq(pdev,0);if(irqno<0){printk("get irq resource error\n");return -ENODATA;}printk("addr:%#llx,irqno:%d\n",res->start,irqno);return 0;
}
int pdir_remove(struct platform_device *pdev)
{printk("%s:%d\n",__func__,__LINE__);return 0;
}
//构建名字表
struct platform_device_id idtable[]={{"hello1",0},{"hello2",1},{"hello3",2},{},
};
//热插拔宏
MODULE_DEVICE_TABLE(platform,idtable);
//对象初始化
struct platform_driver pdir={.probe=pdir_probe,.remove=pdir_remove,.driver={.name="aaaaa",},.id_table=idtable,
};
//使用一键注册宏
module_platform_driver(pdir);
MODULE_LICENSE("GPL");
实验现象>>>
匹配成功,打印设备信息结构体的首地址


驱动程序>>>
#include
#include
#include
#include
#include
struct resource *res;
int irqno;int pdir_probe(struct platform_device *pdev)
{//获取MEM类型的资源res = platform_get_resource(pdev,IORESOURCE_MEM,0);if(res==NULL){printk("get MEM resource error\n");return -ENODATA;}irqno = platform_get_irq(pdev,0);if(irqno<0){printk("get irq resource error\n");return -ENODATA;}printk("addr:%#x,irqno:%d\n",res->start,irqno);return 0;
}
int pdir_remove(struct platform_device *pdev)
{printk("%s:%d\n",__func__,__LINE__);return 0;
}//构建compatible表
struct of_device_id oftable[]={{.compatible="hqyj,platform",},{}
};
//热插拔宏
MODULE_DEVICE_TABLE(of,oftable);
//对象初始化
struct platform_driver pdir={.probe=pdir_probe,.remove=pdir_remove,.driver={.name="aaaaa",.of_match_table=oftable,},
};
module_platform_driver(pdir);
MODULE_LICENSE("GPL");
实验现象>>>

上一篇:世界杯中隐藏的IoT物联网黑科技
下一篇:2022美亚个人赛复盘