Skip to content

Commit 28238d0

Browse files
rruuaanngnashif
authored andcommitted
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 3734268 commit 28238d0

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
@@ -235,6 +235,13 @@
235235
*/
236236
#define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
237237

238+
/**
239+
* @brief Test if the devicetree has a given alias
240+
* @param alias_name lowercase-and-underscores devicetree alias name
241+
* @return 1 if the alias exists and refers to a node, 0 otherwise
242+
*/
243+
#define DT_HAS_ALIAS(alias_name) DT_NODE_EXISTS(DT_ALIAS(alias_name))
244+
238245
/**
239246
* @brief Get a node identifier for an instance of a compatible
240247
*

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)