完成设备树属性获取相关实验,通过键获取值
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
/*mynode@0x12345678{
compatible = "hqyj,mynode";
astring="hello 22071";
uint =<0xaabbccdd 0x11223344>;
binarry=[00 0c 29 7b f9 be];
mixed ="hello",[11 22],<0x12345678>;
};*/
struct device_node *node;
struct property *property;
unsigned int val;
unsigned int array[10];
unsigned char brray[10];
const char *str;
static int __init mycdev_init(void)
{
int lenp,i;
int ret;
//通过路径获取设备树结点信息
node=of_find_node_by_path("/mynode@0x12345678");
if(NULL==node)
{
printk("获取设备树结点信息失败\n");
return -EFAULT;
}
printk("成功获取到设备树结点\n");
/***********************************************************************************/
//直接打印数据
//第一行数据
printk("直接打印数据\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);
//第三行数据
printk("name=%s value=%#x\n",node->properties->next->next->name,__be32_to_cpup((int*)node->properties->next->next->value+1));
/***********************************************************************************/
//通过属性解析API来读取数据
printk("通过属性解析API来读取数据\n");
property=of_find_property(node,"compatible",&lenp);
if(NULL==property)
{
printk("属性解析失败\n");
return -EFAULT;
}
printk("name=%s value=%s\n",property->name,(char *)property->value);
property=of_find_property(node,"uint",&lenp);
if(NULL==property)
{
printk("属性解析失败\n");
return -EFAULT;
}
printk("name=%s value=%#x\n",property->name,*((int *)property->value));
printk("name=%s value=%#x\n",property->name,*((int *)property->value+1));
property=of_find_property(node,"binarry",&lenp);
if(NULL==property)
{
printk("属性解析失败\n");
return -EFAULT;
}
for(i=0;i<lenp;i++)
{
printk("name=%s value=%#x\n",property->name,*((char*)property->value+i));
}
/***********************************************************************************/
//通过键名获取相关属性API
printk("通过键名获取相关属性API\n");
//获取32位无符号整型的值
printk("获取32位无符号整型的值\n");
ret=of_property_read_u32_index(node,"uint",0,&val);
if(ret)
{
printk("通过键名获取相关属性\n");
return -EFAULT;
}
printk("value=%#x\n",val);
//获取32位无符号整型的数组
printk("获取32位无符号整型的数组\n");
ret=of_property_read_variable_u32_array(node,"uint",array,0,10);
if(ret<0)
{
printk("通过键名获取u32数组失败\n");
return -EFAULT;
}
printk("value=%#x %#x\n",array[0],array[1]);
//获取字符除串类型
printk("获取字符除串类型\n");
ret=of_property_read_string(node,"astring",&str);
if(ret)
{
printk("通过键名获取字符串失败\n");
return -EFAULT;
}
printk("value=%s\n",str);
//获取u8类型的数组
printk("获取u8类型的数组\n");
ret=of_property_read_variable_u8_array(node,"binarry",brray,0,10);
if(ret<0)
{
printk("通过键名获取u8数组失败\n");
return -EFAULT;
}
for(i=0;i<ret;i++)
{
printk("value=%#x\n",brray[i]);
}
return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");