Skip to main content
PToma.14
Associate II
April 15, 2019
Question

Receiving data from USART

  • April 15, 2019
  • 5 replies
  • 1321 views

I am trying to use asynchronous USART on stm32L4R5ZI nucleo board for sending and receiving data. Following is related part of code.

while (1)
 {
	 HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin); //Toggle LED
	 HAL_Delay(1000); //Delay 1 Seconds
	 for(i = 0; i < 5; i++)
	 {
	 	 USART1->TDR = p[i];
	 while((USART1->ISR & 0x40) == 0);
	 }
	 while ((USART1->ISR & 0x20) == 0);
	 uint32_t receivedByte = (uint32_t)(USART1->RDR);
 }

Problem is that I am able to send data to PC but I am not able to receive data. What can be the problem?

This topic has been closed for replies.

5 replies

Tesla DeLorean
Guru
April 15, 2019

It is not wired properly.

You are not clearing any pending noise or overflow flags, etc on the receiver.

The code might work more effectively if you don't continually block, and check for TXE before sending a byte.​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
PToma.14
PToma.14Author
Associate II
April 15, 2019

I have checked wiring is proper.

Slh
Senior
April 15, 2019

Or test you this code:

uint8_t getChar()
{
 
 uint8_t InputData = 0;
 
 while (1)
 {
 
 if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE))
 
 __HAL_UART_CLEAR_OREFLAG(&huart1);
 
 
 if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE))
 {
 
 InputData = huart1.Instance->RDR & 0x1FF;
 
 return InputData;
 
 }
 
 }
 
}

PToma.14
PToma.14Author
Associate II
May 25, 2019

This resolved my problem

S.Ma
Principal
May 25, 2019

This is polling based. If you want to keep it that way, add the DMA as cyclic buffer filling from RX side, then check the incoming buffer regularly.

Transmit can be polling as you are in control of sending data timeline.

Imagine that you add a HAL_Delay(10) somewhere in your code, you might lose incoming bytes from USART the way it is here. From example to application type example.