Now I have a stm32f4-discovery. I make a max232 for uart to connect stm32f4. And I use usb-rs232 to connect max232. I hope I can communicate stm32f4 and PC through uart. And I open putty in Linux. But I have a big problem about setting uart baud-rate. If I set the same baud-rate, it can't receive right message. I use oscilloscope observe. Finally, I setting 7680(stm32f4) and 3200(putty). then I can get right message. But I have no idea why it work with different baud-rate. Can anyone tell me why? And it's my code,thx https://gitcafe.com/ctc8631/stm32f4-test-uart
4 Answers
You probably need to check your clock settings. The discovery board has an 8MHz crystal while for example the relevant configuration file (Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c) in the Standard Peripheral Library seem to be set up for a 25Mhz crystal. It has these defines
#define PLL_M 25
#define PLL_N 336
#define PLL_P 2
#define PLL_Q 7
to generate 168Mhz from 25MHz whereas a working ethernet example for the discovery board found off the net uses
#define PLL_M 8
#define PLL_N 288
#define PLL_P 2
#define PLL_Q 6
to get 144Mhz from 8MHz.
Edit: It turned out doing this was not enough as stm32f4xx.h defines "HSE_VALUE" as 25000000 (25MHz) unless it is already defined when the file is included. The properly working example I looked at therefore undefined and redefined it to 8000000 (8MHz) in stm32f4xx_conf.h. But I guess the proper thing to do would be to define it on the compiler command line (i.e. in some properties box if you use an IDE). At least that worked fine in IAR EWARM.
In file stm32f4xx.h you need to fix
HSE_VALUE from 25000000 to 8000000
Then in file system_stm32f4xx.c Set following:
/* 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
I had exactly the same problem. I used various library and include files in my project. Most of them was newer than the original delivered by ST. That was my motivation to "upgrade" them, but I it was a big mistake.
My solution/workaround was:
Use TRUEStudio. Make a new Embedded C application. Set up the wizard correctly (it is unambiguous, but here is a video about it: http://www.youtube.com/watch?v=mT5bAgpW3jU ). It will generate all of the necessary files and set up everything well(project structure, debug interface etc.). Add your own sources and modify the necessary ones and tadaaaaa the USARTx will operate the desired baud-rate.
I guess the problem was that the peripheral clock speed have been set wrong, and that is why the operating speed doesn't match. It is only my opinion I can't find the source of the problem, but if you are using different IDE, I advice to change the board specific libraries back to original.