2026-05-18 10:59 AM - edited 2026-05-18 11:30 AM
Hello everyone,
I have encountered a strange issue with my UART transmission function on an STM32 microcontroller operating in Half-Duplex Open-Drain mode.
Currently, the function works perfectly fine without checking the TC flag. However, when I add a check to wait for the transmission completion at the end of the function, my logic analyzer shows completely garbled bytes (corrupted data).
Here is the code that works perfectly (without the TC check):
void send_array(uint8_t *arr, uint8_t size)
{
/* Receiver disable */
USART3->CR1 &= ~(CR1_RE);
for (int i = 0; i < size; i++)
{
/* Wait until Transmit data register is empty */
while(!(USART3->SR & SR_TXE)){}
/* Send byte */
USART3->DR = arr[i];
}
/* Receiver enable */
USART3->CR1 |= CR1_RE;
}
And here is the modification that breaks the transmission and leads to garbled bytes on the analyzer:
void send_array(uint8_t *arr, uint8_t size)
{
/* Receiver disable */
USART3->CR1 &= ~(CR1_RE);
for (int i = 0; i < size; i++)
{
/* Wait until Transmit data register is empty */
while(!(USART3->SR & SR_TXE)){}
/* Send byte */
USART3->DR = arr[i];
}
/* Waiting for TC here causes garbled data */
while(!(USART3->SR & SR_TC)){}
/* Receiver enable */
USART3->CR1 |= CR1_RE;
}
Can anyone explain the underlying hardware reason for this behavior? Why does waiting for the TC flag disrupt framing or synchronization in Half-Duplex mode?
Thank you in advance!