Showing results for 
Search instead for 
Did you mean: 

Problem with "HAL_UART_Transmit_IT" function

Associate III

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?


Senior III

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.

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

Associate III

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.

Yes, correct​

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

Yevgeni Tunik

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().

Associate III

I've had that same problem.

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