devicetree: introduce DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY

Add a new macro, DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY
name, evaluates to 1 if any enabled instance of `compat` has the
property or to zero if it hasn't.

This macro can be useful in drivers for a family of devices.

Signed-off-by: Efrain Calderon <efrain.calderon.estrada@gmail.com>
This commit is contained in:
Efrain Calderon 2023-06-22 01:55:51 +02:00 committed by Carles Cufí
commit ce9bf1071e
2 changed files with 99 additions and 5 deletions

View file

@ -211,6 +211,16 @@ ZTEST(devicetree_api, test_any_inst_prop)
1, "");
}
#undef DT_DRV_COMPAT
ZTEST(devicetree_api, test_any_compat_inst_prop)
{
zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, foo), 1, "");
zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, bar), 1, "");
zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, baz), 0, "");
zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, does_not_exist),
0, "");
}
ZTEST(devicetree_api, test_default_prop_access)
{
/*
@ -1595,6 +1605,18 @@ ZTEST(devicetree_api, test_foreach_status_okay)
val = DT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
zassert_equal(val, 5, "");
#undef MY_FN
#define MY_FN(inst, compat, operator) DT_ENUM_IDX(DT_INST(inst, compat), val) operator
/* This should expand to something like:
*
* 0 + 2 + 3
*
* and order of expansion doesn't matter, since we're adding
* the values all up.
*/
val = DT_COMPAT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
zassert_equal(val, 5, "");
/*
* Make sure DT_INST_FOREACH_STATUS_OKAY can be called from functions
* using macros with side effects in the current scope.