Zephyr环境搭建 - Board GD32A503
Zephyr 简介
Zephyr 是一个轻量级、模块化且高度可扩展的实时操作系统(RTOS),专为资源受限的嵌入式设备设计,如物联网(IoT)设备、传感器、可穿戴设备和工业控制器。它由 Linux 基金会托管,采用 Apache 2.0 开源许可证,支持多种处理器架构(如 ARM、x86、RISC-V 等)。
核心特点
轻量级内核
Zephyr 内核设计紧凑,内存占用极小(可低至 2KB RAM),适合微控制器(MCU)和低功耗场景。
模块化架构
采用模块化设计,允许开发者按需选择功能组件(如文件系统、网络协议栈),减少资源占用。
实时性能
支持硬实时(Hard Real-Time)任务调度,确保关键任务在严格时间限制内完成。
跨平台支持
支持 200 多种开发板和芯片架构,包括 Nordic nRF、STM32、ESP32 等主流硬件。
丰富的协议栈
内置蓝牙 Low Energy(BLE)、Thread、Zigbee、Wi-Fi 等无线协议栈,以及 IPv4/IPv6、CoAP、MQTT 等网络协议。
开发者友好
提供完善的文档、示例代码和工具链(如基于 CMake 的构建系统),支持 Windows、Linux 和 macOS 开发环境。
典型应用场景
- 智能家居设备(如传感器、网关)
- 工业自动化(PLC、远程监控)
- 医疗穿戴设备(心率监测仪)
- 低功耗边缘计算节点
- 与其他 RTOS 的对比
相比 FreeRTOS 或 Micrium OS,Zephyr 更强调标准化和社区协作,提供更统一的驱动模型和安全性功能(如内存保护单元 MPU 支持)。
搭建开发环境
我的系统是windows 11,其他系统可参考官方文档。
安装依赖
使用winget安装,不要单独安装,如果想踩坑可以去尝试。
winget install Kitware.CMake Ninja-build.Ninja oss-winget.gperf python Git.Git oss-winget.dtc wget 7zip.7zip
获取zephyr并安装python依赖
创建虚拟环境
cd E:\workspace\
python -m venv GD32A503\.venv
激活虚拟环境
GD32A503\.venv\Scripts\activate.bat
安装west
pip install west
获取zephyr
这里需要注意,Exception: Failed to fetch: 403, {“message”:“API rate limit exceeded for 156.146.45.177. (But here’s the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)”,“documentation_url”:“https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting”}
提前在github上生成号token,否则会出现上述的问题,当然也可以忽略,过段时间限制会reset
west init GD32A503
cd GD32A503
west update
导出Zephyr CMake 包。这允许 CMake 自动加载构建 Zephyr 应用程序所需的样板代码。
west zephyr-export
Zephyr west 扩展命令可用于安装 Python 依赖项
west packages pip --install
安装Zephyr SDK
Zephyr 软件开发工具包(SDK) 包含 Zephyr 支持的每个架构的工具链,其中包括编译器、汇编器、链接器和构建 Zephyr 应用程序所需的其他程序。
cd GD32A503zephyr
west sdk install
构建helloworld
(.venv) PS E:\workspace\GD32A503\zephyr>
(.venv) PS E:\workspace\GD32A503\zephyr> west build -b gd32a503v_eval samples/hello_world
烧录
(.venv) PS E:\workspace\GD32A503\zephyr> west flash --runner jlink
-- west flash: rebuilding
[0/1] Re-running CMake...Loading Zephyr default modules (Zephyr base (cached)).
-- Application: E:/workspace/GD32A503/zephyr/samples/hello_world
-- CMake version: 4.0.3
-- Cache files will be written to: E:/workspace/GD32A503/zephyr/.cache
-- Zephyr version: 4.2.0-rc3 (E:/workspace/GD32A503/zephyr)
-- Found west (found suitable version "1.4.0", minimum required is "0.14.0")
-- Board: gd32a503v_eval, qualifiers: gd32a503
-- Found host-tools: zephyr 0.17.2 (C:/Users/hugh-/zephyr-sdk-0.17.2)
-- Found toolchain: zephyr 0.17.2 (C:/Users/hugh-/zephyr-sdk-0.17.2)
-- Found BOARD.dts: E:/workspace/GD32A503/zephyr/boards/gd/gd32a503v_eval/gd32a503v_eval.dts
-- Generated zephyr.dts: E:/workspace/GD32A503/zephyr/build/zephyr/zephyr.dts
-- Generated pickled edt: E:/workspace/GD32A503/zephyr/build/zephyr/edt.pickle
-- Generated devicetree_generated.h: E:/workspace/GD32A503/zephyr/build/zephyr/include/generated/zephyr/devicetree_generated.h
Parsing E:/workspace/GD32A503/zephyr/Kconfig
Loaded configuration 'E:/workspace/GD32A503/zephyr/build/zephyr/.config'
No change to configuration in 'E:/workspace/GD32A503/zephyr/build/zephyr/.config'
No change to Kconfig header in 'E:/workspace/GD32A503/zephyr/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found gen_kobject_list: E:/workspace/GD32A503/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done (13.0s)
-- Generating done (0.4s)
-- Build files have been written to: E:/workspace/GD32A503/zephyr/build[7/8] Linking C executable zephyr\zephyr.elf←[Kr_final.dir/isr_tables.c.obj←[Kon←[K
Memory region Used Size Region Size %age UsedFLASH: 13280 B 384 KB 3.38%RAM: 4248 B 48 KB 8.64%IDT_LIST: 0 GB 32 KB 0.00%
Generating files from E:/workspace/GD32A503/zephyr/build/zephyr/zephyr.elf for board: gd32a503v_eval
←[92m-- west flash: using runner jlink
←[0m←[92m-- runners.jlink: reset after flashing requested
←[0m←[92m-- runners.jlink: JLink version: 7.94e
←[0m←[92m-- runners.jlink: Flashing file: E:\workspace\GD32A503\zephyr\build\zephyr\zephyr.hex←[0m