2025-06-08 4:58 AM
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!
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.
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.
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 } }
2025-06-08 6:11 AM
This can't be right. The RxISR gets set when you call HAL_UART_Receive_IT.