本文记录在 Windows 环境下搭建 Zephyr 4.5 开发环境,并在 STM32H7S78-DK 开发板(STM32H7S7L8,Cortex-M7 @600MHz)上运行官方 blinky 例程的完整流程,包括片上 Flash 直跑和「MCUboot + 外部 Octo-SPI Flash」两种形态。
1. 环境搭建(Windows + Zephyr 4.5)
全程 Windows 原生环境,无需 WSL。以 CMD 为例。
安装基础工具:Python 3.12、CMake、Ninja(后两个可用 winget 安装),再装 west:
winget install Kitware.CMake Ninja-build.Ninja pip install west
拉取 Zephyr 4.5 源码(工作区建在 D:\luglZephyrproject)并安装 Python 依赖:
west init -m https://github.com/zephyrproject-rtos/zephyr --mr v4.5.0 D:\luglZephyrproject cd /d D:\luglZephyrproject west update pip install -r zephyr\scripts\requirements.txt :: imgtool 签名依赖(用 MCUboot 时必需,否则链接后签名报 No module named 'intelhex') pip install -r bootloader\mcuboot\scripts\requirements.txt
安装 Zephyr SDK:本文使用 SDK 1.0.1,解压到 D:\luglZephyrproject\zephyr-sdk-1.0.1(内含 arm-zephyr-eabi 工具链)。
安装 STM32CubeProgrammer:烧录工具,同时提供外部 Flash 加载器 MX66UW1G45G_STM32H7S78-DK.stldr。
每次编译前设置环境变量(指向 SDK 和工作区):
cd /d D:\luglZephyrproject set ZEPHYR_SDK_INSTALL_DIR=D:\luglZephyrproject\zephyr-sdk-1.0.1\zephyr-sdk-1.0.1 set ZEPHYR_TOOLCHAIN_VARIANT=zephyr
验证环境:能列出板子即 OK。
west boards | findstr stm32h7s78_dk
2. 例程源码解析
blinky 位于 Zephyr 源码树 samples/basic/blinky,只有三个文件。
prj.conf —— 只需打开 GPIO 驱动:
CONFIG_GPIO=y
CMakeLists.txt —— 通过 ZEPHYR_BASE 找到 Zephyr 构建系统,把 src/main.c 加入 app 目标:
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)
target_sources(app PRIVATE src/main.c)src/main.c —— 通过 Devicetree 别名 led0 拿到板载 LED(H7S78-DK 上对应 LD1 绿灯 / PO1),配置为输出后每秒翻转一次:
#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#define SLEEP_TIME_MS 1000
#define LED0_NODE DT_ALIAS(led0)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
int ret;
bool led_state = true;
if (!gpio_is_ready_dt(&led)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
return 0;
}
while (1) {
ret = gpio_pin_toggle_dt(&led);
if (ret < 0) {
return 0;
}
led_state = !led_state;
printf("LED state: %s\n", led_state ? "ON" : "OFF");
k_msleep(SLEEP_TIME_MS);
}
return 0;
}应用本身与板子无关,led0 的具体引脚由板级 DTS(boards/st/stm32h7s78_dk)描述,这就是 Zephyr 跨平台的来源。
3. 编译:指定开发板
Zephyr 统一用 west build -b <board> 指定目标板。在 CMD 中执行(先按第 1 节设好环境变量):
cd /d D:\luglZephyrproject :: 编译:指定开发板为 stm32h7s78_dk,-p 表示干净构建(首次或换板时使用) west build -b stm32h7s78_dk zephyr\samples\basic\blinky -p :: 烧录(板载 ST-LINK 直连,自动调用 STM32CubeProgrammer) west flash
编译通过后会打印内存占用,可据此确认放不放得下:
Memory region Used Size Region Size %age Used FLASH: 20860 B 64 KB 31.83% RAM: 4672 B 456 KB 1.00%
烧录后串口(ST-LINK 虚拟串口,115200 8N1)输出:
*** Booting Zephyr OS build v4.5.0 *** LED state: OFF LED state: ON
4. MCUboot + 外部 Flash 运行
H7S7 片上只有 64KB Flash,正式应用需放外部 1Gbit Octo-SPI Flash,由 MCUboot 引导。Zephyr 为此提供了专用板级变体,配合 Sysbuild 一次构建出引导器 + 签名应用两个镜像。完整编译指令:
cd /d D:\luglZephyrproject :: Sysbuild 一次构建 MCUboot(片内 Flash)+ 签名应用(外部 Flash,slot 60MB) west build -b stm32h7s78_dk/stm32h7s7xx/ext_flash_app --sysbuild zephyr\samples\basic\blinky -p :: 烧录两个镜像,自动带外部 Flash 加载器 MX66UW1G45G_STM32H7S78-DK.stldr west flash :: 以后只更新应用、不重烧引导器时: west flash --domain blinky
注意:同一 build 目录在不同板级变体之间切换时,必须加 -p(或删除 build 目录),否则会报 "Build directory targets board ... was specified" 错误。
构建产物:
| build\mcuboot\zephyr\zephyr.hex | MCUboot,烧入片内 64KB Flash(占约 30KB) |
| build\blinky\zephyr\zephyr.signed.hex | 签名后的应用,烧入外部 Octo-SPI Flash(0x70000000) |
烧录后串口可见完整启动链:
*** Using Zephyr OS build v4.5.0-33-gce0d7b14184c *** *** Booting MCUboot 725d63c9d5bc *** I: Starting bootloader I: Bootloader chainload address offset: 0x0 I: Image version: v0.0.0 I: Jumping to the first image slot *** Booting Zephyr OS build v4.5.0-33-gce0d7b14184c *** LED state: OFF LED state: ON LED state: OFF
MCUboot 从片上 Flash 启动,验签后跳转到外部 Flash 中的应用程序,LD1 以 1Hz 闪烁。后续只更新应用时,重烧应用镜像即可(west flash --domain blinky),引导器无需改动;该模式基于 MCUboot swap-using-offset,也为后续 OTA 升级打好了基础。
我要赚赏金
