Commit graph

721 commits

Author SHA1 Message Date
Camille BAUD
d03b7ffdbb scripts: gen_edt.py: add ability to get properties from /cpus
Adds ability to get properties from the /cpus group,
in the goal of retrieving timebase-frequency for CPU groups.

Signed-off-by: Camille BAUD <mail@massdriver.space>
2025-06-18 09:12:26 -04:00
Fin Maaß
c2fff4a3e4 devicetree: Enhance DT_ENUM_HAS_VALUE macro for arrays
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>
2025-06-12 11:44:46 +02:00
Luca Burelli
ed7f2e0833 scripts: dts: dtlib: preserve order of properties in DTS output
Ensure that the order of properties in the output DTS file matches the
order in which they are defined in the DTS source files by moving props
to the end of the dictionary when they are accessed.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-06-05 15:18:40 -05:00
Luca Burelli
0a4e2e383f scripts: dts: dtlib: improve formatting of long arrays
Split long arrays into multiple lines to improve readability of the
output DTS file. A new line is started when the array data exceeds
80 characters.

Add a few test entries to verify the new behavior.

Note: the F821 linter suppression prevents flagging 'array_start' and
'array_newline' as undefined variables. This is because these variables
are initialized when an opening brace is output, which is necessarily
before any element in a byte or prop array. A sequence of markers not
following this pattern would indicate a bug in the DTS parsing code.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-06-05 15:18:40 -05:00
Fin Maaß
f7153012ef dts: enum_macros: make sure that they are lowercase
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>
2025-05-28 17:48:57 +02:00
Grzegorz Swiderski
468db18389 cmake: modules: Add zephyr_dt_import
Extract the part of `dts.cmake` that invokes `gen_dts_cmake.py`, then
generalize it into a CMake extension, which can be reused by sysbuild.

The Python script itself is also updated, so that the generated CMake
file can accept an input variable DEVICETREE_TARGET, which comes from
the `zephyr_dt_import(TARGET ...)` argument.

Signed-off-by: Grzegorz Swiderski <grzegorz.swiderski@nordicsemi.no>
2025-05-22 13:44:18 +02:00
Luca Burelli
fe7f71ba16 dtlib: add test for lineno/filename comments
Tweak the existing filename_and_lineno test to also check the generated
comments in the string representation of the devicetree match expected
contents and alignment.

Using tmpdir as the base directory simplifies the comparison by avoiding
directory separator issues.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Luca Burelli
c1603b3163 gen_edt: use workspace dir as base for relative paths in comments
This commit allows comments to reference files with paths that are relative
to the Zephyr workspace root. This is done by adding a new argument
'--workspace-dir' to the 'gen_edt.py' script, which is passed to the
'EDT' and 'DT' classes and used instead of the current working directory.

The workspace directory is set to WEST_TOPDIR if West is in use,
otherwise it is set to the parent directory of ZEPHYR_BASE so that
Zephyr files have a 'zephyr/' prefix.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Luca Burelli
a63cb8e74d dtlib: fix double empty line before root node
The root node is the first node in the DTS string representation, and is
currently separated from the headers by two empty lines.

Adjust the spacing so that only one line is printed in all situations. A
small adjustment is added to the test suite to keep the current expected
outputs unchanged.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Benjamin Cabé
afdb62d1e2 dtlib: add lineno/filename to string representation of a DT
Output lineno/filename as comments in the string representation of a DT
to help with debugging DT issues. Also, remove the added comments when
comparing the string representation of a DT to a reference string in the
DT testsuite.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Luca Burelli
6b325daa2a dtlib: wrap array properties in string representation
List each element of a property array in a different line to improve
the readability of the generated DTS file.
Update the test suite's expected outputs accordingly.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Luca Burelli
4b8be385c0 dtlib: fix 'phandle' property's filename and line number
The phandle property is auto-generated when parsing the DTS, so there is
no 'source information'. This commit sets the filename and line number
of the phandle property to be the same as the first reference to the
target node.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
2025-05-16 09:38:35 +02:00
Aksel Skauge Mellbye
73b8d1b637 edtlib: Expose binding title in node class
Make the binding title available from the node the same way
the binding description is propagated.

