#include "stdio.h"
#include "xparameters.h"
#include "xgpiops.h"
#include "xscugic.h"
#include "sleep.h"
//ps_led
#define MIO15_LED 15
#define MIO47_KEY 47
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
//GPIO的中断号 52
#define GPIO_INTERRUPT_ID XPAR_XGPIOPS_0_INTR
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
XGpioPs_Config *ConfigPtr;
XGpioPs Gpio;
XScuGic Intc;
XScuGic_Config *IntcConfig;
void IntrHandler();
void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,u16 GpioIntrId);
u32 key_press;
int main(){
u32 led_value = 0;
printf("GPIO interupt TEST !");
//根据器件id,查找器件的配置信息,
ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
//初始化GPIO驱动
XGpioPs_CfgInitialize(&Gpio, ConfigPtr,ConfigPtr->BaseAddr);
//把gpio的方向设置为输出(0:输入,1:输出)
XGpioPs_SetDirectionPin(&Gpio, MIO15_LED, 1);
//设置输出使能(0:关闭/1:打开)
XGpioPs_SetOutputEnablePin(&Gpio, MIO15_LED, 1);
XGpioPs_SetDirectionPin(&Gpio, MIO47_KEY, 0x0);
SetupInterruptSystem(&Intc, &Gpio, GPIO_INTERRUPT_ID);
while(1){
if(key_press){
led_value = ~led_value;
key_press = 0;
XGpioPs_WritePin(&Gpio, MIO15_LED,led_value);
XGpioPs_IntrClearPin(&Gpio, MIO47_KEY);
usleep(200000);
XGpioPs_IntrEnablePin(&Gpio, MIO47_KEY);
}
}
return 0;
}
void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,u16 GpioIntrId)
{
/*
* Initialize the interrupt controller driver so that it is ready to
* use.
*/
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
XScuGic_CfgInitialize(GicInstancePtr, IntcConfig,IntcConfig->CpuBaseAddress);
/*
* Connect the interrupt controller interrupt handler to the hardware
* interrupt handling logic in the processor.
*/
//初始化异常
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,(Xil_ExceptionHandler)XScuGic_InterruptHandler,GicInstancePtr);
/* 使能处理器中断 */
Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
//关联中断处理函数
XScuGic_Connect(GicInstancePtr, GpioIntrId,(Xil_ExceptionHandler)IntrHandler,(void *)Gpio);
/*设置中断类型 */
XGpioPs_SetIntrTypePin(Gpio, MIO47_KEY, XGPIOPS_IRQ_TYPE_EDGE_FALLING);
/* 打开中断使能 */
XGpioPs_IntrEnablePin(Gpio, MIO47_KEY);
/*为GPIO器件使能中断 */
XScuGic_Enable(GicInstancePtr, GpioIntrId);
}
void IntrHandler(){
printf("interupt detected!
");
key_press = 1;
XGpioPs_IntrDisablePin(&Gpio, MIO47_KEY);
}