Commit graph

656 commits

Author SHA1 Message Date
Martí Bolívar 78fca3c19c dtlib: fix comment header
The following section of code has nothing public inside.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Martí Bolívar 6ac19439b2 dtlib: remove dead code
There's no need for _parse_node() to return the Node instance that is
its sole argument. The only user of the return value is a dead store.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Martí Bolívar 5272e7f681 dtlib: add DT.move_node()
This helper lets you place a node (really the entire subtree rooted at
that node) elsewhere in the devicetree. This will be useful when
adding system devicetree support, when we'll want to be able to, for
example, move the CPU cluster node selected by the current execution
domain to /cpus.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Martí Bolívar e479d3f7c6 dtlib: use new helper in test cases
Using dtlib_raises() throughout the test cases saves some typing.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Martí Bolívar 2063ddbc93 dtlib: add new test case helper
Introduce a context manager that will save some typing
when dealing with expected exceptions.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Martí Bolívar faa7e530c2 dtlib: clean up a documentation string
The standard way we write this in the library is 'documentation
string', not 'docstring'.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2023-02-27 17:44:45 +01:00
Radosław Koppel 14a1b2ffec dts: Add _STRING_UNQUOTED string and string-array
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>
2023-02-20 09:49:00 +01:00
Allan Norgaard 23cacb3766 scripts: dts: gen_driver_kconfig_dts: Skip empty yaml files
The YAML parsing will currently return a None-type if no input data
is empty, and the subsequent check for a substring will cause an
exception.

Signed-off-by: Allan Norgaard <allannk@gmail.com>
2023-02-19 20:46:44 -05:00
Henrik Brix Andersen 28819152cb scripts: dts: add special tooling for handling GPIO hog nodes
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>
2023-01-27 14:38:52 -08:00
Jamie McCrae ec7044437e treewide: Disable automatic argparse argument shortening
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>
2023-01-26 20:12:36 +09:00
Chris Duf 92897ef011 python-devicetree: bump version to 0.0.2
Inconsistency between python-devicetree version numbers
may be confusing:
- the last version at PyPI is 0.0.2
- zephyr-rtos/python-devicetree/setup.py sets the version
  to 0.0.2 (this is probably the setup file used when uploading
  to PyPI)
- zephyr-rtos/zephyr/scripts/dts/python-devicetree/setup.py sets
  the version to 0.0.1

This may suggest that the mirror repository, and PyPI, are more
up-to-date than zephyr-rtos/zephyr/scripts/dts/python-devicetree.

Repositories being otherwise mostly identical (1), also bumping
the python-devicetree's version here seems a sane option.

(1) Ignoring the doc directory (only at zephyr-rtos/python-devicetree).

Signed-off-by: Chris Duf <chris@openmarl.org>
2023-01-16 11:19:54 +00:00
Chris Duf 436616c66b python-devicetree: unpin types-PyYAML version
This is essentially a revert of PR #46311 "python-devicetree: CI hotfix",
assuming the original issue has been resolved, either upstream in
types-PyYAML or in Zephyr itself.

Tested with types-PyYAML 6.0.12.2 (current version at PyPI):

  $ python -m mypy --config-file=tox.ini --package=devicetree
  dtlib.py:962: note: By default the bodies of untyped functions [...]
  dtlib.py:964: note: By default the bodies of untyped functions [...]
  dtlib.py:965: note: By default the bodies of untyped functions [...]
  dtlib.py:967: note: By default the bodies of untyped functions [...]
  Success: no issues found in 4 source files

