cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Transmit_IT doesn't transmit

woutnme
Associate II

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);
	}
}

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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

HAL_UART_Transmit blocks, so the data remains valid during the call. HAL_UART_Transmit_IT does not.

If you feel a post has answered your question, please click "Accept as Solution".

 

I has checked the memory and are correct, and yes every reply HAL_OK the HAL_UART_Transmit_IT
i will try too see it with a serial monitor to check if are sending the correct data.
But its so strange.....
On bottom are the code

Thanks

 

mem.jpg

 

 

 

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.

If you feel a post has answered your question, please click "Accept as Solution".

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!!