STM32 gotchas
164.Strange characters received in terminal in PC? Parity or baudrate mismatch, or RS232 converter was used for UART.

UART is perhaps the most ubiquitous method of communication between electronic devices, often used also to transmit debug or other information from a microcontroller such as STM32 to PC. Even if it is relatively simple and straighforward to set it up, it is also not uncommon to make a mistake, which usually results in receiving unintelligible characters.

The most usual problem is baudrate mismatch between transmitter and receiver. This happens usually at the mcu side, and may be consequence of incorrectly set clock train, or misunderstanding of what the primary clock source is. It is relatively easy to check the baudrate by transmitting 0x55 ('U') repeatedly, as that results in a continuous train of 0s and 1s of a single bit duration, which then can be checked using oscilloscope or a logic analyzer. The receiver still may receive some characters, but they are "strange", and often there are also errors (framing/noise) in reception (but they may go unnoticed, as e.g. many terminal programs don't display the UART flags).

Similar problem may be caused by mismatched parity and/or bit-per-frame settings, although in this case some characters may come through correctly. In case of STM32 this problem may be caused by the fact, that unlike any other UART, the STM32 UART counts parity bits into the total number of bits.

Sometimes confusing characters are received even if baudrate, parity, bit-number settings match the transmitter, but a RS232 converter is used. While RS232 indeed was the dominant physical protocol used for UART communication two-three decades ago, currently it is rarely used, and to connect mcu to PC, a "straight" USB-to-UART converter has to be used, rather than a USB-to-RS232 (the latter would be used ONLY if there is an UART-to-RS232 converter already connected to the mcu, the two RS232 converters would then "cancel out" each other).

The reason why such connection "sort-of-works" and characters - even if "strange" - are received, is, that RS232 levels are effectively inverted to UART levels: logic 0 is signalled by a high voltage (+3V..+15V) and logic 1 is signalled by a low voltage (-3V..-15V)1. Even if the mcu's Tx pin cannot produce the negative voltages, RS232 receivers are often built so that their decision level is above 0V (around +1V).

And while it may seem that connection in the other direction (from RS232 to mcu) inevitably results in mcu damage due to high voltage levels, and it indeed does result in damage sometimes; but surprisingly quite often the mcu survives such connection thanks to relatively high output impedance of RS232 transmitters together with the protection structures built into the mcu IO pins.

1.In the RS232 realm, the official nomenclature for logic 0 is "space" and for logic 1 is "mark", but that is rather confusing ("mark" being the idle state) and rarely used.