A library for parsing Devicetree Source v1
Slowing down. Last release over 3 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install pydevicetreepydevicetree
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']>