Skip to content

Commit 7bd2e7e

Browse files
committed
devicetree: Add DT_HAS_ALIAS macro
Add 'DT_HAS_ALIAS' macro to verify node alias existence. Signed-off-by: James Roy <[email protected]>
1 parent 2f23313 commit 7bd2e7e

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

doc/build/dts/api-usage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ By alias
5050
:zephyr:code-sample:`blinky`, which uses the ``led0`` alias) that need to
5151
refer to *some* device of a particular type ("the board's user LED") but
5252
don't care which one is used.
53+
You may also use :c:macro:`DT_HAS_ALIAS()` to verify whether an alias
54+
node exists.
5355

5456
By instance number
5557
This is done primarily by device drivers, as instance numbers are a way to

include/zephyr/devicetree.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@
239239
*/
240240
#define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
241241

242+
/**
243+
* @brief Test if the devicetree has a given alias
244+
* @param alias_name lowercase-and-underscores devicetree alias name
245+
* @return 1 if the alias exists and refers to a node, 0 otherwise
246+
*/
247+
#define DT_HAS_ALIAS(alias_name) DT_NODE_EXISTS(DT_ALIAS(alias_name))
248+
242249
/**
243250
* @brief Get a node identifier for an instance of a compatible
244251
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ ZTEST(devicetree_api, test_path_props)
127127

128128
ZTEST(devicetree_api, test_alias_props)
129129
{
130+
zassert_equal(DT_HAS_ALIAS(test_alias), 1, "");
131+
zassert_equal(DT_HAS_ALIAS(test_alias_none), 0, "");
130132
zassert_equal(DT_NUM_REGS(TEST_ALIAS), 1, "");
131133
zassert_equal(DT_REG_ADDR(TEST_ALIAS), 0xdeadbeef, "");
132134
zassert_equal(DT_REG_SIZE(TEST_ALIAS), 0x1000, "");

0 commit comments

Comments
 (0)