Skip to content

Commit 36a8dc7

Browse files
committed
devicetree.h: Rework DT_ANY_INST_HAS_PROP_STATUS_OKAY
The macro is searching for all instances of specific device that contain specific property and evaluates to true (1) if any device does. The macro used to do that by generating, using DT_ANY_INST_HAS_PROP_STATUS_OKAY, a logical expression like (0 || 1 || 0), where each digit represented existence of property (1) or lack of it (0). Unfortunately other util macros, like IS_ENABLED, were not able to evaluate such expression, as they often simply expect something they can evaluate to 0 or 1. The commit here changes DT_ANY_INST_HAS_PROP_STATUS_OKAY to generate a list of tokens (1) where token is added to list only for instance of a device that has the property; then such list is processed using IS_EMPTY() macro and in the end 0 or 1 is generated, depending on whether any enabled instance of a device has the property or not. This change allows result of DT_ANY_INST_HAS_PROP_STATUS_OKAY to be used with macros like IS_ENABLED, IF_ENABLED or COND_CODE_x. Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 1a55caf commit 36a8dc7

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

include/zephyr/devicetree.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4190,7 +4190,7 @@
41904190
* @endcode
41914191
*/
41924192
#define DT_ANY_INST_HAS_PROP_STATUS_OKAY(prop) \
4193-
(DT_INST_FOREACH_STATUS_OKAY_VARGS(DT_INST_NODE_HAS_PROP_AND_OR, prop) 0)
4193+
COND_CODE_1(IS_EMPTY(DT_ANY_INST_HAS_PROP_STATUS_OKAY_(prop)), (0), (1))
41944194

41954195
/**
41964196
* @brief Call @p fn on all nodes with compatible `DT_DRV_COMPAT`
@@ -4425,6 +4425,34 @@
44254425

44264426
/** @cond INTERNAL_HIDDEN */
44274427

4428+
/** @brief Helper for DT_ANY_INST_HAS_PROP_STATUS_OKAY_
4429+
*
4430+
* This macro generates token "1," for instance of a device,
4431+
* identified by index @p idx, if instance has property @p prop.
4432+
*
4433+
* @param idx instance number
4434+
* @param prop property to check for
4435+
*
4436+
* @return Macro evaluates to `1,` if instance has the property,
4437+
* otherwise it evaluates to literal nothing.
4438+
*/
4439+
#define DT_ANY_INST_HAS_PROP_STATUS_OKAY__(idx, prop) \
4440+
COND_CODE_1(DT_INST_NODE_HAS_PROP(idx, prop), (1,), ())
4441+
/** @brief Helper for DT_ANY_INST_HAS_PROP_STATUS_OKAY
4442+
*
4443+
* This macro uses DT_ANY_INST_HAS_PROP_STATUS_OKAY_ with
4444+
* DT_INST_FOREACH_STATUS_OKAY_VARG to generate comma separated list of 1,
4445+
* where each 1 on the list represents instance that has a property
4446+
* @p prop; the list may be empty, and the upper bound on number of
4447+
* list elements is number of device instances.
4448+
*
4449+
* @param prop property to check
4450+
*
4451+
* @return Evaluates to list of 1s (e.g: 1,1,1,) or nothing.
4452+
*/
4453+
#define DT_ANY_INST_HAS_PROP_STATUS_OKAY_(prop) \
4454+
DT_INST_FOREACH_STATUS_OKAY_VARGS(DT_ANY_INST_HAS_PROP_STATUS_OKAY__, prop)
4455+
44284456
#define DT_PATH_INTERNAL(...) \
44294457
UTIL_CAT(DT_ROOT, MACRO_MAP_CAT(DT_S_PREFIX, __VA_ARGS__))
44304458
/** @brief DT_PATH_INTERNAL() helper: prepends _S_ to a node name

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,25 @@ ZTEST(devicetree_api, test_any_inst_prop)
190190
zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(bar), 1, "");
191191
zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz), 0, "");
192192
zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(does_not_exist), 0, "");
193+
194+
zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo),
195+
(5), (6)),
196+
5, "");
197+
zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo),
198+
(5), (6)),
199+
6, "");
200+
zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz),
201+
(5), (6)),
202+
6, "");
203+
zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz),
204+
(5), (6)),
205+
5, "");
206+
zassert_true(IS_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo)), "");
207+
zassert_true(!IS_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz)), "");
208+
zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo), (1)) + 1,
209+
2, "");
210+
zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz), (1)) + 1,
211+
1, "");
193212
}
194213

195214
ZTEST(devicetree_api, test_default_prop_access)

0 commit comments

Comments
 (0)