ZYNQ学习笔记——AXI_GPIO

ZYNQ学习笔记——AXI_GPIO_第1张图片

ZYNQ学习笔记——AXI_GPIO_第2张图片

 

ZYNQ学习笔记——AXI_GPIO_第3张图片

AXI GPIO 触发中断,控制PS 端的LED亮灭

/*
 * main.c
 *
 *  Created on: 2020年3月12日
 *      Author: 
 */

#include "xparameters.h"
#include "xgpiops.h"
#include "xgpio.h"
#include "xil_exception.h"
 #include "xscugic.h"
 #include "xil_printf.h"
#include "sleep.h"


#define  GPIO_DEVICE_ID   XPAR_XGPIOPS_0_DEVICE_ID
#define MIO7   7

#define AXI_GPIO__DEVICE_ID  XPAR_GPIO_0_DEVICE_ID
#define key_Channel   1

 #define AXI_INTC_DEVICE_ID	  XPAR_SCUGIC_SINGLE_DEVICE_ID
#define INT_ID   61U   //中断ID号
//ps    led
XGpioPs_Config *ConfigPtr;
XGpioPs Gpiops_inst;	/* The driver instance for GPIO Device. */

//AXI    gpio
XScuGic_Config *xscugic_cfg_ptr;
XScuGic        scugic_inst;

XGpio     AXI_Gpio_inst; /* The Instance of the GPIO Driver */
u32 led_value=1;   //初试化点亮LED


void instance_init();
void Axi_Interrupt_Handler(void *CallBackRef);


int main(void)
{
	xil_printf("AXI GPIO inter test is begin!\t\n");

	instance_init();

	//配置PS  GPIO输出
	XGpioPs_SetDirectionPin(&Gpiops_inst, MIO7, 1);
	XGpioPs_SetOutputEnablePin(&Gpiops_inst, MIO7, 1);
	XGpioPs_WritePin(&Gpiops_inst, MIO7,led_value );

	//配置AXI GPIO为输入  使能中断
	 XGpio_SetDataDirection(&AXI_Gpio_inst,  key_Channel, 1);  //1为input
	 XGpio_InterruptEnable(&AXI_Gpio_inst, 1);    //使能通道1中断
	 XGpio_InterruptGlobalEnable(&AXI_Gpio_inst);//使能AXI GPIO全局中断

      //关联中断函数
	 	XScuGic_Connect(&scugic_inst, INT_ID,Axi_Interrupt_Handler, &AXI_Gpio_inst);
	 //设置中断触发方式:优先级和高电平触发(01),上升沿(11),并使能中断
	 	XScuGic_SetPriorityTriggerType(&scugic_inst, INT_ID,0xA0, 0x01);
	 	 XScuGic_Enable(&scugic_inst, INT_ID); //使能中断

//ZYNQ处理器中断必要的三个函数,设置打开中断异常处理功能
	 Xil_ExceptionInit();
	  Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
	 (Xil_ExceptionHandler)XScuGic_InterruptHandler,&scugic_inst);
	 Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);

	 		 	while(1);

return(0);

}

void instance_init(){
	//初试化MIO,并配置输出使能
	ConfigPtr = XGpioPs_LookupConfig(GPIO_DEVICE_ID);
	XGpioPs_CfgInitialize(&Gpiops_inst, ConfigPtr,ConfigPtr->BaseAddr);

	//初始化配置AXI GPIO
	XGpio_Initialize(&AXI_Gpio_inst,AXI_GPIO__DEVICE_ID);

	//初始化中断
	xscugic_cfg_ptr = XScuGic_LookupConfig(AXI_INTC_DEVICE_ID);
	XScuGic_CfgInitialize(&scugic_inst,xscugic_cfg_ptr,xscugic_cfg_ptr->CpuBaseAddress);
	}

//中断处理函数
void Axi_Interrupt_Handler(void *CallBackRef){
		int key_value=1;
		XGpio *GpioPtr=(XGpio *)CallBackRef;
		xil_printf("inter detected!\t\n");
		XGpio_InterruptDisable(GpioPtr, 1); //关闭中断
		key_value=XGpio_DiscreteRead(GpioPtr, key_Channel);
		if(key_value){
			led_value=~led_value;
			XGpioPs_WritePin(&Gpiops_inst, MIO7,led_value );
		}
		sleep(1);
		XGpio_InterruptClear(GpioPtr, 1);   //清除中断
		XGpio_InterruptEnable(GpioPtr, 1);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(学习笔记,ZYNQ)