Skip to main content

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;
        };
};

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.12.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydevicetree-0.0.12-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file pydevicetree-0.0.12.tar.gz.

File metadata

  • Download URL: pydevicetree-0.0.12.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.1

File hashes

Hashes for pydevicetree-0.0.12.tar.gz
Algorithm Hash digest
SHA256 1ca19ec38bec0095a0a8dc4753a578e8c6e9a70873112547d30c92b32411f667
MD5 69c03ab7a5c4ee1e941cfb1f0d82a187
BLAKE2b-256 a183c7e95b50619168ccf4c7c86a2eb6bd6896ff2dde021975b5f889eec5fddf

See more details on using hashes here.

File details

Details for the file pydevicetree-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: pydevicetree-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 26.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.1

File hashes

Hashes for pydevicetree-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4084c448e2cdf7c0a5370d71391560f5b46b85cec9189d1d2df8080b139b0b24
MD5 14fbe58d12ac2c02e2f14f1de6136b7f
BLAKE2b-256 b67260e987aa90e95474bdb9e8834a9dc92ea93f5dde20d132b7c4fc756cbd87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page