嵌入式linux/鸿蒙开发板(IMX6ULL)开发(三十一)驱动进化之路:设备树的引入及简明教程(下)

简介: 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(三十一)驱动进化之路:设备树的引入及简明教程

1.7.3 有些节点不会生成platform_device,怎么访问它们


内核会把dtb文件解析出一系列的device_node结构体,我们可以直接访问这些device_node。

内核源码incldue/linux/of.h中声明了device_node和属性property的操作函数,device_node和property的结构体定义如下:



1670919643166.jpg


1.7.3.1 找到节点


a. of_find_node_by_path


根据路径找到节点,比如“/”就对应根节点,“/memory”对应memory节点。 函数原型:

static inline struct device_node *of_find_node_by_path(const char *path);

b. of_find_node_by_name


根据名字找到节点,节点如果定义了name属性,那我们可以根据名字找到它。 函数原型:

extern struct device_node *of_find_node_by_name(struct device_node *from,
  const char *name);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。

但是在设备树的官方规范中不建议使用“name”属性,所以这函数也不建议使用。


c. of_find_node_by_type


根据类型找到节点,节点如果定义了device_type属性,那我们可以根据类型找到它。 函数原型:

extern struct device_node *of_find_node_by_type(struct device_node *from,
  const char *type);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。

但是在设备树的官方规范中不建议使用“device_type”属性,所以这函数也不建议使用。


d. of_find_compatible_node


根据compatible找到节点,节点如果定义了compatible属性,那我们可以根据compatible属性找到它。 函数原型:

extern struct device_node *of_find_compatible_node(struct device_node *from,
  const char *type, const char *compat);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。

参数compat是一个字符串,用来指定compatible属性的值;

参数type是一个字符串,用来指定device_type属性的值,可以传入NULL。


e. of_find_node_by_phandle


根据phandle找到节点。

dts文件被编译为dtb文件时,每一个节点都有一个数字ID,这些数字ID彼此不同。可以使用数字ID来找到device_node。这些数字ID就是phandle。


函数原型:

extern struct device_node *of_find_node_by_phandle(phandle handle);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。


f. of_get_parent


找到device_node的父节点。 函数原型:

extern struct device_node *of_get_parent(const struct device_node *node);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。


g. of_get_next_parent


这个函数名比较奇怪,怎么可能有“next parent”?

它实际上也是找到device_node的父节点,跟of_get_parent的返回结果是一样的。

差别在于它多调用下列函数,把node节点的引用计数减少了1。这意味着调用of_get_next_parent之后,你不再需要调用of_node_put释放node节点。

of_node_put(node);


函数原型:

extern struct device_node *of_get_next_parent(struct device_node *node);


参数from表示从哪一个节点开始寻找,传入NULL表示从根节点开始寻找。


h. of_get_next_child


取出下一个子节点。 函数原型:

extern struct device_node *of_get_next_child(const struct device_node *node,
          struct device_node *prev);


参数node表示父节点; prev表示上一个子节点,设为NULL时表示想找到第1个子节点。

不断调用of_get_next_child时,不断更新pre参数,就可以得到所有的子节点。


i. of_get_next_available_child


取出下一个“可用”的子节点,有些节点的status是“disabled”,那就会跳过这些节点。 函数原型:

struct device_node *of_get_next_available_child(const struct device_node *node,
  struct device_node *prev);


参数node表示父节点; prev表示上一个子节点,设为NULL时表示想找到第1个子节点。


j. of_get_child_by_name


根据名字取出子节点。

函数原型:

extern struct device_node *of_get_child_by_name(const struct device_node *node,
      const char *name);


参数node表示父节点; name表示子节点的名字。


1.7.3.2 找到属性


内核源码incldue/linux/of.h中声明了device_node的操作函数,当然也包括属性的操作函数。


a. of_find_property


找到节点中的属性。

函数原型:

extern struct property *of_find_property(const struct device_node *np,
      const char *name,
      int *lenp);


参数np表示节点,我们要在这个节点中找到名为name的属性。 lenp用来保存这个属性的长度,即它的值的长度。


在设备树中,节点大概是这样:

xxx_node {
    xxx_pp_name = “hello”;
};


上述节点中,“xxx_pp_name”就是属性的名字,值的长度是6。


1.7.3.3 获取属性的值


a. of_get_property


根据名字找到节点的属性,并且返回它的值。 函数原型:

/*
 * Find a property with a given name for a given node
 * and return the value.
 */
const void *of_get_property(const struct device_node *np, const char *name,
       int *lenp)


参数np表示节点,我们要在这个节点中找到名为name的属性,然后返回它的值。 lenp用来保存这个属性的长度,即它的值的长度。


