cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Transmit works fine, but HAL_UART_TransmitIT does not

rvalente
Associate II

Hello Mates,

started using the HAL_UART_Transmit function, works like a charm. But since I`m using the UART for diagnostics ever 100ms seems better to use the one with interruption.

The arguments seems to be the same, but the read data from the uart while using the IT is gibberish.

Any guesses why can this happen?

0690X000008i9eQQAQ.png0690X000008i9eLQAQ.png

6 REPLIES 6
S.Ma
Principal

I would go for LL (low level) for USART as the peripheral is easy to handle.

Bob S
Principal

You need to show more code. Specifically, what clears the task.txclear flag that then allows the code to call HAL_UART_Transmit_IT() again?

It almost looks like you are restarting the transmit sequence before the previous one has finished. With the polled HAL_UART_Transmit(), the data has been completely sent from the UART before the function returns. WIth HAL_UART_Transmit_IT() that is NOT the case. That function returns as soon as the UART has been configured to START sending the data. You need to add code in HAL_UART_TxCpltCallback() if you need to know when the data has been sent.

Is the data in a auto/local variable? Watch the scope.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hello,

task.txclear is cleared by TxCpltCallback, just like suggested.

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)

{

 /* Set transmission flag: transfer complete */

task.txClear = 0;

}

Also, I tryed to send uart burst very very slow, every second, being the uart set as 57600 the time should not be a problem.

Although, i get the very same behavior.

Hello,

yes, data is available every time, with and without IT.

The function HAL_UART_TxCpltCallback happens and task.txclear is reset in this function, although if I add a 200ms delay after the HAL_UART_Transmit_IT the received data is correct.

Seems to me the HAL_UART_TxCpltCallback is happening prior to UART has really finished sending, is there a fix?

if (task.txClear == 0)

{

HAL_UART_Transmit_IT(&huart1, buffer, i); //Handler, array buffer, number of chars loaded, timeout

task.txClear = 1;

}

HAL_Delay(200);

Bob S
Principal

I'm not sure that "data is available every time" quite answers @Community member​ 's question. For example, if your code looks something like this:

void SendMyData( void )
{
   uint8_t buffer[7] = "*****\r\n";
 
   if ( !task.txClear )
   {
      HAL_UART_Transmit_IT(&huart1, buffer, 7);
	  task.txClear = 1;
   }
   
   // maybe do some more stuff here
   
   // And exit this function before checking if the Tx has completed
}

Where your function can exit before HAL_UART_TxCpltCallback() is called (i.e. exits while data is still being sent), your buffer[] contents will disappear because it is allocated on the stack. And the fact that adding a 200ms delay after HAL_UART_Transmit_IT() makes this work further implies that something like this is the case.

Can you show us more of the code around your HAL_UART_Transmit_IT() call? Is it in a function other than main()? What happens after the transmit call before the function returns?