AnsweredAssumed Answered

STM32 CubeMX bug report: missing initialization of GPIO_InitStruct.Alternate

Question asked by Evgeniy Bobkov on Mar 19, 2018
Latest reply on Apr 11, 2018 by houda ghabri

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.

Outcomes