STM32 Programming· Live

Clocks: where the speed comes from

A clock is the heartbeat of the chip: a steady train of pulses that every instruction and every peripheral follows. We see where it comes from and how CubeMX sets it.

What a clock is

A clock is an oscillator: a crystal, a ceramic resonator or an RC circuit inside the chip. It makes pulses at a fixed frequency, and everything in the microcontroller steps in time with them.

Frequency is measured in hertz (Hz), pulses per second. A higher clock gives more speed and uses more power. The STM32F401RE runs at up to 84 MHz.

The clock tree

CubeMX has a Clock Configuration tab. There you choose the source and the dividers:

  • HSI (High-Speed Internal): an RC oscillator inside the chip, 16 MHz on the STM32F401
  • HSE (High-Speed External): an external crystal or clock signal
  • PLL (Phase-Locked Loop): multiplies a source up to a higher frequency, for example 84 MHz

From there the clock goes to the core and to the buses: the sets of wires that connect the CPU to groups of peripherals (lesson 7.2 shows them). Each bus has its own divider, which lowers the clock for slower peripherals. HCLK is the clock of the core and the main bus. CubeMX checks that every value is allowed.

The STM32F401 clock tree: sources, PLL, and the buses that feed the peripherals
Note
To set a clock, type the value in the HCLK (MHz) box and press Enter: CubeMX finds the settings for you. A new NUCLEO-F401RE project starts at 84 MHz (HSI through the PLL). In lesson 3.3 we set 16 MHz, so the numbers are simple.