OpenHarmony设备树DTS修改实战:从定位到编译验证全流程OpenHarmony Device Tree DTS Modification in Practice: The Full Flow from Location to Build Verification


AI free token/AI免费积分
OpenHarmony设备树DTS修改实战:从定位到编译验证全流程

鸿蒙开发越深入就越绕不开设备树。拿到新板子或外接不常见的传感器、屏幕、模组时,最后都会落到DTS怎么改。设备树本质是描述整块板子硬件资源的一张地图,内核启动时靠它找到内存多大、有几个UART、GPIO哪些引脚复用、I2C控制器地址、中断号。在OpenHarmony的ARM和RISC-V平台上,内核和硬件之间的信息沟通基本靠设备树完成。HDF驱动代码不会写死寄存器物理地址,而是通过设备树节点拿到,设备树信息错了驱动写得再对也跑不起来。

必须动DTS的场景:板级外设适配,官方板只引一路SPI但项目需要两路;屏幕点不亮调试,LCD上电时序、背光引脚、reset引脚、分辨率时序参数都定义在设备树节点里;内存和DDR参数调整;传感器接入,只需加I2C子节点填地址和中断引脚;调试引脚功能切换。

定位DTS:文件跟着内核版本走,路径在kernel/linux/linux-5.10/arch/arm64/boot/dts/或arch/arm/boot/dts/下,按厂商板型分多层目录。.dtsi是公共部分描述SoC内部资源,.dts是板级文件描述具体差异,多个.dtsi会叠加到.dts里。dts/dtsi是源文件,dtb是编译后二进制内核实际加载,dtbo是覆盖层运行时动态加载。dts/dtsi经dtc编译成dtb打包进boot_linux.img,所以改了DTS只编译内核不改打包boot镜像就还是老样子。可用make ARCH=arm64 xxx_defconfig后grep CONFIG_DEFAULT_DEVICE_TREE .config反查板型。

修改DTS场景:GPIO引脚复用最高频,pinctrl节点定义复用状态、外设节点pinctrl-0引用状态,function指定功能、bias-pull-up设上拉、status=okay使能。加I2C外设节点要写compatible驱动匹配关键字段、reg填7位I2C地址(高频坑,别填8位)、interrupt-parent指向pio、interrupts按芯片手册。调整内存改memory节点的reg格式(起始地址高32位、低32位、大小高32位、低32位)。时钟改assigned-clocks和assigned-clock-rates,时钟ID宏在include/dt-bindings/clock/下,写错编译不报错但运行时SPI时钟乱。

编译打包:单独编译dtbs验证语法,或hb build -f -T kernel。反编译dtb用dtc -I dtb -O dts核对内容是否真编进去。再hb build -f -T boot_linux_img重新打包镜像烧录;支持dtbo动态加载则只需覆盖到/vendor或/data分区。运行时验证用ls /proc/device-tree/、cat /proc/device-tree/model、dmesg | grep、HDF调试接口/sys/kernel/debug/hdf/。

避坑:同名节点被覆盖,后面解析的覆盖前面的,要反编译最终dtb看status;pinctrl与GPIO子系统打架报pin already requested;改了DTS外设挂死先查interrupt和reg(8位地址0xEC要填7位0x76);编译缓存导致昨天改的今天还原,用hb build -f强制全量;判断问题在DTS还是驱动,看/proc/device-tree/有节点且/sys/bus/platform/devices/有同名设备则匹配链路通,否则问题在驱动。

本文来源:https://www.hqwc.cn/a/1629023.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

The deeper you go into HarmonyOS development, the more you cannot avoid the device tree. When you get a new board or connect an uncommon sensor, screen or module, it ultimately comes down to how to modify the DTS. A device tree is essentially a map describing the hardware resources of the whole board; at boot the kernel relies on it to find how much memory there is, how many UARTs there are, which GPIO pins are multiplexed, I2C controller addresses and interrupt numbers. On OpenHarmony's ARM and RISC-V platforms, communication between the kernel and hardware is essentially carried out through the device tree. HDF driver code does not hard-code register physical addresses but obtains them from device tree nodes; if the device tree information is wrong, even a perfectly written driver will not run.

Scenarios that require modifying the DTS: board-level peripheral adaptation, such as when the official board routes only one SPI but the project needs two; debugging a screen that will not light up, where LCD power-on timing, backlight pins, reset pins and resolution timing parameters are all defined in device tree nodes; memory and DDR parameter adjustments; sensor connection, which only needs an I2C child node with the address and interrupt pin filled in; and switching debug pin functions.

Locating the DTS: the files follow the kernel version, under paths such as kernel/linux/linux-5.10/arch/arm64/boot/dts/ or arch/arm/boot/dts/, organised into multiple directory levels by vendor and board type. A .dtsi is the common part describing SoC-internal resources, while a .dts is the board-level file describing specific differences; multiple .dtsi files are layered into the .dts. dts/dtsi are source files, dtb is the compiled binary actually loaded by the kernel, and dtbo is an overlay loaded dynamically at runtime. dts/dtsi are compiled by dtc into dtb and packaged into boot_linux.img, so after modifying the DTS, compiling only the kernel without repackaging the boot image leaves everything unchanged. You can use make ARCH=arm64 xxx_defconfig followed by grep CONFIG_DEFAULT_DEVICE_TREE .config to work backwards to the board type.

DTS modification scenarios: GPIO pin multiplexing is the most frequent — the pinctrl node defines mux states, the peripheral node's pinctrl-0 references a state, function specifies the function, bias-pull-up sets a pull-up, and status=okay enables it. Adding an I2C peripheral node requires writing compatible, the key field for driver matching, filling reg with the 7-bit I2C address (a frequent pitfall — don't enter the 8-bit address), pointing interrupt-parent to pio, and setting interrupts according to the chip datasheet. To adjust memory, change the reg format of the memory node (start address high 32 bits, low 32 bits, size high 32 bits, low 32 bits). For clocks, modify assigned-clocks and assigned-clock-rates; the clock ID macros are under include/dt-bindings/clock/, and getting them wrong produces no build error but garbles the SPI clock at runtime.

Build and packaging: compile dtbs alone to verify syntax, or run hb build -f -T kernel. Decompile the dtb with dtc -I dtb -O dts to check whether the content was really compiled in. Then run hb build -f -T boot_linux_img to repackage the image and flash it; if dynamic dtbo loading is supported, you only need to copy the overlay to the /vendor or /data partition. Runtime verification uses ls /proc/device-tree/, cat /proc/device-tree/model, dmesg | grep, and the HDF debug interface at /sys/kernel/debug/hdf/.

Pitfalls to avoid: nodes with the same name are overwritten, with later-parsed ones overriding earlier ones, so decompile the final dtb to check status; conflicts between pinctrl and the GPIO subsystem produce "pin already requested"; if a peripheral hangs after a DTS change, check interrupt and reg first (an 8-bit address of 0xEC should be entered as the 7-bit 0x76); build cache can make yesterday's changes revert today, so use hb build -f to force a full build; to judge whether the problem lies in the DTS or the driver, if /proc/device-tree/ has the node and /sys/bus/platform/devices/ has a device of the same name, the matching chain is intact, otherwise the problem is in the driver.

本文来源:https://www.hqwc.cn/a/1629023.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry