工具下载
参考文档(Getting Started Guide — Zephyr Project Documentation)中介绍,可以直接通过winget下载:
winget download Kitware.CMake Ninja-build.Ninja oss-winget.gperf python Git.Git oss-winget.dtc wget 7zip.7zip
但是因为网络问题安装可能出现问题,这里直接独立下载(可以通过winget的下载过程查看到具体的下载地址)。
- CMake,下载地址Download CMake,下载版本cmake-4.1.0-rc3-windows-x86_64.msi,下载后直接默认安装即可。安装后执行cmake命令,结果如下,表示安装成功。
C:\Users\HOME>cmake --help
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>
# 后面略
ninja:下载地址https://github.com/ninja-build/ninja/releases/download/v1.13.1/ninja-win.zip,下载到的是一个独立的可执行文件。
gperf:下载地址https://github.com/oss-winget/oss-winget-storage/raw/96ea1b934342f45628a488d3b50d0c37cf06012c/packages/gperf/3.1/gperf-3.1-win64_x64.zip,得到的也是一个独立的文件。
dtc:下载地址https://github.com/oss-winget/oss-winget-storage/raw/96ea1b934342f45628a488d3b50d0c37cf06012c/packages/dtc/1.6.1/dtc-msys2-1.6.1-x86_64.zip,下载到的是一些可执行文件。
wget:下载地址https://eternallybored.org/misc/wget/1.21.4/64/wget.exe,下载到的是一个独立的文件。
其他的诸如Python、Git、7zip因为已经在使用,所以这里不额外下载。
需要注意这些工具在后续的操作中会使用,所以要保证它们能被访问到,这里将独立的可执行文件放到一个目录,比如E:\zephyrtools,然后将该目录添加到环境变量中。另外,再将Python、Git、7zip的安装目录也添加到环境变量中,保证在Windows的Shell下能够直接使用,比如:
最后还需要下载一个Qemu,用于测试编译得到的内核。
源码以及SDK下载
- 创建一个目录zephyrproject。
- zephyrproject同级目录下执行如下的命令:
python -m venv zephyrproject\.venv
此时zephyrproject下会创建一个.venv目录,它是一个Python的虚拟环境,后续的操作都在该虚拟环境下进行,以免影响主机的Python环境。
- 执行如下的命令进入Python虚拟环境:
zephyrproject\.venv\Scripts\activate.bat
注意这里有批处理和PowerShelll两个版本,后者似乎会出错,所以这里使用批处理,结果如下(注意开头有(.venv)
,如果要退出当前状态,执行deactivate):
(.venv) E:\>
- 安装west工具:
(.venv) E:\>pip install west
Collecting west
Downloading west-1.4.0-py3-none-any.whl (104 kB)
---------------------------------------- 104.8/104.8 kB 867.3 kB/s eta 0:00:00
# 中间略
Installing collected packages: docopt, six, ruamel.yaml.clib, PyYAML, packaging, colorama, ruamel.yaml, python-dateutil, pykwalify, west
DEPRECATION: docopt is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
Running setup.py install for docopt ... done
Successfully installed PyYAML-6.0.2 colorama-0.4.6 docopt-0.6.2 packaging-25.0 pykwalify-1.8.0 python-dateutil-2.9.0.post0 ruamel.yaml-0.18.14 ruamel.yaml.clib-0.2.12 six-1.17.0 west-1.4.0
[notice] A new release of pip is available: 23.0.1 -> 25.1.1
[notice] To update, run: python.exe -m pip install --upgrade pip
安装成功之后就可以在zephyrproject.venv\Scripts目录下找到west.exe这个程序。
- 下载zephyr源码:
(.venv) E:\>west init zephyrproject
=== Initializing in E:\zephyrproject
--- Cloning manifest repository from https://github.com/zephyrproject-rtos/zephyr
Cloning into 'E:\zephyrproject\.west\manifest-tmp'...
remote: Enumerating objects: 1301937, done.
remote: Counting objects: 100% (904/904), done.
remote: Compressing objects: 100% (334/334), done.
remote: Total 1301937 (delta 615), reused 570 (delta 570), pack-reused 1301033 (from 2)
Receiving objects: 100% (1301937/1301937), 742.54 MiB | 6.20 MiB/s, done.
Resolving deltas: 100% (961206/961206), done.
Updating files: 100% (48763/48763), done.
--- setting manifest.path to zephyr
=== Initialized. Now run "west update" inside E:\zephyrproject.
此时会用到Git,它从Github中下载源码,并放到zephyrproject目录下,源代码在默认名为zephyr的目录中。
- 进入zephyrproject并下载额外的代码:
(.venv) E:\>cd zephyrproject
(.venv) E:\zephyrproject>west update
=== updating acpica (modules/lib/acpica):
--- acpica: initializing
Initialized empty Git repository in E:/zephyrproject/modules/lib/acpica/.git/
--- acpica: fetching, need revision 8d24867bc9c9d81c81eeac59391cda59333affd4
# 后面略
注意前一步中的init只会下载zephyr的主代码,还需要执行update来下载其他的模块,这类似于Git中的submodule。
具体要下载哪些模块可以在zephyr目录下的west.yml文件中找到,它们是一些板极支持,特定功能模块,工具,等等,下载后被放在bootloader、modules、tools等目录。
注意本次update有一个报错:
=== updating hal_nuvoton (modules/hal/nuvoton):
--- hal_nuvoton: fetching, need revision be1042dc8a96ebe9ea4c5d714f07c617539106d6
fatal: unable to access 'https://github.com/zephyrproject-rtos/hal_nuvoton/': Recv failure: Connection was reset
由于是HAL层的模块,这里测试应该也不会用到nuvoton,暂时先不管。
- 导出zephyr的一些信息到环境变量中,这样CMake才能找到zephyr:
(.venv) E:\zephyrproject>west zephyr-export
Zephyr (E:/zephyrproject/zephyr/share/zephyr-package/cmake)
has been added to the user package registry in:
HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\Zephyr
ZephyrUnittest (E:/zephyrproject/zephyr/share/zephyrunittest-package/cmake)
has been added to the user package registry in:
HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\ZephyrUnittest
注意它只在本终端中有效,后续如果重新打开终端,需要重新执行该操作。
- 安装Python依赖包:
(.venv) E:\zephyrproject>west packages pip --install
Ignoring python-magic: markers 'sys_platform != "win32"' don't match your environment
Collecting pyelftools>=0.29
Downloading pyelftools-0.32-py3-none-any.whl (188 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 188.5/188.5 kB 1.1 MB/s eta 0:00:00
Requirement already satisfied: PyYAML>=6.0 in e:\zephyrproject\.venv\lib\site-packages (from -r E:\zephyrproject\zephyr\scripts\requirements-base.txt (line 11)) (6.0.2)
# 后面略
这些下载的依赖来自zephyrproject\zephyr\scripts\requirements.txt。
- 安装编译工具:
(.venv) E:\zephyrproject>west sdk install
Found 'E:\zephyrproject\zephyr\SDK_VERSION', installing version 0.17.2.
Fetching Zephyr SDK list...
Fetching sha256...
Downloading https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.17.2/zephyr-sdk-0.17.2_windows-x86_64_minimal.7z...
zephyr-sdk-0.17.2_windows-x86_64_minimal.7z: 100%|###################################| 4.71k 4.69M/s [00:00]
Downloaded: C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2_windows-x86_64_minimal.7z
Extract: C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2_windows-x86_64_minimal.7z
INFO patool: Extracting C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2_windows-x86_64_minimal.7z ...
INFO patool: could not find a 'file' executable, falling back to guess mime type by file extension
INFO patool: running "D:\Program Files\7-Zip\7z.EXE" x -aou -oC:\Users\HOME\tmprwm3irxg -- C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2_windows-x86_64_minimal.7z
INFO patool: ... C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2_windows-x86_64_minimal.7z extracted to `C:\Users\HOME\tmprwm3irxg'.
Move: C:\Users\HOME\tmprwm3irxg\zephyr-sdk-0.17.2 to C:\Users\HOME\zephyr-sdk-0.17.2.
Zephyr SDK 0.17.2 Setup
Registering Zephyr SDK CMake package ...
Zephyr-sdk (C:/Users/HOME/zephyr-sdk-0.17.2/cmake)
has been added to the user package registry in:
HKEY_CURRENT_USER\Software\Kitware\CMake\Packages\Zephyr-sdk
All done.
Zephyr SDK 0.17.2 Setup
# 中间略
Extracting archive: toolchain_windows-x86_64_xtensa-sample_controller32_zephyr-elf.7z
--
Path = toolchain_windows-x86_64_xtensa-sample_controller32_zephyr-elf.7z
Type = 7z
Physical Size = 41590492
Headers Size = 18827
Method = LZMA2:24 BCJ
Solid = +
Blocks = 2
Everything is Ok
Folders: 228
Files: 1675
Size: 206505310
Compressed: 41590492
Installing host tools ...
SKIPPED: Windows host tools are not available yet.
All done.
(.venv) E:\zephyrproject>
默认下载到了C:\Users\HOME\zephyr-sdk-0.17.2目录。
这一步下载可能会失败,可以尝试手动完成,具体的操作见Zephyr SDK — Zephyr Project Documentation。
编译和执行
SDK下载之后就可以开始编译zephyr,下面是命令格式:
west build -p always -b <your-board-name> app
-p always
表示强制清理并重新创建构建目录。-b
之后跟的是单板名,可以通过如下命令查看:
(.venv) E:\zephyrproject\zephyr>west boards
esp32c3_042_oled
96b_aerocore2
96b_argonkey
96b_avenger96
96b_carbon
96b_meerkat96
96b_neonkey
96b_nitrogen
# 后面略
- 最后是一个应用程序路径,用于指定要编译的Zephyr应用代码位置。
下面是一个例子:
(.venv) E:\zephyrproject\zephyr>west build -p always -b qemu_x86 samples/hello_world
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: E:/zephyrproject/zephyr/samples/hello_world
-- CMake version: 4.1.0-rc3
-- Found Python3: E:/zephyrproject/.venv/Scripts/python.exe (found suitable version "3.10.11", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: E:/zephyrproject/zephyr/.cache
-- Zephyr version: 4.2.99 (E:/zephyrproject/zephyr)
-- Found west (found suitable version "1.4.0", minimum required is "0.14.0")
-- Board: qemu_x86, qualifiers: atom
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.17.2 (C:/Users/HOME/zephyr-sdk-0.17.2)
-- Found toolchain: zephyr 0.17.2 (C:/Users/HOME/zephyr-sdk-0.17.2)
-- Found Dtc: E:/zephyrtools/dtc.exe (found suitable version "1.6.1", minimum required is "1.4.6")
-- Found BOARD.dts: E:/zephyrproject/zephyr/boards/qemu/x86/qemu_x86.dts
-- Generated zephyr.dts: E:/zephyrproject/zephyr/build/zephyr/zephyr.dts
-- Generated pickled edt: E:/zephyrproject/zephyr/build/zephyr/edt.pickle
-- Generated devicetree_generated.h: E:/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/devicetree_generated.h
Parsing E:/zephyrproject/zephyr/Kconfig
Loaded configuration 'E:/zephyrproject/zephyr/boards/qemu/x86/qemu_x86_defconfig'
Merged configuration 'E:/zephyrproject/zephyr/samples/hello_world/prj.conf'
Configuration saved to 'E:/zephyrproject/zephyr/build/zephyr/.config'
Kconfig header saved to 'E:/zephyrproject/zephyr/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: c:/users/home/zephyr-sdk-0.17.2/x86_64-zephyr-elf/x86_64-zephyr-elf/bin/ld.bfd.exe (found version "2.38")
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: C:/Users/HOME/zephyr-sdk-0.17.2/x86_64-zephyr-elf/bin/x86_64-zephyr-elf-gcc.exe
CMake Warning at E:/zephyrproject/zephyr/subsys/random/CMakeLists.txt:12 (message):
Warning: CONFIG_TIMER_RANDOM_GENERATOR and CONFIG_TEST_CSPRNG_GENERATOR are
not truly random generators. This capability is not secure and it is
provided for testing purposes only. Use it carefully.
-- Found gen_kobject_list: E:/zephyrproject/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done (17.2s)
-- Generating done (0.3s)
-- Build files have been written to: E:/zephyrproject/zephyr/build
-- west build: building application
[1/119] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.2.99 (E:/zephyrproject/zephyr), build: v4.2.0-676-g20efb9edbb35
[119/119] Linking C executable zephyr\zephyr.elf
Memory region Used Size Region Size %age Used
RAM: 106656 B 31 MB 0.33%
IDT_LIST: 0 GB 2 KB 0.00%
Generating files from E:/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: qemu_x86
编译得到zephyrproject\zephyr\build\zephyr\zephyr.elf,可以通过qemu执行的结果: