首页 > 其他分享 >设备树下的platform驱动编写

设备树下的platform驱动编写

时间:2024-06-19 19:28:27浏览次数:11  
标签:driver platform myled 驱动 编写 树下 match 设备

文章目录


前言

基于总线、设备和驱动这样的驱动框架, Linux 内核提出来 platform 这个虚拟总线,相应的也有 platform 设备和 platform 驱动。


一、platform是什么?

platform 驱动框架分为总线、设备和驱动,其中总线是 Linux 内核提供的,不需要我们管理。使用设备树时,设备的描述被放到了设备树中,所以设备也不需要管,只需要实现platform_driver(驱动)即可。
加载成功可在/sys/bus/platform/drivers/目录下查看驱动是否存在。
在这里插入图片描述

二、编写步骤

1.在设备树中创建设备节点

在设备树中创建节点描述设备信息,compatible属性很重要,因为 platform 总线需要通过设备节点的 compatible 属性值来匹配驱动。
在使用设备树的时候 platform 驱动会通过 of_match_table 来保存兼容性值,匹配对应设备,加载驱动。
例如一个LED设备节点,compatible属性值为"my_led",若驱动程序中的.driver下的.of_match_table 与之一致,则会执行probe函数。

	led{
		compatible = "my_led";
		status = "okay";
		led-gpio = <&gpio 38 GPIO_ACTIVE_HIGH>
	};

2.注意兼容属性

驱动中的兼容用一个结构体数组来表示:

static const struct of_device_id leds_of_match[] = {
{ 
	.compatible = "my_led" }, /* 兼容属性 */
	{ /* Sentinel */ }
};

static struct platform_driver leds_platform_driver = {
	.driver = {
	.name = "zynq-led",	// /sys/bus/platform/drivers/目录下存在一个名为zynq-led的文件
	.of_match_table = leds_of_match,
	}
	.probe = leds_probe,
	.remove = leds_remove,
};

of_device_id 代表结构体数组,即驱动的兼容表,匹配则对应的probe会执行,最后一个元素一定要为空。
MODULE_DEVICE_TABLE 声明leds_of_match这个设备匹配表,该宏一般用于热插拔设备动态加载卸载中。
driver 将leds_of_match匹配表绑定到platform驱动结构中,若与设备树节点中的compatible一致,则会跳入leds_probe执行。

3.编写 platform 驱动

当驱动和设备匹配成功就会执行probe函数,执行类似字符设备那一套。简单框架如下:

static int myled_probe(struct platform_device *pdev)
{
	struct device_node *nd = pdev->dev.of_node; 
	//设备树匹配下,platform_device中内置一个 struct device 类型的变量 dev,dev.of_node即为设备中定义的节点
	
}

static int myled_remove(struct platform_device *dev) 
{
	;
}

static const struct of_device_id leds_of_match[] = {
{ 
	.compatible = "my_led" }, /* 兼容属性 */
};

static struct platform_driver myled_driver = {
	.driver = {
	.name = "zynq-led", // /sys/bus/platform/drivers/zynq-led
	.of_match_table = leds_of_match, // 设备树匹配表,用于和设备树中定义的设备匹配
	},
	.probe = myled_probe, // probe 函数
	.remove = myled_remove, // remove 函数
};

static int __init myled_driver_init(void)
{
	return platform_driver_register(&myled_driver);
}

static void __exit myled_driver_exit(void)
{
	platform_driver_unregister(&myled_driver);
}

module_init(myled_driver_init);
module_exit(myled_driver_exit);

总结

以上就是今天要讲的内容,本文介绍了设备树下platform驱动的编写,制作不易,多多包涵。

标签:driver,platform,myled,驱动,编写,树下,match,设备
From: https://blog.csdn.net/qq_40514606/article/details/139810946

