2026-09-18 · Embedded / rtos

zephyr-DTS实战

我们要怎么从零开始构建自己板子的设备树文件呢,最近正好在移植一个飞控项目到zephyr,同时为其增加MCUboot,所以来记录一下。

最终的文件结构:

boards/theflyguy/theflyguy_f411/
├── board.yml                                    板级元数据:名字 / 厂商 / SoC / 变体
├── Kconfig.theflyguy_f411                        BOARD_THEFLYGUY_F411 → select SOC_STM32F411XE
├── board.cmake                                   烧录器注册(DAPLink 走 openocd)
│
├── theflyguy_f411_stm32f411xe.dts                基础板级设备树 —— 整块板的硬件描述
├── theflyguy_f411_stm32f411xe.yaml               twister 测试框架元数据
├── theflyguy_f411_stm32f411xe_defconfig          板级 Kconfig 默认值
│
├── theflyguy_f411_stm32f411xe_mcuboot.dts        变体:include 基础 dts 后叠加分区表
├── theflyguy_f411_stm32f411xe_mcuboot.yaml       twister 元数据(变体)
├── theflyguy_f411_stm32f411xe_mcuboot_defconfig  变体 Kconfig
│
├── dts/
│   └── bindings/
│       ├── vendor-prefixes.txt                   注册 theflyguy 厂商前缀
│       ├── misc/
│       │   └── theflyguy,ext-headers.yaml        扩展排针引脚组 binding
│       └── motor/
│           └── theflyguy,motors.yaml             四路电机 binding
│
└── support/
    └── openocd.cfg                               CMSIS-DAP 探针 + SWD + stm32f4x

1. board.yml:从我是谁开始

board.yml 定义板级元数据:名字、厂商、用哪个 SoC、有哪些变体。

board:
  name: theflyguy_f411
  full_name: TheFlyGuy MiniFly F411
  vendor: theflyguy
  socs:
    - name: stm32f411xe
      variants:
        - name: mcuboot
  • name:命令行使用的板名。
  • full_name:给人看的完整名称。
  • vendor:厂商分类,只是个目录名,不是构建命令的一部分。
  • socs:这块板可以使用哪些 SoC。
  • variants:同一硬件的不同构建方式。

我想要编译后的结果有两个版本。一个正式版,会将mcuboot烧进去;另一个用于调试,直接烧写源代码,不会烧写mcuboot。两个目标的链接地址不一样,所以需要写两个构建目标。主目标是调试版,用 variants 引入第二个构建目标。

所以 Zephyr 实际识别出两个完整 target:

  • theflyguy_f411/stm32f411xe
  • theflyguy_f411/stm32f411xe/mcuboot

构建命令:

west build -b theflyguy_f411
west build -b theflyguy_f411/stm32f411xe/mcuboot --sysbuild

构建变体时 使用的名字是 板名/SoC/variant。不要包含 vendor。

