cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407: USART Baudrate wrong by factor 3

tomwinkler9
Associate II
Posted on August 08, 2012 at 20:31

Hello,

I'm in the process of evaluation the STM32F407 for a larger project we are currently working on.

For my evaluations I'm using an Olimex P407 board (link see below). So far I manged to flash the board (using the ST-Link programmer of an STM32F4 discovery) flash some LEDs etc. Next thing on my list is getting the UARTS working to print some messages. I successfully programmed the USARTs (3 and 6) but on the PC side I'm only getting garbage no matter which baud rates I use. I tried 9600 up to 115200 and confirmed that both the PC and the board use the same baud rate and configuration (8N1).

I then attached a scope to the USART pins to check that is going on. I noticed that for 9600 bauds the time for a single bit is 33.4us while I expected 1/9600 = 104us. Similarly for 155200 i measured a bit time of 2.8us while I expected 8.6us. So the bit time is roughly off by a factor of 3. For testing purpose I next simply divided the baud rate in my USART setup code by 3. And suddenly I'm getting clear text on the PC side (set to 115200 and the STM32's USART6 set to 115200/3).

I could not figure out yet what I'm doing wrong. The Olimex board is using an external 25Mhz crystal for HSE which is also set in stm32f4xx.h. I also re-generated the system_stm32f4xx.h using the STM Excel sheet to make sure my settings are correct. For reference purposes I uploaded these two files and my USART setup and test code to pastebin (see links below).

I'd be happy if someone could provide some ideas what I might be doing wrong.

system_stm32f4xx.c: http://pastebin.com/6HgVRR2k

stm32f4xx.h: http://pastebin.com/vqzdsyK4

simple USART init and test: http://pastebin.com/0vGCcz2n

Olimex board: https://www.olimex.com/dev/stm32-p407.html

Thank you,

Thomas

5 REPLIES 5
Posted on August 08, 2012 at 20:44

Are you sure you don't have HSE_VALUE=8000000 defined somewhere in your project (metadata) or files?

In stm32f4xx_conf.h on some of the STM32F4-Discovery projects

#if defined  (HSE_VALUE)

/* Redefine the HSE value; it's equal to 8 MHz on the STM32F4-DISCOVERY Kit */

 #undef HSE_VALUE

 #define HSE_VALUE    ((uint32_t)8000000)

#endif /* HSE_VALUE */

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tomwinkler9
Associate II
Posted on August 09, 2012 at 08:14

Hi,

Oh my.... Thanks for the hint.

I check every possible place in the source code but overlooked a compiler flag for HSE_VALUE that was set in my IDE (to 8MHz of course)...

Thanks again!

Thomas

eo7942
Associate II
Posted on March 27, 2014 at 23:14

eo7942
Associate II
Posted on March 27, 2014 at 23:17

I put the message in the quoted area in the previous post.  Please look at the portion I added.

Posted on March 28, 2014 at 01:12

Grab the template files for the STM32F4-DISCO, and put them in your project directory

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\EXTI\stm32f4xx_conf.h

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\EXTI\system_stm32f4xx.c

/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */

#define PLL_M      8

#define PLL_N      336

/* SYSCLK = PLL_VCO / PLL_P */

#define PLL_P      2

/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */

#define PLL_Q      7

The PLL_M value needs to reflect the number of MHz of the crystal, as the target comparison frequency for the PLL is 1 MHz

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