A device tree management method and device, a terminal device, and a readable storage medium

By establishing virtual device entities and device tree configuration files, instantiating device instances, and searching for the unique identifier of the parent device channel at each level, the problems of slow I2C slave device access speed and unclear logical relationships are solved, achieving efficient device management and unified driver code.

CN116975375BActive Publication Date: 2026-05-15深圳市三旺通信股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311017164.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-08-11
Publication Date
2026-05-15
Estimated Expiration
2043-08-11

AI Technical Summary

Technical Problem

I2C access to devices is slow and the logical relationships between devices are unclear. Existing technologies using gated cascade designs are inefficient and the logical relationships are not intuitive.

Method used

By establishing virtual device entities, obtaining device tree configuration files, instantiating device instances, and searching for parent devices level by level until the root device based on the unique identifier of the parent device, saving the unique identifier of the parent device channel, and accessing the device using the channel linear storage unit, the device can be opened or closed efficiently.

Benefits of technology

It improves the access speed of I2C slave devices, clarifies the logical relationships between devices, has a clear logical approach, supports simultaneous access by multiple I2C slave devices on a limited I2C bus, and has unified driver code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116975375B_ABST
    Figure CN116975375B_ABST
Patent Text Reader

Abstract

The application relates to the field of I2C device management, in particular to a device tree management method and device, a terminal device and a readable storage medium, the method comprising the following steps: a virtual device entity is established according to a device; the attributes of the device entity comprise a device unique identifier, a parent device unique identifier and a parent device channel unique identifier; a device tree configuration file comprises the device unique identifier value, the parent device unique identifier value and the parent device channel unique identifier value of each device; a device instance is obtained by instantiating the corresponding entity, the attributes of the device instance are valued according to the device tree configuration file; the parent device is found step by step according to the parent device unique identifier of the device instance until the root device is found, and the acquired parent device channel unique identifier and parent device unique identifier are stored in a channel linear storage unit step by step from the device; and the device is accessed according to the channel linear storage unit. Therefore, the problems of slow I2C slave device access speed and unclear device logical relationship are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of I2C device management, and more particularly to a device tree management method, apparatus, terminal device, and readable storage medium. Background Technology

[0002] I2C (Inter-Integrated Circuit) devices typically need to be cascaded so that a single I2C channel can manage multiple slave devices with the same or different I2C addresses. This necessitates designing an I2C device tree management method that allows multiple I2C devices to share a single I2C bus.

[0003] like Figure 1 As shown, an I2C multiplexer typically allows one I2C input and multiple I2C outputs, enabling multiple I2C channels to be opened simultaneously or selecting a single channel. Expanding the I2C bus using a multiplexer has two major uses: 1) It allows connecting multiple devices with the same or different I2C addresses using a single I2C channel; 2) Since each device's I2C timing may differ, expanding the I2C bus with a multiplexer can isolate the I2C timing logic of different I2C slave devices, preventing mutual interference.

[0004] However, expanding the I2C channel presents the following problems: access speed to I2C slave devices slows down; each communication with an I2C slave device requires setting up selectors level by level, so devices cascaded via selectors are usually low-priority devices in the system. A recursive call approach is used in the software design. However, recursion is inefficient, and the logical relationships between devices are not intuitively represented. A method that balances efficiency and clear logic needs to be found. Summary of the Invention

[0005] In view of this, embodiments of this application provide a device tree management method, apparatus, terminal device, and readable storage medium, which can effectively solve problems such as slow I2C slave device access speed and unclear device logical relationships.

[0006] In a first aspect, embodiments of this application provide a device tree management method, including:

[0007] A virtual device entity is created based on the device; the device entity includes attributes for describing the cascaded device; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier.

[0008] Obtain the device tree configuration file, which includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, to represent the cascading relationship between devices;

[0009] Instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance;

[0010] Based on the unique identifier of the parent device of the device instance, the parent device is searched level by level until the root device is reached. Starting from the device, the unique identifier of the parent device channel and the unique identifier of the parent device are stored in the channel linear storage unit according to the acquisition order.

[0011] Access the device based on the channel linear storage unit to turn the device on or off.

[0012] In some embodiments, the device tree configuration file configures the attribute values ​​of each device according to the device tree cascading order; that is, it is configured according to the order from the root device, cascaded devices to slave devices.

[0013] The storage device instance includes:

[0014] According to the configuration order of each device in the device tree configuration file, the corresponding device instance is stored in the instance linear storage unit.