b. of_property_count_elems_of_size


根据名字找到节点的属性,确定它的值有多少个元素(elem)。 函数原型:

* of_property_count_elems_of_size - Count the number of elements in a property
 *
 * @np:  device node from which the property value is to be read.
 * @propname: name of the property to be searched.
 * @elem_size:  size of the individual element
 *
 * Search for a property in a device node and count the number of elements of
 * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
 * property does not exist or its length does not match a multiple of elem_size
 * and -ENODATA if the property does not have a value.
 */
int of_property_count_elems_of_size(const struct device_node *np,
    const char *propname, int elem_size)


参数np表示节点,我们要在这个节点中找到名为propname的属性,然后返回下列结果:

return prop->length / elem_size;


在设备树中,节点大概是这样:

xxx_node {
    xxx_pp_name = <0x50000000 1024>  <0x60000000  2048>;
};


调用of_property_count_elems_of_size(np, “xxx_pp_name”, 8)时,返回值是2;

调用of_property_count_elems_of_size(np, “xxx_pp_name”, 4)时,返回值是4。


c. 读整数u32/u64


函数原型为:

static inline int of_property_read_u32(const struct device_node *np,
           const char *propname,
           u32 *out_value);
extern int of_property_read_u64(const struct device_node *np,
    const char *propname, u64 *out_value);


在设备树中,节点大概是这样:

xxx_node {
    name1 = <0x50000000>;
    name2 = <0x50000000  0x60000000>;
};


调用of_property_read_u32 (np, “name1”, &val)时,val将得到值0x50000000;

调用of_property_read_u64 (np, “name2”, &val)时,val将得到值0x0x6000000050000000。


d. 读某个整数u32/u64


函数原型为:

extern int of_property_read_u32_index(const struct device_node *np,
           const char *propname,
           u32 index, u32 *out_value);


在设备树中,节点大概是这样:

xxx_node {
    name2 = <0x50000000  0x60000000>;
};


调用of_property_read_u32 (np, “name2”, 1, &val)时,val将得到值0x0x60000000。


e. 读数组


函数原型为:

int of_property_read_variable_u8_array(const struct device_node *np,
      const char *propname, u8 *out_values,
      size_t sz_min, size_t sz_max);
int of_property_read_variable_u16_array(const struct device_node *np,
      const char *propname, u16 *out_values,
      size_t sz_min, size_t sz_max);
int of_property_read_variable_u32_array(const struct device_node *np,
          const char *propname, u32 *out_values,
          size_t sz_min, size_t sz_max);
int of_property_read_variable_u64_array(const struct device_node *np,
          const char *propname, u64 *out_values,
          size_t sz_min, size_t sz_max);


在设备树中,节点大概是这样:

xxx_node {
    name2 = <0x50000012  0x60000034>;
};


上述例子中属性name2的值,长度为8。

调用of_property_read_variable_u8_array (np,“name2”, out_values, 1, 10)时,out_values中将会保存这8个字节:0x12,0x00,0x00,0x50,0x34,0x00,0x00,0x60。

调用of_property_read_variable_u16_array (np, “name2”, out_values, 1, 10)时,out_values中将会保存这4个16位数值: 0x0012, 0x5000,0x0034,0x6000。


总之,这些函数要么能取到全部的数值,要么一个数值都取不到;

如果值的长度在sz_min和sz_max之间,就返回全部的数值;否则一个数值都不返回。


f. 读字符串


函数原型为:

int of_property_read_string(const struct device_node *np, const char *propname,
    const char **out_string);


返回节点np的属性(名为propname)的值,(*out_string)指向这个值,把它当作字符串。


1.8 怎么修改设备树文件


一个写得好的驱动程序, 它会尽量确定所用资源。 只把不能确定的资源留给设备树, 让设备树来指定。


根据原理图确定"驱动程序无法确定的硬件资源", 再在设备树文件中填写对应内容。 那么, 所填写内容的格式是什么?


1.8.1 使用芯片厂家提供的工具


有些芯片,厂家提供了对应的设备树生成工具,可以选择某个引脚用于某些功能,就可以自动生成设备树节点。

你再把这些节点复制到内核的设备树文件里即可。


1.8.2 看绑定文档


内核文档 Documentation/devicetree/bindings/做得好的厂家也会提供设备树的说明文档


1.8.3 参考同类型单板的设备树文件


1.8.4 网上搜索


1.8.5 实在没办法时, 只能去研究驱动源码

