【大道至简】Zynq 7020 AXI GPIO
#include "xparameters.h"
#include "xgpio.h"
#include "xil_exception.h"
#include "xscugic.h"
static u16 GlobalIntrMask; //GPIO channel mask that is needed by the Interupt Handler
XScuGic Intc; //the Instance of the Interrupt COntroller Driver
XGpio Gpio; //the Instance of the GPIO driver
int GpioIntr4key(XScuGic* IntcInstancePtr, XGpio* InstancePtr, u16 DeviceId, u16 IntrId, u16 IntrMask, u32* DataRead);
int GpioSetupIntrSystem(XScuGic* IntcInstancePtr, XGpio* InstancePtr, u16 IntrId, u16 IntrMask);
static void GpioHandler(void* CallbackRef);
int main(void)
{
int Status = -1;
u32 DataRead;
Status = GpioIntr4key(&Intc, &Gpio, XPAR_GPIO_1_DEVICE_ID, XPAR_FABRIC_AXI_GPIO_1_IP2INTC_IRPT_INTR, 1, &DataRead);
if(0==Status)
{
}
else
{
}
for(;;)
{
}
return XST_SUCCESS;
}
// @brief : application which test the interrupts when enabled in the GPIO
// @param : IntcInstancePtr is a reference to the Interrupt Controller driver Instance
// @param : InstancePtr is a reference to the GPIO driver Instance
// @param : DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from xparameters.h
// @param : IntrId is XPAR_<INTC_instance>_<GPIO_instance>_IP2INTC_IRPT_INTR value from xparameters.h
// @param : IntrMask is the GPIO channel mask
// @param : DataRead is the pointer where the data read from GPIO input is returned
// @retval : - XST_SUCCESS if the function is successful
// : - XST_FAILURE if the function is not successful
// @note : none
int GpioIntr4key(XScuGic* IntcInstancePtr, XGpio* InstancePtr, u16 DeviceId, u16 IntrId, u16 IntrMask, u32* DataRead)
{
int Status;
//----------------
//Initialize the GPIO driver. If an error occurs then exit.
Status = XGpio_Initialize(IntcInstancePtr, DeviceId);
if(XST_SUCCESS != Status)
{
return XST_FAILURE;
}
//----------------
Status = GpioSetupIntrSystem(IntcInstancePtr, InstancePtr, DeviceId, IntrId, IntrMask);
if(XST_SUCCESS != Status)
{
return XST_FAILURE;
}
for(unsigned long int i=0; i<1000000000; i++)
{}
//GpioDisableIntr(IntcInstancePtr, InstancePtr, IntrId, IntMask);
return Status;
}
// @brief : This function performs the GPIO set up for Interrupt
// @param : IntcInstancePtr is a reference to the Interrupt Controller driver Instance
// @param : InstancePtr is a reference to the GPIO driver Instance
// @param : DeviceId is the XPAR_<GPIO_instance>_<GPIO_instance>_IP2INTC_IRPT_INTR value from xparameters.h
// @param : IntrId is XPAR_<INTC_instance>_<GPIO_instance>_IP2INTC_IRPT_INTR value from xparameters.h
// @param : IntrMask is the GPIO channel mask
// @retval : - XST_SUCCESS if the function is successful
// - otherwise XST_FAILURE
// @note : none
int GpioSetupIntrSystem(XScuGic* IntcInstancePtr, XGpio* InstancePtr, u16 IntrId, u16 IntrMask)
{
int Result = -1;
GlobalIntrMask = IntrMask;
XScuGic_Config* IntcConfig;
//----------------
//Initialize the interrupt controller driver so that it is ready to use.
IntcConfig = XScuGic_LookupConfig(XPAR_SCUGIC_SINGLE_DEVICE_ID);
if(NULL == IntcConfig)
{
return XST_FAILURE;
}
//----------------
Result = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);
if(XST_SUCCESS != Result)
{
return XST_FAILURE;
}
//----------------
//0x3 -- Rising Edge Trigger
XScuGic_SetPriorityTriggerType(IntcInstancePtr, IntrId, 0xa0, 0x3);
//----------------
//connect the interrupt handler that will be called when an interrupt occurs for the device.
Result = XScuGic_Connect(IntcInstancePtr, IntrId, (Xil_ExceptionHandler)GpioHandler, InstancePtr);
if(XST_SUCCESS != Result)
{
return Result;
}
//enable the interrupt for the GPIO device
XScuGic_Enable(IntcInstancePtr, IntrId);
//enable the GPIO channel interrupts so that push button can be detected and enable interrupts for the GPIO device
XGpio_InterruptEnable(InstancePtr, IntrMask);
XGpio_InterruptGlobalEnable(InstancePtr);
//initialize the exception table and register the interrupt controller handler with the exception table
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, IntcInstancePtr);
//enable non-critical exceptions
Xil_ExceptionEnable();
return XST_SUCCESS;
}
static void GpioHandler(void* CallbackRef)
{
XGpio* GpioPtr = (XGpio*)CallbackRef;
//user code start
//...
//user code end
//clear the interrupt
XGpio_InterruptClear(GpioPtr, GlobalIntrMask);
}