转自:ZYNQ 中断详解_zynq pl至ps中断 清除中断标志位-CSDN博客
//*******************BTN8按键产生中断打印*******************//
#include <stdio.h>
#include <string.h>
#include "vectors.h"
#include "xil_exception.h"
#include "xil_io.h"
//MIO Register
#define DIRM_1 0xE000A244
#define INT_EN_1 0xE000A250
#define INT_DIS_1 0xE000A254
#define INT_STAT_1 0xE000A258//READ:1-int has occurred WRITE:1-clear int status bit
#define INT_TYPE_1 0xE000A25C
#define INT_POLARITY_1 0xE000A260
#define INT_ANY_1 0xE000A264
//ICC
#define ICCICR 0xF8F00100//CPU Interface Control Register 配置CPU接口
#define ICCPMR 0xF8F00104//Interrupt priority mask Register 配置CPU中断优先级
//ICD
#define ICDDCR 0xF8F01000//Distributor Control Register 控制开启或关闭中断配置
#define ICDISER1 0xF8F01104//Interrupt Set-enable Register 使能ICD中断寄存器
#define ICDICER1 0xF8F01184//Interrupt clear-enable Register 不使能ICD中断寄存器
#define ICDIPR13 0xf8f01434//Interrupt priority Register ICD中断优先级
#define ICDIPTR13 0xF8F01834//Interrupt Processor Targets Register 配置CPU接口选择
#define ICDICFR3 0xF8F01C0C//Interrupt Configuration Register 配置ICD中断触发模式
#define ICDICPR1 0xf8f01284//Interrupt clear-pending Register 清除中断寄存器
#define SPI_STATUS_0 0xF8F01D04//SPI Status Register 0
#define ICCIAR 0xF8F0010C//Interrupt Acknowledge Register
#define ICCEOIR 0xF8F00110//End Of Interrupt Register cpu结束响应,中断状态由active->inactive
//定义中断向量表结构体,Handler为函数,Data为函数Handler的参数
typedef struct {
Xil_ExceptionHandler Handler;
void *Data;
} XExc_VectorTableEntry;
//申明中断向量表
extern XExc_VectorTableEntry XExc_VectorTable[];
void InterruptHandler_IRQ(void); //IRQ中断处理函数
void Int_Init(void); //GIC中断初始化, ICD寄存器组
void Gpio_Init(void);
void CPU_Init(void);
int main(void)
{
XExc_VectorTable[5].Handler =(Xil_ExceptionHandler)InterruptHandler_IRQ;
XExc_VectorTable[5].Data = NULL;
//initialize
Int_Init();
CPU_Init();
Gpio_Init();
xil_printf("begin\r\n");
Xil_ExceptionEnable();//这句放在这里,避免开机中断
while(1);
return 0;
}
void InterruptHandler_IRQ(void)
{
u32 i;
u32 IntID;
u32 IntIDFull;
xil_printf("come in 1\r\n");
for (i = 0; i < 50000000; ++i) {}//延时防抖动
//获取目前发生的中断号(#52)
IntIDFull = Xil_In32(ICCIAR);
IntID = IntIDFull & 0x3FF;
Xil_Out32(ICDICPR1, 0xFFFFFFFF);//ICD 中断清标志位
if(52 == IntID)
{
Xil_Out32(INT_DIS_1, 0x3FFFFF);//GPIO中断屏蔽
Xil_Out32(INT_STAT_1, 0x3FFFFF);//GPIO清除标志位
xil_printf("come in 2\r\n");
Xil_Out32(INT_EN_1, 0x40000);//GPIO中断使能
}
Xil_Out32(ICCEOIR, IntIDFull);//cpu结束响应,中断 状态由active->inactive
}
//对#52号中断
void Int_Init(void)
{
Xil_Out32(ICDDCR, 0UL);//关闭中断配置
Xil_Out32(ICDICER1, 0x100000);//不使能 #52
// Xil_Out32(ICDICFR3, 0x100);//电平触发
Xil_Out32(ICDICFR3, 0x300);//边沿触发
Xil_Out32(ICDIPR13, 0xA0);//优先级A0
Xil_Out32(ICDIPTR13, 0x01);//处理器为CPU0
Xil_Out32(ICDICPR1, 0xFFFFFFFF);//ICD 所有中断清标志位
Xil_Out32(ICDISER1, 0x100000);//使能 #52
Xil_Out32(ICDDCR, 0x01);//中断分配器更新状态
}
//GPIO MIO_50 MIO50(0x40000) 中断#52(0x100000)
void Gpio_Init(void)
{
// Xil_Out32(DIRM_1, 0x40000);//output modem 没影响
Xil_Out32(INT_DIS_1, 0x3FFFFF);//中断屏蔽 [21:0]
// Xil_Out32(INT_TYPE_1, 0x000000);//电平触发
// Xil_Out32(INT_POLARITY_1, 0x3FFFFF);//高电平
Xil_Out32(INT_TYPE_1, 0x3FFFFF);//边沿触发
Xil_Out32(INT_POLARITY_1, 0x3FFFFF);//上升沿
Xil_Out32(INT_ANY_1, 0x0);//单边沿
Xil_Out32(INT_STAT_1, 0x3FFFFF);//GPIO清除标志位
Xil_Out32(INT_EN_1, 0x40000);//中断使能
}
void CPU_Init(void)
{
//中断优先级都是A0,优先级高于F0,CPU可接受这些中断
Xil_Out32(ICCPMR,0xF0);
//处理器能接收IRQ,使能中断信号连接到处理器
Xil_Out32(ICCICR,0x07);
}