2
\$\begingroup\$

I'm trying to use USART in my program on STM32F205VC, but instead of sending my data I get some weird chars in PuTTY. I've used a Saleae Logical Analyzer and the result looks like this:

This is my full app code:

#include <stm32f2xx_gpio.h>
#include <stm32f2xx_rcc.h>
#include <stm32f2xx_usart.h>

#define USART_PORT GPIOB

#define USART_TX_PIN GPIO_Pin_6
#define USART_TX_PIN_SOURCE GPIO_PinSource6

#define USART_RX_PIN GPIO_Pin_7
#define USART_RX_PIN_SOURCE GPIO_PinSource7

void SetupLED(GPIO_TypeDef* port, uint32_t pin)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = pin;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

    GPIO_Init(port, &GPIO_InitStructure);
}

void SetupUSART()
{
    USART_InitTypeDef usartConfig;

    usartConfig.USART_BaudRate = 9600;
    usartConfig.USART_WordLength = USART_WordLength_8b;
    usartConfig.USART_StopBits = USART_StopBits_1;
    usartConfig.USART_Parity = USART_Parity_No;
    usartConfig.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    usartConfig.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Cmd(USART1, ENABLE);
    USART_Init(USART1, &usartConfig);

    GPIO_InitTypeDef gpioConfig;

    gpioConfig.GPIO_Mode = GPIO_Mode_AF;
    gpioConfig.GPIO_Pin = USART_TX_PIN;
    gpioConfig.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(USART_PORT, &gpioConfig);
    GPIO_PinAFConfig(USART_PORT, USART_TX_PIN_SOURCE, GPIO_AF_USART1);

    gpioConfig.GPIO_Mode = GPIO_Mode_AF;
    gpioConfig.GPIO_Pin = USART_RX_PIN;
    GPIO_Init(USART_PORT, &gpioConfig);
    GPIO_PinAFConfig(USART_PORT, USART_RX_PIN_SOURCE, GPIO_AF_USART1);
}

void USART_SendByteSync(USART_TypeDef *USARTx, char byte)
{
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET) { } 
    USART_SendData(USARTx, byte);
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET) { }
}

void StartClocks()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}

int main()
{
    StartClocks();
    SetupUSART();

    SetupLED(GPIOA, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
    GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_3);

    while (true) 
    {
        USART_SendByteSync(USART1, 'e');
        for (int i = 0; i < 1000000; i++)
            asm("nop");
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Format the code properly! \$\endgroup\$ Commented Mar 22, 2015 at 16:00
  • 1
    \$\begingroup\$ Is your external clock frequency properly defined in your project? \$\endgroup\$
    – Milan Tenk
    Commented Mar 22, 2015 at 17:06
  • \$\begingroup\$ How to do this in VisualGDB? \$\endgroup\$
    – user61143
    Commented Mar 22, 2015 at 17:24
  • \$\begingroup\$ There should be a define in your peripheral library for it named as "#define HSE_VALUE". Is the value at this define the value of your external clock frequency? \$\endgroup\$
    – Milan Tenk
    Commented Mar 22, 2015 at 17:40
  • 3
    \$\begingroup\$ It is sending the right bits, but only at about 450 baud. @ramez suggestion about the clock is probably right. \$\endgroup\$ Commented Mar 22, 2015 at 23:32

1 Answer 1

2
\$\begingroup\$

So I summarize it in order not to leave this question unanswered. In most IDE-s if you begin a new project there will be asked somewhere, what your external clock frequency is. This will set your HSE_VALUE macro, witch will be used in setting up the clocks. If the external clock frequency is not correctly set, than you won't get the desired timing for example at your USART. Like @geometrical said, your bits are right, but timing is not correct, so you probably did not set your HSE_VALUE correctly.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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