The "notes" above are harmless (use of type hinting to define local
variables while mypy won't "check the bodies of untyped functions").

References:
- python-devicetree tox run fails (issue #46286)
- python-devicetree: CI hotfix (PR #46311)
- python-devicetree: CI hotfix (commit f6a6843)

Signed-off-by: Chris Duf <chris@openmarl.org>
2023-01-16 11:19:54 +00:00
Moritz Fischer 7dd05967f7 scripts: dts: gen_driver_kconfig_dts: Use SafeLoader
Using SafeLoader is preferrable where possible.

Signed-off-by: Moritz Fischer <moritzf@google.com>
2023-01-04 07:38:56 -08:00
Fabio Baltieri 0daa3a78a9 yamllint: indentation: fix files in scripts/
Fix the YAML files indentation for files in scripts/.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2023-01-04 14:23:53 +01:00
Gerard Marull-Paretas 537133fe44 devicetree: edtlib: tests: add coverage for child includes
Add test coverage for the child-binding include feature. It includes
verification of included properties as well as usage of allow/blocklist.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-11-17 06:57:46 -06:00
Martí Bolívar a97295b6a2 edtlib: re-appease the linter
At some point in the past, we had to suppress a couple of false
positive pylint warnings to pass CI. But now the linter seems to have
figured out its original mistake and is complaining about a useless
supression. Sigh. Play along.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar 15e3e317f7 dtlib: implement copy.deepcopy() for DT
The standard library copy module allows you to implement shallow and
deep copies of objects. See its documentation for more details on
these terms.

Implementing copy.deepcopy() support for DT objects will allow us to
"clone" devicetree objects in other classes. This in turn will enable
new features, such as native system devicetree support, within the
python-devicetree.

It is also a pure feature extension which can't harm anything and is
therefore safe to merge now, even if system devicetree is never
adopted in Zephyr.

Note that we are making use of the move from OrderedDict to regular
dict to make this implementation more convenient.

See https://github.com/devicetree-org/lopper/ for more information on
system devicetree. We want to add system devicetree support to dtlib
because it seems to be a useful way to model modern, heterogeneous
SoCs than traditional devicetree, which can really only model a single
CPU "cluster" within such an SoC.

In order to create 'regular' devicetrees from a system devicetree, we
will want a programming interface that does the following:

   1. parse the system devicetree
   2. receive the desired transformations on it
   3. perform the desired transformations to make
      a 'regular' devicetree

Step 3 can be done as a destructive modification on an object-oriented
representation of a system devicetree, and that's the approach we will
take in python-devicetree. It will therefore be convenient to have an
efficient deepcopy implementation to be able to preserve the original
system devicetree and the derived regular devicetree in memory in the
same python process.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar 3c976cc3b6 devicetree: stop using OrderedDict
Regular dicts are insertion-ordered since CPython 3.6 and Python 3.7.
Zephyr now requires Python 3.8, so it should be OK to replace
OrderedDict with regular dict now. This results in less typing and
more readable object representations.

A nitpicker could argue that this is a functional change, since if a
user is doing 'assert isinstance(node.props, OrderedDict)', that will
fail now, but:

 1. nobody is doing something like that in the zephyr tree
 2. that would be a silly thing to do
 3. we don't currently make any API stability guarantees
    for this module right now anyway

so it should be fine.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar c25dde3511 dtlib: fix Property.labels docstring
The value of the 'labels' attribute is a list, not a set.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar b64d5d6d19 dtlib: move some internal code around
Refactor the file parsing methods for readability by moving the
_parse_header() and _parse_memreserves() calls from _parse_dt() to
_parse_file(). The header and memreserves are not part of the 'tree'
part of the devicetree; now that we have a dedicated _parse_file()
helper, it makes more sense to me to have them there.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar c875cb0f94 dtlib: fix Property.type type annotation
Holy overloaded technical terms, Batman.

Here, 'property' and 'type' each mean two different things, which
we can distinguish like this:

- Property (capital P): dtlib.Property class, represents
  a property in a devicetree node
- @property: a Python property
- type(): an "@property" in the Property class, that returns
  a dtlib.Type value
- Type (capital T): dtlib.Type class, represents the devicetree
  type of a Property value (dtlib.Type.BYTES, etc.)

The type() @property in the Property class currently has an 'int' as
its Python return type annotation. It really returns a dtlib.Type,
which is an int (since it's an IntEnum), but that's not the same thing
as an int.

Change this to Type to be clear that not just any int can be returned
by this @property.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar cd18ffee22 dtlib: clean up Property initialization
Make attribute initialization order match the order that attributes
appear within the class docstring. Move the 'type' property definition
up by the constructor to make it more obvious that this 'attribute' is
a (Python) property. This is for readability.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar 47789aa1fa dtlib: clean up Node initialization
Reorder attribute initialization to match the order that attributes
appear in the class level docstring. This is for readability.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Martí Bolívar cb78a5c322 dtlib: clean up DT initialization
Initialize all the public API interface related attributes within the
constructor instead of scattering them throughout the implementation
of the class, and make sure they all have type annotations.

Move all the parsing code away from the init routines and public API
down to the main parsing block.

This is for readability and paves the way for later changes that
affect the way initialization happens.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-11-07 19:00:31 +01:00
Chris Duf e067be5f0c Devicetree: edtlib: fix possible TypeError in Binding.__repr__()
Calling Binding.__repr__() when the attribute Binding.path is None
would raise TypeError: expected str, bytes or os.PathLike object,
not  NoneType.

Known bindings that may not have a path (Binding.path is None)
include bindings for properties such as 'compatible', 'reg', 'status'.

Signed-off-by: Chris Duf <chris@openmarl.org>

Co-authored-by: Gerard Marull-Paretas <gerard@teslabs.com>
2022-10-13 12:02:31 +02:00
Chris Duf 230c80e000 Devicetree: edtlib: fix possible AttributeError in Property.description
Attempting to access the property Property.description
when Property.spec.description is None would raise
AttributeError: 'NoneType' object has no attribute 'strip'.

Known properties that may not have a description
(Property.spec.description is None):
- 'compatible' for nodes such as / /soc /soc/timer@e000e010 /leds /pwmleds
- 'reg'        for nodes such as /soc/timer@e000e010
- 'status'     for nodes such as /soc/timer@e000e010
- 'gpios'      for nodes such as /leds/led_0 /buttons/button_0
- 'pwms'       for nodes such as /pwmleds/pwm_led_0

This patch checks the PropertySpec.description attribute before calling
strip(): will return None, and not raise AttributeError.

Signed-off-by: Chris Duf <chris@openmarl.org>

Co-authored-by: Gerard Marull-Paretas <gerard@teslabs.com>
2022-10-13 16:03:49 +09:00
Chris Duf 5abce9b8ec Devicetree: edtlib: fix possible KeyError in Binding.description
Attempting to access the Binding.description property
when the description is unavailable would raise KeyError: 'description'.

Known bindings that won't define a 'description' key in the
Binding.raw dictionary include the 'compatible' property's binding
of nodes such as /, /soc, /leds or /pwmleds.
Note that this may also occur when a proper YAML
binding file is available (e.g. pwmleds.yaml).

This patch simply substitutes the Binding.raw dictionary indexing
with the get() function: will return None and not raise KeyError.

Signed-off-by: Chris Duf <chris@openmarl.org>
2022-10-12 18:45:32 +09:00
Maureen Helm e73c363346 scripts: edtlib: gen_defines: Add model name macros based on compat
Follow up to 5b5aa6ebba adding model name
and existence macros for all compatibles of a node that match an entry
in vendor prefixes.

Signed-off-by: Maureen Helm <maureen.helm@intel.com>
2022-10-07 11:48:02 -07:00
Gerard Marull-Paretas fdea3c9a44 devicetree: add DT(_INST)_FOREACH_PROP_ELEM_SEP(_VARGS)
Add a new set of helpers for expanding property entries with a
separator. These macros complement DT(_INST)FOREACH_PROP_ELEM(_VARGS) by
adding the capability to expand with a custom separator between property
entries. This allows, in some cases, to re-use existing macros (e.g.
DT_PROP_BY_IDX) without creating an auxiliary macro that just appends a
separator. Example:

```dts
n: node {
	...
	my-gpios = <&gpioa 0 GPIO_ACTIVE_HIGH>,
		   <&gpiob 1 GPIO_ACTIVE_HIGH>;
};
```

Before:

```c
 #define GPIO_DT_SPEC_BY_IDX_AND_COMMA(node_id, prop, idx) \
	GPIO_DT_SPEC_BY_IDX(node_id, prop, idx),

struct gpio_dt_spec specs[] = {
	DT_FOREACH_PROP_ELEM(DT_NODELABEL(n), my_gpios,
			     GPIO_DT_SPEC_BY_IDX_AND_COMMA)
};
```

After:

```c
struct gpio_dt_spec specs[] = {
	DT_FOREACH_PROP_ELEM_SEP(DT_NODELABEL(n), my_gpios,
				 GPIO_DT_SPEC_BY_IDX, (,))
};
```

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-03 10:11:18 +02:00
Martí Bolívar 22012894c0 dtlib: error out on duplicate node names
Attempts to define two nodes with the same name within a single set of
curly brackets should fail.

For example, this is invalid DTS according to dtc:

    / { foo {}; foo {}; };

By contrast, this is valid since the node named 'foo' appears twice in
two different sets of curly brackets:

    / { foo {}; };
    / { foo {}; };

Zephyr's dtlib currently does not error out on the invalid condition.

Now that Zephyr itself has been updated to not include such nodes (to
the best of my ability), we can fix this divergence from current dtc
behavior and add a regression test in dtlib.

Fixes: #49590
Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-09-21 07:55:40 -07:00
Daniel Leung 418c915f1b dts: add primitive support for multi-bus in bindings
For a single bus that supports multiple protocols, e.g. I3C and I2C,
the single value "bus:" setting is no longer sufficient, as a I3C bus
cannot be matched to a device having "on-bus: I2C". This commit
extends the "bus:" setting so that it can accept a list of values.
This change allows corresponding devicetree macros to be generated
so that DT_ON_BUS() can work properly in this scenario.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2022-09-09 17:42:33 -04:00
Martí Bolívar 7ef9c4b20e edtlib: remove python 3.5 workaround
Remove a yaml monkeypatch. It is no longer needed since we support 3.6
or later on Zephyr v2.7 LTS and 3.8 or later on what will become v3.2.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-09-07 15:38:26 +02:00
Maureen Helm 5b5aa6ebba scripts: gen_defines: Add vendor name macros based on vendor prefixes
Adds vendor name and existence macros for all compatibles of a node that
match an entry in the vendor prefixes file.

Signed-off-by: Maureen Helm <maureen.helm@intel.com>
2022-09-01 14:53:55 -07:00
Maureen Helm a1b0c39c37 scripts: edtlib: Add compat2vendor LUT constructed from vendor prefixes
Adds a new compat2vendor lookup table that maps compatibles to vendor
names, constructed from the vendor prefixes file. This approach is a
more scalable alternative to adding a vendor name property to devicetree
bindings, as was previously proposed.

Signed-off-by: Maureen Helm <maureen.helm@intel.com>
2022-09-01 14:53:55 -07:00
Gerard Marull-Paretas fff9ecbc7f devicetree: add DT_(INST_)FOREACH_CHILD(_STATUS_OKAY)_SEP(_VARGS)
It is frequent to see in Devicetree code constructs like:

```c
 #define NAME_AND_COMMA(node_id) DT_NODE_FULL_NAME(node_id),

const char *child_names[] = {
	DT_FOREACH_CHILD(DT_NODELABEL(n), NAME_AND_COMMA)
};
```

That is, an auxiliary macro to append a separator character in
DT_FOREACH* macros. Non-DT API, e.g. FOR_EACH(), takes a separator
argument to avoid such intermediate macros.

This patch adds DT_FOREACH_CHILD_SEP (and instance/status okay/vargs
versions of it). They all take an extra argument: a separator. With this
change, the example above can be simplified to:

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

Notes:
- Other DT_FOREACH* macros could/should be extended as well

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-30 16:19:57 +02:00
Gerard Marull-Paretas d77f4e61de scripts: dts: gen_defines: add DT_DEBRACKET_INTERNAL helper
Add a utility macro used to remove brackets from around a single
argument. While __DEBRACKET exists in util_internal.h, this change makes
DT independent (otherwise we should include util_internal.h, which is
another option).

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-30 16:19:57 +02:00
Kumar Gala f1660f4d51 edtlib: allow const arrays
Allow for having array types (array, uint8-array, string-array) be const.
This would allow for something like:

properties:
    reg-names:
        const: ["foo", "bar"]

To be supported.

Renamed function _check_prop_type_and_default to _check_prop_by_type
as part of this change and Moved the check for 'const' types into
_check_prop_by_type as its similar to the prop_type check and it was
easier to implement in _check_prop_by_type as we already extract
prop_type from the option in that function.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-08-25 18:11:27 -07:00
Kumar Gala 8a5d568a79 dts: Standardize string token names for arrays
Use the same postfixes for string arrays as we use for strings.  Keep
just the _STRING_*_TOKEN) variants.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-08-25 15:00:24 -07:00
Radosław Koppel 7614110a20 dts: Add _STRING_TOKEN and _STRING_UPPER_TOKEN to string-array
This commit adds string token versions of the values also
in items inside string-array.

Signed-off-by: Radosław Koppel <r.koppel@k-el.com>
Co-authored-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
Co-authored-by: Kumar Gala <galak@kernel.org>
2022-08-22 13:58:30 -05:00
Fabio Baltieri 0226e53e23 Restore "cmake: remove device_extern.h logic"
This reverts commit 87c6789355, restoring
commit 3b341085a2.

Restore the original change now that the underlying issue has been fixed
in 6cfb18686e.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2022-08-12 07:45:01 +01:00
Fabio Baltieri 87c6789355 Revert "cmake: remove device_extern.h logic"
This reverts commit 3b341085a2.

Seems to be breaking CI for few qemu platforms.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2022-08-11 17:21:46 -05:00
Martí Bolívar 3b341085a2 cmake: remove device_extern.h logic
This file is no longer used by device.h, so let's avoid spending time
generating the content formerly in it.

In order to leave a pointer in place for users who are expecting to
see it or are pulling it into their own builds, however, replace its
contents with an #error directive that tells them what happened. This
can be removed later on when we expect people are used to the
transition.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-08-08 10:44:41 +02:00
Martí Bolívar f0d11f780c gen_defines: add helpers for iterating over the entire tree
Add two helper macros:

- DT_FOREACH_HELPER
- DT_FOREACH_OKAY_HELPER

These are internal generated macros which will be used to define
devicetree.h APIs for iterating over every node in the tree, and every
node in the tree with status "okay", respectively.

We can use these new APIs to implement the functionality in
device_extern.h natively from device.h without requiring a generated
header. It will also be useful for other purposes later on.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-08-08 10:44:41 +02:00
Martí Bolívar 190197ec97 gen_defines: trivial rename
Generalize a function name as prep for extendings its scope.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-08-08 10:44:41 +02:00
Kumar Gala b109a0a9f7 dts: Add simple script to generate Kconfig.dts based on bindings
Given binding dirs the script will generate a Kconfig.dts of the
form:

    DT_COMPAT_ADI_ADT7420 := adi,adt7420

    config DT_HAS_ADI_ADT7420_ENABLED
            depends on DTS_HAS_ADI_ADT7420
            def_bool $(dt_compat_enabled,$(DT_COMPAT_ADI_ADT7420))

Than a driver Kconfig can use these Kconfig symbols as follows:

    menuconfig ADT7420
            bool "ADT7420 Temperature Sensor"
            default y
            depends on DT_HAS_ADI_ADT7420_ENABLED
            ...

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-07-19 09:39:49 -07:00
Martí Bolívar d6f68f0bbb scripts: gen_defines: fix tokenization of array elements
This fixes commit
3d5cc38cf6.

That commit only works if removing the quotes from e.g. a string in an
array actually results in a token. If the string's value is something
like

  "foo,bar"

though, it will result in the "token"

  foo,bar

in the generated output.

This is wrong; fix it using the new edtlib.str_as_token() API
introduced to allow callers to perform the same procedure as that
library for fixing up strings.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-07-18 17:50:44 -05:00
Martí Bolívar 7b2a728584 scripts: gen_defines: add a missing comment
Adding a comment at this point in the header file splits these macros
into their own section. They are presently grouped together with the
previous section, which is unrelated. This is confusing; fix it.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-07-18 17:50:44 -05:00
Martí Bolívar 4ef8c8e447 edtlib: expose str_as_token() API
Some callers need to be able to convert strings to tokens in the same
way edtlib does. Make this possible by exposing the internal helper
function used to do that under a suitable name.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-07-18 17:50:44 -05:00
Martí Bolívar 19bb6b330c edtlib: fix error handling in an internal helper
The error message emitted by _interrupt_parent() is wrong; it
mistakenly says:

  node None has an 'interrupts' property, but [...]

This 'None' is appearing because the same routine overwrites the
'node' argument that the caller is asking about with node parents
until it hits the root, at which point root.parent is None.

Fix it by caching the original node and using that in the error
message instead.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-07-18 17:50:44 -05:00
Kumar Gala b200b9a9e0 dts: bindings: bus labels are now optional
All in tree device drivers on a bus use some form of DEVICE_DT_GET
so we no longer need to require label properties.

Signed-off-by: Kumar Gala <galak@kernel.org>
2022-07-14 09:19:35 -07:00
Henrik Brix Andersen 3d5cc38cf6 scripts: dts: gen_defines: define _TOKEN + _UPPER_TOKEN for string arrays
Generate definitions for *_IDX_n_TOKEN and *_IDX_n_UPPER_TOKEN for all
string-array type properties.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2022-06-28 20:46:11 +02:00
Martí Bolívar f6a6843471 python-devicetree: CI hotfix
Pin the types-PyYAML version to 6.0.7. Version 6.0.8 is causing CI
errors for other pull requests, so we need this in to get other PRs
moving.

Fixes: #46286

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-06-06 09:09:27 -07:00
Jordan Yates 8e4107f9be scripts: ensure intended path for edtlib imports
When updating `sys.path` to allow importing the pickled edtlib instance,
add the path to the front of `sys.path`, not the end. This ensures that
the `devicetree.edtlib` module that is imported is the one relative
to the files being run, not some other version which may exist on the
path.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2022-05-13 10:09:45 -07:00
Martí Bolívar 50f9b3c2ca gen_defines: add _CHILD_IDX macros for nodes with parents
These expose every node's index in its parent's list of children to C.
The root node has no parent, so no _CHILD_IDX macro is generated for
it.

Keep macros.bnf up to date with the new generated macros.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-03-24 10:48:40 +01:00
Martí Bolívar 355cc01a36 scripts: gen_defines: rearrange some internals
Consolidate child helper macros into a single function. No functional
changes expected.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-03-24 10:48:40 +01:00
Martí Bolívar 7c3b445323 edtlib: add Node.child_index()
It can be useful to know what the index of a particular child is in
the list of nodes. Add a a helper for computing that and some test
cases.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2022-03-24 10:48:40 +01:00
Carlo Caione bc72fb67b1 linker: Create sections from zephyr,memory-region nodes
Currently when a node has a 'zephyr,memory-region' compatible and a
'zephyr,memory-region' string property, a new memory region is created
in the linker script.

Having a memory region without a section to place variables in could be
not that useful. With this patch we extend the memory-region mechanism
to also create sections.

The user can then place variables in the sections as usual by using for
example the GCC attributes.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-03-19 14:32:17 -04:00
Nazar Kazakov f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Carlo Caione 3c0c03b9eb cmake: dts: Add dt_comp_path() cmake function
Add a new cmake extension function:

  dt_comp_path(<var> COMPATIBLE <compatible> [INDEX <idx>])

to get a list of paths for the nodes with the given <compatible>. This
is useful when we have to cycle through several nodes with the same
compatible.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2022-02-21 22:02:04 -05:00
Jordan Yates 9c98d4f2b7 scripts: gen_defines: zephyr,linker-region unique
Add checks to ensure that `zephyr,linker-region` property values are
always globally unique.

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
2021-12-09 16:23:03 +01:00
Martí Bolívar 6c65b15ce6 edtlib: wrap DTError
Don't let a malformed devicetree escape as a DTError. Wrap it in an
EDTError instead, so callers can just rely on the edtlib APIs as is
generally expected.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-12-07 13:39:37 -05:00
Martí Bolívar 7738977af4 dtlib: handle GCC linemarkers
A GCC linemarker of the form:

  # 1 "filename" 2 3 4

or so is not currently being handled, because the current regular
expression assumes the "flags" values (the numbers after "filename")
are limited to a single value. Tweak the regular expression to allow
for up to 4 flags, which is what GCC documents it may emit:

https://gcc.gnu.org/onlinedocs/gcc-10.2.0/cpp/Preprocessor-Output.html

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-12-07 13:39:37 -05:00
Martí Bolívar 4e783a363a dtlib: add _Token __repr__
Convert numeric IDs to symbolic token ID names for ease of debugging.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-12-07 13:39:37 -05:00
Martí Bolívar 17ae4fe05a scripts: gen_dts_cmake: emit properties for /aliases
This enables cmake extensions that can look up the path for any
devicetree alias, or check if the alias is missing, etc.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-12-06 08:01:48 -05:00
Martí Bolívar b65b49bef6 scripts: gen_dts_cmake: a small optimization
Using edt.chosen_nodes looks like a simple attribute lookup, but it's
actually calling a property that will create a new list of chosen
nodes every time. Apply a small optimization by only creating the list
once.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-12-06 08:01:48 -05:00
Neil Armstrong 1e8f0f3bd1 gen_defines: generate ranges defines for PCIe I/O and memory regions
As described in IEEE Std 1275-1994, the PCIe bindings uses the ranges
property to describe the PCI I/O and memory regions.

Write _RANGES_ defines that will be used to determines the I/O and
memory regions from PCIe Controller drivers.

Also exclude "ranges" & "dma-ranges" property's length generation
alogn "reg" and "interrupt".

Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
2021-11-10 08:23:00 -05:00
Neil Armstrong 2f4a6646cf edtlib: add tests to check Range property
This adds some tests in test_edtlib.py and test.dts to check all
common possible combination of ranges property usage and handling
by edtlib.

Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
2021-11-10 08:23:00 -05:00
Neil Armstrong cd6a5648d7 edtlib: add Range property on Node, used for PCIe I/O and memory regions
As described in IEEE Std 1275-1994, the PCIe bindings uses the ranges
property to describe the PCI I/O and memory regions.

Add parsing of this property in edtlib.

Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
2021-11-10 08:23:00 -05:00
Carlo Caione 42105ed882 dts: Add support for specifier-space property.
Currently all the *-names and *-cells properties are derived from the
name of the base <name>s property. This is a limitation because:

- It forces the base property name to be plural ending in -s
- It doesn't allow the english exception of plural words ending in -es

With this patch we add one additional property 'specifier-space' that
can be used to explicitly specify the base property name.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
Suggested-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-10-13 09:56:46 -05:00
Martí Bolívar ffa1515978 dtlib: fix issue which allowed invalid node names
Node names are subject to the rules in table 2.1 of the devicetree
specification v0.3, while properties are subject to rules in table
2.2. These rules mean that some property names are invalid node names.

However, the same regular expression is being used to validate the
names of nodes and properties in dtlib. This leads to invalid node
names being allowed to pass. Fix this issue by moving the node name
handling code to the Node constructor and checking against the
characters in table 2.1.

The test cases claim that the existing behavior matches dtc. I can't
reproduce that. I get errors when I use invalid characters (like "?")
in a node name. For example:

foo.dts:3.8-11: ERROR (node_name_chars): /node?: Bad character '?' in
node name

Try to make the dtlib error message reminiscent of that.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-31 19:36:31 -04:00
Martí Bolívar fff818bbe6 dtlib: remove unused variable
This is unused since the very beginning of the module's introduction.
It looks like it was abandoned in favor of the approach where each
token can have only one capturing group.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-31 19:36:31 -04:00
Torsten Rasmussen fcf720941f cmake: dts: import devicetree symbols into CMake
This commit introduces devicetree API in CMake so that devicetree
properties and register block can be used in the CMake build system.

The script scripts/dts/gen_dts_cmake.py processes the edt.pickle and
generates a corresponding devicetree property map in a devicetree_target
that is then used in CMake.

The following devicetree API has been made available in Zephyr CMake:
- dt_nodelabel(<var> NODELABEL <label>)
- dt_node_exists(<var> PATH <path>)
- dt_node_has_status(<var> PATH <path> STATUS <status>)
- dt_prop(<var> PATH <path> PROPERTY <prop>)
- dt_prop(<var> PATH <path> INDEX <idx> PROPERTY <prop>)
- dt_num_regs(<var> PATH <path>)
- dt_reg_addr(<var> PATH <path> [INDEX <idx>])
- dt_reg_size(<var> PATH <path> [INDEX <idx>])
- dt_has_chosen(<var> PROPERTY <prop>)
- dt_chosen(<var> PROPERTY <prop>)

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-08-25 20:28:26 -04:00
Martí Bolívar 9df0493dc3 scripts: gen_defines: emit pinctrl helper macros
We need to be able to access pinctrl-<index> property contents by
name, convert names to indexes, convert indexes to names, and perform
existence checks by name and by index.

This is currently not possible because we don't track these properties
the same way we do other named properties. That in turn is because
they are different then the usual named properties.

The usual case looks like this, picking DMAs just for the sake of
example:

  dmas = <&dma0 ...>, <&dma1 ...>;
  dma-names = "tx", "rx";

So "tx" is the name for the <&dma0 ...> element, and "rx" is the name
for the <&dma1 ...> element, all in a single "dmas" property.

By contrast, pinctrl properties look like this:

  pinctrl-0 = <&foo &bar ...>;
  pinctrl-1 = <&baz &blub ...>;
  pinctrl-names = "default", "sleep";

Here, "default" is the name for the entire pinctrl-0 property.
Similarly, "sleep" is the name of the pinctrl-1 property. It's a
strange situation where the node itself is kind of a container for an
array of pin control properties, which Zephyr's bindings language
can't really capture and has some special case handling in edtlib.

This is easiest to handle with ad-hoc code. Add special case macros
for pinctrls.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:09:00 -04:00
Martí Bolívar 7f69a03f72 scripts: gen_defines: refactor write_special_props
Move the partition handling code into its own function and rework the
comment. This is prep work for adding additional generated macros to
this function.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:09:00 -04:00
Martí Bolívar 3252821b4c scripts: gen_defines: fix typo
Missing 's'.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:09:00 -04:00
Martí Bolívar fa2e82becd edtlib: emit pinctrl names as tokens
This will be necessary to going back and forth between indexes and
names in C.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:09:00 -04:00
Martí Bolívar 6e9e984e60 dtlib: remove pylint suppression
This became useless when _init_tokens() was refactored not to use
global variables (in "dtlib: use IntEnum for token IDs"), and the
linter is complaining about it now.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:03:17 -04:00
Martí Bolívar 227ce89c41 edtlib: use f-strings where it makes sense
Similarly to what was done for dtlib, use f-strings in places where it
improves readability. Some places, e.g. __repr__ methods that
construct a string using something like

    "<SomeType, {}>".format(", ".join(...))

are better left off as-is.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:03:17 -04:00
Martí Bolívar 66ee3d291c dtlib: use f-strings where it makes sense
The library was originally developed before Python 3.6 was the minimum
supported version. Use f-strings now that we can do that, as they tend
to be easier to read.

There are a few places where str.format() makes sense to preserve,
specifically where the same argument is used multiple times; leave
those alone.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-25 18:03:17 -04:00
Martí Bolívar 85837e1c54 scripts: gen_defines: allow multiple --vendor-prefixes
Allowing multiple such files will let higher layers take inputs from
multiple DTS_ROOT directories. This in turn will allow out of tree
bindings to manage their own sets of prefixes without patching
upstream Zephyr's file or doing other similar hacks.

Parse each file into a dict, and merge those dicts into a single
dictionary for the EDT constructor.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-20 18:57:30 -04:00
Martí Bolívar 2533246610 edtlib: tweak vendor prefixes implementation details
If the user passes None, set the internal attribute to an empty dict
instead. This lets us avoid some None checking and simplifies things
without changing semantics -- if the user *does* pass an empty dict,
the results are the same.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-20 18:57:30 -04:00
Martí Bolívar d14835812c edtlib: allow .yml files too
This is a common extension for YAML files. We don't have to allow it
in upstream zephyr, but we should allow downstream DTS_ROOTs to have
this ability.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-20 18:57:30 -04:00
Martí Bolívar 9e8da8953c scripts: edtlib: fix werror for unknown vendor
Commit c4079e4be2
("scripts: rework edtlib warnings-turned-errors") was trying to abort
on unknown vendor prefix, but the error log is not fatal.

Fix it by using the same error handling function we use when aborting
due to deprecated property usage.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-17 17:51:57 -04:00
Martí Bolívar e9229f1f2b scripts: edtlib: relax vendor check for root node
There are way too many one-off vendor prefixes set up for individual
boards to bother tracking them in vendor-prefixes.txt. As a practical
matter, the compatible for the root node doesn't matter anyway. So
just relax our check for that node.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-17 17:51:57 -04:00
Kumar Gala 7b9fbcd3b7 devicetree: Fix DT_PROP/DT_PROP_BY_IDX for phandle(s)
DT_PROP for a phandle property should return the node that phandle
points to (similar for DT_PROP_BY_IDX for phandles)  and this wasn't
working as the define generator didn't create the proper defines for
phandle(s).

Fix the generator and add some tests to make sure this continues to
work correctly.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2021-08-12 19:01:38 -05:00
Martí Bolívar e7d42ff879 dts: gen_defines: emit more for-each helpers
Add some helper macros that work similarly to the
'DT_FOREACH_OKAY_INST_<compat>(fn)' macros, except they give 'fn'
node identifiers in their expansion instead of instance numbers.

This makes it possible to add for-each APIs to devicetree.h that work
on an arbitrary compatible, not just DT_DRV_COMPAT.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-06 12:24:57 -05:00
Martí Bolívar c4079e4be2 scripts: rework edtlib warnings-turned-errors
Create a "global" gen_defines.py option and edtlib.EDT constructor
kwarg that turns edtlib-specific warnings into errors. This applies to
edtlib-specific warnings only. Warnings that are just dupes of dtc
warnings are not affected.

Use it from twister to increase DT testing coverage in upstream zephyr.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-03 12:21:45 -05:00
Martí Bolívar f261d77c75 cmake: dts: warn on unrecognized vendor prefixes
This uses the list of vendor prefixes provided by the user by default.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-03 12:21:45 -05:00
Martí Bolívar 6719fae101 edtlib: warn on unrecognized vendors
An unknown vendor prefix is now a warning. We augment the list of
vendor prefixes passed by the user with a grandfathered-in bunch from
Linux.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-03 12:21:45 -05:00
Martí Bolívar 7b60fa3a15 edtlib: move vendor-prefixes.txt parsing in here
Take this helper from the doc tooling and expose it as API.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-03 12:21:45 -05:00
Martí Bolívar 4a313129b2 edtlib: only do compatible checks once
Remember the compatibles we've validated, and don't check them again.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-08-03 12:21:45 -05:00
Carlo Caione f4db14f349 dts: Introduce DT_STRING_TOKEN and DT_STRING_UPPER_TOKEN
To be able to get a tokenize DT string without the quotes. Deprecate
also DT_ENUM_TOKEN and DT_ENUM_UPPER_TOKEN.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
2021-07-15 18:12:51 -05:00
Martí Bolívar 15db98a400 dtlib: allow dangling aliases with DT(..., force=True)
As a first step towards being more forgiving on invalid inputs, allow
string-valued aliases properties that do not point to valid nodes when
the user requests permissiveness.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-07-14 19:51:46 -04:00
Martí Bolívar 176225db58 dtlib: add force DT kwarg
Modeled after dtc's --force option, the idea is this will try harder
and harder over time to produce an object despite malformed input.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-07-14 19:51:46 -04:00
Arvin Farahmand d0b9c03154 devicetree: Add _VARGS variants to _FOREACH_ marcos
`_FOREACH_` macros do not allow the caller to pass additional arguments
to the `fn`. A series of `_VARGS` variants have been added that allow
the caller to pass arbitrary number of arguments to the `fn`:

```
DT_FOREACH_CHILD_VARGS
DT_FOREACH_CHILD_STATUS_OKAY_VARGS
DT_FOREACH_PROP_ELEM_VARGS
DT_INST_FOREACH_CHILD_VARGS
DT_INST_FOREACH_STATUS_OKAY_VARGS
DT_INST_FOREACH_PROP_ELEM_VARGS
```

Signed-off-by: Arvin Farahmand <arvinf@ip-logix.com>
2021-07-14 19:50:39 -04:00
Casper Meijn d25e5c20a0 scripts: dts: Produce error for invalid yaml
I made an alignment error in a dts binding, but the build was
successful. After some debugging I found the following warning
explaining the problem:

  '/home/casper/src/zephyrproject/zephyr/dts/bindings/gpio/
       gpio-keys.yaml' appears in binding directories but isn't valid
       YAML: while parsing a block mapping
    in "<unicode string>", line 11, column 8
  did not find expected key
    in "<unicode string>", line 18, column 9

I think this should be an error as there shouldn't be any invalid yaml.

Signed-off-by: Casper Meijn <casper@meijn.net>
2021-06-14 10:36:27 -05:00
Martí Bolívar 8875340db4 python-devicetree: tox: fix mypy
Recent versions of mypy have learned that the yaml module has type
stubs and the tool is now erroring out when it discovers we import
yaml since the stubs are not involved.

This is breaking CI on unrelated patches; fix it following the
instructions here:

https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-06-09 21:01:55 -04:00
Martí Bolívar bd122e5ea2 edtlib: validate compatible properties
Error out on compatible properties with invalid values. The regular
expression used to validate them matches what's used in dt-schema.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-05-19 22:18:25 -05:00
Martí Bolívar 2e0d95f3cd devicetree: add mypy-based type checking
Extend the steps taken in tox.ini by type checking the 'devicetree'
package. This will make it easier for callers to type-check code that
uses the low level DT module's public APIs, since they can rely on
the type checker a bit more.

It will also help avoid bugs by adding some type checking for future
changes to this module.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-05-05 13:13:12 -05:00
Martí Bolívar a8f7bfbbd4 edtlib: silence type checker issue
Mypy is complaining about this line for some reason I didn't have time
to figure out. Just shut it up for now; I'll look into this when I get
around to type annotating edtlib.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
2021-05-05 13:13:12 -05:00