#include <stdio.h>
#include "platform.h"
#include "xil_types.h"
#include "xtmrctr.h"
#include "xparameters.h"
#include "xil_io.h"
#include "xil_exception.h"
#include "xscugic.h"
#include "xgpio.h"
#define BTNS_DEVICE_ID XPAR_BUTTONS_DEVICE_ID
#define BTN_INT XGPIO_IR_CH1_MASK
#define LEDS_DEVICE_ID XPAR_LEDS_DEVICE_ID
XGpio LEDInst;
XGpio BTNInst;
static int btn_value;
void BTN_Intr_Handler(void *InstancePtr) {
print(" Button Interrupt \n \r ");
print("\r\n");
print("\r\n");
XGpio_InterruptDisable(&BTNInst, BTN_INT);
if ((XGpio_InterruptGetStatus(&BTNInst) & BTN_INT) != BTN_INT) {
return;
}
btn_value = XGpio_DiscreteRead(&BTNInst, 1);
XGpio_DiscreteWrite(&LEDInst, 1, btn_value);
(void) XGpio_InterruptClear(&BTNInst, BTN_INT);
XGpio_InterruptEnable(&BTNInst, BTN_INT);
}
XScuGic InterruptController;
static XScuGic_Config *GicConfig;
extern char inbyte(void);
void Timer_InterruptHandler(void *data, u8 TmrCtrNumber)
{
print(" Interrupt acknowledged \n \r ");
print("\r\n");
print("\r\n");
XTmrCtr_Stop(data,TmrCtrNumber);
XTmrCtr_Reset(data,TmrCtrNumber);
XTmrCtr_Start(data,TmrCtrNumber);
}
int SetUpInterruptSystem(XScuGic *XScuGicInstancePtr)
{
XGpio_InterruptEnable(&BTNInst, BTN_INT);
XGpio_InterruptGlobalEnable(&BTNInst);
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler) XScuGic_InterruptHandler,
XScuGicInstancePtr);
Xil_ExceptionEnable();
return XST_SUCCESS;
}
int ScuGicInterrupt_Init(u16 DeviceId,XTmrCtr *TimerInstancePtr)
{
int Status;
GicConfig = XScuGic_LookupConfig(DeviceId);
if (NULL == GicConfig) {
return XST_FAILURE;
}
Status = XScuGic_CfgInitialize(&InterruptController, GicConfig,
GicConfig->CpuBaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = SetUpInterruptSystem(&InterruptController);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = XScuGic_Connect(&InterruptController,
XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR,
(Xil_ExceptionHandler)XTmrCtr_InterruptHandler,
(void *)TimerInstancePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = XScuGic_Connect(&InterruptController,
XPAR_FABRIC_BUTTONS_IP2INTC_IRPT_INTR,
(Xil_ExceptionHandler) BTN_Intr_Handler,
(void *) &BTNInst);
if (Status != XST_SUCCESS)
return XST_FAILURE;
XGpio_InterruptEnable(&BTNInst, 1);
XGpio_InterruptGlobalEnable(&BTNInst);
XScuGic_Enable(&InterruptController, XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR);
XScuGic_Enable(&InterruptController, XPAR_FABRIC_BUTTONS_IP2INTC_IRPT_INTR);
return XST_SUCCESS;
}
int main()
{
int xStatus;
xStatus = XGpio_Initialize(&LEDInst, LEDS_DEVICE_ID);
if (xStatus != XST_SUCCESS)
return XST_FAILURE;
xStatus = XGpio_Initialize(&BTNInst, BTNS_DEVICE_ID);
if (xStatus != XST_SUCCESS)
return XST_FAILURE;
XGpio_SetDataDirection(&LEDInst, 1, 0x00);
XGpio_SetDataDirection(&BTNInst, 1, 0xFF);
XTmrCtr TimerInstancePtr;
print("##### Application Starts #####\n\r");
print("\r\n");
xStatus = XTmrCtr_Initialize(&TimerInstancePtr,XPAR_AXI_TIMER_0_DEVICE_ID);
if(XST_SUCCESS != xStatus)
print("TIMER INIT FAILED \n\r");
XTmrCtr_SetHandler(&TimerInstancePtr,
Timer_InterruptHandler,
&TimerInstancePtr);
XTmrCtr_SetResetValue(&TimerInstancePtr,
0,
0xf8000000);
XTmrCtr_SetOptions(&TimerInstancePtr,
XPAR_AXI_TIMER_0_DEVICE_ID,
(XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION ));
xStatus=ScuGicInterrupt_Init(XPAR_PS7_SCUGIC_0_DEVICE_ID,&TimerInstancePtr);
if(XST_SUCCESS != xStatus)
print(" :( SCUGIC INIT FAILED \n\r");
XTmrCtr_Start(&TimerInstancePtr,0);
print("timer start \n\r");
print("Wait for the Timer interrupt to tigger \r\n");
print("########################################\r\n");
print(" \r\n");
while(1)
{
}
cleanup_platform();
return 0;
}