Skip to main content
Visitor II
February 16, 2023
Solved

Bug when starting a UART port with UART inversion enabled on STM32303 and STM32F722

  • February 16, 2023
  • 9 replies
  • 4867 views

Hello ST

I have a project where I need to start and stop the UART during runtime, and I have noticed a possible bug in the stm32f3xx_hal_uart.c file.

The issue is that when initializing the UART to use the UART_ADVFEATURE_TXINVERT_INIT parameter, the Tx pin goes high for a very short time (~10us). I suspected that it was because the inversion of the pin took place after the UART was started, and i did some digging in the HAL code.

0693W00000YAugEQAT.png 

By setting the breakpoints as above, i concluded that the signal that was on the Tx pin was high after UART_SetConfig was called, and it went low again after UART_AdvFeatureConfig was called. This seems to me that the pin goes high (UART Idle), before the inversion takes place (UART idle low after inversion).

0693W00000YAudUQAT.jpgThe Tx pin viewed under an oscilloscope where the code is initializing and deinitializing the UART every ~100ms, resulted in a consistent voltage spike.

0693W00000YAubOQAT.pngI tried to confirm my suspicion by swapping the order the two functions were called, so the inversion took place before setting the config, and that seemed to fix the issue.

To check if it was a hardware specific issue, i also checked on an STM32F722, and the behavior was the same.

Uart parameters used:

BAUD: 100000

Databits: 8

Stopbits: 2

Parity: even

Tx inversion: enabled

HAL version on stm32f303: V1.5.6

    This topic has been closed for replies.
    Best answer by miroslav

    OK, problem resolved. Wrong interpretation from my side. RX and TX are active low in non inverted operation.
    I thought non inverted is active high.

    9 replies

    Super User
    February 18, 2023

    Please post content of UART_SetConfig and UART_AdvFeatureConfig functions.

    Nevermind, I now see these are internal Cube functions. If was not clear to me that you're posting content of Cube files, not your code.

    JW

    Super User
    February 18, 2023

    Confirmed experimentally, that after setting USARTx_CR1.TE, the Tx pin starts to be actively driven to level given by USART_CR2.TXINV, regardless that USARTx_CR1.UE is not set. It means, that if the user choses to use TXINV=1, it must be set *before* setting USARTx_CR1.TE.

    Swapping UART_SetConfig() and UART_AdvFeatureConfig() in stm32XXxx_hal_uart.c:HAL_xxxx_Init() functions (as well as in their hal_usart.c counterparts - btw. it's was a rather dumb choice to them separated, both in naming in the DS/RM and files/functions in libraries) as suggested above may have other interactions if some of the other "advanced features" is used, this ought to be meticulously researched. Maybe better, setting TXINV could be separated from UART_AdvFeatureConfig() and moved before UART_SetConfig().

    @Amel NASRI​ , can this please be looked at and fixed, in all STM32 Cubes where this version of USART/UART (i.e. newer than 'F0/F3) is used? Thanks.

    JW

    ST Employee
    February 21, 2023

    Dear @Community member​ , @mlars.1​ ,

    I confirm your analysis, i.e. that current initialization sequence leads to the observed spike. I take the action point to raise the issue in ST to get this sorted out.

    Regards

    Technical Moderator
    February 22, 2023

    For tracking purpose: Internal ticket number: 145958 (This is an internal tracking number and is not accessible or usable by customers).

    Super User
    February 21, 2023

    Thanks, @Guenael Cadier​ .

    JW

    Visitor II
    December 5, 2024

    Hello,

    I'm using stm32l476 and problem is a bit different. Cube package already have update from this discussion and that is fine. What is not fine is that RX stays low forever.

    TX is inverted and that is fine, but RX is actively driven low so device from other side can't pull it up at all. I cut the line on PCB and controlled device pulls it up immediately so it is definitely regarding RX configuration.

    Any thoughts on this?

    Best regards,
    Miroslav

    Graduate II
    December 5, 2024

    Yeah, that seems very odd, double check how you're initializing the pin.

    The peripheral shouldn't be putting the RX in push-pull mode.

    Make sure to clear auto/local structures in subroutines as otherwise they'll contain random stack junk an other functions will misconfigure the hardware.

    Visitor II
    December 5, 2024

    Hello,

    In step by step debug, pin never goes high. It is configured as PP.

    After init, due to this, code stucks in RX interrupt. But now I'm thinking, why is RX push pull when it needs to receive signal? :)

    BR,

    Miroslav 

    Visitor II
    December 5, 2024

    Ah, I answered my questionby myself. Thanx Tesla :)

    Generated code by stm cube is a bit wrong.

    Visitor II
    December 5, 2024

    Ah, no. There is no such thing as  GPIO_MODE_AF_INPUT. Any clue?

    Visitor II
    December 5, 2024

    OK, I was totally wrong. It was not RX but TX. Interrupt routine confused me.
    So, RX is always in interrupt because inversion actually didn't happen at all. Logic one on RX keeps it in receive interrupt. TX is low as no transmit occur. It means that TX and RX were not inverted at all.

    Here is code for STM476:

     

     

    static void MX_USART3_UART_Init(void)

    {

    huart3.Instance = USART3;

    huart3.Init.BaudRate = 115200;

    huart3.Init.WordLength = UART_WORDLENGTH_8B;

    huart3.Init.StopBits = UART_STOPBITS_1;

    huart3.Init.Parity = UART_PARITY_NONE;

    huart3.Init.Mode = UART_MODE_TX_RX;

    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;

    huart3.Init.OverSampling = UART_OVERSAMPLING_16;

    huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

     

    huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT|UART_ADVFEATURE_RXINVERT_INIT;

    huart3.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;

    huart3.AdvancedInit.RxPinLevelInvert = UART_ADVFEATURE_RXINV_ENABLE;

    if (HAL_UART_Init(&huart3) != HAL_OK)

    {

    Error_Handler();

    }

    }

    Any clue on this?

    miroslavAnswer
    Visitor II
    December 5, 2024

    OK, problem resolved. Wrong interpretation from my side. RX and TX are active low in non inverted operation.
    I thought non inverted is active high.