使用zedboard开发板,做GPIO中断触发实验
功能:按键BTN8(MIO50)触发中断,运行中断函数。
完成:中断初始化;中断函数
实现:出现问题,在读取按键状态时直接触发中断。
出现问题位置
解决状态:NONE
#include <stdio.h>
#include "xparameters.h"
#include "xgpiops.h"
#include "xstatus.h"
#include "xplatform_info.h"
#include <xil_printf.h>
#include "sleep.h"
#include "xscugic.h"
#define GPIO_DEVICE_ID XPAR_XGPIOPS_0_DEVICE_ID
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
#define MIO7_LED 7 //ps端LED
#define MIO50_KEY 50 //按键
//GPIO中段号:52
#define GPIO_INTERRUPT_ID XPAR_XGPIOPS_0_INTR
static u32 Input_Pin; /* Switch button */
static u32 Output_Pin; /* LED button */
static u32 AllButtonsPressed; /* Intr status of the bank */
XGpioPs_Config *ConfigPtr;
XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
XGpioPs Gpio; /* The driver instance for GPIO Device. */
static XScuGic Intc; /* The Instance of the Interrupt Controller Driver */
void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio, u16 GpioIntrId);
void IntrHandler();
int main()
{
u32 Data;
printf("***hello,hello,hello,hello,hello,hello***\r\n");
//初始化gpio驱动
//根据器件id,查找器件配置信息
ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
//初始化gpio参数
XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);
//gpio方向设置为输出(0:输入、1:输出)
XGpioPs_SetDirectionPin(&Gpio, MIO7_LED, 1);
XGpioPs_SetDirectionPin(&Gpio, MIO50_KEY, 0);
//设置输出使能(0:关闭、1:打开)
XGpioPs_SetOutputEnablePin(&Gpio, MIO7_LED, 1);
SetupInterruptSystem(&Intc, &Gpio, GPIO_INTERRUPT_ID);
while(1)
{
Data = XGpioPs_ReadPin(&Gpio, MIO50_KEY);
if(Data == 1)
{
//写数据到输出引脚
XGpioPs_WritePin(&Gpio, MIO7_LED, 0x1);
}
else
{
XGpioPs_WritePin(&Gpio, MIO7_LED, 0x0);
}
}
return 0;
}
void SetupInterruptSystem(XScuGic *GicInstancePtr, XGpioPs *Gpio,
u16 GpioIntrId)
{
//查找器件配置信息,并进行初始化
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
XScuGic_CfgInitialize(GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);
// 初始化ARM处理器异常句柄
Xil_ExceptionInit();
// 给中断异常注册一个处理程序
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
GicInstancePtr);
/* Enable interrupts in the Processor.使能处理器中断 */
Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
// 关联 中断处理函数,对中断进行处理
XScuGic_Connect(GicInstancePtr, GpioIntrId, (Xil_ExceptionHandler)IntrHandler, (void *)Gpio);
/* Enable the interrupt for the GPIO device.为GPIO器件使能中断 */
XScuGic_Enable(GicInstancePtr, GpioIntrId);
/* Enable falling edge interrupts for all the pins in bank 0. */
// XGpioPs_SetIntrType(Gpio, GPIO_BANK, 0x00, 0xFFFFFFFF, 0x00);
// 设置引脚终端类型:下降沿触发
XGpioPs_SetIntrTypePin(Gpio, MIO50_KEY, XGPIOPS_IRQ_TYPE_EDGE_RISING);
/* Set the handler for gpio interrupts. */
// XGpioPs_SetCallbackHandler(Gpio, (void *)Gpio, IntrHandler);
/* Enable the GPIO interrupts of Bank 0. */
// XGpioPs_IntrEnable(Gpio, GPIO_BANK, (1 << Input_Pin));
// 打开引脚中断使能
XGpioPs_IntrEnablePin(Gpio, MIO50_KEY);
// return XST_SUCCESS;
}
void IntrHandler()
{
// 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;
// }
printf("interrupt detected!\r\n");
XGpioPs_IntrDisablePin(&Gpio, MIO50_KEY);
}