zedboard 中断

/*该文件包含使用GPIO驱动程序(XGpioPs)在一个设计实例操作中断驱动模式。
*该示例使用GPIO的中断能力检测按钮,事件并设置基于输入输出的LED。用户需要按评估板上所有的开关SW1-SW5从这个例子中退出。
*@注意:*本示例假定有一个UART设备在硬件设计。*/

/***************************** Include Files *********************************/
#include "xparameters.h"
#include "xgpiops.h"
#include "xscugic.h"
#include "xil_exception.h"
#include <xil_printf.h>
/************************** 常量定义 *****************************/
/*
 常量的定义
以下常量映射到硬件实例的名称*是EDK XPS系统创建的。
他们在这里定义,这样*用户可以很容易地更改所有需要的设备id在一个地方。
 */
//#define XPAR_XGPIOPS_0_DEVICE_ID XPAR_PS7_GPIO_0_DEVICE_ID z xp。h中
#define GPIO_DEVICE_ID		XPAR_XGPIOPS_0_DEVICE_ID//0
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID//0
#define GPIO_INTERRUPT_ID	XPAR_XGPIOPS_0_INTR//中断号52 GPIO中断号52
/*
以下常量定义所使用的GPIObank
 */
#define INPUT_BANK	XGPIOPS_BANK0  /* Bank 0 of the GPIO Device */
#define OUTPUT_BANK	XGPIOPS_BANK1  /* Bank 1 of the GPIO Device */
/*
 * The following constants define the positions of the buttons of the GPIO.
*以下常量定义GPIO的按钮的状态。
 */
#define GPIO_ALL_BUTTONS	0xFFFF//

/*
 * The following constant determines which buttons must be pressed to cause
 * interrupt processing to stop.
 * 下面的常数决定哪些按钮必须按下来触发中断
 */
#define GPIO_EXIT_CONTROL_VALUE	0x1F

#define printf			xil_printf	/* Smalller foot-print printf */

/**************************** 类型定义*******************************/

/*****************宏定义*********************/

/************************** 函数原型******************************/

static int GpioIntrExample(XScuGic *Intc, XGpioPs *Gpio, u16 DeviceId,
			   u16 GpioIntrId);
static void IntrHandler(void *CallBackRef, int Bank, u32 Status);
static int SetupInterruptSystem(XScuGic *Intc, XGpioPs *Gpio, u16 GpioIntrId);

/**************************变量定义 *****************************/

/*
 * 以下是全局声明,以便它们将被清零,因此它们是
 *从调试方便。
 * The following are declared globally so they are zeroed and so they are
 * easily accessible from a debugger.
 */
static XGpioPs Gpio; /* The Instance of the GPIO Driver GPIO驱动器的常量*/

static XScuGic Intc; /* The Instance of the Interrupt Controller Driver中断控制器的常量 */

static u32 AllButtonsPressed; /* Intr status of the bank bank的中断状态 */

/****************************************************************************/
/**
*主函数调用GPIO中断实例
* @param	None.
*
* @return
*		- XST_SUCCESS if the example has completed successfully.
*		- XST_FAILURE if the example has failed.
*
* @note		None.
*
*****************************************************************************/
int main(void)
{
	int Status;

	xil_printf("GPIO Interrupt Example Test \r\n");

	/*
	 * Run the GPIO interrupt example, specify the parameters that
	 * are generated in xparameters.h.
	 */
	Status = GpioIntrExample(&Intc, &Gpio, GPIO_DEVICE_ID,
				 GPIO_INTERRUPT_ID);

	if (Status != XST_SUCCESS) {
		xil_printf("GPIO Interrupt Example Test Failed\r\n");
		return XST_FAILURE;
	}

	xil_printf("Successfully ran GPIO Interrupt Example Test\r\n");
	return XST_SUCCESS;
}

/****************************************************************************/
/**
*此功能显示GPIO器件的中断fucntionality的用法。
*这是负责初始化GPIO器件,设置中断和
*提供一前景循环,使得中断可以发生在后台。
* @param	Intc is a pointer to the XScuGic driver Instance.
* intc指向XScuGic driver Instance
* @param	Gpio is a pointer to the XGpioPs driver Instance.
* @param	DeviceId is the XPAR_<Gpio_Instance>_PS_DEVICE_ID value
*		from xparameters.h.
* @param	GpioIntrId is XPAR_<GIC>_<GPIO_Instance>_VEC_ID value
*		from xparameters.h
*
* @return	- XST_SUCCESS if the example has completed successfully.
*		- XST_FAILURE if the example has failed.
*
* @note		None
*
*****************************************************************************/
int GpioIntrExample(XScuGic *Intc, XGpioPs *Gpio, u16 DeviceId, u16 GpioIntrId)
{
	XGpioPs_Config *ConfigPtr;
	int Status;

	/*
	 * 初始化 Gpio driver.
	 */
	ConfigPtr = XGpioPs_LookupConfig(DeviceId);
	if (ConfigPtr == NULL) {
		return XST_FAILURE;
	}
	XGpioPs_CfgInitialize(Gpio, ConfigPtr, ConfigPtr->BaseAddr);

	/*自检
	 * Run a self-test on the GPIO device.
	 */
	Status = XGpioPs_SelfTest(Gpio);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * 设置bank0的方向寄存器,所有引脚都是输入
	 * Setup direction register of bank0, so
	 * that all the pins are configured as inputs.
	 */

	XGpioPs_SetDirection(Gpio, INPUT_BANK, ~GPIO_ALL_BUTTONS);//INPUT_BANK是bank编号,


	/*
	 * 输入模式,使能led的引脚
	 * Set the direction for all signals to be
	 * outputs and Enable the Output enable for the LED Pins.
	 */
	XGpioPs_SetDirection(Gpio, OUTPUT_BANK, GPIO_ALL_BUTTONS);
	XGpioPs_SetOutputEnable(Gpio, OUTPUT_BANK, GPIO_ALL_BUTTONS);

	/*设置,使得中断可以发生
	 * Setup the interrupts such that interrupt processing can occur. If
	 * an error occurs then exit.
	 */
	Status = SetupInterruptSystem(Intc, Gpio, GPIO_INTERRUPT_ID);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	printf("\n\rPush each of the 5 buttons once to exit\n\r");
	AllButtonsPressed = FALSE;

	/*
	 * Loop forever while the button changes are handled by the interrupt
	 * level processing.
	 */
	while(AllButtonsPressed == FALSE);

	printf("\n\r The GPIO Interrupt example has passed Successfully.\n\r");

	return XST_SUCCESS;
}

