驱动设备树节点通过键获取值的相关函数
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
//通过指针指向获取设备树节点信息
struct device_node *node;
struct property *pr;
int len;
int val;
int ret;
int arry[10]={};
const char *str;
static int __init mycdev_init(void)
{
//通过路径方式获取设备树节点结构体(节点信息)
node = of_find_node_by_path("/mynode@0x12345678");
if(node == NULL)
{
printk("of_node is error\n");
return -EFAULT;
}
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, "binarry", &len);
// if(pr == NULL)
// {
// printk("property is 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_variable_u32_array(node,"uint", arry,2,2);
if(ret<0)
{
printk("arry is error\n");
return -EFAULT;
}
printk("111value=%#x,%#x\n", arry[0], arry[1]);
//通过键获取32位无符号整型的值
ret = of_property_read_u32_index(node,"uint", 0, &val);
if(ret)
{
printk("index is error\n");
return -EFAULT;
}
printk("val=%#x\n", val);
//通过键读取字符串类型的值
ret = of_property_read_string(node, "astring", &str);
if(ret)
{
printk("string is error\n");
return -EFAULT;
}
printk("str=%s\n",str);
return 0;
}
static void __exit mycdev_exit(void)
{
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
运行结果