Apr 11, 2014 Library 03- STM32F4 system clock and delay functions. On Clock speeds is a great tutorial how to do it with ST’s excel clock configuration tool, but this tool does not support frequencies larger than 168MHz, so we will do it manually. Next story Library 04- USART for STM32F4.
Clock source selection is done thanks to the CLOCK_SOURCE define mask value: USE_PLL_HSE_EXTC|USE_PLL_HSE_XTAL|USE_PLL_HSI|USE_PLL_MSI
- USE_PLL_HSE_EXTC : the system clock is using an external clock (PLL + HSE_BYPASS used). On ST board, this external clock is often provided by the MCO coming from the ST Link (8 MHz clock).
- USE_PLL_HSE_XTAL : the system clock is using an external xtal (PLL + HSE_ON used). On NUCLEO board, this xtal should be soldered on X3.
Note that default parameters are set with a 8 MHz clock. If the xtal value is different:
- the SDK code must be changed to adapt the different clock dividers and PLL setting (system_clock.c file)
- HSE_VALUE has to be redefined in targets.json file, adding a config value for the target. Ex:
'config': { 'hse_value': { 'value': '24000000', 'macro_name': 'HSE_VALUE' }
- USE_PLL_HSI : the system clock is using the high speed internal clock
- USE_PLL_MSI : the system clock is using the multi speed internal clock (only available in STM32L4 family)
If several values are set in the mask, STM32 SDK tries to configure the clock in this order:
1. USE_PLL_HSE_EXTC:
2. USE_PLL_HSE_XTAL
3. USE_PLL_HSI
4. USE_PLL_MSI
Note that :
- HSE_BYPASS and HSE_ON (USE_PLL_HSE_EXTC and USE_PLL_HSE_XTAL) are exclusive: you should not select both value in the mask.
Clock source selection default mask value is set in targets.json file. Here is an example:
This can be overwritten in mbed_app.json file depending on your application or target. Here is an example:
NB: ST is providing a graphical software configuration tool that allows the generation of C initialization code using graphical wizards, and in particular a clock-tree setting helper:
See the mbed-cli documentation for more information on the configuration system syntax:
https://os.mbed.com/docs/latest/tools/adding-and-configuring-targets.html
Once you venture beyond the tame, comfortable walls of the 8-bit microcontroller world it can feel like you’re stuck in the jungle with a lot of unknown and oft scary hazards jut waiting to pounce. But the truth is that your horizons have expanded exponentially with the acceptable trade-off of increased complexity. That’s a pretty nice problem to have; the limitation becomes how much can you learn.
Here’s a great chance to expand your knowledge of the STM32 by learning more about the system clock options available. We’ve been working with STM32 chips for a few years now and still managed to find some interesting tidbits — like the fact that the High Speed External clock source accepts not just square waves but sine and triangle waves as well, and an interesting ‘gotcha’ about avoiding accidental overclocking. [Shawon M. Shahryiar] even covers one of our favorite subjects: watchdog timers (of which there are two different varieties on this chip). Even if this is not your go-to 32-bit chip family, most chips have similar clock source features so this reading will help give you a foothold when reading other datasheets.
There is a clock diagram at the top of that post which is small enough to be unreadable. You can get a better look at the diagram on page 12 of this datasheet. Oh, and just to save you the hassle of commenting on it, the chip shown above is not an f103… but it just happened to be sitting on our desk when we started writing.