demo.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
struct device_node* node;
struct property* property;
/*
mynode@0x12345678{ //mynode节点名字
mystring = "hello DC24031";//字符串属性
myint = <0x12 0x34 0xabcd>;//无符号32位整数列表
mybin = [01 02 03 04];//二进制列表
myhex = "hello huyue",<0x12ab>,[1a 2b 3c];//混合键值对信息
};
*/
//入口
static int __init demo_init(void)
{
int i = 0;
int len;
//通过路径解析节点结构体指针
node = of_find_node_by_path("/mynode@0x12345678");
if(node == NULL){
printk("of find node by path is error\n");
return -EIO;
}
//通过解析属性API接口
property = of_find_property(node,"mystring",&len);
//解析mystring属性
printk("key = %s value = %s\n",property->name,(char*)(property->value));
//通过解析属性API接口
property = of_find_property(node,"myint",&len);
//解析无符号32位整数列表 完成大小端的转换
printk("key = %s value = %#x\n",property->name,__be32_to_cpup(property->value));
printk("key = %s value = %#x\n",property->name,__be32_to_cpup(property->value+4));
printk("key = %s value = %#x\n",property->name,__be32_to_cpup(property->value+8));
//通过解析属性API接口
property = of_find_property(node,"mybin",&len);
//解析二进制列表
for(i=0;i<4;i++)
{
printk("key = %s value = %#x\n",property->name,*(unsigned char*)(property->value + i));
}
//通过解析属性API接口
property = of_find_property(node,"myhex",&len);
//解析混合键值对信息
printk("key = %s value = %s\n",property->name,
(char*)(property->value));
printk("key = %s value = %#x\n",property->name,
__be32_to_cpup(property->value + 12));
printk("key = %s value = %#x\n",property->name,
*(unsigned char*)(property->value + 16));
printk("key = %s value = %#x\n",property->name,
*(unsigned char*)(property->value + 17));
printk("key = %s value = %#x\n",property->name,
*(unsigned char*)(property->value + 18));
return 0;
}
//出口
static void __exit demo_exit(void)
{
}
module_init(demo_init); //指定入口地址
module_exit(demo_exit); //指定出口地址
MODULE_LICENSE("GPL"); //遵循GPL协议