Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
"For-each" macros

Macros

#define DT_FOREACH_CHILD(node_id, fn)    DT_CAT(node_id, _FOREACH_CHILD)(fn)
 Invokes "fn" for each child of "node_id". More...
 
#define DT_FOREACH_CHILD_VARGS(node_id, fn, ...)    DT_CAT(node_id, _FOREACH_CHILD_VARGS)(fn, __VA_ARGS__)
 Invokes "fn" for each child of "node_id" with multiple arguments. More...
 
#define DT_FOREACH_CHILD_STATUS_OKAY(node_id, fn)    DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY)(fn)
 Call "fn" on the child nodes with status "okay". More...
 
#define DT_FOREACH_CHILD_STATUS_OKAY_VARGS(node_id, fn, ...)    DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY_VARGS)(fn, __VA_ARGS__)
 Call "fn" on the child nodes with status "okay" with multiple arguments. More...
 
#define DT_FOREACH_PROP_ELEM(node_id, prop, fn)    DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM)(fn)
 Invokes "fn" for each element in the value of property "prop". More...
 
#define DT_FOREACH_PROP_ELEM_VARGS(node_id, prop, fn, ...)    DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM_VARGS)(fn, __VA_ARGS__)
 Invokes "fn" for each element in the value of property "prop" with multiple arguments. More...
 
#define DT_FOREACH_STATUS_OKAY(compat, fn)
 Call "fn" on all nodes with compatible DT_DRV_COMPAT and status "okay". More...
 
#define DT_FOREACH_STATUS_OKAY_VARGS(compat, fn, ...)
 Invokes "fn" for each status "okay" node of a compatible with multiple arguments. More...
 

Detailed Description

Macro Definition Documentation

◆ DT_FOREACH_CHILD

#define DT_FOREACH_CHILD (   node_id,
  fn 
)     DT_CAT(node_id, _FOREACH_CHILD)(fn)

#include <include/devicetree.h>

Invokes "fn" for each child of "node_id".

The macro "fn" must take one parameter, which will be the node identifier of a child node of "node_id".

Example devicetree fragment:

n: node {
        child-1 {
                label = "foo";
        };
        child-2 {
                label = "bar";
        };
};

Example usage:

#define LABEL_AND_COMMA(node_id) DT_LABEL(node_id),

const char *child_labels[] = {
    DT_FOREACH_CHILD(DT_NODELABEL(n), LABEL_AND_COMMA)
};

This expands to:

const char *child_labels[] = {
    "foo", "bar",
};
Parameters
node_idnode identifier
fnmacro to invoke

◆ DT_FOREACH_CHILD_STATUS_OKAY

#define DT_FOREACH_CHILD_STATUS_OKAY (   node_id,
  fn 
)     DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY)(fn)

#include <include/devicetree.h>

Call "fn" on the child nodes with status "okay".

The macro "fn" should take one argument, which is the node identifier for the child node.

As usual, both a missing status and an "ok" status are treated as "okay".

Parameters
node_idnode identifier
fnmacro to invoke

◆ DT_FOREACH_CHILD_STATUS_OKAY_VARGS

#define DT_FOREACH_CHILD_STATUS_OKAY_VARGS (   node_id,
  fn,
  ... 
)     DT_CAT(node_id, _FOREACH_CHILD_STATUS_OKAY_VARGS)(fn, __VA_ARGS__)

#include <include/devicetree.h>

Call "fn" on the child nodes with status "okay" with multiple arguments.

The macro "fn" takes multiple arguments. The first should be the node identifier for the child node. The remaining are passed-in by the caller.

As usual, both a missing status and an "ok" status are treated as "okay".

Parameters
node_idnode identifier
fnmacro to invoke
...variable number of arguments to pass to fn
See also
DT_FOREACH_CHILD_STATUS_OKAY

◆ DT_FOREACH_CHILD_VARGS

#define DT_FOREACH_CHILD_VARGS (   node_id,
  fn,
  ... 
)     DT_CAT(node_id, _FOREACH_CHILD_VARGS)(fn, __VA_ARGS__)

#include <include/devicetree.h>

Invokes "fn" for each child of "node_id" with multiple arguments.

The macro "fn" takes multiple arguments. The first should be the node identifier for the child node. The remaining are passed-in by the caller.

Parameters
node_idnode identifier
fnmacro to invoke
...variable number of arguments to pass to fn
See also
DT_FOREACH_CHILD

◆ DT_FOREACH_PROP_ELEM

#define DT_FOREACH_PROP_ELEM (   node_id,
  prop,
  fn 
)     DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM)(fn)

#include <include/devicetree.h>

Invokes "fn" for each element in the value of property "prop".

The macro "fn" must take three parameters: fn(node_id, prop, idx). "node_id" and "prop" are the same as what is passed to DT_FOREACH_PROP_ELEM, and "idx" is the current index into the array. The "idx" values are integer literals starting from 0.

