cancel
Showing results for 
Search instead for 
Did you mean: 

USART baud rate on STM32F4

vijayandra
Associate II
Posted on January 26, 2012 at 05:07

Got freertos running on Discovery board STM32F4

Following is function which initializes USART, but i see only garbage coming out hyper terminal,

void xSerialPortInitMinimal(unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )

{

    USART_InitTypeDef USART_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    /* Create the queues used to hold Rx/Tx characters. */

    xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );

    xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );

    /* If the queue/semaphore was created correctly then setup the serial port

    hardware. */

    if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )

    {

        /* Enable USART2 clock */

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

        /* Connect PD5 and PD6 pins to AF */

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);

        GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);

        /* Configure LED USART2 Tx (PD5) as input floating */

        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5;

        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

        GPIO_Init( GPIOD, &GPIO_InitStructure );

        /* Configure USART2 Rx (PD6) as input floating */

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

        GPIO_Init( GPIOD, &GPIO_InitStructure );

        USART_InitStructure.USART_BaudRate = 230400;

        USART_InitStructure.USART_WordLength = USART_WordLength_8b;

        USART_InitStructure.USART_StopBits = USART_StopBits_1;

        USART_InitStructure.USART_Parity = USART_Parity_No ;

        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

        USART_Init( USART2, &USART_InitStructure );

        USART_ITConfig( USART2, USART_IT_RXNE, ENABLE);

        USART_ITConfig( USART2, USART_IT_TXE,  ENABLE);

        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configMAX_CO_ROUTINE_PRIORITIES;

        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

        NVIC_Init( &NVIC_InitStructure );

        USART_Cmd( USART2, ENABLE );

    }

}
8 REPLIES 8
Posted on January 26, 2012 at 17:39

The initialization looks reasonable.

Try a lower baud rate.

Try measuring the baud rate on a scope to confirm the correct/expected bit time.

Try a different serial port or usb dongle.

Try using 2 stop bits.

Try sending a known character, in place of whatever is in your output buffers.

Try a simple stand-alone application sending data to the USART.

Don't enable, or keep enabled, the TXE interrupt if you have no data to send.

Don't see the transmit code.

HyperTerminal sucks, try something like TeraTerm or RealTerm.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
vijayandra
Associate II
Posted on January 26, 2012 at 19:24

Tried and it works great now, Thanks for help. It was not aware of Crystal speed (8MHZ).

elharrar
Associate II
Posted on February 06, 2012 at 08:34

Can you detail your fix please ?

I have the same problem with freeRtos .

Thanks

Posted on February 06, 2012 at 17:05

Baud rate issues are almost always due to a disparity between the physical hardware, and the software settings.

Make sure your IDE knows what CPU is being used, and what frequency it is being clocked with. Check the HSE_VALUE definition.

If the chip is running at 16 MHz, but the software defines it as 8 MHz, the actual baud rate will be double, assuming that the PLL settings don't exceed the operating limits.

Want to confirm the baud rate, then stick a scope on the transmit pin and measure the bit timings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
vijayandra
Associate II
Posted on February 06, 2012 at 19:10

Use Discovery board setting for PLL, as some one right issue with cystal.

lacava
Associate II
Posted on April 23, 2012 at 16:10

Hi, I have a problem to set usart baud rate  to 921.6 k value.

I work on Discovery board STM32F4.

does anybody have any ideas about it??any help is very appreciate!! :)

Posted on April 23, 2012 at 17:33

Hi, I have a problem to set usart baud rate  to 921.6 k value.

 

I work on Discovery board STM32F4. does anybody have any ideas about it??any help is very appreciate!! :)

 

Can you be more specific about what kind of problem this is?

Can you not set it? Are the bit timings wrong?

Have you tried 8 vs 16 oversampling?

What frequency are you running the bus at?

A lot of RS232 stuff used to filter a ~1Mbit

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lacava
Associate II
Posted on April 30, 2012 at 10:26

Thanks, using oversampling I have resolved the problem.