【学习笔记】zedboard 按键中断

1、常规定义

#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define GPIO_INTERRUPT_ID	XPAR_XGPIOPS_0_INTR

#define INPUT_BANK	XGPIOPS_BANK2
#define LED	55
#define BTN 54

static XGpioPs mGpioPs;
static XScuGic mXScuGic;
static int Set=0;


2、IO管脚配置

XGpioPs_Config *mGpioPsConfig;
	init_platform();
	mGpioPsConfig = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
	XGpioPs_CfgInitialize(&mGpioPs,mGpioPsConfig,mGpioPsConfig->BaseAddr);

	XGpioPs_SetDirectionPin(&mGpioPs,LED,1);//设置LED为输出方向
	XGpioPs_SetDirectionPin(&mGpioPs,BTN,0);//设置BTN为输入方向
	XGpioPs_SetOutputEnablePin(&mGpioPs,LED,1);//LED输出使能

	XGpioPs_SetIntrTypePin(&mGpioPs,BTN,XGPIOPS_IRQ_TYPE_EDGE_RISING);//指定btn管脚为上升沿触发
	XGpioPs_SetCallbackHandler(&mGpioPs,(void*)&mGpioPs,GpioPsHandler);//设置中断处理函数
	XGpioPs_IntrEnablePin(&mGpioPs,BTN);//使能BTN按键中断
3、中断处理函数

void GpioPsHandler(void *CallBackRef,int bank ,u32 Status)
{
	if(bank != INPUT_BANK)
		return;
	Set ^= 1;
	xil_printf("Status:%d\r\n",Set);
	XGpioPs *pXGpioPs = (XGpioPs *)CallBackRef;
	XGpioPs_IntrDisablePin(pXGpioPs,BTN);
	XGpioPs_WritePin(&mGpioPs,LED,Set);
	if(Set)
	{
		xil_printf("Led on\r\n");
	}else{
		xil_printf("Led off\r\n");
	}

	XGpioPs_IntrEnablePin(pXGpioPs,BTN);
}


4、GIC中断控制器配置

XScuGic_Config *mXScuGic_Config;
	mXScuGic_Config = XScuGic_LookupConfig(INTC_DEVICE_ID);//中断设置查找
	XScuGic_CfgInitialize(&mXScuGic,mXScuGic_Config,mXScuGic_Config->CpuBaseAddress);//GIC初始化

	XScuGic_Disable(&mXScuGic,GPIO_INTERRUPT_ID);//禁止52中断
	XScuGic_SetPriorityTriggerType(&mXScuGic,GPIO_INTERRUPT_ID,0x02,0x01);//设置中断优先级和中断触发方式
	//设置中断服务程序入口地址
	XScuGic_Connect(&mXScuGic,GPIO_INTERRUPT_ID,(Xil_ExceptionHandler)XGpioPs_IntrHandler,(void*)&mGpioPs);
	XScuGic_Enable(&mXScuGic,GPIO_INTERRUPT_ID);//使能52中断
	//异常处理函数
	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,(Xil_ExceptionHandler)XScuGic_InterruptHandler,(void*)&mXScuGic);
	Xil_ExceptionEnable();


你可能感兴趣的:(xilinx)