Skip to content

Commit 8fef653

Browse files
mbolivar-nordicnashif
authored andcommitted
devicetree: add DT_SAME_NODE()
It can be useful to check if an unknown devicetree node identifier refers to a known node. Add a helper for this. Under the hood, we take advantage of the ordinals API, which provides the unique identifiers we need. Signed-off-by: Martí Bolívar <[email protected]>
1 parent 9b423e9 commit 8fef653

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

doc/reference/devicetree/api.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Generic APIs
2020
The APIs in this section can be used anywhere and do not require
2121
``DT_DRV_COMPAT`` to be defined.
2222

23-
Node identifiers
24-
================
23+
Node identifiers and helpers
24+
============================
2525

2626
A *node identifier* is a way to refer to a devicetree node at C preprocessor
2727
time. While node identifiers are not C values, you can use them to access
@@ -36,6 +36,8 @@ There are also :c:func:`DT_PARENT` and :c:func:`DT_CHILD` macros which can be
3636
used to create node identifiers for a given node's parent node or a particular
3737
child node, respectively.
3838

39+
The following macros create or operate on node identifiers.
40+
3941
.. doxygengroup:: devicetree-generic-id
4042
:project: Zephyr
4143

include/devicetree.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
*/
6666

6767
/**
68-
* @defgroup devicetree-generic-id Node identifiers
68+
* @defgroup devicetree-generic-id Node identifiers and helpers
6969
* @ingroup devicetree
7070
* @{
7171
*/
@@ -399,6 +399,27 @@
399399
*/
400400
#define DT_NODE_PATH(node_id) DT_CAT(node_id, _PATH)
401401

402+
/**
403+
* @brief Do node_id1 and node_id2 refer to the same node?
404+
*
405+
* Both "node_id1" and "node_id2" must be node identifiers for nodes
406+
* that exist in the devicetree (if unsure, you can check with
407+
* DT_NODE_EXISTS()).
408+
*
409+
* The expansion evaluates to 0 or 1, but may not be a literal integer
410+
* 0 or 1.
411+
*
412+
* @param node_id1 first node identifer
413+
* @param node_id2 second node identifier
414+
* @return an expression that evaluates to 1 if the node identifiers
415+
* refer to the same node, and evaluates to 0 otherwise
416+
*/
417+
#define DT_SAME_NODE(node_id1, node_id2) \
418+
(DT_DEP_ORD(node_id1) == (DT_DEP_ORD(node_id2)))
419+
420+
/* Implementation note: distinct nodes have distinct node identifiers.
421+
* See include/devicetree/ordinals.h. */
422+
402423
/**
403424
* @}
404425
*/

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,12 @@ static void test_path(void)
15451545
"/test/gpio@deadbeef"), "");
15461546
}
15471547

1548+
static void test_same_node(void)
1549+
{
1550+
zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
1551+
zassert_false(DT_SAME_NODE(TEST_DEADBEEF, TEST_ABCD1234), "");
1552+
}
1553+
15481554
void test_main(void)
15491555
{
15501556
ztest_test_suite(devicetree_api,
@@ -1579,7 +1585,8 @@ void test_main(void)
15791585
ztest_unit_test(test_child_nodes_list),
15801586
ztest_unit_test(test_great_grandchild),
15811587
ztest_unit_test(test_dep_ord),
1582-
ztest_unit_test(test_path)
1588+
ztest_unit_test(test_path),
1589+
ztest_unit_test(test_same_node)
15831590
);
15841591
ztest_run_test_suite(devicetree_api);
15851592
}

0 commit comments

Comments
 (0)