GPIO例子代码分析
#include "xgpiops.h"
#include "sleep.h"
int main()
{
static XGpioPs psGpioInstancePtr;
//X代表Xilinx,Gpio通用输入输出I/O,Ps是指在PS部分的
XGpioPs_Config* GpioConfigPtr;
int iPinNumber= 7; //LED显示的接口
u32 uPinDirection = 0x1; //输入输出方向:1代表输出,0代表输入
int xStatus;
//MIO初始化
GpioConfigPtr = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID);
//macro expansion :宏指令 #define XPAR_PS7_GPIO_0_DEVICE_ID 0
if(GpioConfigPtr == NULL)
return XST_FAILURE;
xStatus = XGpioPs_CfgInitialize(&psGpioInstancePtr,GpioConfigPtr, GpioConfigPtr->BaseAddr);
if(XST_SUCCESS != xStatus)
print(" PS GPIO INIT FAILED \n\r");
//MIO的输入输出操作
XGpioPs_SetDirectionPin(&psGpioInstancePtr, iPinNumber,uPinDirection);//设置MIO的输出方向:可通过手册ug585-Zynq-7000-TRM,查找DIRM,明确0是输入,1是输出
XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, iPinNumber,1);//设置MIO的第7位为输出
while(1)
{
XGpioPs_WritePin(&psGpioInstancePtr, iPinNumber, 1);
//深入函数内部:XGpioPs_GetBankPin((u8)Pin, &Bank, &PinNumber);由于BANK号为0,Pin为7,经过判断后,PinNumber被赋值为7
//Value = ~((u32)1 << (PinNumber + 16U)) & ((DataVar << PinNumber) | 0xFFFF0000U);其中 ~((u32)1 << (PinNumber + 16U)) 为FF7F FFFF
//((DataVar << PinNumber) | 0xFFFF0000U)为FFFF 0080;Value=FF7F FFFF & FFFF 0080;
//XGpioPs_WriteReg(InstancePtr->GpioConfig.BaseAddr,((u32)(Bank) * XGPIOPS_DATA_MASK_OFFSET) +RegOffset, Value);该函数通过GPIO的基地址(手册可查)加偏移量找到寄存器,并将value值写入。等价于XGpioPs_WriteReg(0xE000A000,0x00000000,FF7F FFFF & FFFF 0080)
//XGpioPs_WritePin(&psGpioInstancePtr, iPinNumber, 1)=XGpioPs_WriteReg(0xE000A000,0x00000000,FF7F FFFF & FFFF 0080)
sleep(1); //延时1秒
XGpioPs_WritePin(&psGpioInstancePtr, iPinNumber, 0);//
sleep(1); //
}
return 0;
}