Hello! Welcome to the next lesson in our series on bare-metal driver development.
In our last session, we built a set of functions to control GPIO pins, and a key first step was always to enable the peripheral's clock. This highlighted a fundamental principle: no part of the microcontroller can operate without a timing reference.
Today, we will explore this topic in depth. Our learning outcome is to explain the architecture of a microcontroller clock tree, including oscillators, PLLs, and prescalers. You've likely configured clocks using AUTOSAR's Mcu module or configuration tools, but this lesson will demystify the underlying hardware that those tools manipulate. Understanding the clock tree is essential for optimizing performance, managing power consumption, and ensuring peripherals like CAN or USB operate within their required specifications.
1. The Heartbeat of the Microcontroller
At its core, a clock is a digital signal—a continuous square wave—that oscillates between high and low states at a fixed frequency. This signal acts as a metronome for the entire system, synchronizing every action.
What is a Clock in Digital Electronics
To start, let's refresh the fundamental concepts. This article from Embedded Prep provides a clear, high-level overview of what a clock is and why it's so critical.
Please read the sections 'What is a Clock?' and 'What Does the MCU Do with the Clock Signal?'. This will provide a solid conceptual foundation for the more detailed components we'll discuss next.
As the article explains, the clock drives two main things:
- The CPU: The famous fetch-decode-execute cycle happens in lock-step with the clock. A faster clock means the CPU can execute instructions more quickly.
- Peripherals: Communication interfaces (UART, SPI, I2C), timers, and ADCs all rely on a clock signal for their internal operations, timing, and baud rate generation.
The entire system for generating and distributing these clock signals is called the clock tree. Let's look at its main components.
2. The Clock Tree Architecture
A modern microcontroller's clock system is not just a single oscillator. It's a complex network designed for flexibility. The diagram below shows a typical clock tree for an STM32 microcontroller. We will use this as our map for the rest of the lesson.

As you can see, the architecture can be broken down into three main stages:
- Clock Sources: Where the initial signal originates (oscillators).
- Clock Generation: Where the source signal is conditioned, often multiplied to a higher frequency (PLL).
- Clock Distribution: Where the final clock is divided and routed to the CPU and various peripherals (buses and prescalers).
Let's examine each stage.
2.1. Clock Sources: The Oscillators
The clock tree begins with one or more oscillators. These are the primary sources of the timing signal. Microcontrollers typically provide several options to balance cost, performance, and accuracy.
STM32 Clock Configuration Guide
This article from aticleworld provides an excellent, detailed breakdown of the clock sources available on a modern STM32 device. The concepts are applicable across most ARM microcontrollers.
Read the section 'Clock Sources'. Focus on understanding the distinction and typical use cases for HSI, HSE, LSI, and LSE.
To summarize the key sources:
-
High-Speed Internal (HSI): An internal RC (resistor-capacitor) oscillator.
- Pros: No external components needed, fast startup time.
- Cons: Less accurate and stable than an external crystal. Its frequency can vary with temperature and voltage.
- Use Case: A good default clock source when high precision is not required or for quick startup before switching to a more stable clock.
-
High-Speed External (HSE): Uses an external crystal or ceramic resonator.
- Pros: Very accurate and stable. The preferred source for high-performance systems.
- Cons: Requires external components, has a longer startup time as the crystal needs to stabilize.
- Use Case: The primary clock source for the main system clock (SYSCLK) and for peripherals requiring precise timing, like USB or CAN.
-
Low-Speed Internal/External (LSI/LSE): Slower oscillators (typically ~32 kHz) used for low-power functions.
- Use Case: Driving the Real-Time Clock (RTC) and watchdog timers that need to run even when the main CPU is in a low-power state.
Referring back to the STM32F4 Clock Tree Diagram, you can see the HSI, LSE, and HSE on the far left, representing the entry points to the system.
2.2. The Phase-Locked Loop (PLL): The Frequency Multiplier
Most applications require CPU speeds far greater than the typical 8-25 MHz of an external crystal. This is where the Phase-Locked Loop (PLL) comes in. The PLL is a feedback control circuit that takes a stable, low-frequency input and generates a stable, high-frequency output.

