I was going through ST Microelectronics code and found the following code

/*Configure GPIO pin Output Level */

/*Configure GPIO pin : PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

The pin 1 of port A is set to low before initializing the pin.

I was wondering if this method is allowed? Not specific to ST Microelectronics but any vendor offering similar solutions.

If method isn't allowed what will be the state of the GPIO pin?

I went through these questions but they don't answer my query this and this.


2 Answers 2


I guess, yes. But it could have no real significance until the GPIO is configured. As you might see from depiction, the pull ups are separated from GPIO function, in your application they are no-pull meaning both pull-ups disconnected. The GPIO could be read at any time, regardless of the configuration as it doesn't alter any behaviour, well writing on GPIO however does change it, since it turns on/off output transistors P-MOS and N-MOS. To be able to control the output transistors the GPIO has to be configured as output, but if you write the register before that, you can but it won't alter the physical state of the GPIO, just the output register.

enter image description here


Yes, perfectly valid and allowed procedure to set some registers before other registers, like setting the output state register before output direction registers. It is done to prevent any glitches in the wrong direction, because it might do something important like enabling regulators or trigger a latch. STM32 can do it, AVR can do it. The datasheet or reference manual will tell you the default state of registers, so there is no general answer to that if they are high or low. STM32 have the pin data register set low. If the output pin is really important, it can have a pull-up or pull-down resistor to keep default state when MCU is not yet running, but under firmware programming or user pushing a reset button.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.