openEuler下搭建Zephyr构建环境

Creative Commons
本作品采用知识共享署名

本文说明如何在openEuler下搭建Zephyr构建环境.

Zephyr官方文档在Linux下是用Ubuntu作为Zephyr的构建环境,但理论上其它Linux的发行版只要软件包够全也应该能够编译Zephyr,本文做了一些简单的尝试,在openEuler 21.09上成功编译出zephyr并用qemu执行。

安装依赖软件包

1
2
3
4
5
6
sudo dnf -y update

sudo dnf install -y git cmake ninja-build gperf \
ccache wget \
python3-pip python3-setuptools python3-wheel file \
make gcc

以下软件包在官方文档中要求安装,但openEuler未提供,实际测试不安装也不影响构建:

  • dfu-util : dfu用,如果只做编译和一些模拟工作可以不用
  • xz-utils :压缩工具,目前构建没遇到问题
  • device-tree-compiler : dtc设备树编译工具,Zephyr SDK会提供,因此不安装也不影响
  • python3-dev : python的C/C++扩展库,目前不知道是用到那里
  • python3-tk : tcl/tk用,应该是给zephyr gui配置用的,我们通过配置文件进行配置,可以不用
  • gcc-multilib : 64bit下编译32bit档案用,目前不清楚会影响什么
  • g++-multilib : 64bit下编译32bit档案用,目前不清楚会影响什么
  • libsdl2-dev : SDL,目前不清楚会影响什么

目前dnf安装的cmake还是3.19版本不满足zephyr的最低需求,使用下面方法安装3.21版本

1
2
3
4
5
wget https://github.com/Kitware/CMake/releases/download/v3.21.1/cmake-3.21.1-Linux-x86_64.sh
chmod +x cmake-3.21.1-Linux-x86_64.sh
sudo dnf install tar
sudo ./cmake-3.21.1-Linux-x86_64.sh --skip-license --prefix=/usr/local
hash -r

下载Zephyr与安装Python依赖

1 安装west

1
2
3
pip3 install --user -U west
echo 'export PATH=~/.local/bin:"$PATH"' >> ~/.bashrc
source ~/.bashrc

2 下载Zephyr代码

1
2
3
west init ~/zephyrproject
cd ~/zephyrproject
west update

3 导出zephyr cmake package

1
est zephyr-export

4 安装Python依赖

1
pip3 install --user -r ~/zephyrproject/zephyr/scripts/requirements.txt

安装Zephyr SDK

Zephyr SDK中包含了各个架构的Toolchain和一些工具
1 下载SDK

1
2
cd ~
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.13.1/zephyr-sdk-0.13.1-linux-x86_64-setup.run

2 安装SDK

1
2
chmod +x zephyr-sdk-0.13.1-linux-x86_64-setup.run
./zephyr-sdk-0.13.1-linux-x86_64-setup.run -- -d ~/zephyr-sdk-0.13.1

编译和测试

基于qemu_cortex_m3用shell sample进行编译测试

1
west build -b qemu_cortex_m3 zephyr/samples/subsys/shell/shell_module/

可以正常编译完,看到

1
2
3
4
5
6
7
8
9
10
11
12
-- west build: building application
[1/169] Preparing syscall dependency handling

[159/169] Linking C executable zephyr/zephyr_pre0.elf

[163/169] Linking C executable zephyr/zephyr_pre1.elf

[169/169] Linking C executable zephyr/zephyr.elf
Memory region Used Size Region Size %age Used
FLASH: 50924 B 256 KB 19.43%
SRAM: 12480 B 64 KB 19.04%
IDT_LIST: 0 GB 2 KB 0.00%

执行下面命令跑qemu进行测试

1
west build -t run

可以看到正常运行起来zephyr的shell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[frank@localhost zephyrproject]$ west build -t run
-- west build: running target run
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: cortex-m3
qemu-system-arm: warning: nic stellaris_enet.0 has no peer
Timer with period zero, disabling


uart:~$ help
Please press the <Tab> button to see all available commands.
You can also use the <Tab> button to prompt or auto-complete all commands or its subcommands.
You can try to call commands with <-h> or <--help> parameter for more information.

Shell supports following meta-keys:
Ctrl + (a key from: abcdefklnpuw)
Alt + (a key from: bf)
Please refer to shell documentation for more details.

Available commands:
bypass :Bypass shell
clear :Clear screen.
date :Date commands
demo :Demo commands
device :Device commands
devmem :Read/write physical memory"devmem address [width [value]]
dynamic :Demonstrate dynamic command usage.
help :Prints the help message.
history :Command history.
kernel :Kernel commands
log :Commands for controlling logger
log_test :Log test
resize :Console gets terminal screen size or assumes default in
case the readout fails. It must be executed after each
terminal width change to ensure correct text display.
shell :Useful, not Unix-like shell commands.
shell_uart_release :Uninitialize shell instance and release uart, start
loopback on uart. Shell instance is renitialized when 'x'
is pressed
stats :Stats commands
version :Show kernel version
uart:~$