Skip to main content
logan
Associate II
Aug 16, 2014 2:30 AM UTC
Question

USART2 baud rate coming out 1.5x slower than setting on STM32F4

Posted on August 16, 2014 at 04:30

I'm trying to simply send a character over and over to my computer using a USB to TTL adapter and USART2 on the STM32F4 discovery board. I have USART2 configured for 9600 baud which would give 104us bit pulses.

After getting junk characters on the computer, I looked at the TX signal on my scope and saw the bit widths were about 160us, or a baud rate of ~6250 bps. In my program if I set the baud rate to 9600*1.5 I actually get 9600 baud.

All of my reading seems to suggest this is an issue with a clock setting somewhere. Most commonly I see people having an incorrect value of HSE_VALUE. My HSE_VALUE is indeed set to 8000000 for the 8MHz crystal. I also even added -DHSE_VALUE=8000000 into my CFLAGS. Here are some of my other clock settings from system_stm32f4xx.c:

PLL_M: 8

PLL_Q: 7

PLL_N: 336

PLL_P: 2

These all seemed correct to me, but I could be missing something. I'm really at a loss here for what's going on. I'm hoping someone can figure it out. Let me know if you need any additional information. Thanks!

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    Aug 16, 2014 4:42 AM UTC
    Posted on August 16, 2014 at 06:42

    Use the MCO pin (PA8) and configure it to output internal clocks, and measure those on a scope.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    logan
    loganAuthor
    Associate II
    Aug 16, 2014 7:09 AM UTC
    Posted on August 16, 2014 at 09:09

    I first added code to the program to output RCC_MCO1Source_HSE. It showed the correct 8 MHz on the scope. I then checked RCC_MCO1Source_PLLCLK on MCO1 and it was 168 MHz.

    I also looked at RCC_MCO2Source_SYSCLK with MCO2 and it was 168 MHz also.

    Any of these results seem out of the ordinary to you? Thanks.

    Tesla DeLorean
    Guru
    Aug 17, 2014 3:11 AM UTC
    Posted on August 17, 2014 at 05:11

    The maximum output for pins is about 90 MHz, so normally you'd need to divide down the 168 MHz.

    Discrepancies between measured and desired baud rates stems from a difference between the actual clock being used, and the value the software believe is being used.

    The HSE_VALUE of 25 MHz v 8 MHz typically results in a 1:3.125 distortion, not 1.5, which is more indicative of something running at 12 MHz.

    You could print out HSE_VALUE and inspect it's value in various object files.

    9600 baud should have a bit time of ~104.2 us
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    Aug 17, 2014 3:12 AM UTC
    Posted on August 17, 2014 at 05:12

    Suggest you attach a complete project for whatever tool chain you are using.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..