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>
diff --git a/include/zephyr/devicetree.h b/include/zephyr/devicetree.h
index 7255d79..eb28dea 100644
--- a/include/zephyr/devicetree.h
+++ b/include/zephyr/devicetree.h
@@ -552,6 +552,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?
*
* Both @p node_id1 and @p node_id2 must be node identifiers for nodes
@@ -3443,6 +3462,29 @@
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).
*
* The macro @p fn should take one argument, which is the node
diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py
index b23a4cb..eb350b0 100755
--- a/scripts/dts/gen_defines.py
+++ b/scripts/dts/gen_defines.py
@@ -524,6 +524,15 @@
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()))