[0015] The step of searching for the parent device level by level based on the unique identifier of the parent device instance until the root device, and storing the corresponding unique identifier of the parent device channel and the unique identifier of the parent device in the channel linear table in order, includes:

[0016] Based on the unique identifier of the parent device of the device instance, the linear storage unit of the instance is traversed forward to find the parent device level by level until the root device, and the corresponding unique identifier of the parent device channel and the unique identifier of the parent device are stored in the channel linear storage unit level by level.

[0017] In some embodiments, the instance linear storage unit is a singly linked list and is labeled as an instance linked list; the channel linear storage unit is a singly linked list and is labeled as a channel linked list.

[0018] The device entity includes an entity structure and a channel structure. The entity structure contains attributes including a unique device identifier, a unique parent device identifier, a unique parent device channel identifier, an entity structure pointer, a device address, and a channel structure object pointer. The channel structure contains attributes including a unique parent device channel identifier, the address of the device instance corresponding to the parent device, and a channel structure pointer. The entity structure pointer stores the address of the next entity structure. The channel structure pointer stores the address of the next channel structure. The channel structure object pointer points to the channel structure address.

[0019] The entity structure corresponding to each device instance within the instance linear storage unit is used as a node of each singly linked list to obtain an instance linked list; the channel structure within the channel linear storage unit is used as a node of each singly linked list to obtain a channel linked list.

[0020] In some embodiments, the device tree configuration file is a device tree configuration list; the header of the device tree configuration list includes device name, device address, device unique identifier, parent device unique identifier, and parent device channel unique identifier, and each row in the device tree configuration list stores the device name value, device address value, device unique identifier value, parent device unique identifier value, and parent device channel unique identifier value of each device.

[0021] In some embodiments, if the device is a cascaded device or a root device, the device entity includes a multi-channel dynamic array with priority control; the multi-channel dynamic array is used to store the instance linked list; if the device is a sub-device, a register array is specified for the channel structure object pointer, the register array being used to store the channel structure object pointer.

[0022] In some embodiments, the instance list dynamically adds or removes instance nodes based on the addition or removal of devices in the device tree configuration file.

[0023] In some embodiments, the method further includes generating and displaying a device tree cascading relationship diagram based on the device tree configuration file.

[0024] Secondly, embodiments of this application provide a device tree management apparatus, comprising:

[0025] The entity abstraction module is used to create virtual device entities based on the devices; the device entities include attributes for describing cascaded devices; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier.

[0026] The device tree configuration file acquisition module is used to acquire the device tree configuration file, which includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, so as to represent the cascading relationship between the devices;

[0027] The instantiation module is used to instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance.

[0028] The channel linear storage unit establishment module is used to search for the parent device level by level according to the parent device unique identifier of the device instance until the root device, and store the corresponding parent device channel unique identifier and parent device unique identifier into the channel linear storage unit in turn.

[0029] The device control module is used to access the device according to the channel linear storage unit to turn the device on or off.

[0030] Thirdly, embodiments of this application provide a terminal device, the terminal device including a processor and a memory, the memory storing a computer program, and the processor executing the computer program to implement a device tree management method provided in the first aspect of this application.

[0031] Fourthly, embodiments of this application provide a readable storage medium storing a computer program, which, when executed on a processor, implements a device tree management method provided in the first aspect of this application.

[0032] The embodiments of this application have the following beneficial effects:

[0033] This application virtualizes devices as entities, abstracts device cascading information into entity attributes, configures the device cascading information in a device tree configuration file, instantiates the corresponding entity based on the device configuration information in the device tree configuration file, and obtains a device instance; obtains the parent device's unique identifier through the device instance, searches for the parent device level by level, and saves the parent device's channel unique identifier and parent device unique identifier at each level to obtain a channel linear storage unit; accesses the device based on the channel linear storage unit to open or close the device.

[0034] In this application, whenever a slave device is operated, the unique channel identifier and unique parent device identifier of all parent devices of this device are obtained from the channel linear storage unit. Then, starting from the root node, the child cascaded devices are traversed level by level, and the corresponding channels of each cascaded device are opened one by one, so as to achieve high efficiency and clear logical thinking. Attached Figure Description

[0035] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0036] Figure 1 This paper illustrates a schematic diagram of a device tree topology for a device tree management method according to an embodiment of this application.

[0037] Figure 2 This paper illustrates a flowchart of a device tree management method according to an embodiment of this application.

[0038] Figure 3This diagram illustrates a cascaded relationship graph of the device tree obtained in the device tree management method of this application embodiment.

[0039] Figure 4 This illustration shows a schematic representation of the instance chain obtained in the device tree management method of this application embodiment;

[0040] Figure 5 This illustration shows a schematic representation of the channel chain obtained in the device tree management method of this application embodiment;

[0041] Figure 6 A schematic diagram of a device tree management device according to an embodiment of this application is shown.

[0042] Explanation of key component symbols:

[0043] 610 - Entity Abstraction Module; 620 - Device Tree Configuration File Acquisition Module; 630 - Instantiation Module; 640 - Channel Linear Storage Unit Establishment Module; 650 - Device Control Module. Detailed Implementation

[0044] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments.

[0045] The components of the embodiments of this application described and illustrated in the accompanying drawings can be arranged and designed in a variety of different configurations. Therefore, the following detailed description of the embodiments of this application provided in the drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.

[0046] In the following text, the terms "comprising," "having," and their cognates, which may be used in various embodiments of this application, are intended only to indicate a particular feature, number, step, operation, element, component, or combination thereof, and should not be construed as primarily excluding the presence of one or more other features, numbers, steps, operations, elements, components, or combinations thereof, or adding the possibility of one or more combinations thereof. Furthermore, the terms "first," "second," "third," etc., are used only for distinguishing descriptions and should not be construed as indicating or implying relative importance.

[0047] Unless otherwise specified, all terms used herein (including technical and scientific terms) shall have the same meaning as commonly understood by one of ordinary skill in the art to which the various embodiments of this application pertain. Terms (such as those defined in commonly used dictionaries) shall be interpreted as having the same meaning as in their contextual meaning in the relevant technical field and shall not be construed as having an idealized or overly formal meaning, unless clearly defined in the various embodiments of this application.

[0048] The following detailed description of some embodiments of this application is provided in conjunction with the accompanying drawings. Unless otherwise specified, the following embodiments and features can be combined with each other.

[0049] Existing technologies use cascaded gates to design I2C device trees, but the software design for accessing I2C slave devices in device tree management employs recursive calls. However, recursion is inefficient and the logical relationships between devices are not intuitively represented. Therefore, this application proposes a device tree management method, apparatus, terminal device, and readable storage medium, which can effectively solve the problems of slow I2C slave device access speed and unclear device logical relationships.

[0050] The following examples illustrate the device tree management method.

[0051] Figure 2 A flowchart illustrating a device tree management method according to an embodiment of this application is shown. Exemplarily, the device tree management method includes the following steps:

[0052] S10, establish a virtual device entity based on the device; the device entity includes attributes for describing the cascaded device; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier.

[0053] Each device is uniquely identified by its device ID, the parent device by its parent device ID, and the parent device channel by its parent device channel ID. Device entity attributes include device name, device ID, parent device ID, parent device channel ID, and device address. The device name is also unique.

[0054] The device tree comprises multiple devices. For unified management, each device is virtualized into a corresponding entity; that is, each device has a corresponding entity. To distinguish between the root device, cascaded devices, and slave devices in the device tree, devices of the same type have names containing the same information. For example, the root device is CPU_ROOT, cascaded devices are named with CASCADE, and different cascaded devices are distinguished by different serial numbers. Slave devices are named SLAVE, and are also distinguished by different serial numbers, such as SLAVE_1 and SLAVE_2.

[0055] S20, obtain the device tree configuration file. The device tree configuration file includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, which are used to represent the cascading relationship between devices.

[0056] The device tree configuration file is a device tree configuration list. The header of the device tree configuration list includes the device name, device address, device unique identifier, parent device unique identifier, and parent device channel unique identifier. Each row in the device tree configuration list stores the device name value, device address value, device unique identifier value, parent device unique identifier value, and parent device channel unique identifier value for each device.

[0057] The aforementioned device entities include multiple attributes, and the corresponding device tree configuration file configures each device with multiple attribute values. Specifically, the device attributes in the device tree configuration file include device name, device ID, parent device ID, parent device channel ID, and device address. The device tree configuration file can be a .conf file. Each device in the configuration file is assigned a unique device ID value. For example, CPU_ROOT is the root device, providing I2C drive capability; CASCADE_1, CASCADE_2, and CASCADE_3 are cascaded devices, each with one I2C input and two I2C outputs; SLAVE_1, SLAVE_2, SLAVE_3, and SLAVE_4 are four I2C slave devices. The device tree configuration file contains four sets of parameters:

[0058] name: is the device name;

[0059] devid: Each device needs to be assigned a unique ID, also known as device ID, which cannot be duplicated and is numbered starting from 0;

