pydevicetree
This is a Python 3 library for parsing, querying, and modifying Devicetree Source v1 files as described in the Devicetree Specification v0.2.
Install
pydevicetree supports Python >= 3.5 and can be installed with pip from the Python Package Index.
pip install pydevicetree
Tutorial
The Devicetree
Let's say you have a file design.dts with the contents
/dts-v1/;
/ {
#address-cells = <1>;
#size-cells = <1>;
compatible = "my,design";
aliases {
serial0 = "/soc/uart@10000000";
};
chosen {
stdout-path = "/soc/uart@10000000:115200";
};
cpus {
#address-cells = <1>;
#size-cells = <0>;
cpu@0 {
compatible = "sifive,rocket0", "riscv";
device_type = "cpu";
reg = <0>;
riscv,isa = "rv32imac";
status = "okay";
timebase-frequency = <1000000>;
sifive,dtim = <&dtim>;
interrupt-controller {
#interrupt-cells = <1>;
compatible = "riscv,cpu-intc";
interrupt-controller;
};
};
};
soc {
#address-cells = <1>;
#size-cells = <1>;
compatible = "my,design-soc";
ranges;
dtim: dtim@20000000 {
compatible = "sifive,dtim0";
reg = <0x20000000 0x10000000>;
reg-names = "mem";
};
uart: uart@10000000 {
compatible = "sifive,uart0";
reg = <0x10000000 0x1000>;
reg-names = "control";
};
};
};
Parsing the Tree
Parsing the tree is as easy as 1, 2...
>>> from pydevicetree import Devicetree
>>> tree = Devicetree.parseFile("design.dts")
>>> tree
<Devicetree my,design>
Querying the Tree
By compatible string
>>> tree.match("sifive,rocket0")
[<Node cpu>]
By path
>>> tree.get_by_path("/soc/dtim")
<Node dtim@20000000>
Devicetree aliases are allowed in paths
>>> tree.get_by_path("serial0")
<Node uart@10000000>
Getting Node properties
The value (or first value of a list/array) of a property can be retrieved with Node.get_field()
>>> tree.match("sifive,rocket0")[0].get_field("timebase-frequency")
1000000
The list or array of values assigned to a property can be retrieved with Node.get_fields()
>>> tree.match("sifive,rocket0")[0].get_fields("compatible")
<StringList ['sifive,rocket0', 'riscv']>
There are helper methods Node.get_reg() and Node.get_ranges() for the reg and ranges
Devicetree properties.
>>> tree.get_by_path("/soc/dtim").get_reg()
<RegArray [536870912, 268435456]>
>>> tree.get_by_path("/soc/dtim").get_reg().get_by_name("mem")
(536870912, 268435456)
>>> "0x%x" % tree.get_by_path("/soc/dtim").get_reg().get_by_name("mem")[0]
'0x20000000'
Getting chosen properties
Devicetree.chosen() provides quick access to the properties of the chosen node
>>> tree.chosen("stdout-path")
<StringList ['/soc/uart@10000000:115200']>
Converting back to Devicetree
Any tree or subtree can be converted back to Devicetree by calling Node.to_dts() or simply
by printing it:
>>> print(tree.match("sifive,rocket0")[0])
cpu@0 {
#size-cells = <0>;
compatible = "sifive,rocket0", "riscv";
device_type = "cpu";
reg = <0x0>;
riscv,isa = "rv32imac";
status = "okay";
timebase-frequency = <1000000>;
sifive,dtim = <&dtim>;
interrupt-controller {
#interrupt-cells = <1>;
compatible = "riscv,cpu-intc";
interrupt-controller;
};
};
Metadata
Release files for pydevicetree 0.0.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydevicetree-0.0.13.tar.gz | 30.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydevicetree-0.0.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 57.1 kB
Release files / pydevicetree-0.0.13.tar.gz
| Download URL | pydevicetree-0.0.13.tar.gz |
|---|---|
| Size | 30.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5700c05df89bad8fd729c11aa6f764a3323bcb3796f13b32481ae34445cfc1b7
|
|
BLAKE2b-256 checksum How to use checksums |
9220342f3a463a20b4103fc9e7d0f0eb3534c638ac75d54326a6c19473879070
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / pydevicetree-0.0.13-py3-none-any.whl
| Download URL | pydevicetree-0.0.13-py3-none-any.whl |
|---|---|
| Size | 26.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d61c695cec925b90a8b5740053f4b604e51154a9b36e62a2f12ed9ceaf2f8c38
|
|
BLAKE2b-256 checksum How to use checksums |
9e37b74a7a0e75eac002799b2e88b9023d79cd47a5e54b0f6854eecc19eba5b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|