Introduction
In serial communication, we often use receive and transmit interrupts, which everyone is familiar with. There is another very useful interrupt that may be overlooked, namely the bus idle state IDLE interrupt. When a frame of data transmission ends, the bus remains high idle, which triggers the MCU’s IDLE interrupt. In this article, we will introduce a method to use this interrupt for receiving variable-length serial data. By using this interrupt, we can eliminate the need for operations to detect whether data transmission is complete.
Experimental Environment
· STM32F411RE-NUCLEO
· STM32CubeMX
Bus State Analysis
The following figure shows the waveform captured when sending 0xAA 0x55. From the figure, we can see that before and after sending this frame, the bus clock is in the IDLE state. In this frame, there is no IDLE state between bytes, so there will not be an IDLE false trigger.

Receiving Variable Length Data
The project created this time is based on the HAL library. In the native HAL library, the IDLE interrupt processing is not integrated. Therefore, in the method we introduce in this article, some library files need to be modified to achieve this.
Using STM32CubeMX to Generate Experimental Project
The project configuration is as follows:
1. System always configured to 100MHz
2. Configure USART2 as Asynchronous, pin configuration as PA2, PA3.
3. USART2 parameters: 9600Bits/s, 8bits, None, 1Stop



To facilitate printing the received information, the generated project needs to be modified to map the print function.
main.c – Declaration

main.c – Code

Modifying Project Code
Increase Receive Buffer
main.c

stm32f4xx_hal_uart.c

Enable IDLE Interrupt in Receive Function
stm32f4xx_hal_uart.c -> HAL_UART_Receive_DMA() function

Handling IDLE Interrupt
stm32f4xx_hal_uart.c -> HAL_UART_IRQHandler() function

Receive Completion Handling (IDLE Triggered, One Frame Data Transmission Completed)
stm32f4xx_hal_uart.c -> HAL_UART_AbortReceive_IT() function

main.c

Enable Reception
main.c

Experimental Results
Using serial debugging, data is sent through the STLINK virtual serial port, and the MCU will return the number of bytes received and print out the received data. The following figure shows the experimental results of sending 0xAA 0x55.

Conclusion
Using the serial bus idle state interrupt effectively is very convenient for receiving data of uncertain volume, while also optimizing code design.

