Skip to main content
mikmary
Associate
October 3, 2018
Solved

How to initialize STM32L152 UART2 using ST library?

  • October 3, 2018
  • 4 replies
  • 1610 views

Hi,

I'm using the evaluation board STEVAL-IDS001V4M which has a STM32L152 mounted on.

I'm having some trouble on the initialization of the UART2 peripheral.

I'm using the ST library contained into the STSW-CONNECT009 packet.

To initialize the UART2, I'm doing the following steps:

>> RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA); //activate clock on peripheral

>> USART_StructInit(&USART_InitStruct); //initialize parameters (parity, stop bit, bauderate....)

>> USART_Init(USART2, &USART_InitStruct); //initialize USART2 parameters

>> USART_Cmd(USART2, ENABLE); //enable usart2

Then I use the following function to send multiple times a character 'A' to my PC.

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)

{

 /* Check the parameters */

 assert_param(IS_USART_ALL_PERIPH(USARTx));

 assert_param(IS_USART_DATA(Data));

 /* Transmit Data */

 USARTx->DR = (Data & (uint16_t)0x01FF);

}

But nothing arrives at my COM port!

What did I miss in the initialization?

This topic has been closed for replies.
Best answer by mikmary

Thank you all for the answers!

Unfortunately I'm working with the STM32L152C, and the library of the STM32CubeL1 are based on the STM32L152D, so i don't have the "HAL" based library which is used in the examples.

Still I managed to solve the problem.

Initialize the UART by:

>> USART_InitTypeDef USART_InitStruct; //see stm32l1xx_usart.h

>> USART_StructInit(&USART_InitStruct); //set parameters of USART2, see stm32l1xx_usart.c to modify the values

>> SdkEvalComInit(COM1, &USART_InitStruct);//initialize UART2, see SDK_EVAL_Com.c

Then to send a char use:

USART_SendData(USART_TypeDef* USARTx, uint16_t Data)

4 replies

Khouloud GARSI
Technical Moderator
October 3, 2018

Hi @mikmary​ ,

I'd highly advise you to refer to UART examples under the STM32CubeL1 firmware package. 

It's found under the path below:

STM32Cube_FW_L1_V1.8.1\Projects\STM32L152D_EVAL\Examples\UART

You may take the examples as a reference to correctly configure your peripheral.

Link to download the cube package:

https://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-mcu-packages/stm32cubel1.html#design-scroll

Khouloud.

AvaTar
Senior III
October 3, 2018

The SPL-based firmware libraries for the L152 MCU and the L152_discovery are still available for download. The contain UART examples, as far as I remember.

> But nothing arrives at my COM port!

Do you see anything at the TX pin of USART 2 ?

The way to the receiver COM port is long ...

Tesla DeLorean
Guru
October 3, 2018

So I don't have to pull the manuals, PA2 and PA3?

You will need to enable the clocks for the GPIOA and USART2

You will need to configure the pins to associate with USART2

Before you call SendData you must check that the status flags TXE to let you know the USART is ready for the next byte

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
mikmary
mikmaryAuthorBest answer
Associate
October 3, 2018

Thank you all for the answers!

Unfortunately I'm working with the STM32L152C, and the library of the STM32CubeL1 are based on the STM32L152D, so i don't have the "HAL" based library which is used in the examples.

Still I managed to solve the problem.

Initialize the UART by:

>> USART_InitTypeDef USART_InitStruct; //see stm32l1xx_usart.h

>> USART_StructInit(&USART_InitStruct); //set parameters of USART2, see stm32l1xx_usart.c to modify the values

>> SdkEvalComInit(COM1, &USART_InitStruct);//initialize UART2, see SDK_EVAL_Com.c

Then to send a char use:

USART_SendData(USART_TypeDef* USARTx, uint16_t Data)