mpsoc嵌入式vitis开发—AXI GPIO中断实验

前言

vitis版本:Vitis 2023.2
由于Vitis版本更新,很多API发生变化,学习原子哥的教程时很多代码对于不上,所以自己重新写一遍,并记录下自己踩过的坑,方便以后查看。这里直接给出代码,其他的流程参考原子哥的《2_DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南_V1.0.pdf》

代码

#include "sleep.h"
#include "xgpio.h"
#include "xil_exception.h"
#include "xinterrupt_wrap.h"
#include "xparameters.h"

/************************** Constant Definitions *****************************/
#define XGPIO_AXI_BASEADDRESS XPAR_XGPIO_0_BASEADDR

#define GPIO_CHANNEL1 1
#define GPIO_CHANNEL1_MASK XGPIO_IR_CH1_MASK

#define INTR_DELAY 0x00FFFFFF

/************************** Function Prototypes ******************************/
void GpioHandler(void *CallBackRef);

int GpioIntrExample(XGpio *InstancePtr, UINTPTR BaseAddress, u16 IntrMask,
                    u32 *DataRead);
/************************** Variable Definitions *****************************/

XGpio Gpio; /* The Instance of the GPIO Driver */

static volatile u32 IntrFlag; /* Interrupt Handler Flag */

int main(void) {
  int Status;
  u32 DataRead;
  print(" Press button to Generate Interrupt\r\n");

  Status =
      GpioIntrExample(&Gpio, XGPIO_AXI_BASEADDRESS, GPIO_CHANNEL1, &DataRead);

  if (Status == 0) {
    if (DataRead == 0) {
      print("No button pressed. \r\n");
    } else {
      print("Successfully ran Gpio Interrupt Tapp Example\r\n");
    }
  } else {
    print("Gpio Interrupt Tapp Example Failed.\r\n");
    return XST_FAILURE;
  }

  return XST_SUCCESS;
}

int GpioIntrExample(XGpio *InstancePtr, UINTPTR BaseAddress, u16 IntrMask,
                    u32 *DataRead) {
  int Status;
  u32 delay;

  XGpio_Config *ConfigPtr;
  ConfigPtr = XGpio_LookupConfig(BaseAddress);
  /* Initialize the GPIO driver. If an error occurs then exit */
  Status = XGpio_Initialize(InstancePtr, BaseAddress);
  if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }
  XGpio_SetDataDirection(InstancePtr, IntrMask,1); //设置 PL AXI GPIO 通道 1 为输入

  XGpio_InterruptEnable(InstancePtr, GPIO_CHANNEL1_MASK); //使能通道 1 中断
  XGpio_InterruptGlobalEnable(InstancePtr); //使能 AXI GPIO 全局中断

  Status =
      XSetupInterruptSystem(InstancePtr, &GpioHandler, ConfigPtr->IntrId,
                            ConfigPtr->IntrParent, XINTERRUPT_DEFAULT_PRIORITY);
  if (Status != XST_SUCCESS) {
    return XST_FAILURE;
  }

  IntrFlag = 0;
  delay = 0;

  while (!IntrFlag && (delay < INTR_DELAY)) {
    usleep(1);
    delay++;
  }

  XDisconnectInterruptCntrl(ConfigPtr->IntrId, ConfigPtr->IntrParent);
  *DataRead = IntrFlag;

  return Status;
}

void GpioHandler(void *CallbackRef) {
  XGpio *GpioPtr = (XGpio *)CallbackRef;
  IntrFlag = 1;
  /* Clear the Interrupt */
  XGpio_InterruptClear(GpioPtr, GPIO_CHANNEL1_MASK);
}
posted @ 2024-02-21 13:53  USTHzhanglu  阅读(131)  评论(0编辑  收藏  举报