Skip to main content
SDall.11
Associate III
August 6, 2020
Question

Problem with "HAL_UART_Transmit_IT" function

  • August 6, 2020
  • 4 replies
  • 2074 views

Hi, I've a problem with function HAL_UART_Transmit_IT.

If I call this function with global data array, there isn't a problem, but If I call it with a non global data array, I receive wrong data.

This is a sample code:

void SendData()
{
 uint8_t s[10];
 memset(&s, 0x10, 10);
 HAL_UART_Transmit_IT(&huart2, &s[0], sizeof(s));
}

This example gives problems, but if I move "uint8_t s[10]" out of function (make global) all works fine.

I don't understand the problem :(

I'm using IAR.

Some idea?

Thanks.

    4 replies

    prain
    Visitor II
    August 6, 2020

    In c programming language​, you should consider variable "scopes". memory used by variables inside functions, will automatically free after you exit the function. The temporary memory space that holds such variables is called stack.

    Piranha
    Principal III
    August 6, 2020

    It's a stack, not a heap...

    prain
    Visitor II
    August 6, 2020

    Yes, correct​

    SDall.11
    SDall.11Author
    Associate III
    August 6, 2020

    You're right, but...

    when I call HAL_UART_Transmit_IT the function is still alive.

    SendData die when HAL_UART_Transmit_IT returns.

    But now, answering you I've a doubt.

    I think that HAL_UART... returns before to have effectively sent data?

    Tomorrow I make some tests.

    Thanks for help.

    prain
    Visitor II
    August 6, 2020

    HAL_UART_Transmit_IT doesn't wait for transmit completion. It transmit data in background and call you back when transfer complete.

    Visitor II
    August 9, 2020

    HAL_UART_Transmit_IT() starts the transmit and exits. The auto variable s[10] will be out of the scope. Its stack allocated memory can be overwritten by any other function. To fix the issue, you can declare s[10] as static, or insert enough delay after HAL_UART_Transmit_IT().

    E-John
    Associate III
    February 15, 2024

    I've had that same problem.

    My solution is to declare the transmitted buffer as "global" or "static" type.