zynq基础

The Zynq®-7000 family is based on the Xilinx SoC architecture. These products integrate a feature-rich dual-core or single-core ARM® Cortex™-A9 (32bit) based processing system (PS) and 28 nm Xilinx programmable logic (PL) in a single device.

对于 ZYNQ 而言,一个完整的 linux 系统包含 PS 和 PL 两个构件,其中 PS 构件包含 fsbl、uboot、设备树文件、linux 内核、根文件系统共5个要素,PL构件包含 bit 文件一个要素,当不使用 PL 的时候,该要素非必须。

一、术语

FSBL:First Stage Boot Loader

二、设计流程

通常PetaLinux工具遵循顺序设计流程模型。

Design Flow Step Tool/Workflow
Hardware Platform Creation Vivado
Create PetaLinux Project petalinux-create -t project
Initialize PetaLinux Project petalinux-config --get-hw-description
Configure System-Level Options petalinux-config
Create User Components petalinux-create -t COMPONENT
Configure the Linux Kernel petalinux-config -c kernel
Configure the Root Filesystem petalinux-config -c rootfs
Build the System petalinux-build
Package for Deploying the System petalinux-package
Boot the System for Testing petalinux-boot

一般的设计流程如下:

1. 通过Vivado创建硬件平台,得到hdf硬件描述文件;
2. 运行source <petalinux安装路径>/settings.sh,设置Petalinux运行环境
3. 通过petalinux-create -t project创建petalinux工程;
4. 使用petalinux-config --get-hw-description,将hdf文件导入到petalinux工程当中并配置petalinux工程;
5. 使用petalinux-config -c kernel配置Linux内核;
6. 使用petalinux-config -c rootfs配置Linux根文件系统;
7. 配置设备树文件;
8. 使用petalinux-build编译整个工程;
9. 使用petalinux-package --boot制作BOOT.BIN启动文件;
10. 制作SD启动卡,将BOOT.BIN和image.ub以及根文件系统部署到SD卡中;
11. 将SD卡插入开发板,并将开发板启动模式设置为从SD卡启动;
12. 开发板连接串口线并上电启动,串口上位机打印启动信息,登录进入Linux系统。
/opt/Xilinx/Vivado/2020.1/settings64.sh
vivado

三、petalinux命令

1. petalinux-package --boot命令生成可引导映像。

petalinux-package --boot --fsbl --fpga --u-boot --force

选项“--fsbl”用于指定fsbl镜像文件所在位置,后面接文件对应的路径信息,如果不指定文件位置,默认对应的是images/linux/zynq_fsbl.elf;选项“--fpga”用于指定bitstream文件所在位置,后面接该文件对应的路径信息,默认对应的是images/linux/system.bit;选项“--u-boot”用于指定U-Boot镜像所在位置,后面接该文件所在路径信息,默认为images/linux/u-boot.elf。

$ petalinux-package --boot --fsbl --fpga --u-boot --force
INFO: sourcing build tools
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/zynq_fsbl.elf"
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/project-spec/hw-description/z7020_pwm_mio.bit"
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/u-boot.elf"
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/system.dtb"
INFO: Generating Zynq binary package BOOT.BIN...
[INFO]   : Bootimage generated successfully

INFO: Binary is ready.

2. petalinux-build -c device-tree -x distclean

重新构建设备树

petalinux-build -x mrproper
删除images和build,重新构建

3. petalinux-config

同步硬件描述.xsa文件到project-spec/hw-description/目录。

petalinux-config --get-hw-description=<Vivado_Export_to_SDK_Directory>
petalinux-config // 配置子系统
petalinux-config -c u-boot  // 配置uboot
petalinux-config -c kernel  // 配置内核
petalinux-config -c rootfs

四、编译

zynq交叉编译工具:arm-xilinx-linux-gnueabi-gcc

1. 服务器上编译

(STEP1) 切换到embedsw
su embedsw
source /opt/pkg/petalinux/settings.sh

(STEP2) 更改目录权限
sudo chown -R embedsw:embedsw zynq7020_peta

(STEP3) 生成编译依赖
cd zynq7020_peta; petalinux-config --get-hw-description ../hardware/
不要配置直接推出即可!!!

STEP4 进行编译
petalinux-build

STEP5 打包镜像
petalinux-package --boot --fsbl --fpga --u-boot --force
打包生成镜像在images/linux/下面
将生成的镜像文件BOOT.BIN boot.scr image.ub拷贝到sd卡(sd卡格式化为fat32)上即可完成启动

