STM32 gotchas
70.Pins don't work even if set in GPIO (due to nonzero default values in 'L0/'L4/'Gx/'H7 GPIOx_MODER)

Sometimes, users who were used to work with models in some of the STM32Fxx family, especially employing normal register-level approach, when migrating to 'L0/'L4/'Gx/'H71, find with surprise, that the pins initialized in GPIO using an already well-proven routine or method, simply don't work at all.

The reason is, that users often write code such as

GPIOB->MODER |= (0b10 << (2 * 10));  // set PB10 to AF
which relies on the fact, that in 'Fxx, most GPIOx_MODER fields are set to 0 after reset. Thus, in these families, after reset, most pins are set to Digital Input mode2 3.

However, in the 'L0/'L4/'Gx/'H7 families, the default value of GPIOx_MODER fields for the majority of pins is 0b11, i.e. almost all pins are set as Analog. This presumably is aimed at lowering the consumption of unconfigured pins, as in this setting, the Schmitt trigger digital input buffer is disconnected, so should a pin be unused and unconnected, any noise induced to it does not flip state of any internal digital gate.

ORing any value to a field already set to all-ones of course makes no change, so the GPIO initialization routine for these families has to be modified accordingly.


1. For some unknown reason, reset value of GPIOx_MODER for most pins in 'L1 is 0b00, as is in the 'Fxx families. Maybe it is related to the fact that 'L1 was the first low-consumption STM32 family, and in fact the GPIO structure used ever since was introduced with this family.

2. Exceptions to this are generally the pins related to JTAG/SWD, but there may be more in particular models. One of such exception is PA4 in the 'L0 family, which is set by default as (digital) Input, whereas all other pins (except SWD pins) are set as Analog.

3. The ancient 'F1 family has a different GPIO structure, we won't be discussing that here.