#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 27 b9 f9 be];
mixed = "hello", [11 22], <0x12345678>;
}
*/
struct device_node *node;
static int __init demo_init(void)
{
int res, i;
unsigned int value1, value2;
unsigned int value[10] = {0};
const char *buf;
unsigned char arr[128] = "";
node = of_find_node_by_path("/mynode@0x12345678");
if (NULL == node)
{
printk("访问设备树节点失败\n");
return -EFAULT;
}
printk("success to find node\n");
res = of_property_read_u32_index(node, "uint", 0, &value1);
if (res)
{
printk("获取数值失败\n(err %d)", -res);
return -res;
}
res = of_property_read_u32_index(node, "uint", 1, &value2);
if (res)
{
printk("获取数值失败(err %d)\n", -res);
return -res;
}
printk("mynode uint = %#x, %#x\n", value1, value2);
res = of_property_read_variable_u32_array(node, "uint", value, 0, 2);
if (res < 0)
{
printk("获取数值失败(err %d)\n", -res);
return -res;
}
for (i = 0; i < res; i++)
{
printk("mynode uint = %#x\n", value[i]);
}
res = of_property_read_string(node, "astring", &buf);
if (res)
{
printk("获取数值失败(err %d)\n", -res);
return -res;
}
printk("mynode astring = %s\n", buf);
res = of_property_read_variable_u8_array(node, "binarry", arr, 1, 8);
if (res < 0)
{
printk("获取数值失败(err %d)\n", -res);
return -res;
}
for (i = 0; i < res; i++)
{
printk("mynode binary = %#x\n", arr[i]);
}
res = of_property_read_u8_array(node, "compatible", arr, 12);
if (res)
{
printk("获取数值失败(err %d)\n", -res);
return -res;
}
for (i = 0; i < 12; i++)
{
printk("mynode compatible = %c\n", arr[i]);
}
return 0;
}
static void __exit demo_exit(void)
{
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");