本文主要是介绍ZYNQ linux下AXI_BRAM的使用方法,PS与PL端数据交互,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、AXI总线、AXI接口、AXI协议
总线是一组传输通道,是各种逻辑器件构成的传输数据的通道;接口是一种连接标准,又常被称为物理接口;协议是数据传输的规则。
PS与PL连接方式主要是通过AXI总线进行的。ZYNQ上的总线协议有AXI4, AXI4-Lite, AXI4-Stream三种总线协议。而PS与PL之间的接口(AXI-GP、AXI-HP、AXI-ACP)只支持AXI4与AXI4-Lite这两种总线协议。
2、BRAM简介
Block RAM是PL部分的存储器阵列,就相当于在PL中开辟一片空间来存储数据,通过端口来进行读写。
BRAM设置模式:
1、单端口:通过一个端口对BRAM进行读写。
2、简单双端口:有两个端口连接到BRAM,一个端口只读,一个端口只写
3、真双端口:两个端口都可以对BRAM进行读写。
3、vivado设计
前提:创建好了工程并导入了ZYNQ核
打开AXI GP0 和 AXI GP1的接口
将M_AXI_GP0_ACLK连接到FCLK_CLK0
导入两个BRAM 控制器
设置为单端口
导入一个BRAM
设置为真双端口模式
进行设计连接
进行1的时候需要修改主机从GP0 改为GP1
最终的生成图
可以在address editor中查看自动分配的BRAM控制器的地址,不要随意更改地址,因为其他地址可能被其他设备占用了。
axi_bram_ctrl_0 0x40000000
axi_bram_ctrl_1 0x80000000
接下来生成bit流,将硬件资源导入petlainux中,生成BOOT.BIN 和image.ub
将生成的BOOT.BIN和内核放到板子上,启动系统。
进行编程:
#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 0x40000000
#define BRAM_CTRL_1 0x80000000
#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;
}
运行效果
也可以将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>
#define PORT 3333#define BRAM_CTRL0 0x40000000
#define BRAM_CTRL1 0x80000000
#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) {printf("\n========wait for client's request========\n");n = recvfrom(sockfd, map_base0, 1024, 0, (struct sockaddr *)&server_addr, &len);char buf[1024];memcpy(buf, map_base0, 1024);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);}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);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");}udp_server(sockfd);exit(0);
}
最终实现相关效果
这篇关于ZYNQ linux下AXI_BRAM的使用方法,PS与PL端数据交互的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!