linux字符设备驱动开发之设备树(2)

本文章参考正点原子相关教程,仅学习使用

设备树常见of操作函数

设备树描述了设备的详细信息(比如reg->寄存器地址),编写驱动的目的在于获取这些信息.

linux提供了一些of函数读取这些信息, /include/linux/of.h.

查找节点相关函数

// 设备节点
struct device_node {
	const char *name;	
	const char *type;	
	phandle phandle;
	const char *full_name;
	struct fwnode_handle fwnode;

	struct	property *properties;
	struct	property *deadprops;	/* removed properties */
	struct	device_node *parent;
	struct	device_node *child;
	struct	device_node *sibling;
	struct	kobject kobj;
	unsigned long _flags;
	void	*data;
#if defined(CONFIG_SPARC)
	const char *path_component_name;
	unsigned int unique_id;
	struct of_irq_controller *irq_trans;
#endif
};

// 通过节点名查找指定的节点
// from : 节点, 若为NULL,则从根节点查起.
// name : 查找节点名
// return : 找到的节点
struct device_node *of_find_node_by_name(struct device_node *from,
	const char *name)

// 通过节点类型查找指定节点
struct device_node *of_find_node_by_type(struct device_node *from,
	const char *type)

// 通过指定节点类型(device_type)和兼容(compatible)查找节点
struct device_node *of_find_compatible_node(
						struct device_node *from,
						const char *type,
						const char *compat)
// 通过路径查找指定节点
// path : 全路径节点名
struct device_node *of_find_node_by_path(const char *path)

查找父/子节点相关函数

// 获取指定节点的父节点
struct device_node *of_get_parent(const struct device_node *node)

// 迭代查找子节点
// node :父节点
// prev : 前一个子节点, 设置NULL表示 从第一个节点开始
struct device_node *of_get_next_child(
	const struct device_node *node, struct device_node *prev)

提起属性的函数

// 属性结构体
struct property {
	char	*name;
	int	length;
	void	*value;
	struct property *next;
	unsigned long _flags;
	unsigned int unique_id;
	struct bin_attribute attr;
};

// 查找指定节点属性
// name : 属性名称
//lenp : 属性字节数
struct property *of_find_property(const struct device_node *np,
						const char *name,
						int *lenp)

// 获取属性中元素的数量
// proname :统计元素数量的属性名称
// elem_size : 元素长度
int of_property_count_elems_of_size(const struct device_node *np,
			const char *propname, int elem_size)

// 读取属性中 u8,u16,u32和u64类型数组数据
int of_property_read_u8_array(const struct device_node *np,
			const char *propname, u8 *out_values, size_t sz)
int of_property_read_u16_array(const struct device_node *np,
			const char *propname, u16 *out_values, size_t sz)
int of_property_read_u32_array(const struct device_node *np,
			const char *propname,u32 *out_values, size_t sz)
int of_property_read_u64_array(const struct device_node *np,
			char *propname, *out_values, size_t sz)

// 读取属性中字符串数据
int of_property_read_string(struct device_node *np,
					  const char *propname,
					  const char **out_string)

//读取数据整形数据
int of_property_read_u8(const struct device_node *np,
				       const char *propname,
				       u8 *out_value)
int of_property_read_u16(const struct device_node *np,
				       const char *propname,
				       u16 *out_value)
int of_property_read_u32(const struct device_node *np,
				       const char *propname,
				       u32 *out_value)

// 获取节点 address-cells
int of_n_addr_cells(struct device_node *np)
// 获取节点size-cells
int of_n_size_cells(struct device_node *np);

其他常用函数

// 检查节点是否与name兼容
int of_device_is_compatible(const struct device_node *device,
					  const char *name)

//of_address.h

// 转换设备树读取到的地址为物理地址
// addr 要转换的地址
u64 of_translate_address(struct device_node *np, const __be32 *addr)



// 替代ioremap,of_iomap本质是将reg属性中地址信息转化为虚拟地址,若为多段,使用index转化
// index : reg属性中内存映射段,一段设置为0
void __iomem *of_iomap(struct device_node *device, int index);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值