1.设备树是干嘛的
硬件资源有很多,想要实现分类管理,方便驱动去控制它,则需要设备树来管理硬件信息。
所以,设备树主要存放了一些设备节点信息,键值对,和属性;节点中也可以包含子节点。
2.设备树节点信息的获取
有三种方法可以获取设备树信息:
1)通过获取节点的函数---- ---- of_find_node_by_path 顾名思义通过路径获取节点信息
成功返回目标节点的地址,失败返回NULL
为什么调用大小端转换函数?
因为arm架构是大端存储(低地址存储高位数据)正常的阅读习惯是千百十个,低地址存低位
2)通过属性相关函数---struct property *of_find_property(节点地址,节点名,获取到的字节数)
3)通过键名获取数值
int of_property_read_u32_index(np:节点结构体指针 propname:键名 index:索引号 out_value:获取到的值 )
返回值:成功返回0,失败返回错误码
int of_property_read_variable_u32_array()
int of_property_read_string
int of_property_read_variable_u8_array
int of_property_read_u8_array
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
//定义结构体获取设备树节点信息
struct device_node *node;
//compatible
struct device_node *compa;
//定义属性结构体获取设备树节点信息
struct property *pr;
int i,len;
//键名获取数值给到val
unsigned int val;
//获取u32数组
unsigned int array[2];
//获取u8数组
char arry[6];
//获取字符串给到str
const char *str;
int ret;
static int __init mycdev_init(void)
{
//通过路径获取设备节点信息
node=of_find_node_by_path("/mynode@0x12345678");
if(node==NULL)
{
printk("failed\n");
return -EFAULT;
}
printk("succeed\n");
printk("name=%s,value=%s\n",node->properties->name,(char *)node->properties->value);
printk("name=%s,value=%s\n",node->properties->next->name,(char *)node->properties->next->value);
//32位无符号整形需要实现小端转大端再输出
printk("name=%s,value=%#x,%#x\n",node->properties->name,__be32_to_cpup((int *)node->properties->next->next->value),\
__be32_to_cpup((int *)node->properties->next->next->value+1));
//通过compatible获取节点信息
compa=of_find_compatible_node(NULL,NULL,"hqyj,mynode");
if(compa==NULL)
{
printk("failed\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("succeed\n");
printk("name1=%s,value1=%s\n",compa->properties->name,(char *)compa->properties->value);
//1.根据属性名或节点信息结构体名解析出节点内指定名字的属性信息
pr=of_find_property(node,"astring",&len);
if(pr==NULL)
{
printk("属性解析失败\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("属性解析成功\n");
printk("name=%s,value=%s\n",pr->name,(char *)pr->value);
//解析单字节属性
pr=of_find_property(node,"binarry",&len);
if(pr==NULL)
{
printk("属性解析失败\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
//循环遍历单字节属性值
for(i=0;i<len;i++)
{
printk("name=%s value=%#x\n",pr->name,*((char *)pr->value+i));
}
//2.通过键名获取属性名
//1)根据索引号index获取u32
ret=of_property_read_u32_index(node,"unit",1,&val);
if(ret)
{
printk("u32获取失败,ret=%d\n",ret);
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("value=%#x\n",val);
//2)获取u32数组,失败返回错误码
ret=of_property_read_variable_u32_array(node,"unit",array,2,2);
if(ret<0)
{
printk("获取u32数组失败\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("value:%#x %#x\n",array[0],array[1]);
//3)获取u8数组1
ret=of_property_read_variable_u8_array(node,"binarry",arry,6,6);
if(ret<0)
{
printk("获取u8数组失败\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("binarry=[");
for(i=0;i<6;i++)
{
printk("%#x ",arry[i]);
}
printk("]\n");
//3)获取u8数组2
ret=of_property_read_u8_array(node,"binarry",arry,6);
if(ret)
{
printk("获取u8数组失败\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("binarry=[");
for(i=0;i<6;i++)
{
printk("%#x ",arry[i]);
}
printk("]\n");
//属性获取字符串
ret=of_property_read_string(node,"astring",&str);
if(ret)
{
printk("string not get\n");
printk("%d\n",__LINE__);
return -EFAULT;
}
printk("astring=%s\n",str);
return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
打印信息: