devicetree: Add DT/DT_INST_CHILD_NUM and DT/DT_INST_CHILD_NUM_STATUS_OKAY

Add a generated macro for the number of child nodes of a given node.
Add a generated macro for the number of child nodes of a given node which
children's status are "okay".

Signed-off-by: Swift Tian <swift.tian@ambiq.com>
This commit is contained in:
Swift Tian 2024-04-25 18:52:22 +08:00 committed by Carles Cufí
commit 5871ff010b
2 changed files with 51 additions and 0 deletions

View file

@ -551,6 +551,25 @@
*/
#define DT_NODE_CHILD_IDX(node_id) DT_CAT(node_id, _CHILD_IDX)
/**
* @brief Get the number of child nodes of a given node
*
* @param node_id a node identifier
* @return Number of child nodes
*/
#define DT_CHILD_NUM(node_id) DT_CAT(node_id, _CHILD_NUM)
/**
* @brief Get the number of child nodes of a given node
* which child nodes' status are okay
*
* @param node_id a node identifier
* @return Number of child nodes which status are okay
*/
#define DT_CHILD_NUM_STATUS_OKAY(node_id) \
DT_CAT(node_id, _CHILD_NUM_STATUS_OKAY)
/**
* @brief Do @p node_id1 and @p node_id2 refer to the same node?
*
@ -3442,6 +3461,29 @@
#define DT_INST_CHILD(inst, child) \
DT_CHILD(DT_DRV_INST(inst), child)
/**
* @brief Get the number of child nodes of a given node
*
* This is equivalent to @see
* <tt>DT_CHILD_NUM(DT_DRV_INST(inst))</tt>.
*
* @param inst Devicetree instance number
* @return Number of child nodes
*/
#define DT_INST_CHILD_NUM(inst) DT_CHILD_NUM(DT_DRV_INST(inst))
/**
* @brief Get the number of child nodes of a given node
*
* This is equivalent to @see
* <tt>DT_CHILD_NUM_STATUS_OKAY(DT_DRV_INST(inst))</tt>.
*
* @param inst Devicetree instance number
* @return Number of child nodes which status are okay
*/
#define DT_INST_CHILD_NUM_STATUS_OKAY(inst) \
DT_CHILD_NUM_STATUS_OKAY(DT_DRV_INST(inst))
/**
* @brief Call @p fn on all child nodes of DT_DRV_INST(inst).
*

View file

@ -524,6 +524,15 @@ def write_children(node):
out_comment("Helper macros for child nodes of this node.")
out_dt_define(f"{node.z_path_id}_CHILD_NUM", len(node.children))
ok_nodes_num = 0
for child in node.children.values():
if child.status == "okay":
ok_nodes_num = ok_nodes_num + 1
out_dt_define(f"{node.z_path_id}_CHILD_NUM_STATUS_OKAY", ok_nodes_num)
out_dt_define(f"{node.z_path_id}_FOREACH_CHILD(fn)",
" ".join(f"fn(DT_{child.z_path_id})" for child in
node.children.values()))