相关文章
|
9月前
|
Linux 编译器 开发者
Linux设备树解析:桥接硬件与操作系统的关键架构
在探索Linux的庞大和复杂世界时🌌,我们经常会遇到许多关键概念和工具🛠️,它们使得Linux成为了一个强大和灵活的操作系统💪。其中,"设备树"(Device Tree)是一个不可或缺的部分🌲,尤其是在嵌入式系统🖥️和多平台硬件支持方面🔌。让我们深入了解Linux设备树是什么,它的起源,以及为什么Linux需要它🌳。
Linux设备树解析:桥接硬件与操作系统的关键架构
|
9月前
|
Linux
嵌入式linux系统设备树实例分析
嵌入式linux系统设备树实例分析
137 0
|
1月前
|
Ubuntu Linux 开发者
Ubuntu20.04搭建嵌入式linux网络加载内核、设备树和根文件系统
使用上述U-Boot命令配置并启动嵌入式设备。如果配置正确,设备将通过TFTP加载内核和设备树,并通过NFS挂载根文件系统。
95 15
|
6月前
|
Linux SoC
Linux设备树(DTS)
Dts:DTS即Device Tree Source,是一个文本形式的文件,用于描述硬件信息。一般都是固定信息,无法变更,无法overlay。 设备树由来 linux内核源码中,之前充斥着大量的平台相关(platform Device)配置,而这些代码大多是杂乱且重复的,这使得ARM体系结构的代码维护者和内核维护者在发布一个新的版本的时候有大量的工作要做,以至于LinusTorvalds 在2011年3月17日的ARM Linux邮件列表中宣称“Gaah.Guys,this whole ARM thing is a f*cking pain in the ass”这使得整个ARM社区不得不
Linux设备树(DTS)
|
6月前
|
NoSQL Linux C语言
嵌入式GDB调试Linux C程序或交叉编译(开发板)
【8月更文挑战第24天】本文档介绍了如何在嵌入式环境下使用GDB调试Linux C程序及进行交叉编译。调试步骤包括:编译程序时加入`-g`选项以生成调试信息;启动GDB并加载程序;设置断点;运行程序至断点;单步执行代码;查看变量值;继续执行或退出GDB。对于交叉编译,需安装对应架构的交叉编译工具链,配置编译环境,使用工具链编译程序,并将程序传输到开发板进行调试。过程中可能遇到工具链不匹配等问题,需针对性解决。
256 3
|
6月前
|
存储 Unix Linux
揭秘Linux硬件组成:从内核魔法到设备树桥梁,打造你的超级系统,让你的Linux之旅畅通无阻,震撼体验来袭!
【8月更文挑战第5天】Linux作为顶级开源操作系统,凭借其强大的功能和灵活的架构,在众多领域大放异彩。本文首先概述了Linux的四大核心组件:内核、Shell、文件系统及应用程序,并深入探讨了内核的功能模块,如存储、CPU及进程管理等。接着介绍了设备树(Device Tree),它是连接硬件与内核的桥梁,通过DTS/DTB文件描述硬件信息,实现了跨平台兼容。此外,还简要介绍了Linux如何通过本地总线高效管理硬件资源,并阐述了文件系统与磁盘管理机制。通过这些内容,读者可以全面了解Linux的硬件组成及其核心技术。
96 3
|
7月前
|
Linux SoC
Linux设备树(DTS)介绍
**设备树(DTS)是Linux中用于描述硬件信息的文本文件,旨在减少内核与平台相关代码的耦合。DTS文件包含静态硬件配置,不支持动态变更。它被编译成DTB二进制文件,供内核在启动时解析以了解硬件布局。设备树解决了ARM体系结构代码维护的复杂性问题,通过解耦实现vendor修改的独立和共二进制目标。设备树overlay允许对配置进行增量修改,遵循特定规则,如dts覆盖dtsi,先引用后修改。调试时,可使用内置工具反编译dtb或dtbo映像为dts文本以检查内容。**
310 7
|
6月前
|
Linux SoC
【linux】【设备树】中断描述
【linux】【设备树】中断描述
65 0
|
6月前
|
Linux SoC
Linux设备树(DTS)
Dts:DTS即Device Tree Source,是一个文本形式的文件,用于描述硬件信息。一般都是固定信息,无法变更,无法overlay。 设备树由来 linux内核源码中,之前充斥着大量的平台相关(platform Device)配置,而这些代码大多是杂乱且重复的,这使得ARM体系结构的代码维护者和内核维护者在发布一个新的版本的时候有大量的工作要做,以至于LinusTorvalds 在2011年3月17日的ARM Linux邮件列表中宣称“Gaah.Guys,this whole ARM thing is a f*cking pain in the ass”这使得整个ARM社区不得不
|
8月前
|
Linux
【GEC6818开发板】Linux驱动中printk无法在终端输出显示
【GEC6818开发板】Linux驱动中printk无法在终端输出显示