Skip to main content
Vu.Andy
Associate III
January 4, 2018
Question

USart not receiving data after awhile

  • January 4, 2018
  • 3 replies
  • 1459 views
Posted on January 04, 2018 at 20:38

I am using STM32F051 device.  For some reason, after the host sending data for awhile, Rx stops receiving data.

If I use the debugger and stop the code, and try to send something back to the host, then the Rx begins receiving again.

So I am not sure why sending some data back to the host allows the Rx to receive data again.

Is there a flag I can check to see if there is any error? 

Here are the codes to initialize UART.

void MX_USART1_UART_Init(void)

{

  huart1.Instance = USART1;

  huart1.Init.BaudRate = 9600;

  huart1.Init.WordLength = UART_WORDLENGTH_8B;

  huart1.Init.StopBits = UART_STOPBITS_1;

  huart1.Init.Parity = UART_PARITY_NONE;

  huart1.Init.Mode = UART_MODE_TX_RX;

  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart1.Init.OverSampling = UART_OVERSAMPLING_16;

  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  if (HAL_UART_Init(&huart1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    

    UsartBuff[0] = 0;

    UsartBuff[1] = 0;

    L_UsartDataIdx = 0;

    

    InitUsartReceiveIT();

}
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    January 4, 2018
    Posted on January 04, 2018 at 20:52

    Make sure you recognize and clear framing or parity type errors.

    I'd hazard it's not the initialization code.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Andrew Neil
    Super User
    January 5, 2018
    Posted on January 05, 2018 at 14:15

    Clive One wrote:

    I'd hazard it's not the initialization code.

    Indeed.

    The fact that it starts working suggests that the initialisation is OK - something is going wrong after initialisation.

    As

    Golab.Piotr

    ‌ said, are you sure that the

    host is really still sending?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    January 5, 2018
    Posted on January 05, 2018 at 18:24

    I've had Prolific serial drivers go brain dead, but that's been a few years ago.

    When I have serial issues that don't make sense the scope is out pretty quickly to sanity check the situation so I don't spend hours looking in the wrong place.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Bogdan Golab
    Lead
    January 4, 2018
    Posted on January 04, 2018 at 23:07

    I would verify if the host is really sending the data (any logic analyser connected to the uart to sniff the traffic?). Maybe the host was unblocked by the traffic send back (we do not know the code of neither host nor STM32).

    It seems that you handle the the received data through the IT. How much time you spend there after receiving the IT?. You may test the code without IT (waiting for the data in a loop).

    Anyway I would try to simplify the code on both sides  (PC and STM32) to narrow down the problem and eliminate the problems mentioned by Clive One.

    T J
    Senior III
    January 5, 2018
    Posted on January 05, 2018 at 23:20

    if you don't clear the Rx data register fast enough, then the interrupts will stop.

    I use a while loop to clear the double buffered Rx data register.

    while (rxne)

       { take data}