For each node whose binding declares device class membership through
the "class" binding key, emit a per-node existence macro per class
token, mirroring the per-node bus macros:
#define DT_N_<node>_CLASS_adc 1
and for each class with status "okay" nodes, emit global helpers
mirroring the per-compatible ones:
#define DT_CLASS_HAS_OKAY_adc 1
#define DT_N_CLASS_adc_NUM_OKAY 2
#define DT_FOREACH_OKAY_CLASS_adc(fn) ...
#define DT_FOREACH_OKAY_VARGS_CLASS_adc(fn, ...) ...
Class tokens go through str2ident() like bus and compatible names.
Nothing is emitted for builds where no binding declares a class.
Tested by building tests/lib/devicetree/api for native_sim and
checking that the generated header is unchanged when no binding
declares a class; the macros themselves are exercised by the
devicetree API test suite in a following commit.
Assisted-by: Claude:claude-fable-5
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
The banner records the absolute path of the input DTS, which lives in the
build directory. Nearly every translation unit includes this header, so
that one line makes the header differ between build directories and
defeats ccache's direct-mode lookup for everything that includes it.
Building the same application in a second build directory drops from
270/271 direct hits to 26/271, the rest falling back to preprocessed
hits that still have to run the preprocessor.
Emit only the file name, the way the neighbouring bindings directories
are already relativised. The header is then byte-identical across build
directories, which is a small reproducibility improvement in its own
right.
Two limits are worth recording. Harvesting the cache reuse also needs
ccache configured for relocation, via base_dir and hash_dir; with stock
settings this makes no measurable difference. And it does not help a
twister run: each test has its own autoconf.h, which every compile
includes and which fails the direct-mode lookup before this header is
reached, leaving direct hits unchanged.
Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
Update regular expression in str2ident to replace percent sign (%)
character when creating identifiers.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
Add gen defines and bnf for DT_BINDING_COMPAT_*, which allows
for getting a node's compatible string at compile time. Adds
DT_BINDING_COMPAT_TOKEN, DT_BINDING_COMPAT_UPPER_TOKEN, and
DT_BINDING_COMPAT_UNQUOTED.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Perry Naseck <pnaseck@media.mit.edu>
Zephyr's devicetree tooling parses and generates macros
for the ranges property but has no equivalent support for
dma-ranges, even though both are defined the same way by
the Devicetree Specification (§2.3.9) and edtlib.py already
has the machinery to parse address-translation triplets.
This adds the same pipeline that already exists for ranges.
edtlib.py now parses dma-ranges into Node. dma_ranges using
the same cell-based translation logic as Node via a new
_init_dma_ranges() method.
gen_defines.py emits DT_N_..._DMA_RANGES_* macros for each node
with a dma-ranges property through a new write_dma_ranges() function.
devicetree.h gains the DT_DMA_RANGES_* public API, including HAS_IDX,
CHILD_BUS_ADDRESS_BY_IDX, PARENT_BUS_ADDRESS_BY_IDX, LENGTH_BY_IDX,
NUM_DMA_RANGE, and FOREACH_DMA_RANGE. base.yaml documents the dma-ranges
property per DT spec §2.3.9.
Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Muhammad Waleed Badar <walid.badar@gmail.com>
Update "out_dt_define" macro to allow integer types
as the value parameter. Prevents unneeded warning
when "val" is specified as an integer.
Signed-off-by: Omar Naffaa <onaffaa@qti.qualcomm.com>
Fixes this missing output, which helps with repducible build
debugging, to use the zephyr prefix when the bindings are located
there irrespective of if the (long deprecated) environmental value
is set
Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Generates a new file for use by the build system which lists all
the paths to dts bindings used in a build, then from CMake adds a
dependency to these files so that if they are modified, CMake will
re-run.
This has been implemented by adding support to edtlib to output
details on bindings and binding paths inside of the python code
Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Add the `DT_NODELABEL_C_TOKEN` and
`DT_NODELABEL_C_TOKEN_BY_IDX` macros to retrieve
the C symbolic name of a node.
Signed-off-by: James Roy <rruuaanng@outlook.com>
Add generated macros for counting nodes on a specific bus under a bus
controller:
- DT_(INST_)DESCENDANT_NUM_ON_BUS()
- DT_(INST_)DESCENDANT_NUM_ON_BUS_STATUS_OKAY()
The count intentionally walks descendants instead of only direct children.
This covers common devicetree structures where helper/container nodes sit
between a controller and the actual on-bus device nodes.
Traversal stops at child bus-controller nodes, so devices behind sub-buses
are not counted in the parent controller's result.
Also add edtlib Node.on_bus resolution, validated against the parent bus
controller bus types, so generation uses the resolved on-bus value.
Signed-off-by: Swift Tian <swift.tian@ambiq.com>
Introduce DT_FOREACH_REG and DT_INST_FOREACH_REG macros to enable
iteration over reg entries in devicetree nodes by invoking a user-
supplied macro per entry. Add support for macros accepting a
separator and variadic arguments with the new DT_FOREACH_REG_SEP,
DT_FOREACH_REG_VARGS, and DT_FOREACH_REG_SEP_VARGS macros, as well
as their DT_INST_ variants.
The DT_FOREACH_REG macro mirrors the behavior of the ranges property.
Update the devicetree macro generation logic in gen_defines.py to
emit the new macro variants. Document the additions by expanding the
Augmented Backus-Naur Form grammar in macros.bnf. Add detailed
comments with usage examples and parameter details for all new macros.
Expand test coverage with new overlay nodes and dedicated tests to
verify macro correctness and behavior, across various node scenarios
including multi-register, single-register, and nodes lacking reg
properties.
Signed-off-by: Oleksii Moisieiev <oleksii_moisieiev@epam.com>
Signed-off-by: Svitlana Drozd <svitlana_drozd@epam.com>
Adds support for outputting partition information on this new
compatible, to match fixed-partitions
Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
This change introduces generating definitions corresponding to
`*-map` property, which was currently discarded.
For `*-map` properties are made able to be treated as a variation
of phandle-array, assign sequential cell names for each group of
specifiers (child_specifier_0, child_specifier_1, ...,
parent_specifier_0, ...).
The `*-map` data is like a two-dimensional array, so it is difficult to
handle with the existing APIs, so we will also provide new APIs.
Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Add more macros for interacting with controller/data type of
relationships (phandle arrays / cells)
Add macros for arbitrarily iterating cells of phandle specifiers
Add tests for the new macros
Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
On arrays DT_ENUM_HAS_VALUE() and DT_INST_ENUM_HAS_VALUE()
were only checking the first index, this changes it to check all indexes.
Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
DT_ENUM_HAS_VALUE_BY_IDX states, that the value
must be lowercase-and-underscores, this makes sure,
that they can match.
Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
Add a new "hash" attribute to all Devicetree EDT nodes. The hash is
calculated on the full path of the node; this means that its value
remains stable across rebuilds.
The hash is checked for uniqueness among nodes in the same EDT.
This computed token is then added to `devicetree_generated.h` and made
accessible to Zephyr code via a new DT_NODE_HASH(node_id) macro.
Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
Remove deprecated _ENUM_TOKEN and _ENUM_UPPER_TOKEN. These are
deprecated for over three years by now.
Signed-off-by: Joel Hirsbrunner <jhirsbrunner@baumer.com>
It is currently impossible to use enum with any array like type (i.e.
string-array and array, these are the only ones that make sense) in the
devicetree and dt-bindings.
However, there is no such remark in the dt-bindings section of the docs.
Since this is a feature that comes in very handy and is implemented
fairly easily, I adjusted the scripts for this.
It is now possible to do something like this.
```yaml
compatible = "enums"
properties:
array-enum:
type: string-array
enum:
- bar
- foo
- baz
- zoo
```
```dts
/ {
enums {
compatible = "enums";
array-enum = "foo", "bar";
};
};
```
Signed-off-by: Joel Hirsbrunner <jhirsbrunner@baumer.com>
Like some other string properties, I will add a derived form
to FULL_NAME to make it easier to reference from macros.
Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
Fixed escaping of double quotes, backslashes, and new line characters
so they can be used in string properties.
Previously, double quotes and backslashes were escaped in gen_defines.py
but not in gen_dts_cmake.py, and new lines were not escaped in either,
so using any of these characters would break the build.
Signed-off-by: Joel Spadin <joelspadin@gmail.com>
Separate the pickled EDT generation from the C-Macro header
generation in gen_defines.py to have a more clear responsibility
of the scripts in the DTS parsing process.
Signed-off-by: Benedikt Schmidt <benedikt.schmidt@embedded-solutions.at>
Introduces type hints to all functions for improved static type checking
and IDE support.
Also equalizes spacing between functions as the lines are being touched
anyway.
Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
Add the following new macros:
- DT_FOREACH_NODELABEL
- DT_FOREACH_NODELABEL_VARGS
- DT_INST_FOREACH_NODELABEL
- DT_INST_FOREACH_NODELABEL_VARGS
These are for-each helpers for iterating over the node labels of a
devicetree node. Since node labels are unique in the entire
devicetree, their token representations can be useful as unique IDs in
code as well.
As a first user of these, add:
- DT_NODELABEL_STRING_ARRAY
- DT_INST_NODELABEL_STRING_ARRAY
The motivating use case for these macros is to allow looking up a
struct device by devicetree node label in Zephyr shell utilities.
The work on the shells themselves is deferred to other patches.
To make working with the string array helpers easier, add:
- DT_NUM_NODELABELS
- DT_INST_NUM_NODELABELS
Signed-off-by: Martí Bolívar <mbolivar@amperecomputing.com>
Add a generated macro for the number of child nodes of a given node.
Add a generated macro for the number of child nodes of a given node which
children's status are "okay".
Signed-off-by: Swift Tian <swift.tian@ambiq.com>
Currently it is tedious to know the level of an interrupt for
a node in C. One would have to go through a very complex and
error prone macros to check if there's a parent interrupt
controller & if the controller has an interrupt number and thus
not a pseudo interrupt controller like the one found in
`rv32m1`. The level of a node is required to encode the
Zephyr's multi-level interrupt number
Since it is easier to do it in the `gen_defines` script, let's
do the heavy lifting there so that we can introduce new DT
macros to get the interrupt level very easily later.
Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Extend the gen_defines.py write_interrupts(node) function to
generate macros to get the interrupt controller for an
interrupt specifier by idx and by name.
The information is already generated by edtlib.py and stored in
node.interrupts[].controller. This addition uses the node pointed
to by the controller member to generate the following example output
define DT_N_S_device1_IRQ_IDX_0_CONTROLLER \
DT_N_S_gpio_800
define DT_N_S_device1_IRQ_NAME_test4_CONTROLLER \
N_S_device1_IRQ_IDX_0_CONTROLLER
Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
The multi-level encoding of the interrupt number currently
happens in the `gen_defines.py`, which is called in the
`dts.cmake` module after `kconfig.cmake`. However, the number
of bits used by each level is defined in Kconfig and this means
that `gen_defines.py` will not be able to get that information
during build.
To fix this, do the multi-level encoding in C devicetree macro
magic instead of the python script. This ticks one of a
long-standing TODO item from the `gen_defines.py`.
Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Generate a zero padded variant of `_ORD` that is suitable for use in
linker scripts with the `SORT` property, so that `6` is correctly placed
before `24`, and so on.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Generate a zero padded variant of `_ORD` that is suitable for use in
linker scripts with the `SORT` property, so that `6` is correctly placed
before `24`, and so on.
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
Add the _VARGS variant of DT_FOREACH_NODE and
DT_FOREACH_STATUS_OKAY_NODE for when we want to do some kind of
operation on all the nodes in the tree.
Signed-off-by: Carlo Caione <ccaione@baylibre.com>
Support use of these macros with properties of type phandle and
string by allowing iterating over:
- a phandle as if it were a phandles of length 1, for convenience and
consistency with our ability to take its length (and getting 1)
- the non-null characters in a string: we exclude the null for
consistency with the return value of DT_PROP_LEN() on string
properties, which, like strlen(), does not include the null
With this and a previous patch expanding the usage of DT_PROP_LEN(),
there is now a relationship between being able to take a property's
logical length with DT_PROP_LEN() and being able to iterate over its
logical elements with DT_FOREACH_PROP_ELEM(). Explain this in the
documentation.
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
It will be convenient to treat these respectively as degenerate cases
of 'phandles' and 'string-array'. Add support for this and regression
tests. (There's nothing to do in the case of 'phandle' beyond
documenting the guarantee.)
For the record, the other DT_PROP_LEN() tests for each type are in:
type test case property
------------ -------------------- ------------
array test_arrays a
string-array test_path_props compatible
uint8-array test_arrays b
phandles test_phandles phs
phandle-array test_phandles pha-gpios
phandle test_phandles ph
Update docstrings and fix some issues in them.
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
We generally try to have comments in this file that show the form of
each generated macro. This is particularly important in the
write_vanilla_props() function, since that is called on every node in
the tree and handles generic macros that are widely applicable.
Various generated macros have been added over time that don't have
the corresponding comments; add these now.
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
Add a define of the form
`DT_N_<node-id>_P_<prop-id>_ENUM_VAL_<val>_EXISTS` for enumerated
devicetree properties. This enables the devicetree API to check whether
an enum is a given value directly, without resorting to error-prone
checks against the enum index.
Example generated defines (int and string):
`#define DT_N_S_test_S_enum_4_P_val_ENUM_VAL_5_EXISTS 1`
`#define DT_N_S_test_S_enum_6_P_val_ENUM_VAL_zero_EXISTS 1`
Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit adds access to the string values without a quotes.
Signed-off-by: Radosław Koppel <r.koppel@k-el.com>
Co-authored-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
GPIO hog nodes contain a "gpios" property, but unlike other "*-gpios"
properties, these are not phandle-arrays as they only carry the data part
(e.g. pin, flags) but lack the phandles to the (parent) GPIO controller.
Add special devicetree tooling to handle the "gpios" property of GPIO hog
nodes and generate special devicetree helper macros as if they were phandle
arrays.
Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
Disables allowing the python argparse library from automatically
shortening command line arguments, this prevents issues whereby
a new command is added and code that wrongly uses the shortened
command of an existing argument which is the same as the new
command being added will silently change script behaviour.
Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>