STM32CubeMX generates incorrect code for GPIO initialisation with LL-libraries (STM32F1xx)
I beleive that I have discovered a bug where CubeMX v4.27.0 generates incorrect code for GPIO initialisation.
Here is the story:
Pins have been preconfigured via Pin Configuration menu in the CubeMX app the following way:
- PC14 as Input with Pull-Up
- PC15 as Output (push-pull) with Low Initial State

Here is the code generated for MX_GPIO_Init() function with my comments added:
/**/
LL_GPIO_ResetOutputPin(GPIOC, DO_5V_ENABLE_Pin); // DO_5V_ENABLE_Pin is set to be LOW here
/* ... skipped ... */
/**/
GPIO_InitStruct.Pin = DI_5V_FUSE_STAT_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(DI_5V_FUSE_STAT_GPIO_Port, &GPIO_InitStruct);
/**/
GPIO_InitStruct.Pin = DO_5V_ENABLE_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
LL_GPIO_Init(DO_5V_ENABLE_GPIO_Port, &GPIO_InitStruct); // DO_5V_ENABLE_Pin goes HIGH after initialisationAs you see, the DO_5V_ENABLE_Pin ends up HIGH after initialisation although STM-code tried setting it LOW in the beginning.
Why?
Because the GPIO_InitStruct structure is re-used for each pin being configured but not all fields are initialised all the time.
It is obvious that line 9 sets the 'Pull' field to HIGH and later, when another pin is configured as Output, the 'Pull' field is never updated but still gets written to the corresponding ODR-register by the LL_GPIO_SetPinPull() function called by the LL_GPIO_Init() function (file 'stm32f1xx_ll_gpio.c'):
/* Pull-up Pull-down resistor configuration*/
LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);That's how the pin set to have its initial value to be LOW ends up being HIGH as a result.
The solution is either to initialise all fields of the structure explicitly OR update the LL_GPIO_Initi() function so it only calls LL_GPIO_SetPinPull() function when we the pin Mode is set to Input, i.e. change this code in the 'stm32f1xx_ll_gpio.c' file:
/* Pull-up Pull-down resistor configuration*/
LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);to this code:
/* Pull-up Pull-down resistor configuration - ONLY FOR INPUTS (STM bug fix) */
if (GPIO_InitStruct->Mode == LL_GPIO_MODE_INPUT)
{
LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
}Hope it saves time someone ;)
Cheers.
#[STM32 MCUs] #STM32CubeMX #Ll-gpio