Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Introduction

In serial communication applications, we often use receive and transmit interrupts, which most of you are familiar with. There is a very useful interrupt that may be overlooked, which is the bus IDLE interrupt. When a frame of data transmission ends, the bus will maintain a high level state, at which point the MCU’s IDLE interrupt can be triggered. In this article, we will introduce how to use this interrupt to receive variable length serial data. By using this interrupt, we can eliminate a lot of judgment actions used 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 data line is in an IDLE state. In this frame, there is no IDLE state between bytes, which means there will be no accidental triggering of the IDLE interrupt.

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Receiving Variable Length Data

The project created this time is based on the HAL library. In the native HAL library, IDLE interrupt handling 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 configuration of the project is shown in the following figure:

1. System always configured to 100MHz

2. Configure USART2 as Asynchronous, pin configuration as PA2, PA3.

3. USART2 parameters: 9600Bits/s, 8bits, None, 1Stop

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

To facilitate printing the received information, some modifications need to be made to the generated project to map the print function.

main.c – Declaration

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

main.c – Code

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Modifying Project Code

Adding Receive Buffer

main.c

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

stm32f4xx_hal_uart.c

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Enabling IDLE Interrupt in Receive Function

stm32f4xx_hal_uart.c HAL_UART_Receive_DMA() function

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Handling IDLE Interrupt

stm32f4xx_hal_uart.c HAL_UART_IRQHandler() function

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Receiving Completion Handling (IDLE Triggered, One Frame of Data Transmission Completed)

stm32f4xx_hal_uart.c HAL_UART_AbortReceive_IT() function

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

main.c

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Enabling Reception

main.c

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Experimental Results

Using a serial debugging port, data is sent through the virtual serial port of STLINK, and the MCU will return how many bytes of data were received and print the received data. The following figure shows the experimental results of sending 0xAA 0x55.

Using UART IDLE Interrupt for Receiving Variable Length Serial Data

Conclusion

The IDLE interrupt, which indicates the bus idle state, can significantly reduce the workload of code program design when used appropriately. This is just a simple demonstration to inspire others; in actual applications, I hope everyone can flexibly use it to meet their needs.

Leave a Comment