Example devicetree fragment:

n: node {
        my-ints = <1 2 3>;
};

Example usage:

#define TIMES_TWO(node_id, prop, idx) \
        (2 * DT_PROP_BY_IDX(node_id, prop, idx)),

int array[] = {
        DT_FOREACH_PROP_ELEM(DT_NODELABEL(n), my_ints, TIMES_TWO)
};

This expands to:

int array[] = {
        (2 * 1), (2 * 2), (2 * 3),
};

In general, this macro expands to:

fn(node_id, prop, 0) fn(node_id, prop, 1) [...] fn(node_id, prop, n-1)

where "n" is the number of elements in "prop", as it would be returned by DT_PROP_LEN(node_id, prop).

The "prop" argument must refer to a property with type string, array, uint8-array, string-array, phandles, or phandle-array. It is an error to use this macro with properties of other types.

Parameters
node_idnode identifier
proplowercase-and-underscores property name
fnmacro to invoke

◆ DT_FOREACH_PROP_ELEM_VARGS

#define DT_FOREACH_PROP_ELEM_VARGS (   node_id,
  prop,
  fn,
  ... 
)     DT_CAT4(node_id, _P_, prop, _FOREACH_PROP_ELEM_VARGS)(fn, __VA_ARGS__)

#include <include/devicetree.h>

Invokes "fn" for each element in the value of property "prop" with multiple arguments.

The macro "fn" must take multiple parameters: fn(node_id, prop, idx, ...). "node_id" and "prop" are the same as what is passed to DT_FOREACH_PROP_ELEM, and "idx" is the current index into the array. The "idx" values are integer literals starting from 0. The remaining arguments are passed-in by the caller.

Parameters
node_idnode identifier
proplowercase-and-underscores property name
fnmacro to invoke
...variable number of arguments to pass to fn
See also
DT_FOREACH_PROP_ELEM

◆ DT_FOREACH_STATUS_OKAY

#define DT_FOREACH_STATUS_OKAY (   compat,
  fn 
)

#include <include/devicetree.h>

Value:
(UTIL_CAT(DT_FOREACH_OKAY_, compat)(fn)), \
())
#define DT_HAS_COMPAT_STATUS_OKAY(compat)
Does the devicetree have a status "okay" node with a compatible?
Definition: devicetree.h:1881
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition: util_macro.h:156
#define UTIL_CAT(a,...)
Definition: util_internal.h:84

Call "fn" on all nodes with compatible DT_DRV_COMPAT and status "okay".

This macro expands to:

fn(node_id_1) fn(node_id_2) ... fn(node_id_n)

where each "node_id_<i>" is a node identifier for some node with compatible "compat" and status "okay". Whitespace is added between expansions as shown above.

Example devicetree fragment:

/ {
        a {
                compatible = "foo";
                status = "okay";
        };
        b {
                compatible = "foo";
                status = "disabled";
        };
        c {
                compatible = "foo";
        };
};

Example usage:

DT_FOREACH_STATUS_OKAY(foo, DT_NODE_PATH)

This expands to one of the following:

"/a" "/c"
"/c" "/a"

"One of the following" is because no guarantees are made about the order that node identifiers are passed to "fn" in the expansion.

(The "/c" string literal is present because a missing status property is always treated as if the status were set to "okay".)

Note also that "fn" is responsible for adding commas, semicolons, or other terminators as needed.

Parameters
compatlowercase-and-underscores devicetree compatible
fnMacro to call for each enabled node. Must accept a node_id as its only parameter.

◆ DT_FOREACH_STATUS_OKAY_VARGS

#define DT_FOREACH_STATUS_OKAY_VARGS (   compat,
  fn,
  ... 
)

#include <include/devicetree.h>

Value:
(UTIL_CAT(DT_FOREACH_OKAY_VARGS_, \
compat)(fn, __VA_ARGS__)), \
())

Invokes "fn" for each status "okay" node of a compatible with multiple arguments.

This is like DT_FOREACH_STATUS_OKAY() except you can also pass additional arguments to "fn".

Example devicetree fragment:

/ {
        a {
                compatible = "foo";
                val = <3>;
        };
        b {
                compatible = "foo";
                val = <4>;
        };
};

Example usage:

#define MY_FN(node_id, operator) DT_PROP(node_id, val) operator
x = DT_FOREACH_STATUS_OKAY_VARGS(foo, MY_FN, +) 0;

This expands to one of the following:

x = 3 + 4 + 0;
x = 4 + 3 + 0;

i.e. it sets x to 7. As with DT_FOREACH_STATUS_OKAY(), there are no guarantees about the order nodes appear in the expansion.

Parameters
compatlowercase-and-underscores devicetree compatible
fnMacro to call for each enabled node. Must accept a node_id as its only parameter.
...Additional arguments to pass to "fn"