#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
/*mynode@0x12345678{
compatible = "hqyj,mynode";
astring="hello 21091";
uint =<0xaabbccdd 0x11223344>;
binarry=[00 0c 29 7b f9 be];
mixed ="hello",[11 22],<0x12345678>;
};*/
//定义指针指向获取的设备树节点信息空间
struct device_node *node;
struct property *pr;
int len,i,ret;
u32 value;
u32 values[2];
const char *str;
static int __init mycdev_init(void)
{
//通过路径获取设备树节点信息
node=of_find_node_by_path("/mynode@0x12345678");
if(node==NULL)
{
printk("通过路径解析设备树节点失败\n");
return -EFAULT;
}
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,%#x\n",node->properties->next->next->name,__be32_to_cpup((int *)node->properties->next->next->value),__be32_to_cpup((int *)node->properties->next->next->value+1));
//解析字符串
pr=of_find_property(node,"astring",&len);
if(pr==NULL)
{
printk("属性解析失败\n");
return -EFAULT;
}
for(i=0;i<len;i++){
printk("name=%s value=%#x\n",pr->name,*((char *)pr->value+i));
}
//通过键名获取32位无符号整型的值
ret=of_property_read_u32_index(node,"uint",1,&value);
if(ret!=0){
printk("获取U32值失败\n");
return -EFAULT;
}
printk("value=%#x\n",value);
//获取一个U32位数组
ret=of_property_read_variable_u32_array(node,"uint",values,2,2);
if(ret<0){
printk("获取U32组失败\n");
return -EFAULT;
}
printk("value1=%#x %#x\n",values[0],values[1]);
//读取字符串类型的值
ret=of_property_read_string(node,"astring",&str);
if(ret!=0){
printk("读取字符串类型的值失败\n");
return -EFAULT;
}
printk("str:%s\n",str);
//获取一个U8位数组
ret=of_property_read_variable_u32_array(node,"uint",values,2,2);
if(ret<0){
printk("获取U8组失败\n");
return -EFAULT;
}
printk("value1=%#o %#o\n",values[0],values[1]);
return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");