/****************************************************************************/
/**
* This function is the user layer callback function for the bank 0 interrupts of
* the GPIO device. It checks if all the switches have been pressed to stop the
* interrupt processing and exit from the example.
*此功能是用户层的回调函数为GPIO的bank0中断
它如果所有的交换机都按停止检查
*从例如中断处理和退出。
*CallBackRef是指向上层回调函数的指针
*Status是gpio的bank的中断状态
* @param	CallBackRef is a pointer to the upper layer callback reference.
* @param	Status is the Interrupt status of the GPIO bank.
*
* @return	None.
*
* @note		None.
*
******************************************************************************/
static void IntrHandler(void *CallBackRef, int Bank, u32 Status)
{
	XGpioPs *Gpio = (XGpioPs *)CallBackRef;
	static u32 ButtonsChanged;

	/*
	 * Do nothing if the intr is generated for a different bank.
	 */
	if (Bank != INPUT_BANK) {
		return;
	}

	ButtonsChanged |= Status;

	/*
	 * Set the LEDs.
	 */
	XGpioPs_Write(Gpio, OUTPUT_BANK, ButtonsChanged);

	if (ButtonsChanged == GPIO_EXIT_CONTROL_VALUE) {
		/*
		 * Five buttons are pressed to mark the completion of the test.
		 */
		AllButtonsPressed = TRUE;
		ButtonsChanged = 0;
	}
}


/*****************************************************************************/
/**
*他的功能设置中断系统的例子。它使落bank0所有的GPIO器件管脚边缘中断。
*参数GicInstancePtr是一个指针XScuGic驱动程序实例。
*参数GpioInstancePtr包含一个指向到被连接到中断的GPIO的常量
*参数GpioIntrId是中断Id,
* XPAR_<GICPS>_ <GPIOPS_instance>_VEC_ID从价值
* xparameters.h。
*
返回:XST_SUCCESS如果成功,否则XST_FAILURE。
* This function sets up the interrupt system for the example. It enables falling
* edge interrupts for all the pins of bank 0 in the GPIO device.
*
* @param	GicInstancePtr is a pointer to the XScuGic driver Instance.
* @param	GpioInstancePtr contains a pointer to the instance of the GPIO
*		component which is going to be connected to the interrupt
*		controller.
* @param	GpioIntrId is the interrupt Id and is typically
*		XPAR_<GICPS>_<GPIOPS_instance>_VEC_ID value from
*		xparameters.h.
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note		None.
*
****************************************************************************/
static int SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,
				u16 GpioIntrId)
{
	int Status;

	XScuGic_Config *IntcConfig; /* Instance of the interrupt controller
	中断控制器常量 */

	Xil_ExceptionInit();

	/*初始化中断控制器
	 */
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	if (NULL == IntcConfig) {
		return XST_FAILURE;
	}

	Status = XScuGic_CfgInitialize(GicInstancePtr, IntcConfig,
					IntcConfig->CpuBaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}


	/*
	 * 把中断控制器中断处理程序连接到硬件
*中断处理逻辑的处理器。
	 * Connect the interrupt controller interrupt handler to the hardware
	 * interrupt handling logic in the processor.
	 */
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
				(Xil_ExceptionHandler)XScuGic_InterruptHandler,
				GicInstancePtr);

	/*
	 * Connect the device driver handler that will be called when an
	 * interrupt for the device occurs, the handler defined above performs
	 * the specific interrupt processing for the device.
	 * 连接那些当中断发生的时候将会被调用的设备驱动处理函数,
	 * 上述定义的处理程序执行特定中断处理的设备。
	 */
	Status = XScuGic_Connect(GicInstancePtr, GpioIntrId,
				(Xil_ExceptionHandler)XGpioPs_IntrHandler,
				(void *)Gpio);
	if (Status != XST_SUCCESS) {
		return Status;
	}

	/*使能bank0 的下降沿中断
	 * Enable falling edge interrupts for all the pins in bank 0.
	 */
//#define INPUT_BANK 0
	XGpioPs_SetIntrType(Gpio, INPUT_BANK, 0x00, 0x00, 0x00);

	/*设置gpio中断的控制器
	 * Set the handler for gpio interrupts.
	 */
	XGpioPs_SetCallbackHandler(Gpio, (void *)Gpio, IntrHandler);


	/*使能bank0的gpio中断
	 * Enable the GPIO interrupts of Bank 0.
	 */
	XGpioPs_IntrEnable(Gpio, INPUT_BANK, 0xFFFFFFFF);


	/*使能gpio设备的中断
	 * Enable the interrupt for the GPIO device.
	 */
	XScuGic_Enable(GicInstancePtr, GpioIntrId);


	/*使能处理程序的中断
	 * Enable interrupts in the Processor.
	 */
	Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);

	return XST_SUCCESS;
}

你可能感兴趣的:(zedboard 中断)