Skip to content

Commit 869ae2b

Browse files
mbolivar-nordiccarlescufi
authored andcommitted
devicetree.h: add DT_NODE_CHILD_IDX
It can be useful to know what a node's index is in its parent's list of children. This information is now available to C via gen_defines.py, but no user-facing macros are available to access it. Add a macro which exposes this information to users via devicetree.h. Some APIs want to build on devicetree.h by creating some derived structure for each of a node's children. It can therefore be convenient to use each child's index in the list of children as an identifier for the child. Some concrete and common examples are "gpio-keys" and "gpio-leds", which allow you to define arbitrary numbers of keys and LEDs as child nodes of nodes with those compatibles. Derived APIs can use a key or LED node's index in its list of parents as a way to identify which of several structures is relevant to a particular controlled key or LED. These are just examples, though -- the feature added here makes no assumptions about where it's being used. Signed-off-by: Martí Bolívar <[email protected]>
1 parent 50f9b3c commit 869ae2b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/devicetree.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,30 @@
474474
*/
475475
#define DT_NODE_FULL_NAME(node_id) DT_CAT(node_id, _FULL_NAME)
476476

477+
/**
478+
* @brief Get a devicetree node's index into its parent's list of children
479+
*
480+
* Indexes are zero-based.
481+
*
482+
* It is an error to use this macro with the root node.
483+
*
484+
* Example devicetree fragment:
485+
*
486+
* parent {
487+
* c1: child-1 {};
488+
* c2: child-2 {};
489+
* };
490+
*
491+
* Example usage:
492+
*
493+
* DT_NODE_CHILD_IDX(DT_NODELABEL(c1)) // 0
494+
* DT_NODE_CHILD_IDX(DT_NODELABEL(c2)) // 1
495+
*
496+
* @param node_id node identifier
497+
* @return the node's index in its parent node's list of children
498+
*/
499+
#define DT_NODE_CHILD_IDX(node_id) DT_CAT(node_id, _CHILD_IDX)
500+
477501
/**
478502
* @brief Do node_id1 and node_id2 refer to the same node?
479503
*

tests/lib/devicetree/api/src/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,13 @@ static void test_node_name(void)
19961996
"reg-holder"), "");
19971997
}
19981998

1999+
static void test_node_child_idx(void)
2000+
{
2001+
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_a)), 0, "");
2002+
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_b)), 1, "");
2003+
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_c)), 2, "");
2004+
}
2005+
19992006
static void test_same_node(void)
20002007
{
20012008
zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
@@ -2369,6 +2376,7 @@ void test_main(void)
23692376
ztest_unit_test(test_dep_ord),
23702377
ztest_unit_test(test_path),
23712378
ztest_unit_test(test_node_name),
2379+
ztest_unit_test(test_node_child_idx),
23722380
ztest_unit_test(test_same_node),
23732381
ztest_unit_test(test_pinctrl),
23742382
ztest_unit_test(test_mbox),

0 commit comments

Comments
 (0)