cancel
Showing results for 
Search instead for 
Did you mean: 

How to Create a UART Driver Using Idle Line Detection

Vergnani_ElEn
Associate II

I need to create a UART driver that works reliably in all conditions.
The driver must use DMA, and since the incoming messages are NOT fixed-length, I need to use idle line detection.
My question is:

  • how can I use a callback to store the received message when an idle line is detected and then restart the reception using the STM32 HAL idle-line API?

1 REPLY 1
Guenael Cadier
ST Employee

Hi @Vergnani_ElEn 

HAL UART now provides API in order to manage "continuous" reception or reception of unknown length (which is usually the reason for using HAL_UART_Receive_IT() with expected rx length value = 1). It is based on reception going till either expected length is received OR IDLE event occurs. It is available either in Interrupt or DMA modes.

For example, a use case could be based on HAL_UARTEx_ReceiveToIdle_DMA() API with DMA configured in Circular mode, that allows to have continuous reception and user callback executed as soon as either buffers are filled or Idle event occurs (pause in the transmission, i.e. enough time elapses after last received char) to retrieve data.

This callback will be executed when any of following events occurs :

- HT (Half Transfer) : Half of Rx buffer is filled)

- TC (Transfer Complete) : Rx buffer is full.

(In case of Circular DMA, reception could go on, and next reception data will be stored in index 0 of reception buffer by DMA).

- Idle Event on Rx line : Triggered when RX line has been in idle state (normally high state) for 1 frame time, after last received byte.

STM32Cube FW packages contain a UART example highlighting such use case (example name UART_ReceptionToIdle_CircularDMA), with an example of callback implementation for retrieving all received data.

This example illustrates use of this API with a Circular DMA implementation 

Please have a look to this example to confirm it could address your needs.
Regards