cancel
Showing results for 
Search instead for 
Did you mean: 

Issue getting all data over USART with LL API LL_USART_ReceiveData8

mohitmg
Associate II

Hi, 

   I am using STM32F407VET6 MCU and trying to receive data using LL API LL_USART_ReceiveData8 over USART configured as modbus.

   I confirmed USART transmit (

LL_USART_TransmitData8) is successful through an modbus APP, on rx side of the other device as well as fact  that LL_USART_IsActiveFlag_TC is returning True.

  I also confirmed other receiving device also sending a 8 byte response back. (Confirmed via Modbus app)

  But on the STM32F407VET6 MCU side I am just receiving 1 byte of data. 

  Note - If I don't insert delay (LL_mDelay) I only get first byte, if I add delay I only get 2nd byte. 

After receiving one byte , its just forever waiting on LL_USART_IsActiveFlag_RXNE (never comes out of while loop).

Based on online search I see lots of folks are seeing the same problem (

LL_USART_IsActiveFlag_RXNE getting overrun ?)  but could not find solution yet.
If HAL_UART_Receive is used instead of LL_USART_ReceiveDatta8, MCU is receiving complete response.
 
Below is my code :-
void LL_USART_Transmit(USART_TypeDef *USARTx, uint8_t *buff, uint32_t len)
{
    printf("LL_USART_Transmit() rcvd Len= %d\n", len);

   for (int i = 0; i < len; i++)
  {
      LL_USART_TransmitData8(USARTx, buff[i]);
      LL_mDelay(10);
 
      if (LL_USART_IsActiveFlag_TC(USARTx)) {
        printf("LL_USART_Transmit() successful, data= %d\n", buff[i]);
     }
     else
     {
         printf("LL_USART_Transmit() TC flag False");
      }  
   }
}
 
uint8_t LL_USART_Receive_Byte(USART_TypeDef *USARTx) {
       // Wait for Read Data Register is not empty
      while (!LL_USART_IsActiveFlag_RXNE(USARTx));
      return LL_USART_ReceiveData8(USARTx);
}
 
void LL_USART_Receive(USART_TypeDef *USARTx, uint8_t *buff, uint32_t len)
{
        printf("LL_USART_Receive() rcvd Len= %d\n", len);

         for (int i = 0; i < len; i++)
         {
                buff[i] = LL_USART_Receive_Byte(USARTx);

               LL_mDelay(200);
               printf("LL_USART_Receive() rx_data= %d\n", buff[i]);
         }
}
 
3 REPLIES 3

You probably want to pay attention to receive and transmit concurrently and not block, as both things can and do happen at once.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Can you please elaborate ? Is there any code change recommendations compared to what I posted ?

TDK
Guru

Consider what is happening on the line. When you send 8 characters, your code is inside LL_USART_Transmit. It is not reading character coming in. So when characters are sent back, and you're still within the sending code, they are missed. UART reception is typically done best in a non-blocking manner such as using HAL_UART_Receive_IT or HAL_UART_Receive_DMA or HAL_UARTEx_ReceiveToIdle_*.

 

If you feel a post has answered your question, please click "Accept as Solution".