For a home project I was playing around and reinventing the wheel STM32 drivers. It turned out that my driver sometimes loses bytes received via UART. I spent time investigating the problem and found the error. Turns out the same error is in almost every STM32 driver I’ve seen. I filled in a few PRs and an Issue. If you are using UART in STM32 see if you need my fixes.
The point of the error is that UART data is sent usually via a write to the DR register part, which in STM32F4xx is shared for reads and writes.
USART1_Periph.DR.DR := Data;
The compiler expands an assignment to part of the DR register to a read-modify-write sequence. However, reading DR has the side-effect of clearing the SR.RXNE (“Read data register not empty”) bit, so sending a byte could result in the loss of input bytes.