cancel
Showing results for 
Search instead for 
Did you mean: 

Receiving data from USART

PToma.14
Associate II

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?

5 REPLIES 5

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
Up vote any posts that you find helpful, it shows what's working..
PToma.14
Associate II

I have checked wiring is proper.

Slh
Senior

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
Associate II

This resolved my problem

S.Ma
Principal

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.