[0060] address: The device's I2C address, or simply device address. The root device does not need an I2C address, so it is -1. All other devices need an address.

[0061] pdevid: Indicates the devid of the parent device of the current device, or simply the parent device ID. If there is no parent device, set it to -1.

[0062] channel: Indicates a channel under the parent device of the current device. Detect the channel ID of the parent device, starting from 0. If you do not care about the channel, set it to -1.

[0063] In one implementation, the devices in the device tree configuration file can be configured randomly, without following the cascading order, as shown in Table 1 below.

[0064] Table 1 Device Tree Configuration Files - Random Configuration

[0065]

[0066]

[0067] In one implementation, the device tree configuration file configures the attribute values ​​of each device according to the device tree cascading order; that is, it is configured according to the order from the root device, cascaded devices to slave devices, as shown in Table 2. The root device CPU_ROOT is configured in the first row, followed by the cascaded devices CASCADE_1, CASCADE_2, and CASCADE_3, and finally the slave devices SLAVE_1, SLAVE_2, SLAVE_3, and SLAVE_4. The device tree cascading order is based on the order from the root device, cascaded devices to slave devices.

[0068] Table 2 Device Tree Configuration Files - Cascading Order Configuration

[0069]

[0070]

[0071] In one implementation, the method further includes generating and displaying a device tree cascading relationship diagram based on the device tree configuration file. This simplifies the translation of invisible physical device relationships, achieving a clear and intuitive representation of the relationships between I2C devices. For example, by traversing the device tree configuration file and obtaining the device tree cascading relationship based on the device ID and parent device ID, a device tree relationship diagram is drawn. For instance, based on the device tree configuration information in Table 1 or Table 2, if the device ID value of CPU_ROOT is 0, then CPU_ROOT is the root device; if the device ID of CASCADE_1 is 1 and its parent device ID value is 0, then CASCADE_1 is connected to CPU_ROOT; if the parent device IDs of CASCADE_2 and CASCADE_4 are 1, and the parent device channel IDs of CASCADE_2 and CASCADE_4 are 0 and CASCADE_4 is 1, then CASCADE_2 is connected to channel 0 of CASCADE_1, and CASCADE_4 is connected to channel 1 of CASCADE_1; CASCADE_3 and... If the parent device ID of SLAVE_3 is 2, and the parent device channel ID of CASCADE_3 is 1, and the parent device channel ID of SLAVE_3 is 0, then CASCADE_3 is connected to channel 1 of CASCADE_2, and SLAVE_3 is connected to channel 0 of CASCADE_2. If the parent device IDs of SLAVE_1 and SLAVE_2 are both 3, the parent device channel IDs of SLAVE_1 are both 0, the parent device channel IDs of SLAVE_2 are both 1, and the device ID of CASCADE_3 is 3, then SLAVE_1 is connected to channel 0 of CASCADE_3, and SLAVE_2 is connected to channel 1 of CASCADE_3. Figure 3 As shown.

[0072] S30, instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance.

[0073] Regardless of whether the device tree configuration file uses random configuration or a cascading device tree configuration, the configuration file is traversed line by line to obtain the attribute values ​​of each device. The device entity is then instantiated, and the device name, unique device ID, device address, parent device ID, and parent device channel ID are assigned to the corresponding attributes of the device instance. Finally, the device instances are saved, resulting in a device instance set.

[0074] In one implementation, the device tree configuration file is configured in a cascading device tree order. Therefore, the corresponding device instances are stored in the instance linear storage unit according to the configuration order of each device in the device tree configuration file. That is, the attribute values ​​of each device are obtained line by line according to the configuration order of the device tree configuration file, and device entities are instantiated to obtain device instances. Each attribute value is assigned to the corresponding attribute of the device instance, and the device instances are stored according to the device configuration order in the configuration file. It can be understood that the order of the stored device instances also conforms to the cascading device tree order, thus obtaining the instance linear storage unit.

[0075] S40: Search for the parent device level by level according to the unique identifier of the parent device of the device instance until the root device, and starting from the root device, store the obtained parent device channel unique identifier and parent device unique identifier into the channel linear storage unit according to the acquisition order.

