工具下載
參考文檔(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
Usagecmake [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 westDownloading 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, westDEPRECATION: 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/8559Running 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\ZephyrZephyrUnittest (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.29Downloading 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 SetupRegistering 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-sdkAll 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 = 2Everything is OkFolders: 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 arenot truly random generators. This capability is not secure and it isprovided 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 UsedRAM: 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執行的結果: