ZYNQ+PetaLinux控制AXI GPIO实现LED灯亮灭

    科技2024-03-25  13

    文章目录

    代码下载代码编译0 应用需求1 PetaLinux安装1.1 准备工作1.2 PetaLinux安装 2 Vivado硬件设计2.1 系统框图2.2 ZYNQ配置2.3 AXI GPIO配置2.4 Block Design2.5 Top Design 3 PetaLinux工程3.1 新建工程3.2 配置硬件信息3.3 配置内核3.4 配置文件系统3.5 编译3.6 合并BOOT文件 4 运行4.1 启动4.2 LED控制4.3 编译GPIO应用程序

    代码下载

    https://github.com/qiweiii-git/qwi12_petaled

    代码编译

    1.在linux下使用./makeall.sh即可自动编译Vivado及PetaLinux工程。 如何使用linux自动编译工程请参考利用Linux自动编译Vivado工程 2.在Windows下打开Vivado,运行source run.tcl即可自动创建工程并编译。

    0 应用需求

    在Xilinx的PetaLinux中,以控制AXI GPIO的方式来控制LED灯亮灭。

    1 PetaLinux安装

    1.1 准备工作

    PetaLinux工具要求主机系统 /bin/sh 为“bash”。使用sudo dpkg-reconfigure dash命令更改默认的shell /bin/sh(选择“否”)。 查看目前是dash还是bash命令:ls -l /bin/sh 安装库:

    sudo apt-get install libssl-dev:i386 flex:i386 bison:i386 libselinux1:i386 libncurses5 libncurses5-dev libc6:i386 libstdc++6:i386 zlib1g:i386 libssl-dev tftpd tftp openbsd-inetd tofrodos:i386 iproute2:i386 gawk:i386 gcc net-tools:i386 zlib1g-dev:i386

    1.2 PetaLinux安装

    将安装文件(下载链接: https://pan.baidu.com/s/14aQF8kvf9_DELPji3momIw 提取码: n4ju)复制到Ubuntu中。 PetaLinux2015.4将会安装到/opt目录中。

    sudo su chmod +x petalinux-v2015.4-final-installer.run ./petalinux-v2015.4-final-installer.run /opt

    2 Vivado硬件设计

    2.1 系统框图

    系统框图如下:

    2.2 ZYNQ配置

    2.2.1 UART配置 2.2.2 SD配置 2.2.3 DDR配置

    2.3 AXI GPIO配置

    地址分配:

    2.4 Block Design

    2.5 Top Design

    将GPIO输出连接至顶层端口输出。 并为LED端口分配管脚:

    set_property PACKAGE_PIN M14 [get_ports LED] set_property IOSTANDARD LVCMOS33 [get_ports LED]

    3 PetaLinux工程

    3.1 新建工程

    3.1.1 导出Vivado硬件描述文件 3.1.2 复制hdf及bit至工程目录 3.1.3 新建PetaLinux工程

    petalinux-create --type project --template zynq --name qwi12_petaled

    3.2 配置硬件信息

    3.2.1 进入petalinux工程目录

    cd qwi12_petaled

    3.2.2 配置硬件信息

    petalinux-config --get-hw-description ../

    等待一定时间后出现配置页面 无需任何改动,保存后退出。 等待一定时间后配置完成。

    3.3 配置内核

    petalinux-config -c kernel

    等待一定时间后出现配置页面 无需任何改动,保存后退出。

    3.4 配置文件系统

    petalinux-config -c rootfs

    等待一定时间后出现配置页面

    3.5 编译

    petalinux-build

    等待一定时间后编译完成

    3.6 合并BOOT文件

    petalinux-package --boot --fsbl ./images/linux/zynq_fsbl.elf --fpga ../qwi12_petaled.bit --uboot --force

    等待一定时间后合并完成 在images/linux中找到BOOT.BIN及image.ub,复制出来。

    4 运行

    4.1 启动

    4.1.1 将SD卡格式化成FAT32格式

    4.1.2 将BOOT.BIN及image.ub复制到SD卡中 4.1.3 启动 设备上电后,通过UART打印启动信息 4.1.4 登陆

    Login: root Password: root

    登陆完成:

    4.2 LED控制

    4.2.1 查看GPIO设备

    ls /sys/class/gpio

    4.2.2 查看GPIO控制器标签

    cat /sys/class/gpio/gpiochip905/label

    显示该GPIO控制器地址为0x41100000,即是我们设计控制LED的GPIO控制器。而gpiochip906为PS端的GPIO控制器。 4.2.3 使能GPIO控制器

    echo -n 905 > /sys/class/gpio/export

    使能后GPIO设备就会多出gpio905 4.2.4 设置GPIO控制器方向为输出(默认为输入)

    echo out > /sys/class/gpio/gpio905/direction

    设置为输出方向后,LED才能被控制起来,由于GPIO控制器默认数值为0,硬件上设计低电平时LED亮,因此设置为输出方向后,LED即亮起来。 4.2.5 查看当前GPIO数值

    cat /sys/class/gpio/gpio905/value

    当前GPIO值为0,LED亮。

    4.2.6 控制GPIO来控制LED亮灭(1为灭,0为亮)

    echo 1 > /sys/class/gpio/gpio905/value echo 0 > /sys/class/gpio/gpio905/value

    4.3 编译GPIO应用程序

    编写GPIO应用程序,实现LED每隔1s切换一次状态,即1s亮1s灭。

    int main() { int value; int export; int direction; // export GPIO controller export = open("/sys/class/gpio/export", O_WRONLY); if (export < 0) { printf("Cannot open GPIO controller export\n"); exit(1); } write(export, "905", 4); close(export); printf("GPIO controller export successfully\n"); // Modify GPIO controller direction direction = open("/sys/class/gpio/gpio905/direction", O_RDWR); if (direction < 0) { printf("Cannot open GPIO controller direction\n"); exit(1); } write(direction, "out", 4); close(direction); printf("GPIO controller direction changed to output successfully\n"); // Modify GPIO controller value value = open("/sys/class/gpio/gpio905/value", O_RDWR); if (value < 0) { printf("Cannot open GPIO controller value\n"); exit(1); } // Swap GPIO control value each 1 second while (1) { sleep(1); write(value,"1", 2); printf("GPIO controller value changed to 1 successfully\n"); sleep(1); write(value,"0", 2); printf("GPIO controller value changed to 0 successfully\n"); } }

    4.3.1 创建Petalinux App

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

    4.3.2 将GPIO应用程序代码复制到Petalinux App目录中

    cp ../../software/qwi12_petaled.c components/apps/qwi12_petaled/

    4.3.3 按照第三章中步骤编译Petalinux 4.3.4 将BOOT.BIN及image.ub复制到SD卡中 4.3.5 设备启动后执行GPIO应用程序,实现LED每隔1s切换一次状态

    /bin/qwi12_petaled

    4.3.6 将GPIO应用程序设置为上电自启动 修改Makefile

    vi components/apps/qwi12_petaled/Makefile

    在Makefile的install:中添加如下代码:

    $(TARGETINST) -d -p 0755 qwi12_petaled /etc/init.d/qwi12_petaled $(TARGETINST) -s /etc/init.d/qwi12_petaled /etc/rc5.d/S99qwi12_petaled

    设备上电后就会自动运行GPIO应用程序 至此,在Petalinux中控制AXI GPIO来控制LED灯亮灭实验已完成。

    Processed: 0.014, SQL: 9