Definition
A devicetree is a text description of a system’s hardware, arranged as a tree of nodes. Each node stands for one piece of hardware, such as a processor, a memory region, a bus or a serial port, and holds properties: named values such as the address of the device’s registers, its interrupt §6.11 number, the pins it uses and whether it is enabled. Software reads these facts from the description instead of fixing them in code. The Devicetree Specification, published by devicetree.org, defines the format; Linux and Zephyr §6.33 both use it.
The idea
One application should run on two boards. On board A the serial port’s transmit line leaves the chip on pin 6. Board B uses a different chip: its serial port’s registers sit at another address, and the transmit line leaves on pin 20. If the driver contained these numbers, every board would need its own copy of the driver. A devicetree moves the numbers out of the code into a description. The driver is written once and asks the description for its register address and its pins, and each board supplies its own description.
The description has the shape of the hardware. A chip contains buses, buses connect devices, and a device can have devices attached to it, such as a sensor on a bus, so a tree fits. A wiring diagram kept next to a machine is a fair picture: technicians read it instead of tracing the wires. The picture stops at one point. In Zephyr the build turns the devicetree into constants inside the program, so the running firmware contains no tree to consult, and a change to the description takes effect only after a rebuild. Linux, by contrast, receives a compiled tree from the boot loader §6.30 when it starts and reads it at run time.
A devicetree describes hardware; it does not choose software. Whether a UART §6.23 (universal asynchronous receiver-transmitter) driver, a console or a logging system is built into the firmware is decided by Kconfig §6.40, Zephyr’s configuration system for software features.
How it works
A small piece of a devicetree source file looks like this:
serial0: serial@40002000 {
compatible = "vendor,uart";
reg = <0x40002000 0x1000>;
interrupts = <2 1>;
current-speed = <115200>;
status = "okay";
};
Node:
serial@40002000is a node namedserialwhose unit address, here the address of its registers, is0x40002000.Label:
serial0is a short name by which other files refer to the node, written&serial0.Property: a name with a value. A value is a string in quotes or a list of numbers in angle brackets.
compatiblenames the kind of device. It is the key that matches the node to a binding and to a driver.reggives the register address and the size of the register block, here0x1000, which is 4096 bytes.interruptsgives, here, the interrupt number and its priority; the binding of the interrupt controller defines what the numbers mean.statussays whether the device is in use:"okay"means enabled and"disabled"means present but unused. A node withoutstatuscounts as enabled.
A board’s description is spread over several files.
A
.dtsifile describes one chip, a system on chip §6.2 (SoC), and every board that uses that chip includes it. Its device nodes are often marked disabled.The board’s
.dtsfile includes the chip file and adds what the board adds: which devices are enabled, which pins they use, and what else is soldered on the board.Overlay files, with the extension
.overlay, are fragments that an application adds on top for one build.Bindings are files in YAML (YAML Ain’t Markup Language), a text format for structured data. Each binding states, for one
compatiblevalue, which properties a node of that kind must or may have, and the type of each.
Zephyr builds the final description in this order.
The C preprocessor, the program that expands
#includelines and macros before compilation, joins the board file, the files it includes and the selected overlays, in that order, into one text.Zephyr’s devicetree scripts parse that text. A node that appears more than once is merged into one node. A property assigned again replaces the earlier value, so the last assignment wins. The commands
/delete-property/and/delete-node/remove items.Each node is checked against the binding for its
compatiblevalue: required properties must be present, and values must have the declared types.The scripts write the final tree to
build/zephyr/zephyr.dtsand a header file of C macros, names that the preprocessor replaces with values before compilation, todevicetree_generated.h.Code reads the tree only through those macros.
DT_NODELABEL(serial0)names the node;DT_PROP(DT_NODELABEL(serial0), current_speed)gives the number 115200, a hyphen in a property name being written as an underscore in macro arguments; andDEVICE_DT_GET()gives the driver’s device object for a node.
Which overlays a build uses is decided by the build’s selection rules, not by which files exist; the board overlay §6.41 entry gives the rules. The build prints one line containing Found devicetree overlay for each overlay it uses.
Worked example
A board file defines the serial port as in the listing above: enabled, at 115200 baud, meaning 115200 bits per second. An application selects two overlays, first a.overlay and then b.overlay:
a.overlaycontains&serial0 { current-speed = <9600>; };b.overlaycontains&serial0 { current-speed = <1000000>; };
The scripts merge the three definitions of the node. current-speed is assigned three times: 115200 in the board file, 9600 in a.overlay and 1000000 in b.overlay. The last assignment wins, so zephyr.dts shows 1000000, and DT_PROP(DT_NODELABEL(serial0), current_speed) expands to 1000000. The other properties come unchanged from the board file. Selecting the two overlays in the opposite order gives 9600 instead. The number matters: one 10-bit byte takes at 115200 baud, at 1000000 baud and at 9600 baud, and a device at the other end of the wire that expects a different speed receives garbage.
A tree can be valid and still wrong. Suppose the board’s transmit wire is soldered to pin 6, but an overlay assigns the serial port’s transmit signal to pin 13. The binding check passes, because 13 is a valid pin number. The firmware then drives pin 13, exactly as its description says, and pin 6, where the wire is, stays silent. No tool can find this mistake in the tree alone, because the tree does not know where the wires are.
Stated precisely
The final hardware description of a Zephyr build is the tree obtained by reading, in order, the board’s .dts file with everything it includes, and then each selected overlay in the order in which the build selects them. Within that sequence, the final value of a property is the value of its last assignment, unless a later /delete-property/ removes the property. A node exists if some file defines it and no later /delete-node/ removes it. The build stores this tree in build/zephyr/zephyr.dts. Only this tree, not any input file, records the description that the firmware was built with.
A device node takes effect in the firmware only when all of the following hold: its status is "okay" or absent; a binding exists for its compatible value; the driver for that kind of device is compiled in, which Kconfig decides; and the driver’s initialization succeeds at start-up, which code can test with device_is_ready().
Limits and common mistakes
A devicetree states how the hardware is wired; it never proves it. Its values must be checked against the board’s schematic, the drawing of the board’s wiring.
An enabled node does not put a driver into the firmware. In Zephyr, Kconfig decides which drivers are compiled in. Many drivers are switched on by default when an enabled node of their kind exists, but a configuration can still switch them off.
An overlay file in the application’s folder changes nothing unless the build selects it. Check the build’s list of found overlays, or the final zephyr.dts, not the folder.
The checks against bindings catch missing properties and values of the wrong type. They do not catch two nodes that use the same pin, or a speed that the device at the other end of the wire does not expect.
Because Zephyr turns the tree into constants at build time, a deployed firmware image cannot be given a different hardware description. A change needs a new image.
Linux and Zephyr share the format but not the moment of use: Linux reads a compiled tree, the devicetree blob, at start-up, while Zephyr uses the tree only during the build. A statement about devicetree handling in one system does not carry over to the other unchecked.
Questions answered
How does an overlay change the final hardware description?
An overlay changes the final hardware description by being applied after the board’s own description, so that whatever the overlay assigns replaces what came before. A devicetree is the text description of the hardware, a tree of nodes with properties such as addresses, pins and speeds, and an overlay is a fragment of that text that an application adds for one build. The build reads the board’s file first and then each selected overlay in order. An overlay can change a property of an existing node, which it names by its label, as in &serial0. The last assignment of a property wins, so a later overlay beats an earlier one and every overlay beats the board file. An overlay can also add properties, nodes and child nodes, add names for nodes, enable or disable a device through its status property, and remove properties or nodes with /delete-property/ and /delete-node/.
The merged tree is checked against the bindings, the files that list each device kind’s properties, and is turned into C macros that the drivers use, so the overlay’s values become constants in the firmware. Zephyr writes the merged tree to build/zephyr/zephyr.dts. That file, not the overlay files in the application folder, shows what the firmware was built with. An overlay that the build did not select changes nothing.
Related entries: Kconfig (§6.40), Board overlay (§6.41), Development kit versus dongle (§6.5), Device driver (§6.38). Further reading: [17].