Signed-off-by: Aksel Skauge Mellbye <aksel.mellbye@silabs.com>
2025-05-02 09:15:50 +02:00
Jamie McCrae
51cf4157e6 scripts: dts: gen_defines: Add support for fixed-subpartitions
Adds support for generating defines for this binding type

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2025-04-30 18:44:06 +02:00
James Roy
285b8ac933 style: edtlib: Use a more efficient expression
Replaced nested loops with a list comprehension
to improve performance. Execution time improved
from 0.0046203136444s to 0.0040774345397s

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-04-09 22:05:22 +02:00
James Roy
ee17657ad3 edtlib: binding: Add a title keyword
Add a 'title' keyword to the binding to provide a short
description of the binding, while 'description' serves as
the long description.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-04-02 12:54:24 +02:00
James Roy
9b795840f9 style: edtlib: Remove the redundant keys() method
Remove the redundant keys method when using the
in statement on a dict to fix the Ruff SIM118
warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
8a2cde9688 style: edtlib: Use from to chain the exception
Use the from keyword to chain exceptions in
order to fix the Ruff B904 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
826ddaed97 style: edtlib: Use the more readable not-equal operator
Replace 'not xx == xx' with 'xx != xx' to
fix the Ruff SIM201 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
b543c26548 style: edtlib: Reorganize the single-line if statement block
Move the statement in the single-line if block
to a new line to fix the Ruff E701 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
8bf5fd9668 style: edtlib: Remove the redundant call arguments
Remove the redundant positional argument in
open function to fix the Ruff UP015 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
7207e91972 style: edtlib: Merge redundant if statements
Merge redundant if statements to fix the Ruff
SIM102 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
20d8a47ad1 style: edtlib: Use a more readable not in statement
Replace 'not xx in xx' with the more readable
'xx not in xx' syntax to fix the Ruff E713 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
8b142ded55 style: edtlib: Sort the import statements
Sort the import statements to fix the
Ruff I001 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
f2eb205d6d style: edtlib: Add an explicit strict argument to the zip
Add an explicit strict parameter to the zip
function to fix the Ruff B905 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
9e46536a1f style: edtlib: Remove deprecated packages
Remove imported deprecated packages and replace
with new packages to fix Ruff UP035 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
James Roy
1425de54e7 style: edtlib: Remove unnecessary quotes
Remove unnecessary quotes from deferred type
annotations to fix the Ruff UP037 warning.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-03-28 12:22:00 +01:00
Benjamin Cabé
529656e2be devicetree: Add filename and line number tracking for nodes & properties
This change enhances the devicetree library by adding support for tracking
the source filename and line number for nodes and properties.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-02-26 22:02:39 +00:00
Benjamin Cabé
3352c402fb scripts: fix spelling of "below"
s/bellow/below/

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2025-02-19 18:51:13 +01:00
Christophe Dufaza
ad22d34d16 edtlib: amend Node.props API documentation
Node properties are not created for all properties defined
by the node's binding (Binding.prop2specs),
only for those that actually have a value.

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2025-01-30 18:29:28 +01:00
Christophe Dufaza
c6e3de296d edtlib: amend PropertySpec.path API documentation
PropertySpec.path does NOT tell "the file where the property
was last modified", but instead the binding file specifying
the devicetree node of which this is a property.

See: #65135

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2025-01-30 18:29:28 +01:00
Luca Burelli
16d71d0598 edtlib: add "hash" attribute to nodes
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>
2025-01-22 15:49:10 +01:00
Grzegorz Swiderski
15488be6af scripts: dts: Refactor gen_driver_kconfig_dts
The motivation for this patch was to improve the script's performance,
but some stylistic changes and cleanups are included as well.

The main optimization concerns the use of PyYAML, as it offers multiple
functions for interpreting YAML. The commonly used `load`/`safe_load`
converts a YAML stream to a dictionary. There are also `scan`, `parse`,
and `compose`, which return intermediate representations, the last one
being a graph. [1]

Since `gen_driver_kconfig_dts` scans DT bindings for compatible strings,
it only needs to look through top level keys in YAML. The intermediate
PyYAML graph is sufficient for this, and using it reduces the script's
execution time by about 30%, without making the code too complicated.

[1] - https://pyyaml.org/wiki/PyYAMLDocumentation

Signed-off-by: Grzegorz Swiderski <grzegorz.swiderski@nordicsemi.no>
2025-01-19 07:57:57 +01:00
James Roy
34bc4c3e3e style: edtlib: Use a better format string
Use f-strings as recommended by PEP-8
instead of the .format() method.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-01-10 18:57:39 +01:00
Christophe Dufaza
ee5c520326 edtlib: tests: refine coverage of Binding objects initialization
Add a series of unit tests which try to cover somewhat systematically
the possible inputs and what we finally get at the exit
of the Binding constructor.

Running the assumption that any (valid) YAML binding file is
something we can make a Binding instance with:
- check which properties are defined at which level (binding,
  child-binding, grandchild-binding, etc) and their specifications
  once the binding is initialized
- check how including bindings are permitted to specialize
  the specifications of inherited properties
- check the rules applied when overwriting a binding's description
  or compatible string (at the binding, child-binding, etc, levels)

Some tests covering known issues are disabled by default:
- this permits to document these issues
- while not causing CI errors (when running the python-devicetree
  unit tests)
