STM32 gotchas
87.UART Tx is not driven immediately after enabling UART

[EDIT February 2023]
While investigating an issue with Cube generating a spurious pulse on UART_Tx pin during initialization with Tx inversion enabled, I realized that there's a difference between the older and newer version of UART with regard of the issue described below. The newer UART - which appeared in the 'F0/'F3 (and can be easily distinguished from the older as having separate data registers - UART_TDR for Tx and UART_RDR for Rx):

The following waveform is taken from an STM32F030 in the same setup as the 'F411 in the original text (with resistive divider on the Tx pin pulling it to "mid-level", when threestated), with the same code as in the first experiment below. Compare it to the waveform for 'F411 to see that the initial one-bit-delay is missing (repaired?). The initial manually generated "marker" pulse and the subsequent threestate period until TE gets set i.e. Tx starts to drive, are longer than in case of 'F411, as the 'F030 run at half the clock frequency and has less efficient instruction set.

In the new UART version there is no delay.

Following is the original text of this gotcha, which was written based on observation of behaviour of the older version of UART.


This minor issue has been spotted and reported by user with nick il-2 on a russian-language forum.

In STM32, if a pin is set to Alternate Function in GPIO_MODER and assigned to given peripheral in GPIO_AFR[], that pin's output circuitry is controlled from the given peripheral. For example, if pin is assigned to a timer, if given timer channel is set to Input Capture, the pin's output circuitry is disabled (i.e. it functions as an input), if it is set to Output Compare and enabled, it's output circuitry is enabled and pulls up or down depending on the state of given channel (or pulls only down if set to Open Drain in GPIO_OTYPER).

Most of the peripherals have one or more "enable" bits, which directly influence the respective pin's output state. In UART, transmission - thus Tx pin's output state - is driven by UART_CR1.TE, but UART_CR1.UE has to be enabled, too (UE presumably controls the overall clocking of the UART). So, one would expect, that as soon as UART_CR1.TE is set, the given UARTx_TX pin will be driven high (to UART idle state).

But this is not how UART behaves. The following code is aimed at testing that ('F411 is used, PA9 as USART1_TX loaded by a voltage divider so that the Hi-Z state shows as cca 2V; USART1's clock in RCC and PA9's GPIO_AFR[] have been already set):

    GPIOA->MODER = (GPIOA->MODER & ~(3 << (2 * 9))) | (1 << (2 * 9));  // generate a marker by setting PA9 to Output
    GPIOA->ODR |= (1 << 9);                                            // pulse PA9
    GPIOA->ODR &= ~(1 << 9);
    GPIOA->MODER = (GPIOA->MODER & ~(3 << (2 * 9))) | (2 << (2 * 9));  // set PA9 as AF, i.e. place it under control of USART1

    USART1->BRR = 0x20;  // set baudrate

    USART1->CR1 = 0      // enable Tx and enable USART
      | USART_CR1_TE
      | USART_CR1_UE
    ;
    (void)USART1->SR;    // clear TC flag
    USART1->DR=0x55;     // transmit S10101010P -- in reality this gets transmitted after the documented 1-byte delay
and this is how the Tx pin waveform looks like:

USART does not drive Tx pin immediately after TE is set.

As the waveform shows, after TE is enabled, there is one bit long delay, during which Tx pin is in Hi-Z state, before it pulls high. (There is also one frame length Idle transmitted before the first frame - this feature is documented in RM.)

As the discussion in the source forum points out, the output pin is not driven directly by the TE bit, rather, it's controlled from the baudrate generator.

There's also a similar delay after TE is cleared, i.e. Tx pin does not go into Hi-Z immediately. However, this delay appears to be somewhat shorter, maybe half-bit long. For testing that, the above code has been amended by: resulting in the following waveform (the delay in question exhibits itself as a prolonged stopbit, before the pin goes threestate):

    while ((USART1->SR & USART_SR_TXE) == 0);
    while ((USART1->SR & USART_SR_TC) == 0);
    USART1->CR1 = USART_CR1_UE;    // clear UE, Tx pin should go to threestate but does so only after delay added to stopbit

    USART1->CR1 = 0                // enable transmitter again, which results in the 1-bit long Hi-Z delay
      | USART_CR1_TE
      | USART_CR1_UE
    ;
    (void)USART1->SR;  // clear TC
    USART1->DR=0x55;   // again this gets transmitted after the documented 1-byte delay
    while ((USART1->SR & USART_SR_TXE) == 0);
    while ((USART1->SR & USART_SR_TC) == 0);
    USART1->CR1 = USART_CR1_UE;  // again Tx pin goes to Hi-Z after delay added to stopbit

There's delay also after TE is cleared.

As an additional gotcha, the baudrate generator has quirks, if UART_BRR is set to some extreme values:

These limitations are unlikely to be important in the vast majority of usual applications, as system clocks are several tens of MHz and usual baudrates are between 9600 and 115200, resulting in moderate dividers usually fitting into the available range. However, there may be niche applications where behaviour other than "expected" may cause unpleasant surprises. For example, attempting to run UART at 4800/9600 bauds while clocking it from 32.768kHz LSE will fail because of too low baudrate divider (LPUART may run under these conditions, though).

Lessons to be taken from these observations:


1. While manipulating GPIO_MODER runtime and in interrupt/multitasking context, one has to observe atomicity if several such operations are present throughout the program. While this may seem "self-evident", manipulating this sort of registers runtime is unusual and this requirement may be easy to be overlooked resulting usually in rarely occuring and hard to debug issues.