/*
 * main.c
 *
 *  Created on: 2022825
 *      Author: Administrator
 */


#include <stdio.h>
#include <xparameters.h>
#include <xgpiops.h>
#include <xscugic.h>
#include <xil_exception.h>


#define Input_Pin 47	//PS_KEY
#define Output_Pin  7 //PS_LED

static XGpioPs Gpio; /* The Instance of the GPIO Driver */
static XScuGic GicInstancePtr;

static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status)
{
	XGpioPs *Gpio = (XGpioPs *)CallBackRef;
	u32 DataRead;

	/* Push the switch button */
	DataRead = XGpioPs_ReadPin(Gpio, Input_Pin);
	if (DataRead != 0) {
		XGpioPs_SetDirectionPin(Gpio, Output_Pin, 1);
		XGpioPs_SetOutputEnablePin(Gpio, Output_Pin, 1);
		XGpioPs_WritePin(Gpio, Output_Pin, DataRead);
		AllButtonsPressed = TRUE;
	}
}


int main(void)
{

	XGpioPs_Config *ConfigPtr;
	ConfigPtr = XGpioPs_LookupConfig(XPAR_AXI_GPIO_0_DEVICE_ID);
	XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);

	/* Set the direction for the specified pin to be input */
	XGpioPs_SetDirectionPin(&Gpio, Input_Pin, 0x0);

	/* Set the direction for the specified pin to be output. */
	XGpioPs_SetDirectionPin(&Gpio, Output_Pin, 1);
	XGpioPs_SetOutputEnablePin(&Gpio, Output_Pin, 1);
	XGpioPs_WritePin(&Gpio, Output_Pin, 0x0);

	XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
	IntcConfig = XScuGic_LookupConfig(XPAR_SCUGIC_SINGLE_DEVICE_ID);
	XScuGic_CfgInitialize(&GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);

	//עCPU쳣/жϷ
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
					(Xil_ExceptionHandler)XScuGic_InterruptHandler,
					&GicInstancePtr);

	//ע/󶨾жϷ
	XScuGic_Connect(&GicInstancePtr, XPS_GPIO_INT_ID,
					(Xil_ExceptionHandler)XGpioPs_IntrHandler,
					&Gpio);


	/* Enable falling edge interrupts for all the pins in bank 1. */
	XGpioPs_SetIntrType(&Gpio, XGPIOPS_BANK1, 0xffffffff, 0x00000000, 0x00);

	/* Set the handler for gpio interrupts. */
	XGpioPs_SetCallbackHandler(&Gpio, &Gpio, IntrHandler);


	/* Enable the GPIO interrupts of Bank 1. */
	XGpioPs_IntrEnable(&Gpio, XGPIOPS_BANK1, (1 << (Input_Pin-32)));


	//GICжԴCpuж
	XScuGic_Enable(&GicInstancePtr, XPS_GPIO_INT_ID);

	Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);


}
