2017-02-08 09:29 AM
I need to test a fw that plays with uart output/input operating in asynch mode. Although this mcu has 2 uart and 3 usart (asynch/synch configurable) ports the only available USART module is USART2 port in PD5, PD6 pins. Now I'm ready to code in order to get usart2 with asynch mode at those pins.
I'm confused, because according the UM1884 (pg 892, 893) instructions about low level drivers, this manual seems to be wrong. Since this guide uses HAL_USART API (with the corresponding UARt typedef struct) but it only refers to synchronous parameters and synch modes, not asynch ones.
I can't find the way of init USART2 with asynch mode in that guide.
Then, I tried doing it with Cube, and what a surprise when I see that its C generated code uses HAL_UART API, instead the
HAL_USART (neither the corresponding USARt typedef struct), as detailed in its own document. But I can't see how must to be configured those GPIOS.
Hence, what is the correct way of configuring the USART2, PD5 and PD6 pins?
Cube part of init code:
_____________________
/* USART2 init function */
UART_HandleTypeDef huart2; //it's type uart struct
static void MX_USART2_UART_Init(void){huart2.Instance = USART2;
huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_7B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); }}
Manual says about hal_USART uses:
______________________________
1.- Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
...
3. Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware flow control and Mode (Receiver/Transmitter) in the husart handle Init structure.
4.- Initialize the USART registers by calling the HAL_USART_Init() API.
Well, if you go to definition of husart handle Init structure you will see that this field doesn't exists.
...
This subsection provides a set of functions allowing to initialize the USART in
asynchronous and in synchronous modes.
? For the asynchronous mode only these parameters can be configured:
? Baud Rate
? Word Length
? Stop Bit
? Parity: If the parity is enabled, then the MSB bit of the data written in the data register is transmitted but is changed by the parity bit.
? USART polarity
? USART phase
? USART LastBit
? Receiver/transmitter modes
I think the last of these are synch parameters that in asynch mode can't be filled.
Thanks in advance.
#usart2 #stm32l476 #discovery-board #pd5 #pd6 #gpio-pin-configuration2017-02-09 10:08 AM
Hi
suarez.eugenia
,Actually, among all UART/USART instances available on STM32L476xx (USART1/USART2/USART3 and UART4/UART5), all are UART capable (USART instances could be used in Asynchronous mode). Opposite is not true, as UART only instances (as UART4 or UART5) could not be configured in Synchronous mode (no clk generation capability).
So, depending on which API level you want to use (HAL or LL) here is what you should do:
- For HAL use, in order to use USART2 in Asynchronous mode, you should use HAL_UART_*** APIs, with USART2 instance. Code from Cube MX you mentioned seems ok : your huart2 (UART_HandleTypeDef type) handle is pointing on USART2 instance.
in UM1884, you should then refer to chapter 58-59 (HAL UART) instead of 60 (USART) for HAL UART API.
- For LL use, all services are contained in stm32l4xx_ll_usart.h/.c. Here, no more Handle typedef, all APi are to be used directly with USART instance parameter (USART2 in your case).
Capabilities you will try to access using LL services will of course depend upon support by the corresponding instance (for instance, LL_USART_SetClockPhase functions will not be effective if called on UART4 or UART5 instance.
LL services description is also provided in UM1884 (See chapter 85).
In addition, you might also find some additional information by looking at examples provided in STM32L4 Firmware package (not for discovery board, but for Nucleo), in :
- Projects\STM32L476RG-Nucleo\Examples\UART
- Projects\STM32L476RG-Nucleo\Examples_LL\USART
directories.
Hope this helps.
Regards
Guenael
2017-02-28 05:05 AM
Thank you for all this information, finally I could test two uart ports. I was sending and receiving correctly some characters.
I configured and used USART2 with HAL_UART functions for asynch mode USART. Since it was the way followed by CUBE code generator and now it also works propperly in my case.
Regards!
2018-04-24 05:18 AM
Hi, I am facing a similar problem. As you mentioned...
Guenael Cadier wrote:
- For HAL use, in order to use USART2 in Asynchronous mode, you should use HAL_UART_xxx APIs, with USART2 instance.
My question is:
1. Why can't we use HAL_USART_INIT() function for configuring USART in asynchronous mode?
2. When i do it with
HAL_UART_INIT() it receives and transmit correctly. but when i do it with HAL_USART_INIT(), the controller seems to be always receiving data. The received byte flag is always ON. Whats the background reason? I am using STM32F4-discovery board.