- enabling these tests without causing errors should allow us
  to consider the related issues are fixed

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2025-01-08 19:17:51 +01:00
James Roy
c99a61ada4 style: edtlib: Use a better type Annotations
Use built-in types for annotations instead
of types from the typing module.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-01-08 03:26:58 +01:00
James Roy
802eac71f0 style: edtlib: Use a better line continuation operator
Replace backslashes('\') with PEP-8 recommended
parentheses('( )') as the line continuation operator.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2025-01-04 14:15:37 +01:00
James Roy
4553a21fe5 devicetree: Add DT_FOREACH_ANCESTOR macro
Add 'DT_FOREACH_ANCESTOR' macro to get a list of
ancestor node of a given node_id.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2024-12-18 10:17:09 +01:00
James Roy
ab959d5a3b scripts: dts: Use better string concatenation
Replace string concatenation operator with f-string.

Signed-off-by: James Roy <rruuaanng@outlook.com>
2024-12-18 08:33:13 +01:00
Grzegorz Swiderski
f0646d3da4 edtlib: Express Node.matching_compat and Node.binding_path as @property
This simplifies the code and makes it clearer that both properties are
defined in terms of the Binding object matched to a given DT node.

Signed-off-by: Grzegorz Swiderski <grzegorz.swiderski@nordicsemi.no>
2024-11-25 08:31:18 +01:00
Pieter De Gendt
f05deb1aa4 python: Format trivial files where only newlines were missing
Apply formatting on files that only needed adding newlines.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
2024-11-21 20:10:51 +01:00
Florian Grandel
becd9e5b92 scripts: dts: edtlib: fix type docs
The return type of 'uint8-array' properties was not yet documented.

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
2024-11-18 19:30:57 -05:00
Florian Grandel
be4acee09a scripts: dts: edtlib: type hints
Adds type hints to functions that were not yet typed.

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
2024-11-18 19:30:57 -05:00
Florian Grandel
0f1549c575 scripts: dts: edtlib: simplification
Small refactorings to simplify code and improve method encapsulation.

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
2024-11-18 19:30:57 -05:00
Florian Grandel
4bd584cf21 scripts: dts: edtlib: improve Node encapsulation
Moves several node-specific operations inside the Node class to improve
its encapsulation, remove a monkey patch and access to internal methods
and fields.

Signed-off-by: Florian Grandel <fgrandel@code-for-humans.de>
2024-11-18 19:30:57 -05:00
Christophe Dufaza
b0b278503f Revert "edtlib: fix "last modified" semantic for included ... specs"
[1] was introduced to get more valuable answers from
the PropertySpec.path API, which is supposed to tell
in which file the property's specification was "last modfied".

Further work on related issues [2] showed that the
approach chosen in [1] is dead end: we need to first rethink
how bindings (and especially child-bindings) are initialized.

[1] edtlib: fix last modified semantic in included property specs
[2] edtlib: Preserve paths of properties from included child bindings

See also: #65221, #78095

This reverts commit b3b5ad8156.

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2024-11-06 14:43:19 -06:00
Christophe Dufaza
c58d6761bc edtlib: tests: cover basics of filtering inherited properties
Use-case "B includes I includes X":
- X is a base binding file, specifying common properties
- I is an intermediary binding file, which includes X
  without modification nor filter
- B includes I, filtering the properties it chooses
  to inherit with an allowlist or a blocklist

Check that the properties inherited from X via I
are actually filtered as B intends to,
up to the grandchild-binding level.

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2024-11-06 14:43:19 -06:00
Christophe Dufaza
0b946dfc01 Revert "edtlib: test "last modified" semantic for ... specs"
This unit test was added to cover the change introduced by [1].

Further work on related issues [2] showed that the chosen approach
is dead end.
We're reverting all changes made in [1].

[1] edtlib: fix last modified semantic in included property specs
[2] edtlib: Preserve paths of properties from included child bindings

See also: #65221, #78095

This reverts commit 70eaa61cb0.

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2024-11-06 14:43:19 -06:00
Christophe Dufaza
308b568219 Revert "edtlib: test filters set by including bindings"
This unit test was added specifically to cover a regression
reported by the CI while working on [1].

Further work on related issues [2] showed that:
- [1] and [2] are dead end: we need to first rethink
  how bindings (and especially child-bindings) are initialized
- the inclusion mechanism supported by Zephyr deserves more systematic
  testing in edtlib if we want to work with confidence

The approach we choose is to:
- revert all changes made in [1]
- from there, systematically add unit tests as we address
  the issues we identified (or the additional features we need)
  one after the other

[1] edtlib: fix last modified semantic in included property specs
[2] edtlib: Preserve paths of properties from included child bindings

See also: #65221, #78095

This reverts commit 33bb3b60d9.

Signed-off-by: Christophe Dufaza <chris@openmarl.org>
2024-11-06 14:43:19 -06:00
Joel Hirsbrunner
8d07197d6d devicetree: Remove deprecated enum macro
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>
2024-11-01 09:55:11 -05:00