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

Report here problems in any of ChibiOS components. This forum is NOT for support.
mringwal
Posts: 6
Joined: Sun Jan 01, 2023 10:52 pm
Has thanked: 1 time
Been thanked: 1 time

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

Postby mringwal » Mon Jan 02, 2023 10:53 pm

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.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

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

Postby Giovanni » Tue Jan 03, 2023 4:14 pm

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

mringwal
Posts: 6
Joined: Sun Jan 01, 2023 10:52 pm
Has thanked: 1 time
Been thanked: 1 time

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

Postby mringwal » Tue Jan 03, 2023 4:23 pm

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

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

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

Postby Giovanni » Tue Jan 03, 2023 4:24 pm

Propose a patch :-)

Giovanni

mringwal
Posts: 6
Joined: Sun Jan 01, 2023 10:52 pm
Has thanked: 1 time
Been thanked: 1 time

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

Postby mringwal » Tue Jan 03, 2023 4:41 pm

Ok. Will do.

mringwal
Posts: 6
Joined: Sun Jan 01, 2023 10:52 pm
Has thanked: 1 time
Been thanked: 1 time

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

Postby mringwal » Tue Jan 03, 2023 5:27 pm

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.
Attachments
stm32-uart1-avoid-loosing-bytes.patch.zip
(554 Bytes) Downloaded 135 times

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

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

Postby Giovanni » Mon Jan 30, 2023 12:58 pm

Moved in "bug reports".

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

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

Postby Giovanni » Sat Mar 04, 2023 2:02 pm

Hi,

Committed the fix in trunk and 21.11.x.

Giovanni


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 23 guests