devicetree: fix DT_NUM_INST() when the answer is 0

Use UTIL_AND() so it works even when there are no instances.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-03-25 14:40:19 -07:00 committed by Kumar Gala
commit 9d15e98643
2 changed files with 8 additions and 3 deletions

View file

@ -851,7 +851,9 @@
* @param compat lowercase-and-underscores version of a compatible * @param compat lowercase-and-underscores version of a compatible
* @return Number of enabled instances * @return Number of enabled instances
*/ */
#define DT_NUM_INST(compat) UTIL_CAT(DT_N_INST, DT_DASH(compat, NUM)) #define DT_NUM_INST(compat) \
UTIL_AND(DT_HAS_COMPAT(compat), \
UTIL_CAT(DT_N_INST, DT_DASH(compat, NUM)))
/** /**
* @brief Test if the devicetree has a /chosen node * @brief Test if the devicetree has a /chosen node

View file

@ -151,11 +151,14 @@ static void test_has_alias(void)
zassert_equal(DT_HAS_NODE(DT_ALIAS(test_undef)), 0, "test-undef"); zassert_equal(DT_HAS_NODE(DT_ALIAS(test_undef)), 0, "test-undef");
} }
static void test_has_inst(void) static void test_inst_checks(void)
{ {
zassert_equal(DT_HAS_NODE(DT_INST(0, vnd_gpio)), 1, "vnd,gpio #0"); zassert_equal(DT_HAS_NODE(DT_INST(0, vnd_gpio)), 1, "vnd,gpio #0");
zassert_equal(DT_HAS_NODE(DT_INST(1, vnd_gpio)), 1, "vnd,gpio #1"); zassert_equal(DT_HAS_NODE(DT_INST(1, vnd_gpio)), 1, "vnd,gpio #1");
zassert_equal(DT_HAS_NODE(DT_INST(2, vnd_gpio)), 0, "vnd,gpio #2"); zassert_equal(DT_HAS_NODE(DT_INST(2, vnd_gpio)), 0, "vnd,gpio #2");
zassert_equal(DT_NUM_INST(vnd_gpio), 2, "num. vnd,gpio");
zassert_equal(DT_NUM_INST(xxxx), 0, "num. xxxx");
} }
static void test_has_nodelabel(void) static void test_has_nodelabel(void)
@ -1122,7 +1125,7 @@ void test_main(void)
ztest_unit_test(test_inst_props), ztest_unit_test(test_inst_props),
ztest_unit_test(test_has_path), ztest_unit_test(test_has_path),
ztest_unit_test(test_has_alias), ztest_unit_test(test_has_alias),
ztest_unit_test(test_has_inst), ztest_unit_test(test_inst_checks),
ztest_unit_test(test_has_nodelabel), ztest_unit_test(test_has_nodelabel),
ztest_unit_test(test_has_compat), ztest_unit_test(test_has_compat),
ztest_unit_test(test_bus), ztest_unit_test(test_bus),