0
\$\begingroup\$

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). First screen

Second screen

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Vadimchik Dec 1 '17 at 15:46
  • \$\begingroup\$ 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? \$\endgroup\$ – Majestry Dec 1 '17 at 15:56
1
\$\begingroup\$

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.

The UE-bit description reads:

When this bit is cleared the USART prescalers and outputs are stopped and the end of the current byte transfer in order to reduce power consumption. This bit is set and cleared by software.

0: USART prescaler and outputs disabled

1: USART enabled

So while it might sound a bit like a complete USART enable bit, it just works on the prescaler and outputs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.