cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in UART INTERRUPT call back function

Ravi_shankar
Associate

Hey guys!

I am currently working on stm32F2 series, the issue i was facing in   HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) when the UART transmit the first byte the interrupt call back function is not called instead calling the RxCpltCallback it call the " USART1_IRQHandler(void) "and the  RxCpltCallback function does not trigger at first byte in UART at the second byte it call the  RxCpltCallback  function and it works fine and i attached the code i was wrote 

#include "main.h"

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;

unsigned char ra_buffer[32];

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);

int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();

HAL_UART_Receive_IT(&huart1,ra_buffer,12);

while (1)
{

HAL_Delay(1000);

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

HAL_UART_Receive_IT(&huart1,ra_buffer,1);
HAL_UART_Transmit(&huart3,ra_buffer, 1,100);

}

4 REPLIES 4
KnarfB
Super User

> HAL_UART_Receive_IT(&huart1,ra_buffer,12);

will call HAL_UART_RxCpltCallback only after 12 bytes received.

USART1_IRQHandler is called internally after each byte until the limit you have set (here: 12) ist reached.

 

Aime
ST Employee

Hi @Ravi_shankar ,

Your issue is that the first byte received on UART1 does not trigger the HAL_UART_RxCpltCallback immediately, but the second byte does. This behavior usually happens because of how the UART receive interrupt and buffer size are configured and how the callback is re-armed.

Your initial call is:

This means the HAL driver waits to receive 12 bytes before triggering the HAL_UART_RxCpltCallback. So the callback will not be called after the first byte, but only after 12 bytes are received.
 

In the Callback:


Here you re-arm the receive interrupt to receive only 1 byte at a time. This is why after the first 12 bytes, the callback is triggered, and then subsequent single bytes trigger the callback immediately.

 

If you want to receive one byte at a time and have the callback called immediately after each byte, you should:

  • Start the receive interrupt with length = 1 from the beginning.
  • In the callback, re-arm the receive interrupt again for 1 byte.

 

Best Regards,
Aime
 

Saket_Om
ST Employee

Hello @Ravi_shankar 

Please any update on this thread?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
EXUE.2
ST Employee

it is good explanation of other replies for how the 'HAL_UART_RxCpltCallback' work, but generally, we don't put any functions in HAL_UART_RxCpltCallback() but just trigger a flag to indicate one RX completely, because IT handler must be process short code and return quickly to avoid conflict with other IT coming.  just like 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{

   UART_RX_ready = 1;

}