linux of函数实例
/ { wanghb { gpio = <129>; gpios = <129 130 131>; args = "wanghbargs"; compatible = "wanghbcompatible"; child1@0x1000012{ args = "child1"; gpios = <120>; }; child2@0x1000012{ args = "child2"; gpios = <121>; }; }; zmm@0x120000{ args = "zmm"; gpios = <120>; }; };
主要来练习 of_find_node_by_path, of_find_node_by_name,of_property_read_u32_array ,of_property_read_string ,of_property_count_elems_of_size , of_get_next_child ,of_get_parent 函数
#include <linux/of.h> #include <linux/device.h> #include <linux/platform_device.h> #include <linux/err.h> #include <linux/errno.h> #include <linux/list.h> #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> struct device_node * utils_find_node_by_path(char * node_path) { struct device_node * node = NULL; node = of_find_node_by_path(node_path); if(node != NULL) { printk(KERN_INFO "[success find device_node ,%s]\r\n",node_path); } else { printk(KERN_INFO "[could not find device_node ,%s]\r\n",node_path); } return node; } struct device_node * utils_find_node_by_name(char * node_name) { struct device_node * node = NULL; node = of_find_node_by_name(NULL,node_name); if(node != NULL) { printk(KERN_INFO "[success find device_node ,%s]\r\n",node_name); } else { printk(KERN_INFO "[could not find device_node ,%s]\r\n",node_name); } return node; } static int __init test_module_init(void) { struct device_node * wanghb = NULL; struct device_node * zmm = NULL; struct device_node * parent = NULL; struct device_node * child = NULL; struct property * wanghb_args = NULL; u32 value_array[10] = {0}; u32 size = 0; const char * string ; const char * string1 ; const char * string2 ; god_bless(); wanghb = utils_find_node_by_path("/wanghb"); wanghb = utils_find_node_by_name("wanghb"); zmm = utils_find_node_by_path("/zmm@0x120000"); zmm = utils_find_node_by_name("zmm@0x120000"); zmm = utils_find_node_by_name("zmm"); printk(KERN_INFO "===================================================================================="); wanghb_args = of_find_property(wanghb,"args",NULL); printk(KERN_INFO "[wanghb_args value : %s, name :%s]\r\n",(char*)wanghb_args->value,(char*)wanghb_args->name); wanghb_args = of_find_property(wanghb,"compatible",NULL); printk(KERN_INFO "[compatible value : %s, name :%s]\r\n",(char*)wanghb_args->value,(char*)wanghb_args->name); of_property_read_string(wanghb,"args",&string); printk(KERN_INFO "[args : %s, ]\r\n",string); of_property_read_string(wanghb,"compatible",&string); printk(KERN_INFO "[compatible : %s, ]\r\n",string); of_property_read_u32_array(wanghb,"gpio",value_array,1); printk(KERN_INFO "[gpio : %d]\r\n",value_array[0]); of_property_read_u32_array(wanghb,"gpios",value_array,3); printk(KERN_INFO "[gpio [0]: %d ,gpio [1]: %d ,gpio [2]: %d ]\r\n",value_array[0],value_array[1],value_array[2]); size = of_property_count_elems_of_size(wanghb, "gpios", sizeof(u32)); printk(KERN_INFO "[size : %d]\r\n",size); child = of_get_next_child(wanghb,NULL); if(child) { of_property_read_string(child,"args",&string1); printk(KERN_INFO "[args : %s]\r\n",string1); child = of_get_next_child(child,child); of_property_read_string(child,"args",&string2); printk(KERN_INFO "[args : %s]\r\n",string2); } else { printk(KERN_ERR "[could not find childnode ]\r\n"); } parent = of_get_parent(child); of_property_read_string(parent,"args",&string); printk(KERN_INFO "[parentargs : %s]\r\n",string); printk(KERN_INFO "===================================================================================="); return 0; } static void test_module_exit(void) { } module_init(test_module_init); module_exit(test_module_exit); MODULE_AUTHOR("wanghb"); MODULE_LICENSE("GPL");
结果如下: