📄 gpio.c
字号:
//
// Convert from a pin number to a bit position.
//
ucPin = 1 << ucPin;
//
// Return the pin interrupt type.
//
ulIBE = HWREG(ulPort + GPIO_O_IBE);
ulIS = HWREG(ulPort + GPIO_O_IS);
ulIEV = HWREG(ulPort + GPIO_O_IEV);
return(((ulIBE & ucPin) ? 1 : 0) | ((ulIS & ucPin) ? 2 : 0) |
((ulIEV & ucPin) ? 4 : 0));
}
//*****************************************************************************
//
//! Sets the pad configuration for the specified pin(s).
//!
//! \param ulPort is the base address of the GPIO port.
//! \param ucPins is the bit-packed representation of the pin(s).
//! \param ulStrength specifies the output drive strength.
//! \param ulPinType specifies the pin type.
//!
//! This function sets the drive strength and type for the specified pin(s)
//! on the selected GPIO port. For pin(s) configured as input ports, the
//! pad is configured as requested, but the only real effect on the input
//! is the configuration of the pull-up or pull-down termination.
//!
//! The parameter \e ulStrength can be one of the following values:
//!
//! - \b GPIO_STRENGTH_2MA
//! - \b GPIO_STRENGTH_4MA
//! - \b GPIO_STRENGTH_8MA
//! - \b GPIO_STRENGTH_8MA_SC
//!
//! where \b GPIO_STRENGTH_xMA specifies either 2, 4, or 8 mA output drive
//! strength, and \b GPIO_OUT_STRENGTH_8MA_SC specifies 8 mA output drive with
//! slew control.
//!
//! The parameter \e ulPinType can be one of the following values:
//!
//! - \b GPIO_PIN_TYPE_STD
//! - \b GPIO_PIN_TYPE_STD_WPU
//! - \b GPIO_PIN_TYPE_STD_WPD
//! - \b GPIO_PIN_TYPE_OD
//! - \b GPIO_PIN_TYPE_OD_WPU
//! - \b GPIO_PIN_TYPE_OD_WPD
//! - \b GPIO_PIN_TYPE_ANALOG
//!
//! where \b GPIO_PIN_TYPE_STD* specifies a push-pull pin, \b GPIO_PIN_TYPE_OD*
//! specifies an open-drain pin, \b *_WPU specifies a weak pull-up, \b *_WPD
//! specifies a weak pull-down, and \b GPIO_PIN_TYPE_ANALOG specifies an
//! analog input (for the comparators).
//!
//! The pin(s) are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPadConfigSet(unsigned long ulPort, unsigned char ucPins,
unsigned long ulStrength, unsigned long ulPinType)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
ASSERT((ulStrength == GPIO_STRENGTH_2MA) ||
(ulStrength == GPIO_STRENGTH_4MA) ||
(ulStrength == GPIO_STRENGTH_8MA) ||
(ulStrength == GPIO_STRENGTH_8MA_SC));
ASSERT((ulPinType == GPIO_PIN_TYPE_STD) ||
(ulPinType == GPIO_PIN_TYPE_STD_WPU) ||
(ulPinType == GPIO_PIN_TYPE_STD_WPD) ||
(ulPinType == GPIO_PIN_TYPE_OD) ||
(ulPinType == GPIO_PIN_TYPE_OD_WPU) ||
(ulPinType == GPIO_PIN_TYPE_OD_WPD) ||
(ulPinType == GPIO_PIN_TYPE_ANALOG))
//
// Set the output drive strength.
//
HWREG(ulPort + GPIO_O_DR2R) = ((ulStrength & 1) ?
(HWREG(ulPort + GPIO_O_DR2R) | ucPins) :
(HWREG(ulPort + GPIO_O_DR2R) & ~(ucPins)));
HWREG(ulPort + GPIO_O_DR4R) = ((ulStrength & 2) ?
(HWREG(ulPort + GPIO_O_DR4R) | ucPins) :
(HWREG(ulPort + GPIO_O_DR4R) & ~(ucPins)));
HWREG(ulPort + GPIO_O_DR8R) = ((ulStrength & 4) ?
(HWREG(ulPort + GPIO_O_DR8R) | ucPins) :
(HWREG(ulPort + GPIO_O_DR8R) & ~(ucPins)));
HWREG(ulPort + GPIO_O_SLR) = ((ulStrength & 8) ?
(HWREG(ulPort + GPIO_O_SLR) | ucPins) :
(HWREG(ulPort + GPIO_O_SLR) & ~(ucPins)));
//
// Set the pin type.
//
HWREG(ulPort + GPIO_O_ODR) = ((ulPinType & 1) ?
(HWREG(ulPort + GPIO_O_ODR) | ucPins) :
(HWREG(ulPort + GPIO_O_ODR) & ~(ucPins)));
HWREG(ulPort + GPIO_O_PUR) = ((ulPinType & 2) ?
(HWREG(ulPort + GPIO_O_PUR) | ucPins) :
(HWREG(ulPort + GPIO_O_PUR) & ~(ucPins)));
HWREG(ulPort + GPIO_O_PDR) = ((ulPinType & 4) ?
(HWREG(ulPort + GPIO_O_PDR) | ucPins) :
(HWREG(ulPort + GPIO_O_PDR) & ~(ucPins)));
HWREG(ulPort + GPIO_O_DEN) = ((ulPinType & 8) ?
(HWREG(ulPort + GPIO_O_DEN) | ucPins) :
(HWREG(ulPort + GPIO_O_DEN) & ~(ucPins)));
}
//*****************************************************************************
//
//! Gets the pad configuration for a pin.
//!
//! \param ulPort is the base address of the GPIO port.
//! \param ucPin is the pin number.
//! \param pulStrength is a pointer to storage for the output drive strength.
//! \param pulPinType is a pointer to storage for the output drive type.
//!
//! This function gets the pad configuration for a specified pin on the
//! selected GPIO port. The values returned in \e pulStrength and \e pulPinType
//! correspond to the values used in GPIOPadConfigSet(). This function also
//! works for pin(s) configured as input pin(s); however, the only meaningful
//! data returned is whether the pin is terminated with a pull-up or
//! down resistor.
//!
//! \return None
//
//*****************************************************************************
void
GPIOPadConfigGet(unsigned long ulPort, unsigned char ucPin,
unsigned long *pulStrength, unsigned long *pulPinType)
{
unsigned long ulTemp1, ulTemp2, ulTemp3, ulTemp4;
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
ASSERT(ucPin < 8);
//
// Convert from a pin number to a bit position.
//
ucPin = (1 << ucPin);
//
// Get the drive strength for this pin.
//
ulTemp1 = HWREG(ulPort + GPIO_O_DR2R);
ulTemp2 = HWREG(ulPort + GPIO_O_DR4R);
ulTemp3 = HWREG(ulPort + GPIO_O_DR8R);
ulTemp4 = HWREG(ulPort + GPIO_O_SLR);
*pulStrength = (((ulTemp1 & ucPin) ? 1 : 0) | ((ulTemp2 & ucPin) ? 2 : 0) |
((ulTemp3 & ucPin) ? 4 : 0) | ((ulTemp4 & ucPin) ? 8 : 0));
//
// Get the pin type.
//
ulTemp1 = HWREG(ulPort + GPIO_O_ODR);
ulTemp2 = HWREG(ulPort + GPIO_O_PUR);
ulTemp3 = HWREG(ulPort + GPIO_O_PDR);
ulTemp4 = HWREG(ulPort + GPIO_O_DEN);
*pulPinType = (((ulTemp1 & ucPin) ? 1 : 0) | ((ulTemp2 & ucPin) ? 2 : 0) |
((ulTemp3 & ucPin) ? 4 : 0) | ((ulTemp4 & ucPin) ? 8 : 0));
}
//*****************************************************************************
//
//! Enables interrupts for the specified pin(s).
//!
//! \param ulPort is the base address of the GPIO port.
//! \param ucPins is the bit-packed representation of the pin(s).
//!
//! Unmasks the interrupt for the specified pin(s).
//!
//! The pin(s) are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinIntEnable(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Enable the interrupts.
//
HWREG(ulPort + GPIO_O_IM) |= ucPins;
}
//*****************************************************************************
//
//! Disables interrupts for the specified pin(s).
//!
//! \param ulPort is the base address of the GPIO port.
//! \param ucPins is the bit-packed representation of the pin(s).
//!
//! Masks the interrupt for the specified pin(s).
//!
//! The pin(s) are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinIntDisable(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Disable the interrupts.
//
HWREG(ulPort + GPIO_O_IM) &= ~(ucPins);
}
//*****************************************************************************
//
//! Gets interrupt status for the specified GPIO port.
//!
//! \param ulPort is the base address of the GPIO port.
//! \param bMasked specifies whether masked or raw interrupt status is
//! returned.
//!
//! If \e bMasked is set as \b true, then the masked interrupt status is
//! returned; otherwise, the raw interrupt status will be returned.
//!
//! \return Returns a bit-packed byte, where each bit that is set identifies
//! an active masked or raw interrupt, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//! Bits 31:8 should be ignored.
//
//*****************************************************************************
long
GPIOPinIntStatus(unsigned long ulPort, tBoolean bMasked)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Return the interrupt status.
//
if(bMasked)
{
return(HWREG(ulPort + GPIO_O_MIS));
}
else
{
return(HWREG(ulPort + GPIO_O_RIS));
}
}
//*****************************************************************************
//
//! Clears the interrupt for the specified pin(s).
//!
//! \param ulPort is the base address of the GPIO port.
//! \param ucPins is the bit-packed representation of the pin(s).
//!
//! Clears the interrupt for the specified pin(s).
//!
//! The pin(s) are specified using a bit-packed byte, where each bit that is
//! set identifies the pin to be accessed, and where bit 0 of the byte
//! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOPinIntClear(unsigned long ulPort, unsigned char ucPins)
{
//
// Check the arguments.
//
ASSERT((ulPort == GPIO_PORTA_BASE) || (ulPort == GPIO_PORTB_BASE) ||
(ulPort == GPIO_PORTC_BASE) || (ulPort == GPIO_PORTD_BASE) ||
(ulPort == GPIO_PORTE_BASE) || (ulPort == GPIO_PORTF_BASE) ||
(ulPort == GPIO_PORTG_BASE) || (ulPort == GPIO_PORTH_BASE));
//
// Clear the interrupts.
//
HWREG(ulPort + GPIO_O_ICR) = ucPins;
}
//*****************************************************************************
//
//! Registers an interrupt handler for a GPIO port.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -