【Linux】【驱动】设备树常用 of 函数
- 序
- 常用的of函数
- 添加我们要查找的节点的代码 of_find_node_by_path
- 获取 compatible 属性内容的代码 of_find_property
- 获取 reg 属性内容的代码 of_property_read_u32_array
- 获取 status 属性内容的代码 of_property_read_string
- 演示代码
- 操作相关的指令
序
应用层的app代码,通过 of函数来读取设备树中记录的信息
下面就展示一些of函数的使用方式
常用的of函数
添加我们要查找的节点的代码 of_find_node_by_path
test_device_node 这个就是读取到的设备节点
/**********添加我们要查找的节点的代码*******************************/// of_find_node_by_path 函数通过路径查找节点test_device_node = of_find_node_by_path("/test");
获取 compatible 属性内容的代码 of_find_property
test_node_property 这个就包含了设备中的:name,value等信息
/**********获取 compatible 属性内容的代码****************************/// of_find_property 函数查找节点属性test_node_property = of_find_property(test_device_node, "compatible", &size);if (test_node_property == NULL){//判断是否查找到节点属性内容printk("test_node_property is error \n");return -1;}//打印属性 compatible 的名字printk("test_node_property name is %s\n", test_node_property->name);//打印属性 compatible 的值printk("test_node_property value is %s\n", (char *)test_node_property->value);
获取 reg 属性内容的代码 of_property_read_u32_array
out_values,就是读取出的信息
ret ,是判断读取是否正常
/**********获取 reg 属性内容的代码**********************************/ret = of_property_read_u32_array(test_device_node, "reg", out_values, 2);if (ret < 0){//打印获取失败printk("of_property_read_u32_array is error \n");return -1;}printk("out_values[0] is 0x%8x\n", out_values[0]);printk("out_values[1] is 0x%8x\n", out_values[1]);
获取 status 属性内容的代码 of_property_read_string
/**********获取 status 属性内容的代码*********************************/// of_property_read_string 读取字符串属性ret = of_property_read_string(test_device_node, "status", &str);if (ret < 0){//打印获取失败printk("of_property_read_string is error \n");return -1;}//打印 status 属性printk("status is %s \n", str);
演示代码
/*
* @Author:topeet
* @Description: of 函数获取 status 属性
*/
#include <linux/init.h> //初始化头文件
#include <linux/module.h> //最基本的文件,支持动态添加和卸载模块。
#include <linux/of.h> //添加头文件
int size; //定义长度
u32 out_values[2] = {0};
const char *str;
struct device_node *test_device_node; //定义结构体表示我们的节点
struct property *test_node_property; //定义结构体表示我们的节点属性
static int hello_init(void)
{int ret;printk("hello world! \n");/**********添加我们要查找的节点的代码*******************************/// of_find_node_by_path 函数通过路径查找节点test_device_node = of_find_node_by_path("/test");if (test_device_node == NULL){//判断是否查找节点成功printk("of_find_node_by_path is error \n");return -1;}//打印节点的名字printk("test_device_node name is %s\n", test_device_node->name);/**********获取 compatible 属性内容的代码****************************/// of_find_property 函数查找节点属性test_node_property = of_find_property(test_device_node, "compatible", &size);if (test_node_property == NULL){//判断是否查找到节点属性内容printk("test_node_property is error \n");return -1;}//打印属性 compatible 的名字printk("test_node_property name is %s\n", test_node_property->name);//打印属性 compatible 的值printk("test_node_property value is %s\n", (char *)test_node_property->value);/**********获取 reg 属性内容的代码**********************************/ret = of_property_read_u32_array(test_device_node, "reg", out_values, 2);if (ret < 0){//打印获取失败printk("of_property_read_u32_array is error \n");return -1;}printk("out_values[0] is 0x%8x\n", out_values[0]);printk("out_values[1] is 0x%8x\n", out_values[1]);/**********获取 status 属性内容的代码*********************************/// of_property_read_string 读取字符串属性ret = of_property_read_string(test_device_node, "status", &str);if (ret < 0){//打印获取失败printk("of_property_read_string is error \n");return -1;}//打印 status 属性printk("status is %s \n", str);return 0;
}static void hello_exit(void)
{printk("gooodbye! \n");
}module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");