<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mondher Mechri on Medium]]></title>
        <description><![CDATA[Stories by Mondher Mechri on Medium]]></description>
        <link>https://medium.com/@mondermechri2013?source=rss-b30a841332aa------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*emazyOkjuvX5Tsoo</url>
            <title>Stories by Mondher Mechri on Medium</title>
            <link>https://medium.com/@mondermechri2013?source=rss-b30a841332aa------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:34:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mondermechri2013/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Hardware Description Finally Left the Code]]></title>
            <link>https://medium.com/@mondermechri2013/how-hardware-description-finally-left-the-code-7e08aeff50ee?source=rss-b30a841332aa------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e08aeff50ee</guid>
            <category><![CDATA[device-tree]]></category>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <category><![CDATA[zephyrrtos]]></category>
            <dc:creator><![CDATA[Mondher Mechri]]></dc:creator>
            <pubDate>Sun, 01 Feb 2026 18:11:23 GMT</pubDate>
            <atom:updated>2026-02-01T19:15:57.081Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>If you’ve ever worked with embedded systems, you’ve probably experienced the frustration of a driver that only works on one board — or worse, a kernel that refuses to boot because of some obscure hardware mismatch.</p><p>For decades, embedded engineers hard-coded hardware details directly into drivers. Every new board meant rewriting code, hunting down addresses, interrupts, and clocks — a process so tedious that it could make even the most experienced engineer sigh.</p><p>Enter the <strong>Device Tree (DT)</strong>: a simple yet powerful solution that decouples hardware description from software logic. Suddenly, the same kernel could run on multiple boards without rewriting a single driver. Sounds magical? It is — and it’s also a little mysterious until you get your hands dirty.</p><p>In this article, we’ll walk through the <strong>Device Tree step by step</strong>, compare how Linux and Zephyr handle it, and give practical tips for writing and debugging your own DTS files.</p><h3>1. Why Do We Use the Device Tree?</h3><p>Embedded systems often run on custom hardware. Each board can have different peripherals, memory layouts, GPIOs, or communication buses. Hard-coding these details into drivers creates two big problems:</p><ol><li><strong>Poor portability</strong>: One driver per board. One kernel per board.</li><li><strong>Maintenance nightmare</strong>: Any hardware change requires code updates, testing, and possible bugs.</li></ol><p>The Device Tree solves this by <strong>describing the hardware in a separate, machine-readable format</strong>. The kernel or RTOS reads this description at boot time and configures drivers accordingly.</p><h3>1.1 What Problems Does Device Tree Actually Solve?</h3><ul><li><strong>Hardware–software decoupling</strong>: Drivers no longer need to know the exact addresses of peripherals.</li><li><strong>Board portability</strong>: A single kernel image can run on multiple boards.</li><li><strong>Reusable drivers</strong>: One driver can work for multiple devices if the DT describes them correctly.</li></ul><h3>2. Device Tree Fundamentals</h3><p>Before diving into DTS files, you need to understand a few basic concepts.</p><h3>2.1 Device Tree Basics: Nodes, Properties, and Hierarchy</h3><p>A Device Tree is a <strong>tree-like data structure</strong>. Each node represents a hardware component, and properties describe its configuration.</p><figure><img alt="Device Tree diagram showing UART, GPIO, I2C nodes, pinmux, and sensor on STM32 board." src="https://cdn-images-1.medium.com/max/415/1*J_3YmrFk9oDMdxbqO64bXA.png" /></figure><p>Example:</p><pre>uart0: serial@40011000 {<br>    compatible = &quot;st,stm32-usart&quot;;<br>    reg = &lt;0x40011000 0x400&gt;;<br>    interrupts = &lt;37&gt;;<br>};</pre><ul><li>duart0 → label</li><li>serial@40011000 → node name with base address</li><li>compatible → which driver should handle this hardware</li><li>reg → memory address and size</li><li>interrupts → hardware interrupt number</li></ul><h3>2.2 Understanding compatible Strings and Driver Binding</h3><p>The compatible string tells the kernel which driver to bind to the device. For example:</p><pre>compatible = &quot;st,stm32-usart&quot;;</pre><p>This matches the driver in the kernel that supports STM32 USART peripherals. If multiple drivers exist, the kernel chooses the first compatible match.</p><figure><img alt="Diagram showing driver binding flow using compatible strings to select correct driver." src="https://cdn-images-1.medium.com/max/580/1*YNQfm0Q2I7g3ewCV7mq_Rw.png" /></figure><h3>3. Device Tree Syntax and Structure</h3><h3>3.1 Device Tree Syntax: How DTS Files Are Written</h3><p>Device Tree Source (.dts) files are <strong>human-readable</strong>, and are compiled into <strong>Device Tree Blob (</strong><strong>.dtb)</strong> for the kernel.</p><ul><li>Nodes are enclosed in {}</li><li>Properties are key–value pairs</li><li>Comments use /* */</li></ul><p>Example:</p><pre>gpio1: gpio@50000000 {<br>    compatible = &quot;st,stm32-gpio&quot;;<br>    reg = &lt;0x50000000 0x1000&gt;;<br>    #gpio-cells = &lt;2&gt;;<br>};</pre><h3>3.2 Nodes, Properties, Labels, and Phandles Explained</h3><ul><li><strong>Nodes</strong>: hardware components (uart0, gpio1)</li><li><strong>Properties</strong>: configuration (reg, interrupts)</li><li><strong>Labels</strong>: easy reference (gpio1:)</li><li><strong>Phandles</strong>: pointers to other nodes for references</li></ul><h3>4. Device Tree Overlays</h3><p>Sometimes you want to <strong>modify the base Device Tree</strong> without rewriting it. That’s where overlays come in.</p><h3>4.1 What Is a Device Tree Overlay?</h3><p>An <strong>overlay</strong> is a small DTS file that <strong>modifies or extends</strong> an existing Device Tree.</p><p>Example: Enabling a new sensor on a development board.</p><pre>&amp;i2c1 {<br>    status = &quot;okay&quot;;<br>    temp_sensor: tmp102@48 {<br>        compatible = &quot;ti,tmp102&quot;;<br>        reg = &lt;0x48&gt;;<br>    };<br>};</pre><figure><img alt="Diagram showing overlay nodes being merged into base DTS tree to add new devices." src="https://cdn-images-1.medium.com/max/837/1*YI1LXTUmNxWzuLKD_QYAoA.png" /></figure><h3>4.2 Why Overlays Exist (and When You Really Need Them)</h3><p>Overlays allow flexibility:</p><ul><li>Add or remove devices at runtime</li><li>Customize hardware for different board variants</li><li>Keep the main DTS clean and generic</li></ul><h3>4.3 Applying Overlays at Boot Time vs Run Time</h3><ul><li><strong>Boot-time</strong>: DTB is patched before the kernel boots</li><li><strong>Run-time</strong> (Linux): Overlays can be applied dynamically with dtoverlay</li></ul><p>Zephyr currently supports compile-time overlays via prj.conf and DTS fragment inclusion.</p><h3>5. From Source to Binary: Device Tree Compilation</h3><h3>5.1 From DTS to DTB: How Device Tree Is Compiled</h3><pre>dtc -I dts -O dtb -o stm32.dtb stm32.dts</pre><ul><li>dtc → Device Tree Compiler</li><li>.dts → human-readable source</li><li>.dtb → binary blob for kernel consumption</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*4KWnqPWtLi8fEaGNplL5zA.png" /></figure><h3>5.2 The Role of dtc (Device Tree Compiler)</h3><p>dtc validates syntax and generates a binary blob that is easy for the bootloader and kernel to parse.</p><h3>5.3 Where the Device Tree Fits in the Boot Process</h3><ol><li>Bootloader loads DTB into memory</li><li>Kernel parses DTB at startup</li><li>Drivers are bound to hardware based on compatible strings</li></ol><h3>6. Device Tree from the Operating System Perspective</h3><h3>6.1 How Linux Uses the Device Tree</h3><ul><li>Kernel reads DTB at boot</li><li>Registers devices and drivers dynamically</li><li>Supports overlays for flexible hardware configurations</li></ul><h3>6.2 Device Tree in Zephyr RTOS: A Different Philosophy</h3><ul><li>Zephyr uses <strong>compile-time DT parsing</strong></li><li>DTS files are converted into <strong>C headers</strong> with macros</li><li>Drivers use macros instead of runtime parsing</li></ul><h3>6.3 Linux vs Zephyr: Same Device Tree, Different Rules</h3><figure><img alt="Diagram comparing Linux runtime DT vs Zephyr compile-time DT handling." src="https://cdn-images-1.medium.com/max/471/1*_oJyJoGVF4H9WqbGAnfyfg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4eEPkXdkk4xsNDyU4H8vaw.png" /></figure><h3>7. Design Boundaries and Best Practices</h3><h3>7.1 What Goes in Device Tree and What Stays in Code?</h3><p><strong>Device Tree</strong> → hardware addresses, interrupts, clocks, GPIO<br> <strong>Code / Driver</strong> → behavior, protocol, logic</p><h3>7.2 Common Mistakes When Writing Device Trees</h3><ul><li>Incorrect reg addresses</li><li>Missing or wrong compatible strings</li><li>Forgetting to enable clocks or GPIOs</li><li>Confusing labels and phandles</li></ul><h3>Conclusion</h3><h3>Device Tree Is Not Configuration — It’s a Hardware Contract</h3><p>Device Tree forces a clear <strong>separation between hardware and software</strong>. Once you get used to it:</p><ul><li>Your drivers become portable</li><li>Your kernel becomes modular</li><li>Your embedded life becomes slightly less painful</li></ul><p>In short, <strong>DT isn’t just a file — it’s a contract between your hardware and your software.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e08aeff50ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>