Skip to content
You Start with one site
Amounts in Start with one site
Embedded Systems · Concept

Device trees

One idea Advanced 5 min read
Found this useful?

A new worker arrives at a workshop and is given a list: which machines are here, where each one stands, and who runs it. Without the list the machines are still there, and the worker cannot use them. A device tree is that list for Linux on a small board: a description of the hardware on the board, where each part is, and which driver runs it.

The sensor is brokenA bus tool finds the chip at its address.Not the hardware
The driver is missingThe driver is in the kernel. Nothing has started it.Close
Nobody told the kernelThe description of the board has no entry for the sensor.This is the device tree

See it

Predict first

A BME280 temperature sensor is wired correctly to the I2C pins of a small Linux board, and it has power. Nobody has changed the device tree of the board. A program asks Linux for the temperature. What happens?

See it: what the tree says, and what the kernel then does

I2C bus
Driver for the sensor
A program reads

The boxes on the left are the tree: each node sits under the bus it is wired to. The real chip is on the board in every case. Change the tree with the three buttons and see what the kernel does with the same hardware.

The idea

A sensor on the two wires of an I2C bus does not announce itself, so the kernel, the core of Linux, must be told. The device tree is a file that the kernel reads when it starts.

The tree is made of nodes, one for each device or bus, and a device sits under the bus it is wired to. A node holds properties, and three do most of the work. The text in compatible names the device, and the kernel starts the driver (the code that runs one kind of hardware) that lists the same text. Then reg is the address, and status is okay or disabled.

The maker of the board supplies the tree. You add an overlay: a small patch that changes a status or adds a node. The lesson on device trees and hardware description writes one for this sensor.

The tree describes the board and does not check it. If the node says address 0x76 and the chip is wired at 0x77, the kernel believes the node, and the driver finds nothing. Hardware that can announce itself, such as a USB device, needs no node.

One worked number

The question

The tree in the figure can be right or wrong in three places: the status of the bus, the sensor node, and the compatible text. How many combinations are there, and how many give a reading?

Show the worked solution, 3 steps
  1. Count the combinations

    Each of the three has 2 values: 2 × 2 × 2 = 8.

  2. Remove those with the bus disabled

    Half of the 8 have the bus disabled: 4. The kernel uses nothing below a disabled bus.

  3. Follow the tree downwards

    With the bus okay, 2 combinations have no sensor node, and 1 more has the node with a wrong compatible text. 8 − 4 − 2 − 1 = 1: one combination of the eight gives a reading. Check it in the figure.

The mistake to avoid

Wiring a device does not make Linux see it. On a microcontroller your own program talks to the chip, so correct wiring is enough. Linux uses only what the tree describes. When a sensor with correct wiring "is not there", compare the tree with the board before you replace the sensor.

A second trap: a wrong compatible text gives no error. The node appears in the running tree and no driver starts, so compare the text letter by letter with the driver.

Check yourself

Question 1

A wiring document gives the bus address of a sensor as 118 in decimal. Which line in its node is right?

Question 2

A new sensor is wired to a Linux data logger. A bus tool finds the chip at its address, but no reading appears, and the running tree has no node for the sensor. A colleague wants to order another sensor. What do you do first?

Question 3

Other devices on the bus work. The node of the new sensor appears in the running tree with status okay. No driver starts, and no error is printed. What is the most likely cause?

Where you meet it

  • A gateway at a remote siteThe serial port that reads the energy meters works only after the tree enables it.
  • A panel computer with a touch screenThe screen and its touch controller are nodes. A wrong node gives a dark screen on good hardware.
  • A sensor added to a data loggerOne overlay with one node, and the reading appears as a file.
Key points
  • The kernel uses only the hardware that the device tree describes.
  • A node names its device with compatible, gives its address with reg, and is used only when its status, and that of the bus above it, is okay.
  • The tree is believed, not checked. It must agree with the real board.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.