M5_CoreMP135_debian12_20240515
, M5_CoreMP135_buildroot_20240515
and above (enable CONFIG_OF_OVERLAY
support)1.The device tree plug-in is an addition based on the device tree. The syntax is exactly the same as the device tree. You can copy the previously written device tree nodes into the device tree plug-in. The device tree plug-in has a relatively fixed format. It can even be considered that it just adds a "shell" to the device node and the kernel can dynamically load it after compilation. The format is as follows, specific nodes are omitted.
/dts-v1/;
/plugin/;
/{
fragment@0 {
target-path = "/";
__overlay__ {
/*Add the node to be inserted here*/
.......
};
};
fragment@1 {
target = <&XXXXX>;
__overlay__ {
/*Add the node to be inserted here*/
.......
};
};
.......
};
Another device tree plug-in format: The writing method of this plug-in is the same as the formal dts format. The only difference is that the second line declares that this plug-in is applied in the form of a plug-in.
/dts-v1/;
/plugin/;
&{/} {
/*Here, under the root node "/", add the node or attribute to be inserted*/
};
&XXXXX {
/*Here, under the node "XXXXX", add the node or attribute to be inserted*/
};
1.Taking the CoreMP135 device as an example, write a device tree plug-in led-overlay.dts
that inserts the LED device node.
// file: led-overlay.dts
/dts-v1/;
/plugin/;
/{
fragment@0 {
target-path = "/";
__overlay__ {
leds {
compatible = "gpio-leds";
led-blue {
function = "heartbeat"; // LED_FUNCTION_HEARTBEAT
color = <3>; // LED_COLOR_ID_BLUE
gpios = <&gpioc 13 1>; // &gpioc 13 GPIO_ACTIVE_LOW
linux,default-trigger = "heartbeat";
default-state = "off";
};
};
};
};
};
2.Use the dtc tool to compile the device tree
dtc -I dts -O dtb -o led-overlay.dtbo led-overlay.dts
ls
led-overlay.dtbo led-overlay.dts
1.The boot file stored in the /boot
directory of the CoreMP135 root file system, where /boot/extlinux/extlinux.conf
is the boot script of uboot
. Through this script, you can set the device tree to be overlaid on startup.
label stm32mp135f-core135-buildroot
kernel/boot/zImage
devicetree /boot/stm32mp135f-core135.dtb
append root=/dev/mmcblk0p5 rw panic=5 quiet rootwait
2.What we need to pay attention to is the fdtoverlays
parameter, which is used to define the dtbo
device tree file that enables overlay. The specific operations are as follows:
# Compile the dtbo device tree file and transfer it to CoreMP135 (Note: The default root user may not have ssh permissions enabled. Please modify the configuration or create a new user for transfer)
scp led-overlay.dtbo root@192.168.2.52:/root
# Place the device tree file in the /boot boot directory.
cp /root/led-overlay.dtbo /boot
3.Set the dtbo
parameter in the boot file /boot/extlinux/extlinux.conf
label stm32mp135f-core135-buildroot
kernel/boot/zImage
devicetree /boot/stm32mp135f-core135.dtb
fdtoverlays /boot/led-overlay.dtbo
append root=/dev/mmcblk0p5 rw panic=5 quiet rootwait
4.Restart the device and we can see the led device we added in the /sys/class/leds directory.
1.Linux can also dynamically adjust the device tree during runtime. This method relies on the support of the dtbocfg.ko
module. The relevant precautions are as follows.
CONFIG_OF_OVERLAY
is turned on when compiling the kernel. dtbocfg.ko
kernel module. dtbocfg.ko
in the system2.Kernel compilation must be completed before cross-compiling the module. The module generates ko files through external compilation. (Please replace $PROJECT_PATH with the actual project path)
git clone https://github.com/m5stack/dtbocfg.git
cddtbocfg
make ARCH=arm KERNEL_SRC=$PROJECT_PATH/Core135_buildroot/output/build/linux-custom CROSS_COMPILE=$PROJECT_PATH/Core135_buildroot/output/host/bin/arm-buildroot-linux-gnueabihf- -j16
3.Place the generated dtbocfg.ko
in the path /lib/modules/$version/kernel/drivers/
, run depmod -a
to update the driver dependencies, and then load the module through the modprobe
command.
depmod -a
#Load dtbocfg module
modprobe dtbocfg
4.Copy the previously compiled led-overlay.dtbo
to the system and import it according to the following process.
# Mount the configs file system. If the system does not automatically mount the directory, you need to mount it manually.
# mount -t configfs none /sys/kernel/config
#Create loading directory
mkdir /sys/kernel/config/device-tree/overlays/leds
# After the creation is completed, two files dtbo, status will be automatically generated in the directory.
ls /sys/kernel/config/device-tree/overlays/leds
dtbo status
# Fill dtbo
cat led-overlay.dtbo > /sys/kernel/config/device-tree/overlays/leds/dtbo
# Start dtbo
echo 1 > /sys/kernel/config/device-tree/overlays/leds/status
After completing the above steps, the current device tree plug-in has been successfully imported and taken effect.
5.Check the PC13 pin status through an oscilloscope or build a simple LED circuit through a breadboard to check the implementation effect.