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 <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2022-03-23 13:49:15 -07:00 committed by Carles Cufí
commit 869ae2b5ac
2 changed files with 32 additions and 0 deletions

View file

@ -1996,6 +1996,13 @@ static void test_node_name(void)
"reg-holder"), "");
}
static void test_node_child_idx(void)
{
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_a)), 0, "");
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_b)), 1, "");
zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_c)), 2, "");
}
static void test_same_node(void)
{
zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
@ -2369,6 +2376,7 @@ void test_main(void)
ztest_unit_test(test_dep_ord),
ztest_unit_test(test_path),
ztest_unit_test(test_node_name),
ztest_unit_test(test_node_child_idx),
ztest_unit_test(test_same_node),
ztest_unit_test(test_pinctrl),
ztest_unit_test(test_mbox),