2. `Kconfig.xxx:告诉 Kconfig 使用哪个 SoC

例如我的 Kconfig.theflyguy_f411:

config BOARD_THEFLYGUY_F411
    select SOC_STM32F411XE

根据kconfig语法,此处意为选择BOARD_THEFLYGUY_F411 这块板,就必须选择 STM32F411xE SoC 支持。后者会拉进 STM32F411 的时钟、GPIO、外设驱动等一整套支持。

3. 编写 DTS

根据规范编写DTS,之前有一篇详细介绍DTS的文章 zephyr-DTS讲解 | GoDm@’s Blog,此处不再赘述。

唯一需要说的就是,如果你有 DTS 语法解析,需要配置好inc路径,例如我这里用的是 devicetree 插件,需要在 .vscode/setting.json 这样配置

{
    "devicetree.defaultIncludePaths": [
     "${workspaceFolder}/../../zephyr/include",
     "${workspaceFolder}/../../zephyr/dts", 
     "${workspaceFolder}/../../zephyr/dts/arm",
     "${workspaceFolder}/../../zephyr/dts/common",
     "${workspaceFolder}/../../modules/hal/stm32/dts",
   ],

    "devicetree.defaultZephyrBindings": [
    "${workspaceFolder}/../../zephyr/include",
    ]

}
/*
 * TheFlyGuy · MiniFly F411 飞控板级设备树(STM32F411xE target)
 *
 * MCU: STM32F411CEU6 (LQFP48, 512K Flash / 128K RAM, 8MHz HSE → 96MHz)
 *
 * 引脚来源:doc/Firmware_F411 V1.3/STMpin分配表.md 及原固件
 *           HARDWARE/src/{led,motors,i2c_drv,uart1,uart_syslink,spi}.c
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/dts-v1/;

/* 48 脚 CEU6 封装自己的 pinctrl,取代原来借用 DISCO 的 100 脚 V 封装版本 */
#include <st/f4/stm32f411Xe.dtsi>
#include <st/f4/stm32f411c(c-e)ux-pinctrl.dtsi>
#include <zephyr/dt-bindings/gpio/gpio.h>
#include <zephyr/dt-bindings/i2c/i2c.h>
#include <zephyr/dt-bindings/pwm/pwm.h>
#include <freq.h>

/ {
	model = "TheFlyGuy MiniFly F411";
	compatible = "theflyguy,theflyguy-f411";

	chosen {
		/* 扩展 UART1 排针(PA15/PB3),原固件用作摄像头串口 */
		zephyr,console = &usart1;
		zephyr,shell-uart = &usart1;
		zephyr,sram = &sram0;
		zephyr,flash = &flash0;
		/*
		 * 基础变体不设 zephyr,code-partition:应用从 0x08000000 起,
		 * 整个 512K 可用。需要 A/B 升级请用 mcuboot 变体。
		 */
	};

	/* 五路状态 LED。灯语定义见原固件 HARDWARE/interface/led.h */
	leds {
		compatible = "gpio-leds";

		led_blue_l: led_blue_l {	/* 充电指示 CHG_LED, 高电平点亮 */
			gpios = <&gpiob 12 GPIO_ACTIVE_HIGH>;
		};

		led_green_l: led_green_l {	/* 无线接收 DATA_RX_LED, 低电平点亮 */
			gpios = <&gpioa 6 GPIO_ACTIVE_LOW>;
		};

		led_red_l: led_red_l {		/* 无线发送 DATA_TX_LED / ERR1 */
			gpios = <&gpioa 7 GPIO_ACTIVE_LOW>;
		};

		led_green_r: led_green_r {	/* 系统状态 SYS_LED / 传感器校准 */
			gpios = <&gpioc 13 GPIO_ACTIVE_LOW>;
		};

		led_red_r: led_red_r {		/* 低电量 LOWBAT_LED / ERR2 */
			gpios = <&gpioc 14 GPIO_ACTIVE_LOW>;
		};
	};

	/*
	 * 四路有刷电机。纯数据容器,没有驱动绑定到 "theflyguy,motors";
	 * 应用通过 PWM_DT_SPEC_GET() 拿到底层 PWM 通道自行驱动。
	 *
	 * 周期 2667ns @ 96MHz 定时器时钟 = 256 计数 ≈ 375kHz,
	 * 与原固件 96MHz/256 的 8bit PWM 一致。
	 *
	 * 通道号是 1 基:1 = CH1 ... 4 = CH4(0 非法)。
	 */
	motors {
		compatible = "theflyguy,motors";

		motor_m1: motor_m1 {		/* PB7  TIM4_CH2 */
			pwms = <&pwm4 2 2667 PWM_POLARITY_NORMAL>;
		};

		motor_m2: motor_m2 {		/* PB6  TIM4_CH1 */

			pwms = <&pwm4 1 2667 PWM_POLARITY_NORMAL>;
		};

		motor_m3: motor_m3 {		/* PB10 TIM2_CH3 */
			pwms = <&pwm2 3 2667 PWM_POLARITY_NORMAL>;
		};

		motor_m4: motor_m4 {		/* PA5  TIM2_CH1 */
			pwms = <&pwm2 1 2667 PWM_POLARITY_NORMAL>;
		};
	};

	/*
	 * 扩展排针上没有硬件外设的引脚。
	 *
	 * 注意 PB5 在本封装上没有 I2C_SCL 复用功能,所以那颗"扩展 I2C"
	 * 是位翻转接口(原固件用 HARDWARE/src/myiic.c),不能建成硬件 I2C 总线。
	 * PB13/PB14/PB15 是真正的硬件 SPI2,见下方 &spi2。
	 * PB3/PA15 属于 &usart1,不在这里重复声明。
	 */
	ext-headers {
		compatible = "theflyguy,ext-headers";

		ext_i2c_sda: ext_i2c_sda {
			gpios = <&gpiob 4 GPIO_ACTIVE_HIGH>;
		};

		ext_i2c_scl: ext_i2c_scl {
			gpios = <&gpiob 5 GPIO_ACTIVE_HIGH>;
		};

		ext_cs0: ext_cs0 {
			gpios = <&gpioc 15 GPIO_ACTIVE_LOW>;
		};

		ext_cs1: ext_cs1 {
			gpios = <&gpiob 0 GPIO_ACTIVE_LOW>;
		};

		ext_cs2: ext_cs2 {
			gpios = <&gpiob 1 GPIO_ACTIVE_LOW>;
		};

		ext_cs3: ext_cs3 {
			gpios = <&gpioa 8 GPIO_ACTIVE_LOW>;
		};

		ext_cs4: ext_cs4 {
			gpios = <&gpioa 9 GPIO_ACTIVE_LOW>;
		};
	};

	aliases {
		/* 蓝灯保持为 led0,M0 的心跳灯语义依赖它 */
		led0 = &led_blue_l;
		led1 = &led_green_l;
		led2 = &led_red_l;
		led3 = &led_green_r;
		led4 = &led_red_r;
	};
};

/*
 * 时钟:8MHz 晶振 → 96MHz SYSCLK / 48MHz USB。
 * 参数取自原固件 USER/system_stm32f4xx.c:PLL_M=8, PLL_N=192, PLL_P=2, PLL_Q=4,
 * VCO 输入 8/8 = 1MHz(ST 推荐值),VCO 输出 192MHz。
 *
 * 备用方案(stm32f411e_disco 同款,已实测可用):div-m=<4> mul-n=<96>,
 * 同样是 VCO 192MHz,只是 VCO 输入为 2MHz。
 */
&clk_hse {
	clock-frequency = <DT_FREQ_M(8)>;
	status = "okay";
};

&pll {
	div-m = <8>;
	mul-n = <192>;
	div-p = <2>;
	div-q = <4>;
	clocks = <&clk_hse>;
	status = "okay";
};

&rcc {
	clocks = <&pll>;
	clock-frequency = <DT_FREQ_M(96)>;
	ahb-prescaler = <1>;
	apb1-prescaler = <2>;
	apb2-prescaler = <1>;
};

/* MOTOR2(PB6) + MOTOR1(PB7)。st,prescaler 0 → 96MHz 定时器时钟 */
&timers4 {
	status = "okay";
	st,prescaler = <0>;

	pwm4: pwm {
		status = "okay";
		pinctrl-0 = <&tim4_ch1_pb6 &tim4_ch2_pb7>;
		pinctrl-names = "default";
	};
};

/* MOTOR4(PA5) + MOTOR3(PB10) */
&timers2 {
	status = "okay";
	st,prescaler = <0>;

	pwm2: pwm {
		status = "okay";
		pinctrl-0 = <&tim2_ch1_pa5 &tim2_ch3_pb10>;
		pinctrl-names = "default";
	};
};

/*
 * IIC1:MPU6500 六轴 IMU,AD0 拉高 → 地址 0x69,数据就绪中断在 PA4。
 * 原固件用模拟 IIC 驱动这颗芯片,硬件 I2C1 控制器在同样的引脚上工作正常。
 */
&i2c1 {
	status = "okay";
	clock-frequency = <I2C_BITRATE_FAST>;
	pinctrl-0 = <&i2c1_scl_pb8 &i2c1_sda_pb9>;
	pinctrl-names = "default";

	imu: mpu6500@69 {
		compatible = "invensense,mpu6050";
		reg = <0x69>;
		int-gpios = <&gpioa 4 GPIO_ACTIVE_HIGH>;
		accel-fs = <16>;
		gyro-fs = <2000>;
		smplrt-div = <7>;
	};

	/*
	 * 气压计 BMP280 经 MPU 的 aux I2C 挂在同一条总线上(原固件同样是
	 * bmp280Init(I2C1_DEV)),地址 0x76 已确认。
	 * 但它要能被访问,前提是 MPU 驱动先打开 I2C bypass(把 aux 引脚
	 * 直连到 PB8/PB9)。bypass 未开之前保持 disabled,否则探测必失败。
	 */
	bmp280: bmp280@76 {
		compatible = "bosch,bmp280";
		reg = <0x76>;
		status = "disabled";
	};
};

/* 调试控制台走扩展 UART1 排针 */
&usart1 {
	status = "okay";
	current-speed = <115200>;
	pinctrl-0 = <&usart1_tx_pa15 &usart1_rx_pb3>;
	pinctrl-names = "default";
};

/*
 * NRF51822 2.4GHz 无线模块,接在 USART2 上,1 Mbaud。
 * PA0 是模块的流控/使能脚,但究竟是 USART2_CTS 还是普通握手 GPIO 尚未确认,
 * 所以这里只配 TX/RX,待接无线时对原理图再定。
 */
&usart2 {
	status = "disabled";
	current-speed = <1000000>;
	pinctrl-0 = <&usart2_tx_pa2 &usart2_rx_pa3>;
	pinctrl-names = "default";
};

/* 扩展排针上的硬件 SPI2,板上没有挂 SPI 从设备 */
&spi2 {
	status = "disabled";
	pinctrl-0 = <&spi2_sck_pb13 &spi2_miso_pb14 &spi2_mosi_pb15>;
	pinctrl-names = "default";
};

/* USB OTG FS:PA11 = D-, PA12 = D+,PA10 = VBUS/ID */
zephyr_udc0: &usbotg_fs {
	status = "disabled";
	pinctrl-0 = <&usb_otg_fs_dm_pa11 &usb_otg_fs_dp_pa12>;
	pinctrl-names = "default";
};

4. xxx_defconfig:定义最小运行条件

只要为这块板构建,就应该具备的最小软件能力。

例如我的 theflyguy_f411_stm32f411xe_defconfig:

CONFIG_ARM_MPU=y
CONFIG_HW_STACK_PROTECTION=y

CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

CONFIG_GPIO=y

这里没有放:

CONFIG_SENSOR=y
CONFIG_PWM=y
CONFIG_SHELL=y
CONFIG_LOG=y

因为这些属于具体应用需求,目前放在根目录的 prj.conf

最终合并为:

板级 *_defconfig
       +
应用 prj.conf
       +
Kconfig 依赖和默认值
       ↓
构建目录/zephyr/.config

5. <target>.yaml:给 Twister 测试系统看的元数据

例如我的 theflyguy_f411_stm32f411xe.yaml

identifier: theflyguy_f411
type: mcu
arch: arm
ram: 128
flash: 512
toolchain:
  - zephyr
  - gnuarmemb
supported:
  - counter
  - gpio
  - i2c
  - pwm
  - serial
  - spi

它主要供 Zephyr 的测试工具 Twister 做:

  • 平台识别;
  • 测试筛选;
  • RAM/Flash 条件筛选;
  • toolchain 筛选;
  • platform_allow 和能力筛选。

6. board.cmake:选择 west 的烧录/调试 runner

# SPDX-License-Identifier: Apache-2.0
  
# 调试器:DAPLink(CMSIS-DAP 兼容)。

# 探针与目标配置在 support/openocd.cfg 
# openocd runner 会自动加载它,无需在此处传 --config。

# 使用 openocd.board.cmake,而不是 openocd-stm32.board.cmake。
# 后者会设置 --cmd-erase=stm32f2x mass_erase 0,使每次 west flash 都整片擦除,
# 会连带抹掉位于 0x08000000 的 MCUboot。
# openocd.board.cmake只擦镜像覆盖到的扇区。

include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)

核心只有:

include(${ZEPHYR_BASE}/boards/common/openocd.board.cmake)

作用是注册 OpenOCD runner,让下面的命令可用:

west flash
west debug

7. support/openocd.cfg:OpenOCD的配置

openocd runner 在未显式指定 --config 时会自动加载 <board_dir>/support/openocd.cfg。

# CMSIS-DAP 调试器 + STM32F4 目标
source [find interface/cmsis-dap.cfg]

# STM32F411 使用 SWD
transport select swd

source [find target/stm32f4x.cfg]

三个语句分别表示:

  1. 使用 CMSIS-DAP/DAPLink 探针;
  2. 使用 SWD,而不是 JTAG;
  3. 目标是 STM32F4 系列。

8. 自定义 binding 文件

新版本的DTS不能为无法识别的节点生成宏定义。而DTS依靠binding+compatible识别节点。

8.1. vendor-prefixes.txt

theflyguy    TheFlyGuy

用于登记:

compatible = "theflyguy,...";

中的厂商前缀 theflyguy。

标准兼容字符串范式是:

厂商前缀,设备或抽象名称

例如:

bosch,bmp280
invensense,mpu6050
theflyguy,motors

8.2. theflyguy,motors.yaml

这是我的自定义电机节点的 binding

compatible: "theflyguy,motors"

child-binding:
  properties:
    pwms:
      required: true
      type: phandle-array

它规定:

  • 与 theflyguy,motors 匹配的节点是容器;
  • 它的每个直接子节点都必须有 pwms;
  • pwms 必须是 phandle-array。

因此下面合法:

motor_m1 {
    pwms = <&pwm4 2 2667 PWM_POLARITY_NORMAL>;
};

下面会在构建期失败:

motor_m1 {
    pwm-channel = <2>;   /* binding 没定义这个属性 */
};

child-binding 是最关键的设计。它把规则应用到 motor_m1 等子节点,而不是只应用于 /motors 自己。

再次强调:binding 不是驱动。这个 binding 的价值是:

  • 校验设备树;
  • 告诉生成器属性类型;
  • 生成 PWM_DT_SPEC_GET() 所需的 C 宏。

8.3. theflyguy,ext-headers.yaml

这是我为从排针引出的空闲引脚定义的节点。结构与电机 binding 类似:

compatible: "theflyguy,ext-headers"

child-binding:
  properties:
    gpios:
      required: true
      type: phandle-array

它规定每个扩展排针子节点必须有 gpios。

应用中读取:

static const struct gpio_dt_spec cs0 =
    GPIO_DT_SPEC_GET(DT_NODELABEL(ext_cs0), gpios);

9. MCUboot支持

theflyguy_f411_stm32f411xe_mcuboot.dts:

/*
 * TheFlyGuy · MiniFly F411 —— MCUboot 变体
 *
 * 在基础板级之上叠加 A/B 双槽分区表。应用链接进 slot0_partition。
 *
 * 构建方式:
 *   west build -b theflyguy_f411/stm32f411xe/mcuboot --sysbuild
 *     -DSB_CONFIG_BOOTLOADER_MCUBOOT=y
 *     -DSB_CONFIG_MCUBOOT_MODE_SWAP_USING_MOVE=y
 *     -DSB_CONFIG_BOOT_SIGNATURE_KEY_FILE="<mcuboot>/root-rsa-2048.pem"
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "theflyguy_f411_stm32f411xe.dts"

/ {
	chosen {
		zephyr,code-partition = &slot0_partition;
	};
};
/*
 * STM32F411 扇区图:
 *   扇区 0-3 : 0x00000 - 0x0FFFF   16K × 4
 *   扇区 4   : 0x10000 - 0x1FFFF   64K
 *   扇区 5   : 0x20000 - 0x3FFFF   128K
 *   扇区 6   : 0x40000 - 0x5FFFF   128K
 *   扇区 7   : 0x60000 - 0x7FFFF   128K
 */
&flash0 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		/* 扇区 0-3。F411 上的 MCUboot 通常约 40K,64K 留有余量 */
		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x00000000 DT_SIZE_K(64)>;
			read-only;
		};

		/* 扇区 4(64K) + 扇区 5(128K) */
		slot0_partition: partition@10000 {
			label = "image-0";
			reg = <0x00010000 DT_SIZE_K(192)>;
		};

		/*
		 * 扇区 6(128K) + 扇区 7(128K)
		 *
		 * MCUboot 只要求 slot1 >= slot0,256K >= 192K 成立。
		 * 非对称布局依赖 SB_CONFIG_MCUBOOT_MODE_SWAP_USING_MOVE=y(无 scratch)。
		 */
		slot1_partition: partition@40000 {
			label = "image-1";
			reg = <0x00040000 DT_SIZE_K(256)>;
		};
	};
};

首先继承基础 DTS:

#include "theflyguy_f411.dts"

然后只增加 MCUboot 特有内容。

指定应用链接分区:

/ {
    chosen {
        zephyr,code-partition = &slot0_partition;
    };
};

MCUboot 从 0x08000000 开始,应用从 slot0,即 0x08010000 开始

like this:

0x08000000 ┌──────────────────────┐
           │ MCUboot       64 KiB │ 扇区 0~3
0x08010000 ├──────────────────────┤
           │ slot0/image-0 192KiB │ 扇区 4~5,当前运行镜像
0x08040000 ├──────────────────────┤
           │ slot1/image-1 256KiB │ 扇区 6~7,升级镜像
0x08080000 └──────────────────────┘

Zephyr 的多个 _defconfig 不会自动层层继承。因此需要为mcuboot变体也编写一个defconfig

theflyguy_f411_stm32f411xe_mcuboot_defconfig:

在原 defconfig 上增加一项:

CONFIG_USE_DT_CODE_PARTITION=y

含义是:链接器应使用 DTS 中 zephyr,code-partition 指向的分区。也就是让应用链接进 slot0_partition,而不是链接到 Flash 起点。

10. 构建后的重点文件

设备树会在构建期生成 C 宏。构建后(调试)最有价值文件,位于构建目录:

  1. 最终合并的设备树 zephyr.dts
  2. 最终的Kconfig配置 .config
  3. 生成的设备树C宏 devicetree_generated.h