Zynq开发-通过寄存器读写调用HLS IP核

一般情况下,在使用Vivado HLS工具将设计导出为RTL IP时,会附带一个在Xilinx SDK中调用HLS IP核的驱动程序,我们可以直接使用已经写好的驱动程序来使用PL端的ip来为我们做一些计算,本文则旨在直接通过读写寄存器来进行HLS IP的调用。
HLS IP的设计流程以及Vivado中的Block Deisgn在这里略去,主要讲如何使用寄存器来控制我们的IP。
首先我们在system.hdf中查看HLS IP核的寄存器地址:
在这里插入图片描述
因为不使用中断,我们主要关心以下三个寄存器地址
在这里插入图片描述
在HLS提供的驱动文件中,我们能看到更详细的信息
在这里插入图片描述
可以看到,控制信号中,低4位分别是ap_ready、ap_idle、ap_done和ap_start信号,第8位(也就是bit7)是自动重启的信号。因此,我们可以编写如下代码调用HLS IP:

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xmy_cnn.h"
#include"xil_cache.h"
#include "xil_io.h"

u32 myip_isdone(int addr){
	u32 Data;
	Data=Xil_In32(addr);
	return (Data >> 1) & 0x1;
}


int main()
{
    //u32 a=0x10000000;
    //u32 b=0x10001000;
	int* a=(int*)malloc(sizeof(int)*20);
	int* b=(int*)malloc(sizeof(int)*20);
	//int a[30];
	//int b[30];
    int i;
	init_platform();
    for(i=0;i<20;i++)
    	*(a+i)=i;
    Xil_DCacheFlushRange((u32)a,20*sizeof(int));
    print("start\n\r");
    Xil_Out32(0x43c00010,(u32)a);
    Xil_Out32(0x43c00018,(u32)b);
    Xil_Out32(0x43c00000,(u32)0x01);
    while(1){
    	u32 Data;
    	Data=Xil_In32(0x43c00000);
    	if((Data >> 1) & 0x1)
    		break;
    }
    Xil_DCacheInvalidateRange((u32)b,20*sizeof(int));
    for(i=0;i<20;i++){
    	printf("%d\n",b[i]);
    }
    cleanup_platform();
    return 0;
}

得到如下结果,表明HLS IP运行正常(不必理会这个IP实现什么功能)
在这里插入图片描述
同样用数组也能实现:

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xmy_cnn.h"
#include"xil_cache.h"
#include "xil_io.h"

u32 myip_isdone(int addr){
	u32 Data;
	Data=Xil_In32(addr);
	return (Data >> 1) & 0x1;
}


int main()
{
    //u32 a=0x10000000;
    //u32 b=0x10001000;
	//int* a=(int*)malloc(sizeof(int)*20);
	//int* b=(int*)malloc(sizeof(int)*20);
	int a[20];
	int b[20];
    int i;
	init_platform();
    for(i=0;i<20;i++)
    	*(a+i)=i;
    Xil_DCacheFlushRange((u32)a,20*sizeof(int));
    print("start\n\r");
    Xil_Out32(0x43c00010,(u32)a);
    Xil_Out32(0x43c00018,(u32)b);
    Xil_Out32(0x43c00000,(u32)0x01);
    while(1){
    	u32 Data;
    	Data=Xil_In32(0x43c00000);
    	if((Data >> 1) & 0x1)
    		break;
    }
    Xil_DCacheInvalidateRange((u32)b,20*sizeof(int));
    for(i=0;i<20;i++){
    	printf("%d\n",b[i]);
    }
    cleanup_platform();
    return 0;
}

当然,为了更加规范,可以在使用前对ap_idle是否为1进行判断,若为1,则表示可以调用hls ip进行计算

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xmy_cnn.h"
#include"xil_cache.h"
#include "xil_io.h"

u32 myip_isdone(int addr){
	u32 Data;
	Data=Xil_In32(addr);
	return (Data >> 1) & 0x1;
}


int main()
{
    //u32 a=0x10000000;
    //u32 b=0x10001000;
	int* a=(int*)malloc(sizeof(int)*20);
	int* b=(int*)malloc(sizeof(int)*20);
	//int a[20];
	//int b[20];
    int i;
	init_platform();
    for(i=0;i<20;i++)
    	*(a+i)=i;
    Xil_DCacheFlushRange((u32)a,20*sizeof(int));
    print("start\n\r");
    while(1){
    	u32 Data;
    	Data=Xil_In32(0x43c00000);
    	if((Data >> 2) & 0x1)
    		break;
    }
    Xil_Out32(0x43c00010,(u32)a);
    Xil_Out32(0x43c00018,(u32)b);
    Xil_Out32(0x43c00000,(u32)0x01);
    while(1){
    	u32 Data;
    	Data=Xil_In32(0x43c00000);
    	if((Data >> 1) & 0x1)
    		break;
    }
    Xil_DCacheInvalidateRange((u32)b,20*sizeof(int));
    for(i=0;i<20;i++){
    	printf("%d\n",b[i]);
    }
    cleanup_platform();
    return 0;
}

奇怪的是,加入对ap_idle的判断后,使用数组则结果的第一个数会变成-1,不是预期的0,而用malloc则没有这个问题????

上面加入ap_idle后用数组有错解决了,但始终不知道为什么(给Data1加上volatile关键字,不给Data2加上volatile关键字,或者给Data2加volatile关键字而不给Data1加volatile加关键字,运行结果正确,但是两者同时加或者同时不加则结果错误,玄学bug???

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xmy_cnn.h"
#include"xil_cache.h"
#include "xil_io.h"

u32 myip_isdone(int addr){
	u32 Data;
	Data=Xil_In32(addr);
	return (Data >> 1) & 0x1;
}


int main()
{
    //u32 a=0x10000000;
    //u32 b=0x10001000;
	//int* a=(int*)malloc(sizeof(int)*20);
	//int* b=(int*)malloc(sizeof(int)*20);
	int a[20];
	int b[20];
    int i;
	init_platform();
    for(i=0;i<20;i++)
    	*(a+i)=i;
    Xil_DCacheFlushRange((u32)a,20*sizeof(int));
    print("start\n\r");
    while(1){
    	//u32 Data1;
    	volatile u32 Data1;
    	Data1=Xil_In32(0x43c00000);
    	if((Data1 >> 2) & 0x1)
    		break;
    }
    Xil_Out32(0x43c00010,(u32)a);
    Xil_Out32(0x43c00018,(u32)b);
    Xil_Out32(0x43c00000,(u32)0x01);
    while(1){
    	//volatile u32 Data
    	u32 Data2;
    	Data2=Xil_In32(0x43c00000);
    	if((Data2 >> 1) & 0x1)
    		break;
    }
    Xil_DCacheInvalidateRange((u32)b,20*sizeof(int));
    Xil_DCacheInvalidateRange((u32)a,20*sizeof(int));
    for(i=0;i<20;i++){
    	printf("%d\n",b[i]);
    }
    cleanup_platform();
    return 0;
}

总结:使用malloc时,无论volatile怎么加,结果都没问题,数组时,两个Data必须两者之中其一加volatile结果才会正确,建议使用malloc


版权声明:本文为qq_40268672原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。