1 #define LED2        57
 2 #define LED1        56
 3 #define KEY2        55
 4 #define KEY1        54
 5 
 6 #define GPIO_DEVICE_ID        XPAR_XGPIOPS_0_DEVICE_ID
 7 XGpioPs Gpio;
 8 
 9 void Gpio_Init(void){
10     XGpioPs_Config *ConfigPtr;
11 
12     ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
13     XGpioPs_CfgInitialize(&Gpio, ConfigPtr,ConfigPtr->BaseAddr);
14 
15     XGpioPs_SetDirectionPin(&Gpio, LED1, 1);        //设置为输出模式
16     XGpioPs_SetOutputEnablePin(&Gpio, LED1, 1);     //输出模式需要开启使能
17     XGpioPs_WritePin(&Gpio, LED1, 1);               //设置输出状态"1"
18 
19     XGpioPs_SetDirectionPin(&Gpio, LED2, 1);
20     XGpioPs_SetOutputEnablePin(&Gpio, LED2, 1);
21     XGpioPs_WritePin(&Gpio, LED2, 1);
22 
23     XGpioPs_SetDirectionPin(&Gpio, KEY1, 0);    //设置为输入模式
24     XGpioPs_SetDirectionPin(&Gpio, KEY2, 0);25 }

 

#include "xparameters.h"
#include "xgpiops.h"
#include "xscugic.h"
#include "xil_exception.h"


#define INTC_DEVICE_ID        XPAR_SCUGIC_SINGLE_DEVICE_ID
#define GPIO_INTERRUPT_ID    XPAR_XGPIOPS_0_INTR

static XScuGic Intc; /* The Instance of the Interrupt Controller Driver */

void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,
                u16 GpioIntrId){

    XScuGic_Config *IntcConfig;
    Xil_ExceptionInit();

    IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);

    XScuGic_CfgInitialize(GicInstancePtr, IntcConfig,
                    IntcConfig->CpuBaseAddress);

    Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
                (Xil_ExceptionHandler)XScuGic_InterruptHandler,
                GicInstancePtr);
    XScuGic_Connect(GicInstancePtr, GpioIntrId,
                (Xil_ExceptionHandler)IntrHandler,
                (void *)Gpio);


    XScuGic_Enable(GicInstancePtr, GpioIntrId);

    XGpioPs_SetIntrTypePin(Gpio, KEY1,  XGPIOPS_IRQ_TYPE_EDGE_FALLING);
    XGpioPs_SetIntrTypePin(Gpio, KEY2,  XGPIOPS_IRQ_TYPE_EDGE_FALLING);
    
    XGpioPs_IntrEnablePin(Gpio, KEY1);
    XGpioPs_IntrEnablePin(Gpio, KEY2);

    Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
}

static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status)
{
    XGpioPs *Gpio_cb = (XGpioPs *)CallBackRef;
    if (XGpioPs_IntrGetStatusPin(Gpio_cb, KEY1)){
        XGpioPs_WritePin(&Gpio, LED1, 1);
        XGpioPs_WritePin(&Gpio, LED2, 0);
        XGpioPs_IntrClearPin(Gpio_cb, KEY1);
    }
    else if (XGpioPs_IntrGetStatusPin(Gpio_cb, KEY2)){
        XGpioPs_WritePin(&Gpio, LED1, 0);
        XGpioPs_WritePin(&Gpio, LED2, 1);
        XGpioPs_IntrClearPin(Gpio_cb, KEY2);
    }
}

SetupInterruptSystem(&Intc, &Gpio, GPIO_INTERRUPT_ID);