[Get device tree node information]

What is a device tree

A device tree is a structured file that is usually used to represent and control hardware devices in embedded Linux. It consists of a series of key-value pairs that identify a hardware node and assign it properties and device IDs. The node that describes a hardware device information is called a device node. A device node contains multiple different attributes of the current hardware. Different attributes of the same node are stored in a chain structure.

Format of key-value pairs

Text strings (null terminated) are denoted by double quotes:
string-property = “a string”;
“cell” is a 32-bit unsigned integer delimited by angle brackets:
cell-property = <0xbeef 123 0xabcd1234>;
binary data is delimited by Square brackets separate:
binary-property = [0x01 0x23 0x45 0x67];
data in different representations can be connected together using commas:
mixed-property = “a string”, [0x01 0x23 0x45 0x67], <0x12345678>;
commas are also used To create a list of strings:
string-list = “red fish”, “blue fish”;

Add a custom device node

 mynode@0x12345678{
    compatible = "hqyj,mynode";
        astring="hello world";
    uint  =<0xaabbccdd 0x11223344>;
        binarry=[00 0c 29 7b f9 be];
        mixed ="hello",[11 22],<0x12345678>;
     };

How to obtain device number information in the driver

Get device tree node information

1. Parse the device tree node according to the name of the device tree node

struct device_node *of_find_node_by_name(struct device_node *from,
    const char *name);

2. Parse the device tree node according to the device tree node path

struct device_node *of_find_node_by_path(const char *path)

3. Parse the device tree node according to the compatibe key in the device tree node

struct device_node *of_find_compatible_node(struct device_node *from,
    const char *type, const char *compat);

Parse device tree node properties

Parse the specified attribute key-value pair based on the parsed device tree node information structure

struct property *of_find_property(const struct device_node *np,const char *name,int *lenp)

code example

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/poll.h>
#include <linux/of.h>
struct device_node *node;
struct property *pr;
int len;
static int __init mycdev_init(void)
{
    
    
    int i;
    // 解析设备树节点
    node = of_find_node_by_path("/mynode@0x12345678");
    if (node == NULL)
    {
    
    
        printk("设备树节点信息解析失败\n");
        return -ENOENT;
    }
    pr = of_find_property(node, "astring", &len);
    printk("name = %s value = %s", pr->name, (char *)pr->value);
    
    if(len) {
    
    
        for (i = 0; i < len; i++)
        {
    
    
            printk("name=%s,value=%x\n", pr->name, ((char *)pr->value)[i]);
        }
    }
    return 0;
}
static void __exit mycdev_exit(void)
{
    
    
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

Guess you like

Origin blog.csdn.net/a1379292747/article/details/129016041