← Writing

Post

MCU Clock System

The clock system is the basis for timers and interrupts. It sets the pace of every digital component of the microcontroller.

15 July 2025 Anatolii Maltsev 2 min read Embedded Rust & firmware embedded basic clock

The clock system is the basis for timers and interrupts. It sets the pace of every digital component of the microcontroller. Timers take their signals from it to generate interrupts and to run tasks with precise time control.

What is a clock in an MCU?

A clock is a system that generates clock signals to synchronize the CPU and the peripheral modules (timers, UART, ADC). Its frequency determines how fast the MCU executes instructions and processes events.

MCU components usually operate at different frequencies and need to be synchronized to perform operations at the same time.

A simple digital signal switches between high and low levels at a certain frequency (in Hertz). Frequency is the number of events that occur per unit of time, usually per second.

  • 1 Hz = 1 switching per second
  • 1 MHz = 1 million switchings per second

High frequency provides performance, but increases power consumption and heat generation. Peripheral devices therefore often operate at a lower frequency.

How clocks work

The most common clock generator is the quartz crystal. It is used because it can vibrate at a stable frequency (the resonant frequency) when voltage is applied to it.

One of the disadvantages of a quartz crystal is that it is usually limited to generating a single frequency in the megahertz range. On complex boards this can be a problem, since different components often operate at different frequencies and at much higher speeds. To solve the problem of having to use multiple frequencies on a single board, designers often use a PLL and prescalers.

To obtain different frequencies, use:

  • PLL (phase-locked loop): a circuit that creates a desired frequency from a base signal, for example 1 GHz from 100 MHz. It obtains high and accurate clock signals from a limited number of resonators.
  • Prescalers: reduce the frequency for individual modules.

Clock system components

  • Clock sources:
    • Internal RC oscillator (less accurate, but energy-saving)
    • External quartz (high stability)
    • PLL (frequency multiplication)
  • Dividers and prescalers: allow you to set the desired frequency for each block.
  • System clock (SYSCLK): the main processor clock signal.
  • Peripheral clock: frequency division between modules, for example AHB and APB in STM32.

Clock types

Clocks are also used for their usual function, keeping time. There are two types of clocks for this purpose:

  • RTC (Real-Time Clock): a hardware clock that works even when the power is off, from the battery. Used for accurate real-time counting.
  • System clock: a virtual clock, monitored by the OS, initialized at startup (usually from the RTC).

Example of clocks setup in Rust (RP2040)

use rp2040_hal as hal;
const XTAL_FREQ_HZ: u32 = 12_000_000;

let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);

let clocks = hal::clocks::init_clocks_and_plls(
    XTAL_FREQ_HZ,
    pac.XOSC,
    pac.CLOCKS,
    pac.PLL_SYS,
    pac.PLL_USB,
    &mut pac.RESETS,
    &mut watchdog,
).unwrap();

Communicating with interrupts

  • Timers receive their frequency from the system clock and use it to accurately generate interrupts.
  • Without proper frequency settings, timers may work with errors or become unstable.
  • Some interrupts, for example the Watchdog, use independent clock sources such as LSI in STM32.

Neighbors in time

when name
2025-07 Interrupts (ISR, IVT, NVIC) Read →
2025-07 Timers Read →

2025-07 MCU Synchronization Read →
2025-07 Interrupts (ISR, IVT, NVIC) Read →
2025-07 Timers Read →

Related: the posts that share this one's subject, and where the thread goes next.