跳转到主要内容

开发者分享 | 在 Zynq UltraScale 器件上通过 Vitis 创建 Linux 用户应用

judy 提交于

Linux 嵌入式设计中最基本的任务之一是创建用户应用程序。

在本篇博文中,我们将探讨如何在 Vitis™ 中使用 UIO 驱动框架创建简单的 Linux 用户应用。

<strong>1. 硬件设计</strong>

本次使用的是 Zynq UltraScale+ MPSoC ZCU104 评估板。但是,无论您使用任何器件,下列步骤都应适用。

先使用 Vivado® 来创建了块设计。建立了 Zynq UltraScale PS,并把 AXI GPIO 连接到 ZCU104 评估板上的 4 个 LED。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113268-01…; alt=""></center>

地址映射如下所示:
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113269-02…; alt=""></center>

首先 在创建 XSA 时使用了以下选项:
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113271-03…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113272-04…; alt=""></center>

<strong>2. Linux 镜像</strong>

如果您使用开发板,则建议使用 BSP(如果存在)。

但在本示例中,我们使用模板来创建镜像。已经添加了 UIO 驱动程序,用于 AXI GPIO。

然后创建了 sysroot,在 Vitis 中需要使用它来进行交叉编译。
<pre>petalinux-create -t project --template zynqMP -n zcu104_linux

cd zcu104_linux

petalinux-config --get-hw-description=&lt;path to xsa&gt;</pre>

依次选择“DTG Settings -> (zcu104-revc)MACHINE_NAME”
<pre>
petalinux-config -c kernel
Select Device Drivers -&gt; Userspace I/O drivers
&lt;*&gt; Userspace I/O platform driver with generic IRQ handing
&lt;*&gt; Userspace platform driver with generic irq and dynamic memory</pre>

将 system-user.dtsi 替换为:
<pre>/include/ "system-conf.dtsi"

/ {

chosen {

bootargs = "earlycon clk_ignore_unused uio_pdrv_genirq.of_id=generic-uio";

stdout-path = "serial0:115200n8";

};

};

&axi_gpio_0 {

compatible = "generic-uio";

};</pre>

然后,运行以下命令:
<pre>petalinux-build

cd images/linux
petalinux-build --sdk
petalinux-package --sysroot</pre>

<strong>3. 创建平台</strong>

这并非必要步骤,因为用户只需在 Vitis 中使用 sysroot 即可。

但为了便于使用,我们可以创建一个平台并在 Vitis 中使用此平台来创建 Linux 应用。

首先,设置平台文件。

把平台文件组织为一种文件夹结构。这并非必要步骤,但用户需要注意 BIF 中的文件路径。

在 Bootgen 中使用 BIF 来创建可启动的镜像。此处我们仅使用占位符文件名。
<pre>mkdir -p sw_comp/src/a53/xrt/image
mkdir sw_comp/src/boot</pre>

<li>将 image.ub、boot.scr 和 rootfs.cpio.gz 文件从 PetaLinux image/linux 文件夹复制到sw_comp/src/a53/image </li>

<li>将 system.bit、bl31.elf、uboot.elf、zynqmp_fsbl(已重命名为 fsbl.elf)和 pmufw.elf 文件从 PetaLinux image/linux 文件夹复制到sw_comp/src/boot</li>

创建 BIF:
<pre>the_ROM_image:

{

[fsbl_config] a53_x64

[bootloader] &lt;zcu104_base/boot/fsbl.elf&gt;

[pmufw_image] &lt;zcu104_base/boot/pmufw.elf&gt;

[destination_device=pl] &lt;system.bit&gt;

[destination_cpu=a53-0, exception_level=el-3, trustzone] &lt;zcu104_base/boot/bl31.elf&gt;

[destination_cpu=a53-0, exception_level=el-2] &lt;zcu104_base/boot/u-boot.elf&gt;

}</pre>

将 linux.bif 复制到sw_comp/src/boot。现在,在 Vitis 中创建一个新平台工程,如下所示:
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113273-05…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113274-06…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113275-07…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113276-08…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113277-09…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113278-10…; alt=""></center>

这样就会在zcu104_base/export中创建平台。

<strong>4. 在 Vitis 中创建 Linux 镜像</strong>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113279-11…; alt=""></center>

选择“从存储库中选择平台 (Select a platform from therepository)”,单击 + 图标并浏览至您的平台。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113280-12…; alt=""></center>
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113281-13…; alt=""></center>

创建新应用:
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113282-14…; alt=""></center>

此处可以看到,“应用设置 (Application settings)”默认使用的是平台中的设置。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113283-15…; alt=""></center>

选择“空白应用 (Empty Application)”模板,因为我们将创建自己的自定义应用。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113284-16…; alt=""></center>

右键单击 led_test 应用下的 src 文件夹,然后选择“新建 (New)”->“文件 (File)”
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113285-17…; alt=""></center>

指定其文件名 (.c),然后单击“完成 (Finish)”。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113286-18…; alt=""></center>

现在,即可将以下代码复制到其中。这是一个简单的 UIO 示例,可用于开关 LED。
<pre>#include &lt;stdio.h&gt;

#include &lt;stdlib.h&gt;

#include &lt;unistd.h&gt;

#include &lt;sys/mman.h&gt;

#include &lt;fcntl.h&gt;

#define GPIO_MAP_SIZE 0x10000

#define GPIO_DATA 0x00

#define GPIO_TRI 0x04

#define LED_NUM 256

#define LED_DELAY 10000000

int main(int argc, char *argv[])

{

int fd;

char *uiod = "/dev/uio0";

void *gpio_ptr;

volatile int Delay;

printf("AXI GPIO UIO test.\n");

// open the UIO device file to allow access to the device in user space

fd = open(uiod, O_RDWR);

if (fd &lt; 1) {

printf("Invalid UIO device file:%s.\n", uiod);

return -1;

}

// mmap the GPIO device into user space

gpio_ptr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

if (gpio_ptr == MAP_FAILED) {

printf("Mmap call failure.\n");

return -1;

}

// set bit0 on the GPIO to be output

// see pg144 for ref

*((volatile unsigned *)(gpio_ptr + GPIO_TRI)) = 0x0;

// Toggle the LED

while (1) {

int i;

unsigned char led_pin = 0x0;

for (i = 0; i &lt; LED_NUM; i++) {

*((volatile unsigned *)(gpio_ptr + GPIO_DATA)) = led_pin;

for (Delay = 0; Delay &lt; LED_DELAY; Delay++);

*((volatile unsigned *)(gpio_ptr + GPIO_DATA)) = 0x0;

led_pin++;

}

}

// unmap the GPIO device from user space

munmap(gpio_ptr, 4096);

return 0;

}</pre>

选择系统工程,然后单击锤子图标。这样即可构建可执行文件,并创建启动镜像。
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113287-19…; alt=""></center>

<strong>5. 在硬件上执行测试</strong>

将所有镜像从led_app_system\Debug\sd_card复制到 SD 卡上。

启动后,将自动装载 SD 卡。

在此处更改目录,并执行led_app.elf,如下所示:
<center><img src="http://xilinx.eetrend.com/files/2020-11/wen_zhang_/100059003-113288-20…; alt=""></center>

同时,您在板上应该还可以看到 LED 闪烁。

使用 Ctrl + c 即可取消。

本文转载自:<span id="profileBt"><a href="https://mp.weixin.qq.com/s?__biz=Mzg3NDAxNzU1MA==&mid=2247487246&idx=1&…; Xilinx技术社区微信公众号</a></span>