本文主要是介绍ZYNQ通过AXI_BRAM实现PL与PS数据互联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、AXI BRAM Controller
PS模块通过axi_bram模块,可以使用AXI接口读写PL内的Block RAM,实现PS与PL之间的数据互联.。
axi_bram需要与Block Memory Generator模块共同使用,axi_bram将PS的AXI操作转化为bram的控制接口时序。
二、配置
1、首先搭建好zynq的基础框架,保证设备可以正常启动,非此处讨论重点,略去不谈。
2、添加axi bram controller ,其配置如下
其中,memory此处不能设置,且data width 最小宽度为32位。可以在后续地址分配处设置。
3、添加block memory generater
整体框图如下
4、查看地址分配
接下来生成bit流,将硬件资源导入petlainux中,生成BOOT.BIN 和image.ub
将生成的BOOT.BIN和内核放到板子上,启动系统。
三、编程
1、直接读取数据
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#define BRAM_CTRL_0 0x82000000
#define BRAM_CTRL_1 0x84000000
#define DATA_LEN 10int main(int argc, char **argv)
{unsigned int *map_base0;unsigned int *map_base1;int fd = open("/dev/mem", O_RDWR | O_SYNC);if (fd < 0) {printf("can not open /dev/mem \n");return (-1);} printf("/dev/mem is open \n");map_base0 = mmap(NULL, DATA_LEN * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BRAM_CTRL_0);map_base1 = mmap(NULL, DATA_LEN * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BRAM_CTRL_1);if (map_base0 == 0 || map_base1 == 0 ) { printf("NULL pointer\n");} else {printf("mmap successful\n");} unsigned long addr;unsigned int content;int i = 0;printf("\nwrite data to bram\n");for (i = 0; i < DATA_LEN; i++) {addr = (unsigned long)(map_base0 + i); content = i + 2;map_base0[i] = content;printf("%2dth data, address: 0x%lx data_write: 0x%x\t\t\n", i, addr, content);} printf("\nread data from bram\n");for (i = 0; i< DATA_LEN; i++) {addr = (unsigned long)(map_base1 + i); content = map_base1[i];printf("%2dth data, address: 0x%lx data_read: 0x%x\t\t\n", i, addr, content);} close(fd);munmap(map_base0, DATA_LEN);munmap(map_base1, DATA_LEN);return 0;
}
2、也可以将BRAM与socket进行联合使用,将PC端的数据发到PS端,PS端放在PL端,PL端再发给PS端,PS端发回PC端。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#define PORT 3333#define BRAM_CTRL0 0x82000000
#define BRAM_CTRL1 0x84000000
#define DATA_LEN 100unsigned int *map_base0;
unsigned int *map_base1;void udp_server(int sockfd)
{socklen_t len;struct sockaddr_in server_addr;int n;int opt = 1;len = sizeof(server_addr);server_addr.sin_family = AF_INET;server_addr.sin_addr.s_addr = htonl(INADDR_ANY);server_addr.sin_port = htons(PORT);setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {printf("can not bind\n");exit(1);} while (1) {char buf[1024];printf("\n========wait for client's request========\n");// n = recvfrom(sockfd, (void*)map_base0, 400, 0, (struct sockaddr *)&server_addr, &len);//memcpy(buf, map_base0, 1024);n = recvfrom(sockfd, buf, 400, 0, (struct sockaddr *)&server_addr, &len);memcpy(map_base0,buf,400);printf("\nret(%d), errno(%d)\n", n,errno);printf("\nreceive client's data: %s\n", buf);sendto(sockfd, map_base0, n, 0, (struct sockaddr *)&server_addr, len);memcpy(buf, map_base1, 1024);printf("\nsend data to client: %s\n", buf);}close(sockfd);
}int main(int argc, char **argv)
{int sockfd, devfd;if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {printf("create socket false\n");exit(1);}printf("create socket sucessful\r\n");if ((devfd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {printf("can not open /dev/mem \n");return (-1);}printf("\nopen /dev/mem successful\n");map_base0 = mmap(NULL, DATA_LEN * 4, PROT_READ | PROT_WRITE, MAP_SHARED, devfd, BRAM_CTRL0);printf("\nmap_base0(%x), errno(%d)\n", map_base0,errno);map_base1 = mmap(NULL, DATA_LEN * 4, PROT_READ | PROT_WRITE, MAP_SHARED, devfd, BRAM_CTRL1);if (map_base0 ==0 || map_base1 == 0) {printf("NULL pointer \n");}else {printf("mmap successful\n");}
/*{uint8_t buffer[400];memset(buffer,0x34,400);//memset(map_base0,0x12,400);memcpy(map_base0,buffer,400);printf("mem test:\n");for(int i=0; i<100; i++){printf("%x ",map_base0[i]); }fflush(stdout);}*/udp_server(sockfd);exit(0);
}
四、体会
这种操作思路就是把PL中的ram块当做PS端的ram在使用,也就是说存在FPGA的ram中的数据,可以通过axi接口的方式直接供arm调用,效率非常高,极大的降低了开发的难度,节省了大量的时间。
五、参考
https://blog.csdn.net/yohe12/article/details/105264853/
https://blog.csdn.net/botao_li/article/details/86301995?spm=1001.2014.3001.5501
https://blog.csdn.net/yohe12/article/details/105264853/
六、源工程
这篇关于ZYNQ通过AXI_BRAM实现PL与PS数据互联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!