Search code examples
cstm32uart

STM32 IRQ Firing Too Early


I am using an STM32L010 micro to receive some data via the UART. Specifically, I am looking to receive six bytes and can block until I get it. For this reason, I am using the HAL_UART_Receive_IT function that is part of the STM32 HAL Driver library:

while(1)
{
    if ( Data_Ready == FALSE )
    {
        HAL_UART_Receive_IT(&huart2, RX_Data, 6);
    }
    else if ( Data_Ready == TRUE )
    {
    //Do some data processing
        Data_Ready = FALSE;       //Clear flag
    }
}

I have a flag in the UART IRQ handler that should get set when all six bytes are transferred.

void USART2_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart2);
  Data_Ready = TRUE;
}

However, the IRQ is firing when I have only one byte in RX_Data. I have verified with a scope that the data is NOT just one byte and then all zeroes.

Am I misunderstanding how this handler works?


Solution

  • The interrupts are purely hardware, they have nothing to do with the HAL. Firing every byte is normal if the hardware is configured that way. What you want is the HAL callback, namely HAL_UART_RxCpltCallback. This one will be called once the requested transfer is completed. The HAL takes care of processing the interrupts, possibly every byte, and abstracts them into your HAL requests.