From 49f97f64dd90377935b8f448208d9d3c6c7f92ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Tue, 24 Mar 2020 10:18:15 -0700 Subject: [PATCH] devicetree: add DT_HAS_DRV_INST(instance_number) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Convenience macro for checking the existence of a node by instance number. Signed-off-by: Martí Bolívar --- include/devicetree.h | 10 ++++++++++ tests/lib/devicetree/src/main.c | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/include/devicetree.h b/include/devicetree.h index a13c59655a8..fae4f672601 100644 --- a/include/devicetree.h +++ b/include/devicetree.h @@ -1281,6 +1281,16 @@ */ #define DT_INST_BUS_LABEL(inst) DT_BUS_LABEL(DT_DRV_INST(inst)) +/** + * @brief Does the devicetree have a particular instance number? + * + * This is equivalent to DT_HAS_NODE(DT_DRV_INST(inst)). + * @param inst instance number + * @return 1 if the devicetree has that numbered instance, + * 0 otherwise. + */ +#define DT_HAS_DRV_INST(inst) DT_HAS_NODE(DT_DRV_INST(inst)) + /** * @brief Test if a DT_DRV_COMPAT's bus type is a given type * This is equivalent to DT_ON_BUS(DT_DRV_INST(inst), bus). diff --git a/tests/lib/devicetree/src/main.c b/tests/lib/devicetree/src/main.c index 3136d3223ba..aa6ca2f2a2a 100644 --- a/tests/lib/devicetree/src/main.c +++ b/tests/lib/devicetree/src/main.c @@ -259,6 +259,10 @@ static void test_bus(void) #undef DT_DRV_COMPAT #define DT_DRV_COMPAT vnd_spi_device + zassert_equal(DT_HAS_DRV_INST(0), 1, "missing spi inst 0"); + zassert_equal(DT_HAS_DRV_INST(1), 1, "missing spi inst 1"); + zassert_equal(DT_HAS_DRV_INST(2), 0, "unexpected spi inst 2"); + zassert_equal(DT_INST_ON_BUS(0, spi), 1, "spi inst 0 not on spi"); zassert_equal(DT_INST_ON_BUS(0, i2c), 0, "spi inst 0 on i2c"); @@ -272,6 +276,9 @@ static void test_bus(void) #undef DT_DRV_COMPAT #define DT_DRV_COMPAT vnd_i2c_device + zassert_equal(DT_HAS_DRV_INST(0), 1, "missing i2c inst 0"); + zassert_equal(DT_HAS_DRV_INST(1), 0, "unexpected i2c inst 1"); + zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "i2c inst 0 not on i2c"); zassert_equal(DT_INST_ON_BUS(0, spi), 0, "i2c inst 0 on spi");