Page 1 of 1

Avoid lost bytes on STM32/USARTv1 with RTS/CTS  Topic is solved

Posted: Mon Jan 02, 2023 10:53 pm
by mringwal
In uart_lld_serve_interrupt() of the STM32/USARTv1 implementation, the following line reads the data register, clearing it by that:

(void)u->DR; /* SR reset step 2.*/

This causes a problem in the following context:
- DMA is IDLE
- An send operation is started
- The UART receives a single byte and RTS goes up
- the send operation completes and uart_lld_serve_interrupt is called
- uart_lld_serve_interrupt reads the DR, clearing it
- a call to uartStartReceive should receive the byte it, but it's already gone

This scenario requires the changes from my previous request as DMA wouldn't be in the idle state when the byte is received. However, I guess it's possible to construct a race condition where the data register is read (as part of TX complete/uart_lld_serve_interrupt) at the very same moment/clock cycle the byte was stored there by the UART peripheral, just before DMA got a chance to read it out - very unlikely, but not impossible.

A simple fix would be to remove that line from the UART driver. I'm not aware of a reason to read the data register in the interrupt handler, after all, it was received and should be provided to the application.

Background: I was running our Bluetooth A2DP Source demo that streams music to a remote speaker and got dropped bytes when the new read request was issued after the first byte of the next packet was received and the TX complete fell between those two events. After commenting this line, the demo worked as expected.

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Tue Jan 03, 2023 4:14 pm
by Giovanni
Hi,

That function is meant to handle errors, it is done in case there is an ORE to be handled, from the STM32 RM:

"The ORE bit is reset by a read to the USART_SR register followed by a USART_DR register read operation."

Giovanni

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Tue Jan 03, 2023 4:23 pm
by mringwal
Giovanni wrote:Hi,

That function is meant to handle errors, it is done in case there is an ORE to be handled, from the STM32 RM:

"The ORE bit is reset by a read to the USART_SR register followed by a USART_DR register read operation."

Giovanni


Hi Giovanni

That's correct. However, by unconditionally reading the DR register the received byte gets discarded and I need to avoid that.

Would you be ok to read the DR only in case of an ORE? i.e. first check the SR to see if there's an ORE, and only then read the DR?

Best
Matthias

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Tue Jan 03, 2023 4:24 pm
by Giovanni
Propose a patch :-)

Giovanni

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Tue Jan 03, 2023 4:41 pm
by mringwal
Ok. Will do.

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Tue Jan 03, 2023 5:27 pm
by mringwal
This patch that moves the 'read DR' into the existing error handling.
By this, the DR is only read in case of an error, which include the ORE. In the case of no error, the DR is not read and no bytes get lost.

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Mon Jan 30, 2023 12:58 pm
by Giovanni
Moved in "bug reports".

Giovanni

Re: Avoid lost bytes on STM32/USARTv1 with RTS/CTS

Posted: Sat Mar 04, 2023 2:02 pm
by Giovanni
Hi,

Committed the fix in trunk and 21.11.x.

Giovanni