#include <stdio.h>
#include "led2_ip.h"
#include "xil_io.h"
#include "xil_types.h"
#include "axi_interrupt.h"
#include "xparameters.h"
#include "xil_io.h"
#include "xil_exception.h"
#include "xscugic.h"

XScuGic InterruptController; /* Instance of the Interrupt Controller */
static XScuGic_Config *GicConfig;/* The configuration parameters of the controller */


int ScuGicInterrupt_Init()
{
	int Status;
	/*
	 * Initialize the interrupt controller driver so that it is ready to
	 * use.
	 * */
	Xil_ExceptionInit();

	GicConfig = XScuGic_LookupConfig(XPAR_PS7_SCUGIC_0_DEVICE_ID);
	if (NULL == GicConfig) {
		return XST_FAILURE;
	}

	Status = XScuGic_CfgInitialize(&InterruptController, GicConfig,
			GicConfig->CpuBaseAddress);

	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Setup the Interrupt System
	 * */

	/*
	 * Connect the interrupt controller interrupt handler to the hardware
	 * interrupt handling logic in the ARM processor.
	 */
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
			(Xil_ExceptionHandler) XScuGic_InterruptHandler,
			(void *) &InterruptController);


	/*
	 * Connect a device driver handler that will be called when an
	 * interrupt for the device occurs, the device driver handler performs
	 * the specific interrupt processing for the device
	 */
	Status = XScuGic_Connect(&InterruptController,91,
			(Xil_ExceptionHandler)AXI_INTERRUPT_Intr_DefaultHandler,
			(void *) &InterruptController);

	XScuGic_Enable(&InterruptController, 91);

	AXI_INTERRUPT_EnableInterrupt(XPAR_AXI_INTERRUPT_0_BASEADDR);

	/*
	 * Enable interrupts in the ARM
	 */
	Xil_ExceptionEnable();

	//Only used for edge sensitive Interrupts
	XScuGic_SetPriorityTriggerType(&InterruptController, 91,
						0xa0, 3);

	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

}


int main()
{

	int xstatus;
	int write_loop_index;
	int read_loop_index;
	int TEST_AXI_USER_NUM_REG = 4;

	xstatus = ScuGicInterrupt_Init();

	if (xstatus != XST_SUCCESS) {
		return XST_FAILURE;
	}


	LED2_IP_mWriteReg(XPAR_LED2_IP_0_BASEADDR, 0, 0xAB);


	while(1)
	{
	}

	return 0;
}