相关文章

  • 请编写函数fun,对长度位7个字符的字符串,除首尾字符外,将其余5个字符按ASCII码降序排列
    请编写函数fun,对长度位7个字符的字符串,除首尾字符外,将其余5个字符按ASCII码降序排列#include<stdio.h>#include<string.h>voidsortDescending(charstr[]){intlen=strlen(str);if(len!=7) {printf("字符串长度不为7,无法进行排序。\n");......
  • 编写函数int fun(int lim,int aa[MAX]),该函数的功能是求出小于或等于lim的所有素数并
    编写函数intfun(intlim,intaa[MAX]),该函数的功能是求出小于或等于lim的所有素数并放在aa数组中,该函数返回所求的素数的个数。#include<stdio.h>#defineMAX100intisPrime(intnum){if(num<2){return0;}for(inti=2;i*i<=num;......
  • 配置platformio的血泪坑,学不会你来问我
    背景:本人于今年三月使用platformio进行esp32c3的学习。最开始使用arduino的ide。之后使用platformio,最开始使用没有任何问题,创建第一个工程很慢是正常的,挂个梯子就好。由于项目搁置过一段时间,故disable掉这个extension,五月底再次启用,发现已经无法初始化。此问题在我的ubuntu系......
  • Rust中 测试用例编写
    //注定会断言失败的代码:断言1和2会不会相等#[cfg(test)]modtests{usesuper::*;#[test]fnone_result(){assert_eq!(1,2);}}注意点 1.编程环境:vscode+rust-analyzer(插件式)2.方法上添加标签(Attribute):#[cfg(test)]3.断言语句:asser......
  • 编写函数fun,该函数的功能是:从字符中删除指定的字符,同一字母的大、小写按不同字符处理
    编写函数fun,该函数的功能是:从字符中删除指定的字符,同一字母的大、小写按不同字符处理。#include<stdio.h>#include<string.h>voidfun(char*str,charch){intlen=strlen(str);inti,j;for(i=0;i<len;i++){if(str[i]==ch||(......
  • 10、ansible-YAML-非标记语言-剧本的编写-.yaml -剧本执行ansible-playbook-handlers
     ============================================================剧本的编写==================================================通过YAML编写一个剧本,完成web的部署,配置,启动的全过程1、先将目标主机的网站服务卸载ansibleall-myum-a'name=httpdstate=removed'-o·a......
  • 编写多个函数的ROP链
    我们已经学会了编写单个和两个简单函数的ROP链,在这里我们说一下,编写ROP链多个需要注意的问题之前我们在学习两个函数的ROP时,编写了这样的payload我们当时没有考虑,参数冲突和栈溢出大小,现在我们来说一说举个例子,如果我们上次学习的两个函数的ROP中没有gets函数,而是read函数我们......
  • 编写单个函数的ROP链
    什么是ROP链在我初识栈溢出那篇博客已经详细的讲了函数的调用过程(基于X86框架),不了解的可以看一下,没有这个理论基础,是学不好ROP的。现在我们说一下什么是ROP。ROP链就是通过返回地址的修改来完成的编程,调用特定的函数的一种编程模式。我们可以联想一下你做的最简单的栈溢出的题,返......
  • 使用Modbus转Profinet网关无需编写Modbus轮询程序,实现PLC和电表通信
    一、无需编写Modbus轮询程序实现PLC与电表通信的方法在工业自动化领域,PLC(可编程逻辑控制器)与电表之间的通信是非常常见的需求。传统上,为了让PLC与电表进行通信,通常需要编写Modbus轮询程序来实现数据的读取和控制。然而,近年来出现了一种新的方法,即通过使用Modbus转Profinet网关,可......
  • [转]Power Platform / Power Apps
    管理MicrosoftPowerPlatform-PowerPlatform|MicrosoftLearn PowerPlatform管理中心(https://admin.powerplatform.microsoft.com)是管理员用于管理PowerApps、PowerAutomate、PowerPages和MicrosoftCopilotStudio的环境和设置的统一门户。PowerBI管理......