Skip to main content
jgauthier
Senior
May 25, 2022
Solved

BUG in CubeMX code generation for STM32G050K6Tx

  • May 25, 2022
  • 2 replies
  • 749 views

Enable both USART1 and USART2.

The code generated for initialization of the GPIO looks like this:

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
 
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 if(uartHandle->Instance==USART1)
 {
 /* USER CODE BEGIN USART1_MspInit 0 */
 
 /* USER CODE END USART1_MspInit 0 */
 /** Initializes the peripherals clocks
 */
 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 {
 Error_Handler();
 }
 
 /* USART1 clock enable */
 __HAL_RCC_USART1_CLK_ENABLE();
 
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**USART1 GPIO Configuration
 PA9 ------> USART1_TX
 PA10 ------> USART1_RX
 */
 GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 /* USER CODE BEGIN USART1_MspInit 1 */
 
 /* USER CODE END USART1_MspInit 1 */
 }
 else if(uartHandle->Instance==USART2)
 {
 /* USER CODE BEGIN USART2_MspInit 0 */
 
 /* USER CODE END USART2_MspInit 0 */
 
 /* USART2 clock enable */
 __HAL_RCC_USART2_CLK_ENABLE();
 
 __HAL_RCC_GPIOA_CLK_ENABLE();
 /**USART2 GPIO Configuration
 PA2 ------> USART2_TX
 PA3 ------> USART2_RX
 */
 GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 /* USER CODE BEGIN USART2_MspInit 1 */
 
 /* USER CODE END USART2_MspInit 1 */
 }
}

Please note that the generation for USART1 is missing the line:

GPIO_InitStruct.Alternate = GPIO_AF1_USART1;

and therefore will not operate as a UART.

Confirmed with CubeMX 6.5.0

This topic has been closed for replies.

2 replies

KnarfB
KnarfBAnswer
Super User
May 25, 2022
jgauthier
jgauthierAuthor
Senior
May 25, 2022

Dang, if only all my searches revealed that before fighting with it myself.