AnsweredAssumed Answered

Multiple BOARD_PinInit() functions cause GPIO Direction Register to be corrupted.

Question asked by Frank Sundermeyer on Sep 3, 2019
Latest reply on Sep 6, 2019 by Frank Sundermeyer

My main application breaks up the initialization into two separate
routines. BOARD_InitPins(), and BOARD_InitLEDsPins().
Both of these files are specified in the pin wizard, and affect
different pins.
During initialization, this fails, since the generated code does
not preserve port direction and overwrites the previous port
direction bits for pins set as outputs.

MCUXpresso configuration tools version:
=========================================
product: Pins v6.0
processor: LPC51U68
package_id: LPC51U68JBD64
mcu_data: ksdk2_0
processor_version: 6.0.2
board: LPCXpresso51U68


Application Code:
==================
The main routine call the two generated routines in sequence:

//*******************************************
// Initialize board hardware.
//*******************************************
// Set up basic I/O Pins
// Apply power to and reset both GPIO Ports
GPIO_PortInit(GPIO, 0);
GPIO_PortInit(GPIO, 1);
BOARD_InitPins();
// Set up pins for LEDs
BOARD_InitLEDsPins();


PinMux.c code, generated by Configuration tool:
=================================================

void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_Iocon);
GPIO->DIR[0] = ((GPIO->DIR[0] &
(~(GPIO_DIR_DIRP_MASK)))
| GPIO_DIR_DIRP(0x0D00u)
);
IOCON->PIO[0][0] = ((IOCON->PIO[0][0] &
(~(IOCON_PIO_FUNC_MASK | IOCON_PIO_DIGIMODE_MASK)))
| IOCON_PIO_FUNC(PIO00_FUNC_ALT1)
| IOCON_PIO_DIGIMODE(PIO00_DIGIMODE_DIGITAL)
);
....etc


void BOARD_InitLEDsPins(void) {
CLOCK_EnableClock(kCLOCK_Iocon);
GPIO->DIR[0] = ((GPIO->DIR[0] &
(~(GPIO_DIR_DIRP_MASK)))
| GPIO_DIR_DIRP(0xE0u)
);
GPIO->DIR[1] = ((GPIO->DIR[1] &
(~(GPIO_DIR_DIRP_MASK)))
| GPIO_DIR_DIRP(0x0800u)
);

Problem Description:
======================
When I execute the application code, BOARD_InitPins() executes
and sets up Port 0 direction register (CPIO->DIR[0]) to 0xD00,
which sets up Port 0, pins 8,10, and 11 as outputs. This is correct.
Next, executing the BOARD_InitLEDsPins() function sets up Port 0
direction register (CPIO->DIR[0]) to 0x0E0. It reads the Port Direction
register, but does not preserve the contents of the DIR[0] register.

Expected result: DIR[0] = 0x0DE0
Actual Result: DIR[0] = 0x00E0

In summary, the code generated by the configuration wizard always
appears to overwrite previously set direction bits, and does not
allow the configuration wizard to be used to set up port direction,
if the pin I/O is separated into sections.

 

My workaround is to handle pin setup separately in the application,
to ensure the pins are properly configured, but I would like to 
clean up the code, and leave this to the configuration tools. Is
there something I need to do to prevent interaction, or do I need to
merge all my GPIO configuration into the single BOARD_InitPins()

function.

Outcomes