Hi
The following configures port A bits0, 3, 6, 22, 25 and 31 to GPIO functionality with enabled pull-up resistor, passive input filter:
PORTA_GPCLR = ((PORTA_BIT6 | PORTA_BIT3 | PORTA_BIT0) | (PORT_MUX_GPIO | PORT_PS_UP_ENABLE | PORT_PFE));PORTA_GPCHR = (((PORTA_BIT22 | PORTA_BIT25 | PORTA_BIT31) >> 16) | (PORT_MUX_GPIO | PORT_PS_UP_ENABLE | PORT_PFE));
This is however only a part of what may be needed to configure the complete port functionality and characterisics (eg. first power needs to be applied to the port).
The use of PORTx_GPCLR/PORTx_GPHR may save a few instructions but is not that generic since it requires grouping pins requiring identical settings into lower 16 and upper 16 bits.
More generally, the following can be used for complete initialision of GPIO input use (for example):
_CONFIG_PORT_INPUT(A, (PORTA_BIT22 | PORTA_BIT25 | PORTA_BIT31 | PORTA_BIT6 | PORTA_BIT3 | PORTA_BIT0), (PORT_PS_UP_ENABLE | PORT_PFE));
where:
#define _CONFIG_PORT_INPUT(ref, pins, chars) SIM_SCGC5 |= SIM_SCGC5_PORT##ref; \
fnConnectGPIO(PORT##ref, pins, chars); \
GPIO##ref##_PDDR &= ~(pins);and
// This routine is used to connect a number of port pins to their GPIO function with defined characteristics
//
extern void fnConnectGPIO(int iPortRef, unsigned long ulPortBits, unsigned long ulCharacteristics)
{
unsigned long ulBit = 0x00000001;
volatile unsigned long *ptrPCR = (volatile unsigned long *)(PORT0_BLOCK + (iPortRef * 0x1000));
while (ulPortBits != 0) {
if (ulPortBits & ulBit) {
*ptrPCR = (PORT_MUX_GPIO | ulCharacteristics); // set the GPIO characteristics for each port pin
ulPortBits &= ~(ulBit);
}
ptrPCR++;
ulBit <<= 1;
}
}
Similar macros (with small changes) also allow generic output and peripheral type configuration.
Regards
Mark