如何在 Vitis 中使用 UIO 驱动框架创建简单的 Linux 用户应用

电子说

1.2w人已加入

描述

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

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

1

硬件设计

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

先使用 Vivado 来创建了块设计。建立了 Zynq UltraScale PS,并把 AXI GPIO 连接到 ZCU104 评估板上的 4 个 LED。

地址映射如下所示:

首先 在创建 XSA 时使用了以下选项:

2

Linux 镜像

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

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

然后创建了 sysroot,在 Vitis 中需要使用它来进行交叉编译。

petalinux-create -t project --template zynqMP -n zcu104_linux

cd zcu104_linux

petalinux-config --get-hw-description=

依次选择“DTG Settings -> (zcu104-revc)MACHINE_NAME”

petalinux-config -c kernel
Select Device Drivers -> Userspace I/O drivers
<*> Userspace I/O platform driver with generic IRQ handing
<*> Userspace platform driver with generic irq and dynamic memory

将 system-user.dtsi 替换为:

/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";

};

然后,运行以下命令:

petalinux-build

cd images/linux
petalinux-build --sdk
petalinux-package --sysroot

3

创建平台

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

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

首先,设置平台文件。 

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

在 Bootgen 中使用 BIF 来创建可启动的镜像。此处我们仅使用占位符文件名。

mkdir -p sw_comp/src/a53/xrt/image
mkdir sw_comp/src/boot

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

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

创建 BIF:

the_ROM_image:

{

[fsbl_config] a53_x64

[bootloader]

[pmufw_image]

[destination_device=pl]

[destination_cpu=a53-0, exception_level=el-3, trustzone]

[destination_cpu=a53-0, exception_level=el-2]

}

将 linux.bif 复制到sw_comp/src/boot。现在,在 Vitis 中创建一个新平台工程,如下所示:

Linux

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

4

在 Vitis 中创建 Linux 镜像

选择“从存储库中选择平台 (Select a platform from therepository)”,单击 + 图标并浏览至您的平台。

Linux

创建新应用:

此处可以看到,“应用设置 (Application settings)”默认使用的是平台中的设置。

选择“空白应用 (Empty Application)”模板,因为我们将创建自己的自定义应用。

右键单击 led_test 应用下的 src 文件夹,然后选择“新建 (New)”->“文件 (File)”

指定其文件名 (.c),然后单击“完成 (Finish)”。

Linux

现在,即可将以下代码复制到其中。这是一个简单的 UIO 示例,可用于开关 LED。

#include

#include

#include

#include

#include

#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. ");

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

fd = open(uiod, O_RDWR);

if (fd < 1) {

printf("Invalid UIO device file:%s. ", 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. ");

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 < LED_NUM; i++) {

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

for (Delay = 0; Delay < 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;

}

选择系统工程,然后单击锤子图标。这样即可构建可执行文件,并创建启动镜像。

5

在硬件上执行测试

将所有镜像从led_app_systemDebugsd_card复制到 SD 卡上。

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

在此处更改目录,并执行led_app.elf,如下所示:

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

使用 Ctrl + c 即可取消。

责任编辑:xj

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

文章出处:【微信公众号:XILINX技术社区】欢迎添加关注!文章转载请注明出处。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分