Skip to main content
Graduate
February 10, 2021
Question

How to config UART in STM32F446?

  • February 10, 2021
  • 2 replies
  • 1865 views

I'm using STM32F446, I found one issue.

  1. In STM32F4xx_hal_uart.c, it stated that.

 ==============================================================================

            ##### How to use this driver #####

 ==============================================================================

 [..]

  The UART HAL driver can be used as follows, pay attention to the last line about UART pins, it's stated that RX as alternate function input.

  (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).

  (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:

    (##) Enable the USARTx interface clock.

    (##) UART pins configuration:

      (+++) Enable the clock for the UART GPIOs.

      (+++) Configure these UART pins (TX as alternate function pull-up, RX as alternate function Input).

  1. When I generate code by CubeMX, the generated code in function HAL_UART_MspInit, GPIO_PIN_3 is RX, and GPIO_PIN_2 is TX, but both of them are configured the same. It's different from the description in STM32F4xx_hal_uart.c. I tested, and found that the CubeMX generated code works. If I configure GPIO_PIN_3 as GPIO_MODE_INPUT, UART does not work, is the comment in STM32F4xx_hal_uart.c wrong?

    GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_PULLUP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    This topic has been closed for replies.

    2 replies

    Super User
    February 10, 2021

    > is the comment in STM32F4xx_hal_uart.c wrong?

    Yes.

    There's no "alternate function input"

    Except in the old 'F1 family, if you set a pin to AF in GPIO_MODER (and assign some AF in respective GPIO_AFR), the module to which this pin is assigned controls the direction of that pin. UARTs set Rx pin to Input, Tx to Output, as soon as you enable the receiver/transmitter/UART in UART_CR1.

    JW

    Technical Moderator
    February 12, 2021

    Hello @HXiao.1​ and welcome to the STM32 Community,

    I escalated the wrong comment to the development team for correction in the coming releases.

    Thanks for your contribution.

    Imen