How to properly set clock speed for STM32F4xx devices
http://stm32f4-discovery.com/2015/01/properly-set-clock-speed-stm32f4xx-devices/
I see that you have problems with your devices when you don’t know even
(and you don’t even ask) on which speed your device is actually running.
Speed of your device depends on PLL settings or clock source you have selected for system core clock.
In most cases system core clock’s source is PLL output.
To get proper value, you check ALWAYS first these settings when something is not working.
Because I’m tired of questions that something is not working but then your MCU clock is totally failed.
STM32F4xx series MCUs have ALL 16MHz RC oscillator inside which can be used for PLL input clock.
It is not so accurate but it works just well if you don’t have connected external clock.
External clock is probably your crystal you have connected.
In this example we will talk in case that you have external crystal connected.
If your clock is not properly set, then you have problems with everything.
USB not working, USART baudrate failed, timer’s clock not OK…
This content has only sense, if you use Standard Peripheral Drivers and not HAL drivers from ST!
To be able to get your MCU to full speed, you have to do the following changes:
- In file stm32F4xx.h set HSE_VALUE to match your crystal frequency (In Hz)
- In case of any STM32F4xx Discovery board, select HSE_VALUE =8000000
- In other case, you can set this to your crystal
- If you have an option for global defines in your compiler, then addHSE_VALUE=8000000 to your defines.
- If you don’t use external crystal or clock, then you don’t need to change anything
- In file system_stm32f4xx.c set PLL_M value to match your crystal frequency (In MHz)
- In case of any STM32F4xx Discovery board, select PLL_M = 8
- This will divide input clock with 8 to get 1MHz on the input for PLL
- If you don’t use external clock, then this value MUST be set to 16,
because internal RC will be used for PLL
- Now you can expect top speed for your device.
Some informations about Nucleo boards:
- Nucleo boards don’t use external clock by default,
but they have pins for adding external crystal. - In case you don’t add external crystal or clock, then set PLL_M value to 16
- I have at home set F401 Nucleo board with external capacitor 8MHz
(so PLL_M is set to 8 and HSE_VALUE set to 8000000) and
F411 without external capacitor (PLL_M set to 16)
STM32F4xx Clocks
Proper PLL settings to get maximum clocks
(except of PLL_M parameter everything should be already defined in system_stm32f4xx.c file).
SystemCoreClock = (( INPUT_CLOCK (HSE_OR_HSI_IN_HZ) / PLL_M) * PLL_N) / PLL_P
STM32F4xx | MAX CLOCK in MHz | APB1 CLOCK in MHz </2> | APB2 CLOCK in MHz </1> | PLL_M | PLL_N | PLL_P |
---|---|---|---|---|---|---|
STM32F401 | 84 | 42 | 84 | Must match input frequency in MHz (HSE or HSI) | 336 | 4 |
STM32F411 | 100 | 50 | 100 | Must match input frequency in MHz (HSE or HSI) | 400 | 4 |
STM32F405 | 168 | 42 | 84 | Must match input frequency in MHz (HSE or HSI) | 336 | 2 |
STM32F407 | 168 | 42 | 84 | Must match input frequency in MHz (HSE or HSI) | 336 | 2 |
STM32F415 | 168 | 42 | 84 | Must match input frequency in MHz (HSE or HSI) | 336 | 2 |
STM32F417 | 168 | 42 | 84 | Must match input frequency in MHz (HSE or HSI) | 336 | 2 |
STM32F427 | 180 | 45 | 90 | Must match input frequency in MHz (HSE or HSI) | 360 | 2 |
STM32F429 | 180 | 45 | 90 | Must match input frequency in MHz (HSE or HSI) | 360 | 2 |
STM32F437 | 180 | 45 | 90 | Must match input frequency in MHz (HSE or HSI) | 360 | 2 |
STM32F439 | 180 | 45 | 90 | Must match input frequency in MHz (HSE or HSI) | 360 | 2 |
STM32F446 | 180 | 45 | 90 | Must match input frequency in MHz (HSE or HSI) | 360 | 2 |
Settings for STM32F4xx boards
This settings should be used in case of any STM32F4xx boards used.
STM32F4xx | MAX CLOCK in MHz | APB1 CLOCK in MHz | APB2 CLOCK in MHz | DEFAULT PLL INPUT CLOCK | PLL_M | PLL_N | PLL_P |
---|---|---|---|---|---|---|---|
STM32F401 -Discovery |
84 | 42 | 84 | HSE 8MHz | 8 | 336 | 4 |
STM32F411 -Discovery |
100 | 50 | 100 | HSE 8MHz | 8 | 400 | 4 |
STM32F4 -Discovery |
168 | 42 | 84 | HSE 8MHz | 8 | 336 | 2 |
STM32F429 -Discovery |
180 | 45 | 90 | HSE 8MHz | 8 | 360 | 2 |
Nucleo F401 | 84 | 42 | 84 | HSI 16MHz | HSE_VALUE / 1MHz in case of external crystal or 16 in case of internal HSI RC oscillator |
336 | 4 |
Nucleo F411 | 100 | 50 | 100 | HSI 16MHz | HSE_VALUE / 1MHz in case of external crystal or 16 in case of internal HSI RC oscillator |
400 | 4 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2014-07-29 USB DATA Toggle
2013-07-29 MAX II Device Compatibility with 5.0-V CMOS Devices