In essence, the PLL works by:
- Taking an input clock (e.g., from the HSE).
- Optionally dividing it down with a prescaler (M).
- Feeding this into a Voltage-Controlled Oscillator (VCO), which generates a high-frequency signal.
- Dividing the VCO output down with a feedback divider (N).
- Comparing this divided-down signal with the input signal at a Phase Detector.
- Adjusting the VCO's control voltage until the feedback signal's phase and frequency are "locked" to the input.
The final system clock is then derived by dividing the VCO output with another post-divider (P). The key formula is:
This process allows you to generate a wide range of precise, high-speed system clocks from a single, standard crystal frequency.
Mastering Microcontroller: Clocking MCU by Internal PLL (Running MCU at Full Speed)
To see this in a practical context, watch this segment from Fastbit Embedded Brain Academy. It demonstrates how to calculate the M, N, and P values to achieve a target frequency.
Watch from 00:52 to 03:25. The video explains the process of selecting PLL prescaler values (M, N, and P) to derive a 168 MHz system clock from an 8 MHz source. Pay close attention to how they satisfy the VCO input frequency constraints.
Test your understanding!
You are using a microcontroller with an 8 MHz external crystal (HSE) as the PLL input. You need to generate a system clock (SYSCLK) of 72 MHz. The PLL's VCO requires an input frequency between 1 and 2 MHz. Which of the following PLL parameter sets (M, N, P) would be valid?
a) M=1, N=18, P=2
b) M=4, N=144, P=4
c) M=2, N=72, P=2
Show answer
The correct answer is b) M=4, N=144, P=4.
Let's analyze why:
-
Check the VCO input frequency:
F_VCO_IN = F_IN / M. This must be between 1 and 2 MHz.- a)
8 MHz / 1 = 8 MHz(Invalid) - b)
8 MHz / 4 = 2 MHz(Valid) - c)
8 MHz / 2 = 4 MHz(Invalid)
- a)
-
Calculate the output frequency for the valid option:
- b)
F_OUT = (F_IN / M) * N / P = (8 MHz / 4) * 144 / 4 = 2 MHz * 144 / 4 = 288 MHz / 4 = 72 MHz. This matches our target.
- b)
This exercise shows that configuring a PLL isn't just about the final output; you must also satisfy the operational constraints of its internal components.
2.3. Clock Distribution: Buses and Prescalers
Once the main System Clock (SYSCLK) is generated, it's not sent to every part of the MCU at full speed. Different parts of the chip have different speed requirements and power constraints. The clock is distributed through a hierarchy of buses, primarily:
- AHB (Advanced High-performance Bus): Connects the CPU core, memory (Flash, SRAM), and high-performance peripherals like DMA. This bus runs at a high speed, often derived directly from SYSCLK. The clock for this bus is called HCLK.
- APB (Advanced Peripheral Bus): Connects lower-speed peripherals like UART, I2C, SPI, and timers. This bus runs at a lower speed to save power. The clocks are called PCLK. Modern MCUs often have multiple APB buses (APB1, APB2) that can be clocked at different speeds.
The mechanism to slow the clock down from SYSCLK to HCLK, and from HCLK to PCLK, is the prescaler—a simple frequency divider.
Processing speed | #4 STM32 Clock configuration
The STM32CubeIDE's clock configuration tool provides an excellent visualization of this distribution network. Watch this video to see how the SYSCLK is stepped down for the AHB and APB buses.
Watch from 03:08 to 06:10. Observe how the tool shows the SYSCLK being generated by the PLL, and then fed into prescalers for the AHB and APB buses, resulting in different clock frequencies (HCLK, PCLK1, PCLK2) for various peripherals.
This tiered approach is critical for power management. There is no need to run a 115200-baud UART at 168 MHz; by using a prescaler to provide a slower clock to the APB bus, you significantly reduce the system's overall power consumption.
Interview Preparation
Understanding the clock tree is a common topic in embedded interviews. Here are some questions you might encounter:
-
Question: "What is the purpose of the RCC peripheral in an STM32 microcontroller?"
- Your Answer: "The RCC, or Reset and Clock Control peripheral, is the central hub for managing the entire clock system. Its responsibilities include selecting the system clock source (like HSI, HSE, or PLL), enabling and disabling the PLL, configuring prescalers for the AHB and APB buses, and gating the clocks for individual peripherals to save power."
-
Question: "You're designing a low-power, battery-operated device that needs to keep accurate time. Which clock source would you use for the Real-Time Clock (RTC), and why?"
- Your Answer: "I would use the Low-Speed External (LSE) oscillator, which is typically a 32.768 kHz crystal. While the Low-Speed Internal (LSI) is an option, it's an RC oscillator and not very accurate over temperature. For accurate timekeeping, the stability of an external crystal is necessary. The LSE is also designed for low power consumption and can continue running even when the main high-speed clocks are turned off in deep sleep modes."
-
Question: "What are the trade-offs between using the internal HSI oscillator versus an external HSE crystal as the main clock source?"
- Your Answer: "The main trade-off is between cost/convenience and accuracy/stability. The HSI is 'free' as it requires no external components and has a very fast startup time. However, it's an RC oscillator, so its frequency is not very precise and can drift with temperature. The HSE, using an external crystal, is highly accurate and stable, making it a requirement for peripherals with strict timing needs like USB or CAN. The downsides are the added cost of the crystal and its associated capacitors, and a longer startup time while the crystal stabilizes."
-
Question: "Explain the term 'prescaler' in the context of a microcontroller's clock tree."
- Your Answer: "A prescaler is a frequency divider. In a clock tree, prescalers are used to reduce the main system clock frequency for different parts of the microcontroller. For example, the high-speed SYSCLK is often divided down by an AHB prescaler to generate the HCLK for the CPU and memory. This HCLK is then further divided by APB prescalers to generate slower PCLKs for peripherals. This allows high-speed operation where needed (the core) while saving significant power by running slower peripherals at lower frequencies."
Conclusion
In this lesson, we've dissected the clock system of a modern microcontroller. You now understand how a stable timing reference is generated, multiplied, and distributed throughout the chip.
Key Takeaways:
- The clock tree is the system that generates and distributes timing signals.
- Oscillators (HSI, HSE, LSI, LSE) are the primary sources, offering a trade-off between accuracy and cost.
- The Phase-Locked Loop (PLL) is a critical component that multiplies a low-frequency source into a high-frequency, stable system clock.
- Prescalers are dividers used to create slower clocks for different bus domains (AHB, APB), enabling a balance of performance and power efficiency.
Next Lesson Preview:
Now that we have a solid architectural understanding of the clock tree, our next step is to control it. In the next lesson, we will implement a Reset and Clock Control (RCC) driver function to configure system and peripheral clocks, translating these concepts into C code that manipulates the hardware registers directly.