Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

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.

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

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

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt
Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt
Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

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

main.c – Declaration

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

main.c – Code

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

Modifying Project Code

Increase Receive Buffer

main.c

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

stm32f4xx_hal_uart.c

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

Enable IDLE Interrupt in Receive Function

stm32f4xx_hal_uart.c -> HAL_UART_Receive_DMA() function

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

Handling IDLE Interrupt

stm32f4xx_hal_uart.c -> HAL_UART_IRQHandler() function

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

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

stm32f4xx_hal_uart.c -> HAL_UART_AbortReceive_IT() function

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

main.c

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

Enable Reception

main.c

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

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.

Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt

Conclusion

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

END
Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt
Engineer Notes | Receiving Variable Length Data Using UART IDLE Interrupt
Click “Read Original” to download relevant technical documents

Leave a Comment