2. 增加模块

petalinux-create -t modules --name ax-ledgpio-drv --enable

3. 增加应用

petalinux-create -t apps --name myapp --enable
petalinux-create -t apps --template c --name myapp --enable

// 重新构建选定的用户应用
petalinux-build -c myapp

五、调试

1. 设备树操作

配置设备树编辑petalinux工程目录下的project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi文件。

基础设备树zynq7000.dtsi怎样包含进去的???component下的设备文件怎样导入的

led { 
    compatible = "alientek,led";
    status = "okay";
    default-state = "on";
    reg = <0xE000A040 0x4
        0xE000A204 0x4
        0xE000A208 0x4
        0xE000A214 0x4
        0xF800012C 0x4
    >;
};

led {
compatible = "alientek,led";
status = "okay";
default-state = "on";
led-gpio = <&gpio0 7 GPIO_ACTIVE_HIGH>;
};

gpiokey {
compatible = "alientek,key";
status = "okay";
key-gpio = <&gpio0 12 GPIO_ACTIVE_HIGH>;
};

        eeprom_gpio {
                compatible = "canaan,eeprom-gpio";
                status = "okay";
                eeprom-gpios = <&gpio0 11 GPIO_ACTIVE_LOW>;
                reg = <0xE000A040 0x4
                        0xE000A060 0x4
                        0xE000A204 0x4
                        0xE000A208 0x4
                        0xE000A214 0x4
                        0xF800012C 0x4
                        >;
        };

2. LED测试

petalinux-create -t modules --name ax-ledgpio-drv --enable
```shell
### 3. at21cs01测试
```shell
at21cs01(A0 3F 1E 8B 0 0 81 F0) probe OK!

4. pwm调试

petalinux-config -c kernel // 选中PWM 在driver配置页面
petalinux-config -c buxybox // process utilities -> support thread display

vi project-spec/configs/config // 修改MAC地址

六、分离设备树、内核、根文件系统

1. petalinux-config配置设备树、文件系统

// petalinux-config --get-hw-description ../hardware/
Subsystem AUTO Hardware Settings ->
    dtb image settings ->
        image storage media(primary sd)
Image Packaging Configuration ->
    Root filesystem type(INITRAMFS)
        -> EXT4 (SD/eMMC/SATA/USB)
petalinux-build -c u-boot
petalinux-package --boot --fsbl --u-boot --force

$ petalinux-package --boot --fsbl --uboot --force
INFO: sourcing build tools
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/zynq_fsbl.elf"
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/u-boot.elf"
INFO: File in BOOT BIN: "/home//repo/zynq/zynq_bsp_gpio/zynq7020_peta/images/linux/system.dtb"
INFO: Generating Zynq binary package BOOT.BIN...

[INFO]   : Bootimage generated successfully

INFO: Binary is ready.
WARNING: Unable to access the TFTPBOOT folder /tftpboot!!!
WARNING: Skip file copy to TFTPBOOT folder!!!

2. 生成设备树文件

petalinux-build -c uboot后会在工程目录components/plnx_workspace/device-tree/device-tree/下生成设备树文件。
主要是pcw.dtsi、pl.dtsi、system-conf.dtsi、system-top.dts、zynq-7000.dtsi。

3. 编译kernel

1)拷贝内核源码
2)拷贝原来生成的components/plnx_workspace/device-tree/device-tree/目录下pcw.dtsi、pl.dtsi、system-top.dts 以及 zynq-7000.dtsi到arch/arm/boot/dts目录下。
适当修改system-top.dts后在arch/arm/boot/dts目录下的Makefile中增加system-top.dtb。

3)采用默认配置

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- xilinx_zynq_defconfig

4)编译内核

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage -j10

编译完成后的arch/arm/boot下的zImage

5)编译设备树

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- system-top.dtb -j10

4. 编译rootfs

petalinux-build -c rootfs

images/linux下的rootfs.tar.gz或rootfs.tar.bz2。

5. 启动开发板

将内核镜像文件zImage、内核设备树文件以及根文件系统从image.ub文件中分离出来;而将 bit 文件从BOOT.BIN文件中分离出来,这样SD卡中将会存在5部分内容:zImage、dtb、rootfs、bit 以及 BOOT.BIN(fsbl 镜像与u-boot镜像的集合体)

1)SD卡两个分区:FAT32和EXT4。

