📄 gpio1.txt
字号:
* Inputs: UINT8 uGPIOPin = which GPIO
* BOOL bOpt = option
*
* Outputs: none
*
***************************************************************************/
void SetGPIOUsage( UINT8 uGPIOPin, BOOL bOpt )
{
#ifndef DEVICE_YELLOWSTONE
/*assert ( (uGPIOPin > 31) && (uGPIOPin < NUM_OF_GPIOS) );*/
if (uGPIOPin > 31) /*only GPIOs 32-39 have options available */
{
if ( 0 == bOpt )
{
*GPIO_OPT &= ~( 1 << (uGPIOPin - 32));
}
else
{
*GPIO_OPT |= ( 1 << (uGPIOPin - 32));
}
}
#endif
}
/****************************************************************************
*
* Name: void SetGPIODir( UINT8 uGPIOPin, BOOL bDir )
*
* Description: Sets the GPIO direction
*
* Inputs: UINT8 uGPIOPin = which GPIO
* BOOL bDir = direction
*
* Outputs: none
*
***************************************************************************/
void SetGPIODir( UINT8 uGPIOPin, BOOL bDir )
{
UINT32 uBitPos = 0;
UINT32 uValue;
volatile UINT32* pGPIOOutPtr = 0;
volatile UINT32* pGPIOInPtr = 0;
volatile UINT32* pImagePtr = 0;
UINT32 ByteOffSet = 0;
if (uGPIOPin < NUM_OF_GPIOS)
{
ByteOffSet = ((uGPIOPin / NUM_GPIOS_PER_REGISTER) * GRP_REG_OFFSET);
// Calculate corresponding GPIO Output Enable register. Use byte pointer addition
// to match HW register definitions.
pGPIOOutPtr = (volatile UINT32 *) ((volatile UINT8 *)(p_GRP0_OUTPUT_ENABLE) + ByteOffSet);
if (HWSTATE.eDeviceType >= DEVICE_RUSHMORE)
{
// Calculate corresponding GPIO Output Enable register. Use byte pointer addition
// to match HW register definitions.
pGPIOInPtr = (volatile UINT32 *) ((volatile UINT8 *)(p_GRP0_INPUT_ENABLE) + ByteOffSet);
pImagePtr = (&HWSTATE.p_GRP0_INPUT_ENABLE_Image) + (uGPIOPin / NUM_GPIOS_PER_REGISTER);
}
// Calculate bit position within the register.
uBitPos = (uGPIOPin % NUM_GPIOS_PER_REGISTER);
/************************/
#ifdef DEVICE_YELLOWSTONE
/************************/
// Upper WORD of register is the write mask to access
// the desired bit in the lower WORD of register.
// So first set the write mask for the desired bit.
uValue = 1 << (uBitPos + 16);
if ( GP_INPUT == bDir )
{
// Disable GPIO output driver bit by writing 0 to lower WORD
HW_REG_WRITE(pGPIOOutPtr, uValue);
if (pGPIOInPtr)
{
// Keep same mask and enable GPIO input receiver bit by writing 1
// to lower WORD
uValue |= (1 << uBitPos);
HW_REG_WRITE(pGPIOInPtr, uValue);
}
}
else
{
if (pGPIOInPtr)
{
// Disable GPIO input receiver bit by writing 0 to lower WORD
HW_REG_WRITE(pGPIOInPtr, uValue);
}
// Keep same mask and enable GPIO output driver bit by writing 1
// to lower WORD
uValue |= (1 << uBitPos);
HW_REG_WRITE(pGPIOOutPtr, uValue);
}
/************************/
#else
/************************/
uValue = 1 << uBitPos;
if ( GP_INPUT == bDir )
{
// Disable GPIO output driver
*pGPIOOutPtr &= ~uValue;
if (pGPIOInPtr)
{
#if 0
// Enable GPIO input receiver
*pGPIOInPtr |= uValue;
// Update SW image of GPIO Input Enable register
*pImagePtr = *pGPIOInPtr;
#else
// Enable GPIO input receiver
// Update SW image of GPIO Input Enable register
*pImagePtr |= uValue;
// Update GPIO Input Enable register
*pGPIOInPtr = *pImagePtr;
#endif
}
}
else
{
if (pGPIOInPtr)
{
#if 0
// Disable GPIO input receiver
*pGPIOInPtr &= ~uValue;
// Update SW image of GPIO Input Enable register
*pImagePtr = *pGPIOInPtr;
#else
// Disable GPIO input receiver
// Update SW image of GPIO Input Enable register
*pImagePtr &= ~uValue;
// Update GPIO Input Enable register
*pGPIOInPtr = *pImagePtr;
#endif
}
// Enable GPIO output driver
*pGPIOOutPtr |= uValue;
}
#endif
}
}
/****************************************************************************
*
* Name: void SetGPIOInputEnable( UINT8 uGPIOPin, BOOL bState )
*
* Description: Sets the GPIO direction
*
* Inputs: UINT8 uGPIOPin = which GPIO
* BOOL bDir = direction
*
* Outputs: none
*
***************************************************************************/
void SetGPIOInputEnable( UINT8 uGPIOPin, BOOL bState )
{
/* New register in Rushmore and OldFaithful devices to enable input. */
UINT32 uBitPos;
UINT32 uValue;
UINT32 ByteOffSet = 0;
volatile UINT32* pGPIOInPtr;
volatile UINT32* pImagePtr;
if ( (HWSTATE.eDeviceType >= DEVICE_RUSHMORE) && (uGPIOPin < NUM_OF_GPIOS) )
{
ByteOffSet = ((uGPIOPin / NUM_GPIOS_PER_REGISTER) * GRP_REG_OFFSET);
// Calculate corresponding GPIO Output Enable register. Use byte pointer addition
// to match HW register definitions.
pGPIOInPtr = (volatile UINT32 *) ((volatile UINT8 *)(p_GRP0_INPUT_ENABLE) + ByteOffSet);
pImagePtr = (&HWSTATE.p_GRP0_INPUT_ENABLE_Image) + (uGPIOPin / NUM_GPIOS_PER_REGISTER);
// Calculate bit position within the register.
uBitPos = (uGPIOPin % NUM_GPIOS_PER_REGISTER);
/************************/
#ifdef DEVICE_YELLOWSTONE
/************************/
// Upper WORD of register is the write mask to access
// the desired bit in the lower WORD of register.
// So first set the write mask for the desired bit.
uValue = 1 << (uBitPos + 16);
if ( GP_INPUTON == bState )
{
// Keep same mask and enable GPIO input receiver bit by writing 1
// to lower WORD
uValue |= (1 << uBitPos);
HW_REG_WRITE(pGPIOInPtr, uValue);
}
else
{
// Disable GPIO input receiver bit by writing 0 to lower WORD
uValue &= (~(1 << uBitPos));
HW_REG_WRITE(pGPIOInPtr, uValue);
}
/************************/
#else
/************************/
uValue = (1 << uBitPos);
if ( GP_INPUTON == bState )
{
*pImagePtr |= uValue;
}
else
{
*pImagePtr &= ~uValue;
}
*pGPIOInPtr = *pImagePtr;
#endif
} /*End if DEVICE_RUSHMORE*/
}
/****************************************************************************
*
* Name: void WriteGPIOData( UINT8 uGPIOPin, BOOL bState )
*
* Description: Sets the state of a GPIO output pin
*
* Inputs: UINT8 uGPIOPin = which GPIO
* BOOL bState = pin state
*
* Outputs: none
*
***************************************************************************/
/* The register bit position setting/resetting.
* Note that only mask bit is important since it should "mask"
* off setting/resetting of other bits in the register
* value to write to register for direction
*/
void WriteGPIOData( UINT8 uGPIOPin, BOOL bState )
{
UINT32 uBitPos = 0;
UINT32 uValue = 0;
UINT32 ByteOffSet = 0;
volatile UINT32* pGPIOOutPtr = 0;
int OldLevel = 0;
if (uGPIOPin < NUM_OF_GPIOS)
{
ByteOffSet = ((uGPIOPin / NUM_GPIOS_PER_REGISTER) * GRP_REG_OFFSET);
// Calculate corresponding GPIO Output Enable register. Use byte pointer addition
// to match HW register definitions.
pGPIOOutPtr = (volatile UINT32 *) ((volatile UINT8 *)(p_GRP0_DATA_OUT) + ByteOffSet);
// Calculate bit position within the register.
uBitPos = (uGPIOPin % NUM_GPIOS_PER_REGISTER);
/************************/
#ifdef DEVICE_YELLOWSTONE
/************************/
// Upper WORD of register is the write mask to access
// the desired bit in the lower WORD of register.
// So first set the write mask for the desired bit.
uValue = 1 << (uBitPos + 16);
if ( 0 != bState )
{
uValue |= (1 << uBitPos);
}
else
{
uValue &= (~(1 << uBitPos));
}
HW_REG_WRITE(pGPIOOutPtr, uValue);
/************************/
#else
/************************/
/* Set all mask bits and do read-modify-write due to register 2 mask bug */
OldLevel = intLock();
HW_REG_READ(pGPIOOutPtr, uValue);
uValue |= 0xFFFF0000;
if ( 0 != bState ) /*uValue 0 in lower 16 bits, only check for positive*/
{
uValue |= (1 << uBitPos);
}
else
{
uValue &= ~(1 << uBitPos);
}
HW_REG_WRITE(pGPIOOutPtr, uValue);
intUnlock(OldLevel);
#endif
}
}
/****************************************************************************
*
* Name: BOOL ReadGPIOData( UINT8 uGPIOPin )
*
* Description: Reads a GPIO input pin
*
* Inputs: UINT8 uGPIOPin = which GPIO
*
* Outputs: Returns BOOL pin state
*
***************************************************************************/
BOOL ReadGPIOData( UINT8 uGPIOPin )
{
UINT32 uBitPos = 0;
UINT32 uValue = 0;
volatile UINT32* pGPIOInPtr = 0;
UINT32 ByteOffSet = 0;
if (uGPIOPin < NUM_OF_GPIOS)
{
ByteOffSet = ((uGPIOPin / NUM_GPIOS_PER_REGISTER) * GRP_REG_OFFSET);
// Calculate corresponding GPIO Output Enable register. Use byte pointer addition
// to match HW register definitions.
pGPIOInPtr = (volatile UINT32 *) ((volatile UINT8 *)(p_GRP0_DATA_IN) + ByteOffSet);
// Calculate bit position within the register.
uBitPos = (uGPIOPin % NUM_GPIOS_PER_REGISTER);
HW_REG_READ(pGPIOInPtr, uValue);
if( uValue & (1 << uBitPos) )
{
return TRUE;
}
}
return FALSE;
}
/****************************************************************************
*
* Name: BOOL ReadGPIOIntStatus( UINT8 uGPIOPin )
*
* Description: Reads a GPIO pin interrupt status bit
*
* Inputs: UINT8 uGPIOPin = which GPIO
*
* Outputs: Returns BOOL interrupt status
*
***************************************************************************/
BOOL ReadGPIOIntStatus( UINT8 uGPIOPin )
{
register UINT32 uBitPos;
register volatile UINT32* pGPIOPtr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -