devicetree: add DT_HAS_DRV_INST(instance_number)

Convenience macro for checking the existence of a node by instance
number.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-03-24 10:18:15 -07:00 committed by Kumar Gala
commit 49f97f64dd
2 changed files with 17 additions and 0 deletions

View file

@ -1281,6 +1281,16 @@
*/ */
#define DT_INST_BUS_LABEL(inst) DT_BUS_LABEL(DT_DRV_INST(inst)) #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 * @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). * This is equivalent to DT_ON_BUS(DT_DRV_INST(inst), bus).

View file

@ -259,6 +259,10 @@ static void test_bus(void)
#undef DT_DRV_COMPAT #undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_spi_device #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, spi), 1, "spi inst 0 not on spi");
zassert_equal(DT_INST_ON_BUS(0, i2c), 0, "spi inst 0 on i2c"); 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 #undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_i2c_device #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, i2c), 1, "i2c inst 0 not on i2c");
zassert_equal(DT_INST_ON_BUS(0, spi), 0, "i2c inst 0 on spi"); zassert_equal(DT_INST_ON_BUS(0, spi), 0, "i2c inst 0 on spi");