AnsweredAssumed Answered

How to init USART2 in asynch mode in stm32L476 discovery kit

Question asked by Eugenia Suarez on Feb 8, 2017
Latest reply on Apr 24, 2018 by Ijaz Ahmad

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)



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.