STM32 gotchas
163.UART Rx corruption if glich occurs during 2nd half of stopbit

Byte received by UART may be received as corrupted, i.e. having a different value than received, if a glitch (a short 0 pulse) occurs during second half of stopbit. There's no indication that this occured in the UART's flags, RXNE is set as normally.

This issue pertains probably to the "new" UART used in all STM32 since 'F0/'F3. It is documented in an erratum e.g. for 'G0B1, but was observed e.g. also on 'L431 which errata don't contain this erratum. It takes time until ST propagates a new erratum to all relevant models' errata.

What could be the possible mechanism of this problem? UARTs should stop the Rx process by sampling the stopbit at its middle, as due to possible timing mismatch between transmitter and receiver, this may be in reality the end of stopbit and it's just time to start to wait for the next frame's startbit downgoing (leading) edge. The just received frame should be transferred to the Rx buffer at this point, and the RXNE flag should be risen.

Here, transfer of the received frame appears to be delayed for some reason. The glitch (a short 0 pulse) during startbit detection should be ignored; however, it appears that it also through some error in design impacts the frame which is still in the receiver's shift register (or its equivalent), and then the corrupted frame is transferred to the Rx buffer.

The following waveform is from a real-world occurence of the described problem:

UART waveform with glitch

The logic analyzer was set not to suppress glitches, so - unlike normal UARTs - it considered the glitch to be the startbit of the next byte. While the logic analyzer correctly decoded the byte just before the glitch as 0xAC, the UART in STM32L431 received it incorrectly as 0xD6.

UART transmits LSB-first, so in a shift-register implementation, it shifts "to the right" and inserts new bits as MSB. As 0xD6 = (0xAC >> 1) | 0x80, the glitch caused the UART to errorneously insert 1 as an extra bit, before transferring the result to UART_RDR.

How can such glitch happen? UART transmitters which can control a transceiver (e.g. RS485) often switch the transceiver's direction at the middle of last transmitted byte (again, to accomodate for possible timing mismatch between parties on a shared bus and prevent collision). Some less-than-perfect combination of transceivers and their loads (e.g. cables) may result in such short unwanted glitch to be generated. UARTs are normally designed to ignore short glitches, so - unless UART is flawed as is in this case - such conditions usually go unnoticed.