Skip to main content
Kumar B
Associate III
March 13, 2020
Question

STM32 UART Call back function

  • March 13, 2020
  • 2 replies
  • 6440 views

Hi,

I am using STM32F4 based microcontroller and configured it using STM32Cube IDE and free rtos.

I have my uart driver interrupt for reception with call back function.

HAL_UART_Receive_IT(&huart2, (uint8_t *)&Uart.RxByte, 1);

and call back function HAL_UART_RxCpltCallback

Its working fine.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Receive_IT();

}

I want to transmit the received data back using transmission interrupt using HAL_UART_TxCpltCallback function.

I have transmitted this string using a serial console . The string started with $ and ended with \r\n (0x0D,0X0A).

$GPGGA,181908.00,3404.7041778,N,07044.3966270,

W,4,13,1.00,495.144,M,29.200,M,0.10,0000*40\r\n

But, the problem I faced is, when I transmit this string it is executing in infinite loop.

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Transmit_IT();

}

int main(void)

{

//Inialization of clocks, USART, and RTOS threads.

void Default task(void)

{

for(;;)

{

}

}

void SerialTask(void)

{

for(;;)

{

if(Frameisready)

{

//sending one byte of data for triggering uart call back.

HAL_UART_Transmit_IT ();

}

}

}

void Transmit_Task(void)

{

for(;;)

{

HAL_UART_TxCpltCallback(&huart2);

}

}

After I send that $GPGGA,181908.00,3404.7041778,N,07044.3966270,

W,4,13,1.00,495.144,M,29.200,M,0.10,0000*40\r\n

I am getting infinite sentences instead of single one. How to break the HAL_UART_TxCpltCallback(&huart2); function. Kindly help me,

This topic has been closed for replies.

2 replies

Bob S
Super User
March 13, 2020

You don't call HAL_UART_TxCpltCallback() from YOUR code, it gets called only from the HAL UART driver (stm32f4xx_hal_uart.c), and only when the driver thinks it is done sending the last buffer that you gave it in a HAL_UART_Transmit_IT() call. Not enough detail to tell anything else.

Tesla DeLorean
Guru
March 13, 2020

Seem to have scrubbed a lot of useful information from your cut-n-paste

As Bob indicates the HAL system needs to call the callback, and does so as a response to you calling into the HAL via your IRQHandler. You should be careful in callbacks as they occur under interrupt context, and might be blocking other things.

You should have only one HAL_UART_Transmit_IT (); pending at a time

You might also want to consider better solutions, where you handle buffering in the IRQHandler rather than involving the HAL, this would be especially true if trying to handle concurrent full-duplex communications.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Kumar B
Kumar BAuthor
Associate III
March 14, 2020

Can you provide me any better example that works with HAL based UART interrupt both transmission and reception without call back.

Regards,

Kumar