[0076] In one implementation, the device tree configuration file is randomly configured, and the device instance set obtained in step S30 does not conform to the device tree cascading order. Therefore, when searching for the parent device level by level based on the unique identifier of the parent device of each device instance, the unique identifier of the device is determined. For example, using a device ID, the unique identifier of the parent device channel and the unique identifier of the parent device in the device instance corresponding to the device itself are obtained. These unique identifiers are then stored in the channel linear storage unit as the first value of the channel linear storage unit, which is an ordered linear storage unit. Then, based on the parent device ID of the device, the device instance set is traversed to obtain a device whose device ID matches the parent device ID of the device. This device is marked as the first parent device. The parent device channel ID and the parent device ID of the first parent device are obtained and stored in the channel linear storage unit as the second value of the channel linear storage unit. Next, based on the parent device ID of the first parent device, a device with the same device ID as the parent device ID of the first parent device is found in the device instance set and marked as the second parent device. The parent device channel ID and parent device ID of the second parent device are then stored in the channel linear storage unit as the third value of the channel linear storage unit. This process is repeated until the root device is found, finally resulting in a channel linear storage unit from the root device to that child device. The same method can be used to obtain a corresponding channel linear storage unit for each device.

[0077] In one implementation, the device tree configuration file is configured according to the device tree cascading order, and in step S30, an instance linear storage unit conforming to the device tree cascading order is obtained. To reduce traversal time, the instance linear storage unit is traversed forward according to the unique identifier of the parent device of the device instance to find the parent device level by level until the root device, and the corresponding parent device channel unique identifier and parent device unique identifier are stored in the channel linear storage unit level by level.

[0078] S50 accesses the device based on the channel linear storage unit to enable or disable the device.

[0079] For each device, the channel linear storage unit stores the parent device ID and parent device channel ID from the root device to the cascaded devices and then to the current device. Naturally, the parent device ID and parent device channel ID can be read level by level, and the parent device channel can be opened or closed according to the parent device channel ID, thereby realizing the management and control of the device.

[0080] In one implementation, the instance linear storage unit is a singly linked list, labeled as an instance linked list; the channel linear storage unit is a singly linked list, labeled as a channel linked list.

[0081] The device entity includes an entity structure and a channel structure. The attributes within the entity structure include a unique device identifier, a unique parent device identifier, a unique parent device channel identifier, an entity structure pointer, a device address, and a channel structure object pointer. The attributes within the channel structure include a unique parent device channel identifier, the address of the device instance corresponding to the parent device, and a channel structure pointer. The entity structure pointer is used to store the address of the next entity structure. The channel structure pointer is used to store the address of the next channel structure. The channel structure object pointer is used to point to the channel structure address.

[0082] The instance linked list is obtained by using the entity structure corresponding to each device instance within the instance linear storage unit as the node of each singly linked list. The channel linked list is obtained by using the channel structure within the channel linear storage unit as the node of each singly linked list.

[0083] If the device is a cascaded device or a root device, the device entity includes a multi-channel dynamic array with priority control; the multi-channel dynamic array is used to store the instance linked list; if the device is a child device, a register array is specified for the channel structure object pointer, which is used to store the channel structure object pointer.

[0084] For example, a solid structure is defined as follows:

[0085] typedef struct i2c_instance{

[0086] int32_t devid; / / Device ID;

[0087] int32_t pdevid; / / Parent device ID. If it is -1, it represents the root device. If it is greater than or equal to 0, it represents the ID number of the parent device.

[0088] uint8 address; / / Device address, 0~0x7f, -1 indicates no address;

[0089] uint8 name

[32] ; / / Device name;

[0090] init8 channel; / / The ID of the current device connected to a channel on the parent device, i.e., the parent device channel ID, numbered starting from 0. If it is -1, it means that we do not care;

[0091] struct i2c_instance* next; / / Since the I2C devices are different in each product, a singly linked list is used to store these devices;

[0092] i2c_channel_t*channels;

[0093] }i2c_instance_t.

[0094] The *channels above represents the channel instance unit (or channel list) in the cascade. It is an array used to record the pointer from the root node through the cascaded devices to the parent node pointer of the current device. The first pointer element is always the root node of the current slave device, because when configuring channels, intermediate devices always configure them level by level from the root node.

[0095] In the aforementioned I2C entity, to facilitate the configuration of intermediate cascaded devices, an I2C channel object `i2c_channel_t` is defined. `i2c_channel_t` is a singly linked list that records the addresses of all I2C device instances starting from the root device and passing through intermediate cascaded devices. It also records the parent device channel ID of each cascaded device.

[0096] The channel structure is defined as follows:

[0097] typedef struct i2c_channel{

[0098] i2c_instance_t*parent; / / Pointer to the physical address of the parent device;

[0099] init8 channel; / / The ID of the current device on a channel connected to the parent device, numbered starting from 0. If it is -1, it means that we don't care;

[0100] struct i2c_channel*next; / / Store cascaded devices using a singly linked list} i2c_channel_t.

