赞
踩
在PL端设计两个IP核,具有中断功能。在PS端调用这两个IP核,IP核1计算完成后,如果其调用次数小于10,则继续调用,否则结束运行,IP核2每次计算完成后,如果其调用次数小于5,则继续调用,否则结束运行。
为了简单起见,我们实现两个简单的IP核,它们分别实现向量加法和向量减法的功能。如下
adder.cpp
#include"adder.h"
void adder(int* a,int* b,int* c,int len){
#pragma HLS INTERFACE s_axilite port=return bundle=CTRL
#pragma HLS INTERFACE s_axilite port=len bundle=CTRL
#pragma HLS INTERFACE m_axi depth=100 port=a offset=slave bundle=A
#pragma HLS INTERFACE m_axi depth=100 port=b offset=slave bundle=B
#pragma HLS INTERFACE m_axi depth=100 port=c offset=slave bundle=C
for(int i=0;i<len;i++){
#pragma HLS PIPELINE
c[i]=a[i]+b[i];
}
}
adder.h
void adder(int* a,int* b,int* c,int len);
#include"subber.h"
void subber(int* a,int* b,int *c,int len){
#pragma HLS INTERFACE s_axilite port=return bundle=CTRL
#pragma HLS INTERFACE s_axilite port=len bundle=CTRL
#pragma HLS INTERFACE m_axi depth=100 port=a offset=slave bundle=A
#pragma HLS INTERFACE m_axi depth=100 port=b offset=slave bundle=B
#pragma HLS INTERFACE m_axi depth=100 port=c offset=slave bundle=C
for(int i=0;i<len;i++){
#pragma HLS PIPELINE II=1
c[i]=a[i]-b[i];
}
}
注意通过concat ip将两个中断拼接,然后连接到zynq的IRQ中断管脚。如下图所示:
#include <stdio.h>
#include "xscugic.h"
#include "xil_exception.h"
#include "sleep.h"
#include"xadder.h"
#include"xsubber.h"
#define ADDER_DEVICE_ID XPAR_ADDER_0_DEVICE_ID
#define SUBBER_DEVICE_ID XPAR_SUBBER_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define ADDER_IRPT_INTR XPAR_FABRIC_ADDER_0_INTERRUPT_INTR
#define SUBBER_IRPT_INTR XPAR_FABRIC_SUBBER_0_INTERRUPT_INTR
static XAdder adder;
static XSubber subber;
static XScuGic INTCInst;
static void PL_intr_Handler(void *param);
static int IntcInitFunction(u16 DeviceId);
static int cnt1=0,cnt2=0;
int a[10000];
int b[10000];
int c[10000];
void start_adder(){
XAdder_Set_a_r(&adder, (u32)a);
XAdder_Set_b_r(&adder, (u32)b);
XAdder_Set_c_r(&adder, (u32)c);
XAdder_Set_len(&adder, (u32)10);
XAdder_Start(&adder);
}
void start_subber(){
XSubber_Set_a_r(&subber, (u32)a);
XSubber_Set_b_r(&subber, (u32)b);
XSubber_Set_c_r(&subber, (u32)c);
XSubber_Set_len(&subber, (u32)1000000);
XSubber_Start(&subber);
}
static void PL_intr_Handler(void *param){
int sw_id = (int)param;
printf("INTR ID=%d\n",sw_id);
if(sw_id==1){
if(cnt1<5){
printf("SUBBER %d,END\n\r",cnt1);
XSubber_InterruptClear(&subber,1);
cnt1++;
start_subber();
}
else{
printf("SUBBER %d,END\n\r",cnt1);
XSubber_InterruptClear(&subber, (u32)1);
}
}
if(sw_id==2){
//cnt2++;
if(cnt2<10){
printf("ADDER %d,END\n\r",cnt2);
XAdder_InterruptClear(&adder,1);
cnt2++;
start_adder();
}
else
{
printf("ADDER %d,END\n\r",cnt2);
XAdder_InterruptClear(&adder, (u32)0x1);
}
}
}
int IntcInitFunction(u16 DeviceId){
XScuGic_Config *IntcConfig;
int status;
// Interrupt controller initialisation
IntcConfig = XScuGic_LookupConfig(DeviceId);
status = XScuGic_CfgInitialize(&INTCInst, IntcConfig,
IntcConfig->CpuBaseAddress);
if(status != XST_SUCCESS) return XST_FAILURE;
// Call to interrupt setup
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
&INTCInst);
Xil_ExceptionEnable();
// Connect SW1~SW3 interrupt to handler
status = XScuGic_Connect(&INTCInst,
ADDER_IRPT_INTR,
(Xil_ExceptionHandler)PL_intr_Handler,
(void *)2);
if(status != XST_SUCCESS) return XST_FAILURE;
status = XScuGic_Connect(&INTCInst,
SUBBER_IRPT_INTR,
(Xil_ExceptionHandler)PL_intr_Handler,
(void *)1);
if(status != XST_SUCCESS) return XST_FAILURE;
//
XScuGic_Enable(&INTCInst,ADDER_IRPT_INTR );
XScuGic_Enable(&INTCInst,SUBBER_IRPT_INTR);
XAdder_InterruptEnable(&adder, (u32)1);
XAdder_InterruptGlobalEnable(&adder);
XSubber_InterruptEnable(&subber, (u32)1);
XSubber_InterruptGlobalEnable(&subber);
Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
return XST_SUCCESS;
}
int main(void){
//
XAdder_Initialize(&adder,ADDER_DEVICE_ID);
XSubber_Initialize(&subber,SUBBER_DEVICE_ID);
IntcInitFunction(INTC_DEVICE_ID);
XAdder_Start(&adder);
XSubber_Start(&subber);
while(1);
return 0;
}
可以看到,adder ip核运行了10次,subber ip核运行了5次,符合预期!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。