/dev/mmcblk0p1       vfat           98.4M     15.7M     82.7M  16% /media/sd-mmcblk0p1
/dev/mmcblk0p2       ext4           14.2G     56.0M     13.4G   0% /media/sd-mmcblk0p2

2)拷贝BOOT.BIN、zImage、system-top.dtb(或system.dtb)、system.bit到FAT分区。

3)解压文件系统到EXT4分区。

sudo tar -xvf rootfs.tar.gz -C /media/sd-mmcblk0p2
sync
umount /media/sd-mmcblk0p2

4)配置uboot

env default -a
setenv bitstream_load_address 0x100000
setenv bitstream_image system.bit
setenv bitstream_size 0x300000
setenv kernel_img zImage
setenv dtbnetstart 0x2000000
setenv netstart 0x2080000
setenv default_bootcmd 'if mmcinfo; then run uenvboot; echo Copying Linux from SD to RAM... && load mmc 0 ${bitstream_load_address} ${bitstream_image} && fpga loadb 0 ${bitstream_load_address} ${bitstream_size} && run cp_kernel2ram && run cp_dtb2ram && bootz ${netstart} - ${dtbnetstart}; fi'

saveenv

boot
console=ttyPS0,115200 earlycon root=/dev/mmcblk0p2 rw rootwait

七、应用

source /opt/zynq_7000_toolchain/2020.1/environment-setup-cortexa9t2hf-neon-xilinx-linux-gnueabi

arm-xilinx-linux-gnueabi- --sysroot=/opt/zynq_7000_toolchain/2020.1/sysroots/cortexa9t2hf-neon-xilinx-linux-gnueabi -mfloat-abi=hard -mfpu=neon

八、sd卡制作

可用ubuntu或开发版制作启动SD卡(支持fdisk命令)。
参考:《嵌入式Linux开发指南》6.2.10制作SD启动卡

root@canfs:~# fdisk /dev/mmcblk0

The number of cylinders for this disk is set to 3881.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): m
Command Action
a       toggle a bootable flag
b       edit bsd disklabel
c       toggle the dos compatibility flag
d       delete a partition
l       list known partition types
n       add a new partition
o       create a new empty DOS partition table
p       print the partition table
q       quit without saving changes
s       create a new empty Sun disklabel
t       change a partition's system id
u       change display/entry units
v       verify the partition table
w       write table to disk and exit
x       extra functionality (experts only)

Command (m for help): p
Disk /dev/mmcblk0: 30 GB, 31927042048 bytes, 62357504 sectors
3881 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
/dev/mmcblk0p1    0,130,3     1023,254,63       8192   62357503   62349312 29.7G  c Win95 F       AT32 (LBA)

Command (m for help): d
Selected partition 1

Command (m for help): p
Disk /dev/mmcblk0: 30 GB, 31927042048 bytes, 62357504 sectors
3881 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type

Command (m for help): d
No partition is defined yet!

Command (m for help): n
Partition type
   p   primary partition (1-4)
   e   extended
p
Partition number (1-4): 1
First sector (63-62357503, default 63): 2048
Last sector or +size{,K,M,G,T} (2048-62357503, default 62357503): +512M

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (Win95 FAT32 (LBA))

Command (m for help): a
Partition number (1-4): 1

Command (m for help): n
Partition type
   p   primary partition (1-4)
   e   extended
p
Partition number (1-4): 2
First sector (63-62357503, default 63): 2048
Sector 2048 is already allocated
First sector (1050624-62357503, default 1050624):
Using default value 1050624
Last sector or +size{,K,M,G,T} (1050624-62357503, default 62357503):
Using default value 62357503

Command (m for help): p
Disk /dev/mmcblk0: 30 GB, 31927042048 bytes, 62357504 sectors
3881 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
/dev/mmcblk0p1 *  0,32,33     65,101,36         2048    1050623    1048576  512M  c Win95 F       AT32 (LBA)
/dev/mmcblk0p2    65,101,37   1023,254,63    1050624   62357503   61306880 29.2G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table
[ 3473.120406]  mmcblk0: p1 p2
sudo mkfs.vfat -F 32 -n boot /dev/sdb1
sudo mkfs.ext4 -L rootfs /dev/sdb2

参考

  1. Zynq Linux USB Device Driver wiki
  2. 使用Petalinux定制自己的linux系统
  3. 解析Zynq的加载方式
posted @ 2023-11-12 21:52  yuxi_o  阅读(32)  评论(0编辑  收藏  举报