# STM32F103 TC\TXE bits are set automatically after USART1 clock enable

I'm trying to enable the USART1 periferial on Stm32F103 MCU. My code looks like this:

#include "stm32f10x.h"

void UARTInit(void);
void GPIOInit(void);
USART_InitTypeDef USART_1;

int main()
{
SystemInit();
GPIOInit();
UARTInit();
USART_SendData(USART1, 0x12);
while (1)
{

}
}

void UARTInit()
{
RCC ->APB2ENR |= RCC_APB2ENR_USART1EN;
USART_StructInit(&USART_1);
USART_Init(USART1, &USART_1);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
USART_Cmd(USART1, ENABLE);
}

void GPIOInit()
{
RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN |RCC_APB2ENR_AFIOEN;
GPIOA-> CRH &= ~GPIO_CRH_CNF9;
GPIOA -> CRH |= GPIO_CRH_CNF9_1;
GPIOA -> CRH |= GPIO_CRH_MODE9_1;

GPIOA->CRH  &= ~GPIO_CRH_CNF10;
GPIOA->CRH  |= GPIO_CRH_CNF10_0;
GPIOA->CRH  &= ~GPIO_CRH_MODE10_1;
}


As you can see, i firstly enable clock for USART1 and after that initialize it. Before executing this: RCC ->APB2ENR |= RCC_APB2ENR_USART1EN all bits in USART registers are set to 0 (see first attached image). But when clock is enabled, TC and TXE bits are set to 1, even when USART is not enabled yet. (see second screen).

So, if you have any ideas why ths happens - i'll be very appreciate for yours help. Thanks.

• I don't see anything strange, due to reference manual page 822 those bits are set by the hardware and they are likely to indicate current peripheral state - USART is ready to transmit data. Please, clarify what are you asking and what is your specific problem. – Vadimchik Dec 1 '17 at 15:46
• I read that manual. But, please, notice that USART is not enabled, not configured - so, how this bits can be set? Also, no transmit was done at all. So, my question sounds like: what is the reason, that this bits are set to 1 after clock enable for USART? – Majestry Dec 1 '17 at 15:56

What you see is the normal behavior of the USART peripheral of the STM32.

Those bits are set independently of the UE bit in the CR1 register. They just indicate, that the transmit buffer is empty (TXE) and the transfer shift register is empty (TC).

Which is true (because you just enabled the clock, nothing can be in there) and usually not a problem - just don't enable the TXE or TC interrupt before the UE bit is set, otherwise your interrupt will probably not do what you'd like it to do.