AXI_GPIO中断

#include "xparameters.h"
#include "xstatus.h"
#include "xscugic.h"
#include "sleep.h"
#include "xplatform_info.h"
#include <xil_printf.h>
#include "xgpio.h"
#include "xil_exception.h"

#define GPIO_DEVICE_ID  	XPAR_AXI_GPIO_0_DEVICE_ID    //AXI GPIO设备ID
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID  //中断设备ID
#define GPIO_INTERRUPT_ID	XPAR_XGPIOPS_0_INTR        	  //GPIO中断ID   根据中断类型 选择合适的ID

/* Definitions for Fabric interrupts connected to ps7_scugic_0      set  61  */
#define INTC_GPIO_INTERRUPT_ID	XPAR_FABRIC_AXI_GPIO_0_IP2INTC_IRPT_INTR //中断ID 61:68  84:91
//#define INTC_GPIO_INTERRUPT_ID0	62U										 //第二个按键中断的ID,ID需要根据AXI GPIO的端口设置

#define GPIO_CHANNEL1		1											//通道1

void IntrHandler(); 		                                             //中断执行函数
int GpioSetupIntrSystem(XScuGic *IntcInstancePtr, XGpio *InstancePtr,
			u16 DeviceId, u16 IntrId, u16 IntrMask);					//AXI_GPIO中断配置函数

 XScuGic Intc;   /* The Instance of the Interrupt Controller Driver */
 XGpio AXI_Gpio; /* The Instance of the GPIO Driver */


int main()
{


	xil_printf(" test success\n\r");

	/* Initialize the AXI_GPIO driver.       初始化只需配置这一个函数 */
	   XGpio_Initialize(&AXI_Gpio, INTC_DEVICE_ID);
	//设置AXI GPIO 端口方向  设置1位输入 0为输出
	   XGpio_SetDataDirection(&AXI_Gpio, GPIO_CHANNEL1,0x1);

     //中断配置函数
	 GpioSetupIntrSystem(&Intc, &AXI_Gpio, GPIO_DEVICE_ID,INTC_GPIO_INTERRUPT_ID, GPIO_CHANNEL1);
	// GpioSetupIntrSystem(&Intc, &AXI_Gpio, GPIO_DEVICE_ID,INTC_GPIO_INTERRUPT_ID0, GPIO_CHANNEL1);

	while(1)
	{
		u32 state;
		u32 int_state;
		//读取AXI GPIO的状态
		state = XGpio_DiscreteRead(&AXI_Gpio, GPIO_CHANNEL1);
		int_state = XGpio_InterruptGetStatus(&AXI_Gpio);
		if(state !=int_state)
		{
		  xil_printf("state == %x;int_state == %x \n\t",state,int_state);
		  sleep(1);
		}
	}

	return 0;
}

/******************************************************************************/
/**
*
* This function performs the GPIO set up for Interrupts
*
* @param	IntcInstancePtr is a reference to the Interrupt Controller
*		driver Instance
* @param	InstancePtr is a reference to the AXI_GPIO driver Instance
* @param	DeviceId is the XPAR_<GPIO_instance>_DEVICE_ID value from
*		xparameters.h  GPIO 设备 ID
* @param	IntrId is XPAR_<INTC_instance>_<GPIO_instance>_IP2INTC_IRPT_INTR
*		value from xparameters.h   AXI_GPIO 中断ID
* @param	IntrMask is the GPIO channel mask
*
******************************************************************************/
int GpioSetupIntrSystem(XScuGic*IntcInstancePtr, XGpio *InstancePtr,
			u16 DeviceId, u16 IntrId, u16 IntrMask)
{

	XScuGic_Config *IntcConfig;

	/* Initialize the interrupt controller driver*/
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
    XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,IntcConfig->CpuBaseAddress);
	/*
	 * Connect the interrupt controller interrupt handler to the hardware
	 * interrupt handling logic in the processor.
	 * 很重要 写中断程序就要加上下面的代码。
	 */
	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
				(Xil_ExceptionHandler)XScuGic_InterruptHandler,
				IntcInstancePtr);
	Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);/* Enable interrupts in the Processor. */



	/*
	 * 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.
	 * 中断发送时 跳转到(Xil_ExceptionHandler)IntrHandler函数 执行中断函数
	 */
	XScuGic_Connect(IntcInstancePtr, IntrId,
				(Xil_ExceptionHandler)IntrHandler,
				(void *)&AXI_Gpio);

	//中断优先级和触发方式设置
	//IntrId:中断ID号     0xA0:优先级设置    0x03:中断触发方式设置

	XScuGic_SetPriorityTriggerType(IntcInstancePtr, IntrId,0xA0, 0x1);

	//使能全局中断
	XGpio_InterruptGlobalEnable(InstancePtr);
	XScuGic_Enable(IntcInstancePtr, IntrId);
	XGpio_InterruptEnable(InstancePtr, IntrMask);

	return 0;
}

/*****************************************************************************/
/**
*
* 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.
*
****************************************************************************/

 void IntrHandler()
{
	 xil_printf("interrupt success!\n\t");
	 //disable interrupt
	 XGpio_InterruptDisable(&AXI_Gpio, GPIO_CHANNEL1);

	 /*
	  * 用户自定义处理代码
	  */

	 //clear state  and enable interrupt
    XGpio_InterruptClear(&AXI_Gpio, GPIO_CHANNEL1);
    XGpio_InterruptEnable(&AXI_Gpio, GPIO_CHANNEL1);
    /*
    XGpioPs_IntrClearPin(&Gpio, Input_pin0);
    XGpioPs_IntrEnablePin(&Gpio, Input_pin0);*/
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个会飞的小苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值