How to config UART in STM32F446?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-02-10 4:44 AM
I'm using STM32F446, I found one issue.
- 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).
- 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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-02-10 5:43 AM
> 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-02-12 2:24 AM
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
Thanks
Imen
