2024-03-17 10:52 AM
STM32F103
Im using HAL_UART_Transmit_IT but doesnt work, if change to HAL_UART_Transmit all works fime but add delays to move in motors. Where are my error? i see all videos on youtb and cant find the problem.
I have enabled the IT on MX the priority are on 0, (try other combinations but same result)
i call the enviar4Variables_IT on the while of movement motors, for update the position without delay the motors.
Out side the while i call with out IT with no problems.
I check the debug up to return HAL_OK (not are busy, the addres of pointer its correct inside of HAL_UART_Transmit_IT )
I think must works but i dont know what happen
Thanks in advance
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
uartIT_free = 1;
}
void enviar4Variables_IT(uint16_t variable, uint16_t valor1, uint16_t valor2, uint16_t valor3, uint16_t valor4)
{
if(uartIT_free)
{
uartIT_free = 0;
uint8_t data[] = {0x82, (uint8_t)(variable >> 8), (uint8_t)(variable & 255), (uint8_t)(valor1 >> 8), (uint8_t)(valor1 & 255), (uint8_t)(valor2 >> 8), (uint8_t)(valor2 & 255), (uint8_t)(valor3 >> 8), (uint8_t)(valor3 & 255), (uint8_t)(valor4 >> 8), (uint8_t)(valor4 & 255)};
uint16_t crcreturn = ModRTU_CRC(data, 11);
uint8_t data_to_send[] = {0x5A,0xA5,0x0D,0x82, (uint8_t)(variable >> 8), (uint8_t)(variable & 255), (uint8_t)(valor1 >> 8), (uint8_t)(valor1 & 255), (uint8_t)(valor2 >> 8), (uint8_t)(valor2 & 255), (uint8_t)(valor3 >> 8), (uint8_t)(valor3 & 255), (uint8_t)(valor4 >> 8), (uint8_t)(valor4 & 255),(uint8_t)(crcreturn&255),(uint8_t)(crcreturn>>8)};
HAL_UART_Transmit_IT(&huart2, data_to_send, 16);
}
}
Solved! Go to Solution.
2024-03-17 12:37 PM
The memory is correct when you make it call. It is after the call that it changes. HAL_UART_Transmit_IT works asynchonously, in the background.
It is not strange that sending data that has gone out of scope does not work correctly. That is expected.
2024-03-17 11:28 AM - edited 2024-03-17 11:29 AM
You're sending data on the stack, which immediately goes out of scope when enviar4Variables_IT returns. This results in the call sending random data. Define data_to_send as a global buffer instead to prevent it from going out of scope. Or define it as static within the function which has the same effect.
The data you are sending with HAL_UART_Transmit_IT must remain valid until the transmission completes.
The code you've presented is otherwise fine. Could be other issues in code you aren't showing.
Hopefully you have defined uartIT_free as volatile.
2024-03-17 11:30 AM
But if this are the problem (i will check now) why work the HAL_UART_Transmit not IT ?
I will check now and leave a reply, thanks
2024-03-17 11:36 AM - edited 2024-03-17 11:36 AM
HAL_UART_Transmit blocks, so the data remains valid during the call. HAL_UART_Transmit_IT does not.
2024-03-17 11:44 AM
2024-03-17 12:37 PM
The memory is correct when you make it call. It is after the call that it changes. HAL_UART_Transmit_IT works asynchonously, in the background.
It is not strange that sending data that has gone out of scope does not work correctly. That is expected.
2024-03-17 01:26 PM
You are right my friend, i think start to send bytes correct but a few bytes after changes the memory and send wrong bytes.
THANKS!!