[0101] The instance linked list is obtained by using the i2c_instance_t corresponding to each device instance within the instance linear storage unit as the node of each singly linked list. The channel linked list is obtained by using the i2c_channel_t corresponding to each channel linear storage unit as the node of each singly linked list.

[0102] Preferably, after establishing the device tree cascading relationship graph, memory space is allocated for each device to store the content of each device instance, and the device instances (i2c_instance_t) are cascaded into an instance linked list according to the order of the cascading relationship, such as... Figure 4 As shown.

[0103] like Figure 4 For each device, an entity structure is instantiated to obtain an i2c_instance_t. Using the next pointer of i2c_instance_t, a singly linked list of devices is created. This linked list can dynamically add instance nodes as the configuration file i2c_dev.conf is updated, enabling dynamic addition of devices during system operation.

[0104] After instantiating the entity structure, refresh the channel list (channels) in each device instance's i2c_instance_t, and establish a link from the root device to the parent device for each device instance, based on... Figure 3 Taking SLAVE_2 as an example, this section will explain how to create channels.

[0105] according to Figure 3 The device tree cascading topology diagram, which establishes a parent device channel list for all device instances, can form... Figure 5 The parent device relationship diagram. Except for the root device, each device can always specify a parent device. The I2C slave device closest to the root device has its parent device pointing only to the root device, and its channel is -1, because the root device does not need a channel. Figure 3In the process, the last two slave devices, SLAVE4 and SLAVE_5, have the most parent device information within their channels, ranging from the root device CPU_ROOT to the cascaded device CASCADE_3. Starting from SLAVE_2 and moving forward in the instance list towards CPU_ROOT, the parent device is searched. From the device instance corresponding to SLAVE_2, the parent device name is found to be CASCADE_3, and the parent device channel ID is 1. A channel structure is instantiated based on the device name and channel ID, resulting in a channel instance, which is stored as a node at the head of the channel list, i.e., node ③. Then, starting from node SLAVE_2 in the instance list, the device with the name CASCADE_3 is searched forward. Through the device instance corresponding to CASCADE_3, its parent device name is found to be CASCADE_2, and the parent device channel ID is 1. D is set to 1, and a channel structure is instantiated based on the device name and channel ID. This channel instance is then stored as a node at the head of the channel linked list, i.e., node ②. Next, starting from node CASCADE_3 in the instance linked list, the device with the device name CASCADE_2 is searched backwards. The parent device name is CASCADE_1, and the parent device's channel ID is 0. A channel structure is then instantiated based on the device name and channel ID, resulting in a channel instance, which is also stored as a node at the head of the channel linked list, i.e., node ①. This process continues until the root device is found, finally obtaining the channel linked list. Similarly, this method yields a corresponding channel linked list for each device.

[0106] Similarly, regardless of the number of device levels and root devices in the device tree, a singly linked list of `i2c_channel_t` objects can always be formed, i.e., a channel list. Using this channel list, each time a device is accessed, the corresponding channel is always set from the parent device specified by `channels`. For example, in... Figure 5 The three parent devices of SLAVE_2, indicated by subscripts ①②③, are designed to first open channel 0 of the cascaded device CASCADE_1, then open channel 1 of the cascaded device CASCADE_2, and finally open channel 1 of the cascaded device CASCADE_3. After this is completed, the root device CPU_ROOT is now directly connected to the slave device SLAVE_2, and the channels of CPU_ROOT and other slave devices are closed. Therefore, the slave device SLAVE_2 can be operated directly at this time.

[0107] After the operation is complete, all channels in the cascaded device need to be shut down. The shutdown order is the reverse of the opening order. Figure 5For example, the shutdown sequence is ③②①, which means first shutting down channel 1 of the cascaded device CASCADE_3, then shutting down channel 1 of the cascaded device CASCADE_2, and finally shutting down channel 0 of the cascaded device CASCADE_1. At this point, the entire operation from the target device SLAVE_2 is complete.

[0108] Generally, all intermediate cascaded devices have no actual function; only the slave devices have practical significance. Therefore, intermediate cascaded devices are usually not operated independently. Of course, if necessary, the operation can be performed level by level using the method described above.

[0109] In this application, whenever an I2C sub-device is operated, the channel linked list corresponding to that device is obtained, and the information of the parent device (parent device name and parent device channel ID) is obtained. Then, starting from the root device, the I2C cascaded devices are traversed level by level, and the corresponding channels of each cascaded device are opened one by one, so as to achieve high efficiency and clear logical thinking.

