2020-08-06 09:10 AM
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.
2020-08-06 09:32 AM
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.
2020-08-06 09:51 AM
It's a stack, not a heap...
2020-08-06 11:13 AM
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.
2020-08-06 12:14 PM
Yes, correct
2020-08-06 12:18 PM
HAL_UART_Transmit_IT doesn't wait for transmit completion. It transmit data in background and call you back when transfer complete.
2020-08-09 12:40 AM
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().
2024-02-15 04:01 AM - edited 2024-02-15 04:02 AM
I've had that same problem.
My solution is to declare the transmitted buffer as "global" or "static" type.