cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CubeMX bug report: missing initialization of GPIO_InitStruct.Alternate

Evgeniy Bobkov
Associate II
Posted on March 19, 2018 at 20:15

Looks like I found a small bug in STM32 CubeMX code generation. It can be reproduced this way:

1. Create new project for STM32F091RB.

2. Enable USART5 with Asynchronous mode and Hardware Flow Control (RS485) on.

3. In Project Settings select Toolchain/IDE = SW4STM32 (probably irrelevant).

4. Generate the code.

As result in the stm32f0xx_hal_msp.c file we have:

void HAL_UART_MspInit(UART_HandleTypeDef* huart)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(huart->Instance==USART5)

  {

    GPIO_InitStruct.Pin = GPIO_PIN_12;

    ...

    GPIO_InitStruct.Alternate = GPIO_AF2_USART5;

    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    ...

    GPIO_InitStruct.Pin = GPIO_PIN_2;

    ...

    

GPIO_InitStruct.Alternate = GPIO_AF2_USART5;

    

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    

GPIO_InitStruct.Pin = GPIO_PIN_5;

    

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    

GPIO_InitStruct.Pull = GPIO_NOPULL;

    

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;     /* [1]  */

    

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  

}

}

The initialization of 

GPIO_InitStruct.

Alternate field for PB5 is missing after the line marked by /* [1] */. The missing line should be:

GPIO_InitStruct.Alternate = GPIO_AF4_USART5;

Otherwise the previous value for PD2 pin is used (

GPIO_AF2_USART5

) and the UART5_DE pin doesn't work.
4 REPLIES 4
Houda GHABRI
ST Employee
Posted on April 11, 2018 at 17:07

Hello Evgeniy,

Thanks for your feedback.

I confirm the bug. The issue will be fixed next CubeMX release.

Best regards,

Houda

Houda GHABRI
ST Employee
Posted on June 08, 2018 at 12:39

Hello

evgen

You can switch to STM32CubeMX4.0 available now on ST website.

This issue is resolved.

regards,

Houda

Breukers.Ron
Associate II
Posted on June 25, 2018 at 15:49

I found similar - maybe related - bug. I used 4.26.0 to generate code for the STM32F042K6Ux. It worked fine with 4.25, but now it completely skipped the generation of MX_GPIO_Init.

Can anybody help me how to get back to 4.25, because I am sort of stuck. I could try to generate the block by hand but I am not sure I can trust the 4.6.0

Thanks,

Ron

smith
Associate II

I have seen this problem today as well with Truestudio 9.0.1 and cube but it seems intermittent. I take an old project and move it and sometimes it does not generate the MX_GPIO_Init code even though I set some GPIO pins explicitly in Cube.