[0110] Meanwhile, when there are many slave devices but a limited number of I2C buses, this application allows for unlimited expansion of I2C slave devices, avoiding the slow access speed issues caused by recursive access in existing technologies. This application allows multiple slave devices with the same I2C address to be connected simultaneously to the same I2C bus. Although the devices have the same I2C address, their corresponding channel lists are different, allowing for separate access and control. This application eliminates the need for different driver codes for multiple slave devices as their I2C timings differ. In summary, all slave devices and cascaded devices have clear relationships during design, are easy to modify, and theoretically can be compatible with cascading any I2C device, with only one set of driver code required.

[0111] Figure 6 A schematic diagram of a device tree management apparatus according to an embodiment of this application is shown. Exemplarily, the device tree management apparatus includes: an entity abstraction module 610, a device tree configuration file acquisition module 620, an instantiation module 630, a channel linear storage unit establishment module 640, and a device control module 650.

[0112] The entity abstraction module 610 is used to create virtual device entities based on the devices; the device entities include attributes for describing cascaded devices; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier.

[0113] The device tree configuration file acquisition module 620 is used to acquire the device tree configuration file, which includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, in order to represent the cascading relationship between the devices.

[0114] The instantiation module 630 is used to instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance.

[0115] The channel linear storage unit establishment module 640 is used to search for the parent device level by level according to the parent device unique identifier of the device instance until the root device, and to store the corresponding parent device channel unique identifier and parent device unique identifier into the channel linear storage unit in turn.

[0116] The device control module 650 is used to access the device according to the channel linear storage unit to enable or disable the device.

[0117] It is understood that the apparatus in this embodiment corresponds to the device tree management method in the above embodiments, and the options in the above embodiments are also applicable to this embodiment, so they will not be described again here.

[0118] This application also provides a terminal device, exemplary of which includes a processor and a memory, wherein the memory stores a computer program, and the processor executes the computer program to enable the terminal device to perform the functions of the various modules in the above-described device tree management method or device tree management apparatus.

[0119] The processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, including at least one of a Central Processing Unit (CPU), Graphics Processing Unit (GPU), Network Processor (NP), Digital Signal Processor (DSP), Application-Specific Integrated Circuit (ASIC), Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The general-purpose processor can be a microprocessor or any conventional processor, capable of implementing or executing the methods, steps, and logic block diagrams disclosed in the embodiments of this application.

[0120] The memory can be, but is not limited to, Random Access Memory (RAM), Read Only Memory (ROM), Programmable Read-Only Memory (PROM), Erasable Programmable Read-Only Memory (EPROM), Electrically Erasable Programmable Read-Only Memory (EEPROM), etc. The memory is used to store computer programs, and the processor can execute the computer programs accordingly after receiving execution instructions.

[0121] This application also provides a readable storage medium for storing the computer program used in the aforementioned terminal device.

[0122] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings show the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that, in alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, and combinations of blocks in the block diagram and / or flowchart, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.

[0123] In addition, the functional modules or units in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.

[0124] If the aforementioned functions are implemented as software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a smartphone, personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0125] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.

Claims

1. A device tree management method, characterized in that, include: A virtual device entity is created based on the device; the device entity includes attributes for describing the cascaded device; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier. Obtain the device tree configuration file, which includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, to represent the cascading relationship between devices; Instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance; Based on the unique identifier of the parent device of the device instance, the parent device is searched level by level until the root device is reached. Starting from the device, the unique identifier of the parent device channel and the unique identifier of the parent device are stored in the channel linear storage unit according to the acquisition order. Accessing the device via the channel linear storage unit allows for turning the device on or off. The device tree configuration file configures the attribute values ​​of each device according to the device tree cascading order; that is, it is configured according to the order from the root device, cascading devices to slave devices. The storage device instance includes: According to the configuration order of each device in the device tree configuration file, the corresponding device instance is stored in the instance linear storage unit. The step of searching for the parent device level by level based on the unique identifier of the parent device instance until the root device, and storing the corresponding unique identifier of the parent device channel and the unique identifier of the parent device in the channel linear table in order, includes: Based on the unique identifier of the parent device of the device instance, the linear storage unit of the instance is traversed forward to find the parent device level by level until the root device, and the corresponding unique identifier of the parent device channel and the unique identifier of the parent device are stored in the channel linear storage unit level by level. The instance linear storage unit is a singly linked list, labeled as an instance linked list; the channel linear storage unit is a singly linked list, labeled as a channel linked list. The device entity includes an entity structure and a channel structure. The entity structure contains attributes including a unique device identifier, a unique parent device identifier, a unique parent device channel identifier, an entity structure pointer, a device address, and a channel structure object pointer. The channel structure contains attributes including a unique parent device channel identifier, the address of the device instance corresponding to the parent device, and a channel structure pointer. The entity structure pointer stores the address of the next entity structure. The channel structure pointer stores the address of the next channel structure. The channel structure object pointer points to the address of the channel structure. The entity structure corresponding to each device instance within the instance linear storage unit is used as a node of each singly linked list to obtain an instance linked list; the channel structure within the channel linear storage unit is used as a node of each singly linked list to obtain a channel linked list.

