Vivado HLS创建AXI接口进行数据传输

在zynq软硬协同开发中,AXI接口是PS和PL交互很重要的一个总线协议,本次介绍如何通过HLS创建具有AXI接口的IP核同其余部件交互。

HLS code

#include
void mem_test(volatile int *a){
#pragma HLS INTERFACE m_axi depth=50 port=a offset=slave bundle=MASTER_BUS
#pragma HLS INTERFACE s_axilite port=return bundle=CTRL_BUS
//Port a is assigned to an AXI4 master interface
int i;
int buff[50];
//memcpy creates a burst access to memory
memcpy(buff,(const int*)a,50*sizeof(int));
for(i=0; i < 50; i++){
buff[i] = buff[i] + 100;
}
memcpy((int *)a,buff,50*sizeof(int));
}

代码如上,函数传入参数a为起始地址,memcpy实现将地址为a处的连续50*4字节拷贝到函数内的buffer内部,buffer每项自增100,再将buffer拷贝给a。
#pragma HLS INTERFACE m_axi depth=50 port=a offset=slave bundle=MASTER_BUS实现将a打包为m_axi接口,以方便数据传输(m_axi支持突发传输,有利于增大吞吐量)

Block Design

Vivado HLS创建AXI接口进行数据传输_第1张图片
连接生成的IP核和BRAM(BRAM CONTROLLER MODE),生成bit流,打开sdk

CPU端

代码如下:

#include 
#include
#include "platform.h"
#include "xil_printf.h"
#include "xmem_test_hw.h"
#include "xmem_test.h"
int main()
{
    init_platform();

    int i;
    for(i=0;i<100;i++){
    	Xil_Out32(XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+i*4,i);
    }
    for(i=0;i<100;i++){
    	int rev=Xil_In32(XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+i*4);
    	printf("%d\n",rev);
    }
    XMem_test HlsXMem_test;
    XMem_test_Config *ExamplePtr;
    printf("Look Up the device configuration.\n");
    ExamplePtr = XMem_test_LookupConfig(XPAR_MEM_TEST_0_DEVICE_ID);
    if (!ExamplePtr) {
    printf("ERROR: Lookup of accelerator configuration failed.\n\r");
    return XST_FAILURE;
    }
    printf("Initialize the Device\n");
    long status = XMem_test_CfgInitialize(&HlsXMem_test, ExamplePtr);
    if (status != XST_SUCCESS) {
    printf("ERROR: Could not initialize accelerator.\n\r");
    return(-1);
    }
    XMem_test_Set_a(&HlsXMem_test,XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+50*4);
    XMem_test_Start(&HlsXMem_test);
    while (XMem_test_IsDone(&HlsXMem_test) == 0);

    int p=XMem_test_Get_a(&HlsXMem_test);
    printf("%x\n",p);
    for(i=0;i<100;i++){
        	int rev=Xil_In32(XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+4*i);
        	printf("%d\n",rev);
        }
    cleanup_platform();
    return 0;
}

该代码实现将0到99一次写入BRAM的前400个字节(100个int),给IP核传入参数XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR+50*4,意为将BRAM的后50个整型变量增加100,然后打印输出,结果如下图:
Vivado HLS创建AXI接口进行数据传输_第2张图片
原先的50-99变成了150-199,而前50个整数不变。
(注意本次实验HLS CODE中offset=slave)

你可能感兴趣的:(Vivado HLS创建AXI接口进行数据传输)