当前位置:   article > 正文

Zynq PL端中断实验_zynq pl中断

zynq pl中断

实验设计

在PL端设计两个IP核,具有中断功能。在PS端调用这两个IP核,IP核1计算完成后,如果其调用次数小于10,则继续调用,否则结束运行,IP核2每次计算完成后,如果其调用次数小于5,则继续调用,否则结束运行。

PL端IP核设计

为了简单起见,我们实现两个简单的IP核,它们分别实现向量加法和向量减法的功能。如下

向量加法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];
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

adder.h


void adder(int* a,int* b,int* c,int len);

  • 1
  • 2
  • 3

向量减法IP

#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];
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Block Design

注意通过concat ip将两个中断拼接,然后连接到zynq的IRQ中断管脚。如下图所示:
在这里插入图片描述

SDK代码编写


#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;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

运行结果

可以看到,adder ip核运行了10次,subber ip核运行了5次,符合预期!
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/830715
推荐阅读
相关标签
  

闽ICP备14008679号