2. The device tree management method according to claim 1, characterized in that, The device tree configuration file is a device tree configuration list; the header of the device tree configuration list includes device name, device address, device unique identifier, parent device unique identifier, and parent device channel unique identifier. Each row in the device tree configuration list stores the device name value, device address value, device unique identifier value, parent device unique identifier value, and parent device channel unique identifier value for each device.

3. The device tree management method according to claim 1, characterized in that, If the device is a cascaded device or a root device, the device entity includes a multi-channel dynamic array with priority control; the multi-channel dynamic array is used to store the instance linked list; if the device is a sub-device, a register array is specified for the channel structure object pointer, and the register array is used to store the channel structure object pointer.

4. The device tree management method according to claim 1, characterized in that, The instance linked list dynamically adds or removes instance nodes based on the addition or removal of devices in the device tree configuration file.

5. The device tree management method according to claim 1, characterized in that, The method also includes generating and displaying a device tree cascading relationship diagram based on the device tree configuration file.

6. A device tree management apparatus, characterized in that, include: The entity abstraction module is used to create virtual device entities based on the devices; the device entities include attributes for describing cascaded devices; the attributes include a unique device identifier, a unique parent device identifier, and a unique parent device channel identifier. The device tree configuration file acquisition module is used to acquire the device tree configuration file, which includes the device unique identifier value of each device, the parent device unique identifier value, and the parent device channel unique identifier value, so as to represent the cascading relationship between the devices; The instantiation module is used to instantiate the corresponding entity to obtain a device instance, assign values ​​to the attributes of the device instance according to the device tree configuration file, and save the device instance. The channel linear storage unit establishment module is used to search for the parent device level by level according to the parent device unique identifier of the device instance until the root device, and store the corresponding parent device channel unique identifier and parent device unique identifier into the channel linear storage unit in turn. The device control module is used to access the device according to the channel linear storage unit in order to turn the device on or off; The device tree configuration file configures the attribute values ​​of each device according to the device tree cascading order; that is, it is configured according to the order from the root device, cascading devices to slave devices. The storage device instance includes: According to the configuration order of each device in the device tree configuration file, the corresponding device instance is stored in the instance linear storage unit. The channel linear storage unit establishment module searches for the parent device level by level according to the unique identifier of the parent device of the device instance until the root device, and stores the corresponding parent device channel unique identifier and parent device unique identifier into the channel linear table in sequence, including: Based on the unique identifier of the parent device of the device instance, the linear storage unit of the instance is traversed forward to find the parent device level by level until the root device, and the corresponding unique identifier of the parent device channel and the unique identifier of the parent device are stored in the channel linear storage unit level by level. The instance linear storage unit is a singly linked list, labeled as an instance linked list; the channel linear storage unit is a singly linked list, labeled as a channel linked list. The device entity includes an entity structure and a channel structure. The entity structure contains attributes including a unique device identifier, a unique parent device identifier, a unique parent device channel identifier, an entity structure pointer, a device address, and a channel structure object pointer. The channel structure contains attributes including a unique parent device channel identifier, the address of the device instance corresponding to the parent device, and a channel structure pointer. The entity structure pointer stores the address of the next entity structure. The channel structure pointer stores the address of the next channel structure. The channel structure object pointer points to the address of the channel structure. The entity structure corresponding to each device instance within the instance linear storage unit is used as a node of each singly linked list to obtain an instance linked list; the channel structure within the channel linear storage unit is used as a node of each singly linked list to obtain a channel linked list.

7. A terminal device, characterized in that, The terminal device includes a processor and a memory, the memory storing a computer program, and the processor executing the computer program to implement the device tree management method according to any one of claims 1-5.

8. A readable storage medium, characterized in that, It stores a computer program that, when executed on a processor, implements the device tree management method according to any one of claims 1-5.