
1. 环境准备与依赖安装在开始Petalinux2021.1环境搭建之前我们需要确保系统满足基本要求。官方推荐使用Ubuntu 16.04/18.04 LTS版本我这里实测Ubuntu 18.04.6 LTS最为稳定。建议准备至少100GB的磁盘空间因为编译过程中会产生大量中间文件。安装依赖库是第一个容易踩坑的环节。很多新手会直接运行安装程序结果遇到各种报错。我建议先执行以下命令一次性安装所有基础依赖sudo apt-get update sudo apt-get install -y tofrodos iproute2 gawk xvfb gcc git make net-tools libncurses5-dev tftpd zlib1g-dev libssl-dev flex bison libselinux1 gnupg wget diffstat chrpath socat xterm autoconf libtool tar unzip texinfo gcc-multilib build-essential libsdl1.2-dev libglib2.0-dev screen pax gzip这里有个细节要注意Ubuntu默认使用dash作为/bin/sh但Petalinux要求使用bash。执行以下命令修改默认shellsudo dpkg-reconfigure dash # 选择No安装过程中可能会遇到locale相关的警告可以通过以下命令解决sudo apt-get install locales sudo locale-gen en_US.UTF-82. Petalinux 2021.1安装详解从Xilinx官网下载两个关键文件petalinux-v2021.1-final-installer.runxilinx-zcu102-v2021.1-final.bsp建议下载后校验MD5值避免文件损坏导致安装失败。安装时要注意不要使用root账户安装路径不要包含空格或特殊字符确保目标目录有写入权限具体安装步骤chmod x petalinux-v2021.1-final-installer.run ./petalinux-v2021.1-final-installer.run /opt/pkg/petalinux/2021.1安装过程会提示接受三个许可协议按y确认即可。安装完成后需要设置环境变量source /opt/pkg/petalinux/2021.1/settings.sh建议将这条命令添加到~/.bashrc中这样每次打开终端都会自动加载Petalinux环境。验证安装是否成功echo $PETALINUX petalinux-util --webtalk off # 禁用数据收集3. ZCU102工程创建与配置使用下载的BSP文件创建工程petalinux-create -t project -n zcu102_2021 -s xilinx-zcu102-v2021.1-final.bsp cd zcu102_2021工程配置是关键步骤这里分享几个实用技巧petalinux-config --get-hw-descriptionpath_to_hdf # 如果有自定义硬件在配置界面中需要特别关注Subsystem AUTO Hardware Settings → 确认处理器配置Image Packaging Configuration → 选择EXT4根文件系统Device Drivers → 启用必要的驱动模块保存配置后可以开始编译工程petalinux-build编译过程可能需要1-3小时取决于机器性能。如果遇到并行编译错误可以尝试petalinux-build -j $(($(nproc)/2)) # 使用一半CPU核心4. QEMU仿真调试实战编译完成后images/linux目录下会生成以下关键文件BOOT.BIN启动镜像image.ub内核镜像rootfs.ext4根文件系统启动QEMU仿真petalinux-boot --qemu --prebuilt 3 --qemu-args -net nic -net user参数说明--prebuilt 1FPGA比特流QEMU中无效--prebuilt 2U-Boot阶段--prebuilt 3完整Linux启动成功启动后会看到ZynqMP登录提示用户名/密码都是root。调试技巧使用串口终端screen /dev/ttyUSB0 115200网络调试方法ifconfig eth0 192.168.100.100 ping 192.168.100.1 # 宿主机地址退出QEMU的正确姿势CtrlA → X # 不要直接关闭终端5. 常见问题解决方案安装时报错gawk not foundsudo apt-get install gawk编译时出现No rule to make target错误清理工程后重新配置petalinux-build -x distclean petalinux-configQEMU启动后网络不可用检查是否添加了网络参数--qemu-args -net nic -net user根文件系统挂载失败检查petalinux-config中的配置Image Packaging Configuration → Root filesystem type → EXT4性能优化建议增加编译缓存export BB_ENV_PASSTHROUGH_ADDITIONSHTTP_PROXY HTTPS_PROXY FTP_PROXY NO_PROXY使用ccache加速sudo apt-get install ccache export PATH/usr/lib/ccache:$PATH6. 进阶调试技巧内核调试在petalinux-config中启用Kernel hacking → Kernel debugging Compile-time checks and compiler options → Compile the kernel with debug info设备树调试提取当前使用的设备树dtc -I dtb -O dts -o system.dts images/linux/system.dtb性能分析工具top查看系统资源使用perf性能分析strace系统调用跟踪自定义rootfs通过petalinux-config添加软件包Image Features → package-management → opkg User Packages → 选择需要添加的软件自动化脚本示例创建build.sh自动化编译#!/bin/bash source /opt/pkg/petalinux/2021.1/settings.sh petalinux-build petalinux-package --boot --fsbl images/linux/zynqmp_fsbl.elf --u-boot images/linux/u-boot.elf --pmufw images/linux/pmufw.elf --fpga images/linux/*.bit --force7. 工程管理与版本控制建议的目录结构projects/ ├── zcu102_2021/ # Petalinux工程 ├── hardware/ # Vivado工程 ├── scripts/ # 自动化脚本 └── docs/ # 文档.gitignore配置示例# Petalinux /build/ /images/ /tmp/ *.log *.jou多人协作时建议只提交自定义的配置和代码记录使用的BSP版本维护完整的依赖列表8. 硬件协同调试当需要与真实硬件联调时准备SD卡sudo dd ifimages/linux/BOOT.BIN of/dev/sdX bs1M sudo dd ifimages/linux/boot.img of/dev/sdX bs1M seek256串口调试screen /dev/ttyUSB0 115200网络配置ifconfig eth0 192.168.1.100 scp root192.168.1.100:/var/log/messages .性能对比比较QEMU和真实硬件的启动时间验证外设驱动行为差异检查内存占用情况9. 编译优化技巧增量编译只重新编译修改的组件petalinux-build -c component-name并行编译合理设置并行度petalinux-build -j $(nproc)缓存利用配置sstate-cachepetalinux-config → Yocto Settings → Local sstate feeds容器化构建使用Docker保持环境一致FROM ubuntu:18.04 RUN apt-get update apt-get install -y \ tofrodos iproute2 gawk xvfb gcc git make net-tools \ libncurses5-dev tftpd zlib1g-dev libssl-dev flex bison \ libselinux1 gnupg wget diffstat chrpath socat xterm \ autoconf libtool tar unzip texinfo gcc-multilib \ build-essential libsdl1.2-dev libglib2.0-dev screen pax gzip10. 扩展应用开发创建自定义应用petalinux-create -t apps --template install -n myapp --enable添加内核模块petalinux-create -t modules --name mydriver --enable文件系统定制通过rootfs_config配置petalinux-config -c rootfs系统服务添加创建systemd服务单元mkdir -p project-spec/meta-user/recipes-core/systemd/systemd-system/性能监控实现使用sysstat工具集sar -u 1 # CPU使用率 sar -r 1 # 内存使用