A library for parsing Devicetree Source v1
Project description
pydevicetree
This is a Python 3 library for parsing, querying, and modifying Devicetree Source v1 files as described in the Devicetree Specification v0.2.
Tutorial
The Devicetree
Let's say you have a file design.dts with the contents
/dts-v1/;
chosen {
stdout-path = "/soc/uart@10000000:115200";
};
/ {
#address-cells = <1>;
#size-cells = <1>;
compatible = "my,design";
cpus {
cpu@0 {
#size-cells = <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").get_field("timebase-frequency")
[<Node cpu>]
Getting Node
properties
>>> tree.match("sifive,rocket0")[0].get_field("timebase-frequency")
1000000
>>> tree.match("sifive,rocket0")[0].get_fields("compatible")
<StringList ['sifive,rocket0', 'riscv']>
Getting chosen
properties
>>> 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 print
ing 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;
};
};
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydevicetree-0.0.3.tar.gz
(15.2 kB
view hashes)
Built Distribution
Close
Hashes for pydevicetree-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31ef5af7803a6842f221efdb25329f28ede6b608379a3095317ffc57d5cd13ca |
|
MD5 | 806eaacb19f135c4acf49a4e4340e6c5 |
|
BLAKE2b-256 | 8aae1454904299d75e17501cc9bb808ff7212c9247cc427d54d5c874cc71f00f |