#include <linux/module.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#include <linux/of.h>
/*mynode@0x12345678{
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 i,len,ret;
unsigned int val;
const char *str;
unsigned int array[2];
static __init int demo_init(void)
{
//通过路径获取设备树节点信息
node=of_find_node_by_path("/mynode@0x12345678");
if(node==NULL)
{
printk("error\n");
return -EFAULT;
}
//解析字符串
pr=of_find_property(node,"astring",&len);
if(pr==NULL)
{
printk("error\n");
return -EFAULT;
}
printk("name=%s,value=%s\n",pr->name,(char *)pr->value);
解析字符串的现象
获取单字节属性数值
//解析单字节属性
pr=of_find_property(node,"binarry",&len);
if(pr==NULL)
{
printk("error\n");
return -EFAULT;
}
//循环遍历单字节属性数值
for(i=0;i<len;i++)
{
printk("name=%s,value=%#x\n",pr->name,*((char *)pr->value+i));
}
实验现象
获取u32的值、数组、字符串
ret=of_property_read_u32_index(node,"uint",1,&val);
if(ret<0)
{
printk("error\n");
return -EFAULT;
}
printk("value=%#x\n",val);
//获取u32的数组
ret=of_property_read_variable_u32_array(node,"uint",array,2,2);
if(ret<0)
{
printk("error\n");
return -EFAULT;
}
printk("value=%#x,%#x\n",array[0],array[1]);
//获取字符串类型的值
ret=of_property_read_string(node,"astring",&str);
if(ret)
{
printk("error\n");
return -EFAULT;
}
printk("value=%s\n",str);
实验现象