Device trees
On this page
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.
See it
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?
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 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
- Count the combinations
Each of the three has 2 values: 2 × 2 × 2 = 8.
- Remove those with the bus disabled
Half of the 8 have the bus disabled: 4. The kernel uses nothing below a disabled bus.
- 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
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
A wiring document gives the bus address of a sensor as 118 in decimal. Which line in its node is right?
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?
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.
- 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.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.