xgpio函数
1、int XGpio_Initialize(XGpio * InstancePtr, u16 DeviceId)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_Initialize | 初始化GPIO |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | u16 DeviceId | ID号,自动生成,在xparameters.h文件中定义 |
返回值 | int | XST_SUCCESS/XST_FAILURE |
应用举例:
///
XGpio GpioOutput;
XGpio_Initialize(&GpioOutput, XPAR_AXI_GPIO_0_DEVICE_ID) ;
///
2、void XGpio_SetDataDirection(XGpio *InstancePtr, unsigned Channel,u32 DirectionMask)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_SetDataDirection | 设置GPIO为输入/输出 |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | unsigned Channel | 待设置GPIO的通道(Vivado中设置gpio IP时的设置通道,为1或2) |
参数3 | u32 DirectionMask | 方向设置。0:output;1:input |
返回值 | void |
应用举例:
///
XGpio GpioOutput;
XGpio_SetDataDirection(&GpioOutput,1,0x00000001) ;
///
说明:关于参数Channel,在gpio的IP设置时有如下配置:
配置为上半部分的GPIO,则Channel为1;若设置下半部分配置的GPIO 2,则Channel为2;
3、u32 XGpio_DiscreteRead(XGpio * InstancePtr, unsigned Channel)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_DiscreteRead | 读取GPIO的值 |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | unsigned Channel | 通道号,同上一函数 |
返回值 | u32 | 最多32位的实际值 |
应用举例:
///
XGpio GpioOutput;
int btn_val ;
btn_val =XGpio_DiscreteRead(&GpioOutput,1) ;
///
3、void XGpio_DiscreteWrite(XGpio * InstancePtr, unsigned Channel, u32 Data)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_DiscreteWrite | 写GPIO |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | unsigned Channel | 通道号,同上一函数 |
参数3 | u32 Data | 需要写的值 |
返回值 | void |
应用举例:
///
XGpio GpioOutput;
int led_value = 0x02;
XGpio_DiscreteWrite(&GpioOutput,1,led_value ) ;
///
4、void XGpio_DiscreteSet(XGpio * InstancePtr, unsigned Channel, u32 Data)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_DiscreteSet | 置位某些GPIO的位(给某些GPIO的位写1) |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | unsigned Channel | 通道号,同上一函数 |
参数3 | u32 Data | 需要给哪些位写1 |
返回值 | void |
XGpio_DiscreteSet是分三步操作:1、使用XGpio_DiscreteRead读回原来的GPIO状态:n0(假设读回值为n0);2、执行:n1=n0 | n(“|”为“位或”运算,n为XGpio_DiscreteSet第三个参数,即要置1的位数值);3、使用XGpio_DiscreteWrite将n1写回GPIO。当读回值n0等于0时,XGpio_DiscreteSet的n1=n,即XGpio_DiscreteSet等价于XGpio_DiscreteWrite;
其原型语言如下(参考:D:\Xilinx\14.7\ISE_DS\EDK\sw\XilinxProcessorIPLib\drivers\gpio_v3_00_a\src\xgpio_extra.c):
/**
* Set output discrete(s) to logic 1 for the specified GPIO channel.
*
* @param InstancePtr is a pointer to an XGpio instance to be worked on.
* @param Channel contains the channel of the GPIO (1 or 2) to operate on.
* @param Mask is the set of bits that will be set to 1 in the discrete
* data register. All other bits in the data register are
* unaffected.
*
* @return None.
*
* @note
*
* The hardware must be built for dual channels if this function is used
* with any channel other than 1. If it is not, this function will assert.
*
* This API can only be used if the GPIO_IO ports in the IP are used for
* connecting to the external output ports.
*
*****************************************************************************/
void XGpio_DiscreteSet(XGpio * InstancePtr, unsigned Channel, u32 Mask)
{
u32 Current;
unsigned DataOffset;
Xil_AssertVoid(InstancePtr != NULL);
Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertVoid((Channel == 1) ||
((Channel == 2) && (InstancePtr->IsDual == TRUE)));
/*
* Calculate the offset to the data register of the GPIO once
*/
DataOffset = ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_DATA_OFFSET;
/*
* Read the contents of the data register, merge in Mask and write
* back results
*/
Current = XGpio_ReadReg(InstancePtr->BaseAddress, DataOffset);
Current |= Mask;
XGpio_WriteReg(InstancePtr->BaseAddress, DataOffset, Current);
}
应用举例:
///
XGpio GpioOutput;
int led_value = 0x02;
XGpio_DiscreteSet(&GpioOutput,1,led_value ) ;
///
5、void XGpio_DiscreteClear(XGpio * InstancePtr, unsigned Channel, u32 Data)
名称 | 代码 | 解释 |
---|---|---|
函数名 | XGpio_DiscreteClear | 清零某些GPIO的位(给某些GPIO的位写0) |
参数1 | XGpio * InstancePtr | 指向GPIO实例的指针 |
参数2 | unsigned Channel | 通道号,同上一函数 |
参数3 | u32 Data | 需要给哪些位写0 |
返回值 | void |
XGpio_DiscreteClear也是分三步操作:1、使用XGpio_DiscreteRead读回原来的GPIO状态:n0;2、执行:n1=n0 & ~n(“&”为“位与”运算,“~”为“位非”运算,n为XGpio_DiscreteClear第三个参数,即要清0的位数值);3、使用XGpio_DiscreteWrite将n1写回GPIO。当读回值n0等于0时,即XGpio_DiscreteClear等价于全部清0,与n无关。
其原型语言如下:
/****************************************************************************/
/**
* Set output discrete(s) to logic 0 for the specified GPIO channel.
*
* @param InstancePtr is a pointer to an XGpio instance to be worked on.
* @param Channel contains the channel of the GPIO (1 or 2) to operate on.
* @param Mask is the set of bits that will be set to 0 in the discrete
* data register. All other bits in the data register are
* unaffected.
*
* @return None.
*
* @note
*
* The hardware must be built for dual channels if this function is used
* with any channel other than 1. If it is not, this function will assert.
*
* This API can only be used if the GPIO_IO ports in the IP are used for
* connecting to the external output ports.
*
*****************************************************************************/
void XGpio_DiscreteClear(XGpio * InstancePtr, unsigned Channel, u32 Mask)
{
u32 Current;
unsigned DataOffset;
Xil_AssertVoid(InstancePtr != NULL);
Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertVoid((Channel == 1) ||
((Channel == 2) && (InstancePtr->IsDual == TRUE)));
/*
* Calculate the offset to the data register of the GPIO once
*/
DataOffset = ((Channel - 1) * XGPIO_CHAN_OFFSET) + XGPIO_DATA_OFFSET;
/*
* Read the contents of the data register, merge in Mask and write
* back results
*/
Current = XGpio_ReadReg(InstancePtr->BaseAddress, DataOffset);
Current &= ~Mask;
XGpio_WriteReg(InstancePtr->BaseAddress, DataOffset, Current);
}
应用举例:
///
XGpio GpioOutput;
int led_value = 0x02;
XGpio_DiscreteSet(&GpioOutput,1,led_value ) ;
///