devicetree.h: Add macro DT_INST_FOREACH

With this macro device drivers can call macros and functions
on every device instance compatible to that driver.
This makes it possible to make drivers agnostic to the
potential counts of instances.
Sidenote: Introduces helper macro DT_CALL_WITH_ARG.

Signed-off-by: Ioannis Papamanoglou <iopapamanoglou@gmail.com>
This commit is contained in:
Ioannis Papamanoglou 2020-03-12 10:44:42 +01:00 committed by Kumar Gala
commit 7cb7dd277a

View file

@ -1331,6 +1331,22 @@
#define DT_ANY_INST_ON_BUS(bus) \
(UTIL_LISTIFY(DT_NUM_INST(DT_DRV_COMPAT), DT_INST_ON_BUS_OR, bus) 0)
/**
* @def DT_INST_FOREACH
*
* @brief Call specified macro for all nodes with compatible DT_DRV_COMPAT
*
* @details This macro will scan for all DT_INST_ device nodes for that driver.
* The macro then calls the supplied macro with the instance number.
* This macro can be used for example to call the init macro of a driver
* for each device specified in the device tree.
*
* @param inst_expr Macro or function that is called for each device node.
* Has to accept instance_number as only parameter.
*/
#define DT_INST_FOREACH(inst_expr) \
UTIL_LISTIFY(DT_NUM_INST(DT_DRV_COMPAT), DT_CALL_WITH_ARG, inst_expr)
/**
* @brief Does a DT_DRV_COMPAT instance have a property?
* Equivalent to DT_NODE_HAS_PROP(DT_DRV_INST(inst), prop)
@ -1431,5 +1447,7 @@
#define DT_DASH_PREFIX(name) _##name
/** @internal DT_ANY_INST_ON_BUS helper */
#define DT_INST_ON_BUS_OR(inst, bus) DT_ON_BUS(DT_DRV_INST(inst), bus) ||
/** @internal DT_INST_FOREACH helper */
#define DT_CALL_WITH_ARG(arg, expr) expr(arg);
#endif /* DEVICETREE_H */