1. ZYNQ中断简述
ZYNQ中断类型:普通中断请求(IRQ, Interrupt Request)和快速中断请求(FIQ, Fast Interrupt Request)。
ZYNQ中断源:软件中断(SGI, Software Generated Interrupt)、CPU私有设备中断(PPI, Private Peripheral Interrupt)和共享设备中断(SPI, Shared Peripheral Interrupt)。而PL端的按键中断属于共享设备中断。
ZYNQ随笔——PL端按键中断之裸机设计_第1张图片
2. 硬件平台搭建
在Block Design里添加ZYNQ7 Processing System和AXI_GPIO模块,双击AXI_GPIO设置为输入,且允许中断,驱动外部IO器件(如KEY)。
ZYNQ随笔——PL端按键中断之裸机设计_第2张图片
双击ZYNQ,使能IRQ_F2P,如下图所示。
ZYNQ随笔——PL端按键中断之裸机设计_第3张图片
搭建好的系统结构如下图所示,橘×××的连线表示中断连接:
ZYNQ随笔——PL端按键中断之裸机设计_第4张图片
2. 软件SDK设计
SDK软件设计可以参考官方设计文档,主要API函数有,

  • int XGpio_Initialize(XGpio * InstancePtr,u16 DeviceId)
  • void XGpio_SetDataDirection(XGpio * InstancePtr,unsigned Channel,u32 DirectionMask)
  • XScuGic_Config *XScuGic_LookupConfig(u16 DeviceId)
  • s32 XScuGic_CfgInitialize(XScuGic InstancePtr,XScuGic_Config ConfigPtr,u32 EffectiveAddr)
  • void Xil_ExceptionRegisterHandler(u32 Exception_id, Xil_ExceptionHandler Handler,void *Data)
  • Xil_ExceptionEnable()
  • s32 XScuGic_Connect(XScuGic InstancePtr, u32 Int_Id,Xil_InterruptHandler Handler, void CallBackRef)
  • void XGpio_InterruptEnable(XGpio *InstancePtr, u32 Mask)
  • void XGpio_InterruptGlobalEnable(XGpio *InstancePtr)
  • void XScuGic_Enable(XScuGic *InstancePtr, u32 Int_Id)
    具体代码如下:
    
    XGpio KEYInst;
    XScuGic INTCInst;

int main(void)
{
// initial KEY
int status;
status = XGpio_Initialize(&KEYInst, KEY_DEVICE_ID);
if(status != XST_SUCCESS)
return XST_FAILURE;

// set KEY IO direction as in
XGpio_SetDataDirection(&KEYInst, 1, 0xFF);

// initial interrupt controller
status = IntcInitFunction(INTC_DEVICE_ID, &KEYInst);
if(status != XST_SUCCESS)
    return XST_FAILURE;

while(1);

return 0;

}

//----------------------------------------------------------------------------
// This is the interrupt handler routine for the GPIO for this example
//----------------------------------------------------------------------------
void GpioHandler(void CallbackRef)
{
XGpio
GpioPtr = (XGpio *)CallbackRef;

/* Clear the Interrupt */
XGpio_InterruptClear(GpioPtr, GlobalIntrMask);

}

//----------------------------------------------------------------------------
// Interrupt controller initial function
//----------------------------------------------------------------------------
static int IntcInitFunction(u16 DeviceId, XGpio GpioInstancePtr)
{
XScuGic_Config
IntcConfig;
int status;

// Interrupt controller initialization
IntcConfig = XScuGic_LookupConfig(DeviceId);
status = XScuGic_CfgInitialize(&INTCInst, IntcConfig, IntcConfig->CpuBaseAddress);
if(status != XST_SUCCESS)
    return XST_FAILURE;

// Call interrupt setup function
Xil_ExceptionInit();

Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
        (Xil_ExceptionHandler)XScuGic_InterruptHandler, XScuGicInstancePtr);

Xil_ExceptionEnable();

// Register GPIO interrupt handler
status = XScuGic_Connect(&INTCInst, INTC_GPIO_INTERRUPT_ID,
        (Xil_ExceptionHandler)GpioHandler, (void*)GpioInstancePtr);
if(status != XST_SUCCESS)
    return XST_FAILURE;

// Enable GPIO interrupts
XGpio_InterruptEnable(GpioInstancePtr, 1);
XGpio_InterruptGlobalEnable(GpioInstancePtr);

// Enable GPIO interrupts in the controller
XScuGic_Enable(&INTCInst, INTC_GPIO_INTERRUPT_ID);

return XST_SUCCESS;

}


**3. 编译运行**
下载FPGA代码,以Hardware运行软件