Section 6.41

Board overlay

Definition

A board overlay is a devicetree §6.39 file that an application adds on top of a board’s own hardware description to change that description for one build. A devicetree is a text description of the hardware as a tree of nodes, one per device, with properties such as register addresses, pin assignments, speeds and whether each device is enabled. An overlay can enable or disable devices, change properties and add devices wired to the board. Zephyr §6.33 merges the overlay in at build time; the board’s files stay unchanged.

The idea

A developer connects a sensor to two spare pins of a development board §6.5 and wants a third pin to pulse at each event, so that an oscilloscope can time the firmware. The board’s description comes from the board’s maker and is shared by every application that targets the board. Editing it would change every application, and the edit would be lost at the next update. An overlay keeps the change with the one application that needs it.

A transparent sheet laid over a printed map is a fair picture: the map shows through, and whatever is drawn on the sheet covers what lies beneath. The picture stops at two points. First, a later value for the same property replaces the earlier one completely; nothing of the old value shows through. Second, two drawings in different places never collide on a map, but two devices in different nodes can name the same physical pin. Both values then survive the merge, and no step of the build notices that one pin has been promised to two devices.

How it works

Zephyr decides which overlay files a build uses as follows; the files are looked for in the application’s folder.

  1. If the build variable DTC_OVERLAY_FILE is set, exactly the files it lists are used, in the order listed.

  2. Otherwise the build looks for an overlay in the socs folder named after the chip, one in the boards folder named after the board, and one in the boards folder named after the board and its revision, and uses every one of these that exists.

  3. If none of those exists, it uses the overlay named after the board in the application folder itself, or, failing that, app.overlay.

  4. Files listed in EXTRA_DTC_OVERLAY_FILE are added after all of the above, so their assignments come last and win.

Shields, add-on boards that plug onto the main board, bring overlays of their own. The build prints one line containing Found devicetree overlay for each overlay it uses.

The build then reads the board’s description and the selected overlays in that order. Overlays refer to existing nodes by their labels, as in &uart0. When a property is assigned more than once, the last assignment wins, without an error or a warning. The merged tree is checked against bindings, the files that list which properties each kind of device must or may have and their types, and is turned into constants that the drivers read.

A pin reaches a device through the chip’s pin multiplexer, the hardware that connects each pin to one of several functions: a GPIO §6.22 (general-purpose input/output) line that software sets high or low directly, or a signal of a peripheral, such as the transmit line of a UART §6.23 (universal asynchronous receiver-transmitter). Chips do this in two ways.

In Zephyr, pin assignments are properties of pin-control nodes in the devicetree, and each driver applies its assignments when it initializes its device at start-up.

Worked example

A board’s description routes the UART’s transmit signal to pin P0.06, that is, port 0, pin 6, at 115200 baud. Two overlays, each correct on its own, are selected for one build:

The two claims sit in different nodes, so both survive the merge, the bindings accept both, and the build succeeds.

Count the active claims on P0.06 in the final tree: the UART node is enabled and claims the pin, and the marker node is enabled and claims it, which makes 2 claims on a pin that can carry 1 signal. At start-up both drivers configure the pin. Depending on the chip, the marker never appears on the pin, the log output never appears, or the two signals mix. A mix would not be rare: one 10-bit UART frame takes 10/115200s≈86.8μs10 / 115200\ \text{s} \approx 86.8\ \mu\text{s}, so a 40-byte log line occupies the pin for 40×86.8≈3472μs40 \times 86.8 \approx 3472\ \mu\text{s}, about 3.5 ms, during which the 1 ms marker changes state 3 or 4 times. The frames of a line follow one another without gaps, so every marker change that reaches the pin while the line is being sent falls inside some frame and corrupts it.

A second kind of collision involves one property. a.overlay sets the UART’s pin-assignment property so that the transmit signal uses P0.06, and b.overlay sets the same property so that it uses P0.26. With both selected, in the order a.overlay then b.overlay, the final tree says P0.26, and the build gives no warning. An application written for the wiring of a.overlay then sends its output to a pin with no wire.

The repair is to give each pin one owner: move the marker to a free pin, or disable one of the two nodes with status = "disabled". If one pin really must serve two functions, the sharing has to be explicit: one driver releases the pin, by stopping its device and returning the pin to an unused state, before the other driver connects it, and the code that switches between them makes sure the two uses never overlap in time.

Stated precisely

For each pin pp, let C(p)C(p) be the set of enabled nodes in the final tree whose properties route a signal through pp, and let |C(p)||C(p)| be the number of nodes in that set. The final tree is free of pin conflicts when |C(p)|≤1|C(p)| \le 1 for every pin pp, or when the nodes that share a pin are used under an explicit hand-over, in which each driver releases the pin before another connects it.

Collisions on one property follow the merge rule: among the board’s files and the selected overlays, read in the build’s order, the last assignment wins. Zephyr’s binding checks examine one node at a time, so they cannot test the condition on |C(p)||C(p)|, which concerns several nodes; and no check compares the tree with the board’s actual wiring.

Limits and common mistakes

An overlay file in the folder is not proof that the build used it. The build’s list of found overlays and the final zephyr.dts are.

An overlay written for one board, or for one revision of a board, can be wrong for another whose wiring differs. Zephyr’s revision-specific overlay files let each revision have its own.

A pin can be claimed outside the devicetree’s nodes as well: wired on the board to a button or an LED (light-emitting diode), used by the debug connection, or written by code that sets the pin registers directly. Using such a pin for another signal also lights the LED or fights the button.

When two output drivers, inside or outside the chip, drive one wire to opposite levels at the same time, a large current flows between them. The signal is then undefined, and the parts can be damaged.

A label or an alias is only a name: it neither adds hardware nor protects a pin from other users.

The merge rule is silent. An overlay that overrides an earlier value gets no warning, so a review must compare the final tree with what each overlay intended.

Questions answered

What happens if two overlays assign incompatible uses to one pin?

In most cases the build succeeds, and the conflict shows up only when the firmware runs. An overlay is a devicetree file, a fragment of the text description of the board’s hardware, that an application adds on top of the board’s own description; the build merges the board’s description and the selected overlays in a fixed order. Two cases differ.

The fix is one owner per pin: move one function to a free pin, or disable one of the nodes. A pin that must serve two functions needs an explicit hand-over in code, so that one driver releases the pin before the other takes it. To find such conflicts, list the pins of every enabled node in the final zephyr.dts and compare them with each other and with the board’s wiring diagram.

Related entries: Devicetree (§6.39), Development kit versus dongle (§6.5), GPIO (§6.22). Further reading: [17].