课程附件
GPIO_Interrupt.rar
(3.21 MB, 下载次数: 557)
[C] 纯文本查看 复制代码
/*
* main.c
*
* Created on: 2022年8月25日
* Author: Administrator
*/
#include <stdio.h>
#include <xparameters.h>
#include <xgpiops.h>
#include <xscugic.h>
#include <xil_exception.h>
#include <unistd.h>
#define Input_Pin 47 //PS_KEY
#define Output_Pin 7 //PS_LED
static XGpioPs Gpio; /* The Instance of the GPIO Driver */
static XScuGic GicInstancePtr;
int flag;
//按下按键,CPU驱动PS_LED闪烁1次,周期为1S
static void IntrHandler(void *CallBackRef, u32 Bank, u32 Status)
{
XGpioPs *Gpio = (XGpioPs *)CallBackRef;
u32 DataRead;
if(Status)
{
DataRead = XGpioPs_ReadPin(Gpio, Input_Pin);
if (0 == DataRead) {
flag ++;
}
}
}
int main(void)
{
flag = 0;
XGpioPs_Config *ConfigPtr;
ConfigPtr = XGpioPs_LookupConfig(XPAR_AXI_GPIO_0_DEVICE_ID);
XGpioPs_CfgInitialize(&Gpio, ConfigPtr, ConfigPtr->BaseAddr);
/* Set the direction for the specified pin to be input */
XGpioPs_SetDirectionPin(&Gpio, Input_Pin, 0x0);
/* Set the direction for the specified pin to be output. */
XGpioPs_SetDirectionPin(&Gpio, Output_Pin, 1);
XGpioPs_SetOutputEnablePin(&Gpio, Output_Pin, 1);
XGpioPs_WritePin(&Gpio, Output_Pin, 0x0);
XScuGic_Config *IntcConfig; /* Instance of the interrupt controller */
IntcConfig = XScuGic_LookupConfig(XPAR_SCUGIC_SINGLE_DEVICE_ID);
XScuGic_CfgInitialize(&GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);
//注册CPU的总异常/中断服务函数
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
&GicInstancePtr);
//注册/绑定具体外设的中断服务函数
XScuGic_Connect(&GicInstancePtr, XPS_GPIO_INT_ID,
(Xil_ExceptionHandler)XGpioPs_IntrHandler,
&Gpio);
/* Enable falling edge interrupts for all the pins in bank 1. */
XGpioPs_SetIntrType(&Gpio, XGPIOPS_BANK1, 0xffffffff, 0x00000000, 0x00);
/* Set the handler for gpio interrupts. */
XGpioPs_SetCallbackHandler(&Gpio, &Gpio, IntrHandler);
/* Enable the GPIO interrupts of Bank 1. */
XGpioPs_IntrEnable(&Gpio, XGPIOPS_BANK1, (1 << (Input_Pin-32)));
//在GIC中允许该外设的中断源向Cpu发出中断
XScuGic_Enable(&GicInstancePtr, XPS_GPIO_INT_ID);
Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
while(1)
{
if(flag > 0)
{
flag--;
XGpioPs_WritePin(&Gpio, Output_Pin, 0x1);
usleep(500000);
XGpioPs_WritePin(&Gpio, Output_Pin, 0x0);
usleep(500000);
}
}
}
|