cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeH5 (v1.5.0) HAL_UART_Receive_IT Only Triggers Once (RxISR Pointer Cleared)

Microsparky
Visitor

Problem Description

I am having some trouble using HAL_UART_Receive_IT() to receive UART data in interrupt mode, the callback HAL_UART_RxCpltCallback() only triggers once for the first byte, subsequent bytes do not trigger the interrupt. I don't know if there is some problem with my implementation or if the problem is in STM32CubeH5 (v1.5.0). I would really appreciate some help!

Symptoms

  • HAL_UART_RxCpltCallback() executes correctly for the first received byte.

  • Calling HAL_UART_Receive_IT() again does not fix the issue.

  • No further interrupts are generated, even though UART data is arriving.

Root Cause?

I did some digging and found the UART interrupt handler (UART_RxISR_8BIT) clears the RxISR function pointer (huart->RxISR = NULL) after processing the first byte. This prevents the interrupt from being handled correctly again, even if HAL_UART_Receive_IT() is called again.

Workaround: Manually Restore huart->RxISR

To workaround this, I added one line to manually reassign huart->RxISR in the HAL_UART_RxCpltCallback(), when I do this I get the expected behavior:

uint8_t rx_byte;  // Single-byte buffer for interrupt mode

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
    if (huart == &huart2) {
        // Process the received byte (e.g., store or print)
        printf("Received: %c\n", rx_byte);

        // FIX: Restore the RxISR function pointer
        huart->RxISR = UART_RxISR_8BIT;  // Use UART_RxISR_16BIT for 16-bit mode

        // Restart interrupt-based receive
        HAL_UART_Receive_IT(huart, &rx_byte, 1);
    }
}

int main() {
    HAL_Init();
    SystemClock_Config();
    MX_USART2_UART_Init();

    // Start the first interrupt-based receive
    HAL_UART_Receive_IT(&huart2, &rx_byte, 1);

    while (1) {
        // Main loop does other tasks
    }
}

 

1 REPLY 1
TDK
Super User

This can't be right. The RxISR gets set when you call HAL_UART_Receive_IT.

stm32h5xx-hal-driver/Src/stm32h5xx_hal_uart.c at e27e6065b14d934aa06a1168188c38cd12a4d9f0 · STMicroelectronics/stm32h5xx-hal-driver

 

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