RCC Clock SystemHSI, HSE, PLL & Bus Clocks-Embedded C Programming Training Online

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Tutorial 06 — Clocks

RCC Clock System
HSI, HSE, PLL & Bus Clocks

Complete STM32F407 clock tree — from the 8 MHz crystal to 168 MHz CPU. RCC_CR, RCC_CFGR, RCC_PLLCFGR registers explained bit by bit with full PLL configuration code.

⏱ ~30 min read⚡ 168 MHz PLL Setup📈 Clock Tree Diagram📌 RCC Registers

Why Clock Configuration Matters

Before any peripheral works — UART, SPI, I2C, Timers, ADC — you need the right clocks. The STM32F407 boots from its internal 16 MHz RC oscillator (HSI) by default. But for full-speed 168 MHz operation, you need to configure the PLL (Phase-Locked Loop) using the more accurate external 8 MHz crystal (HSE).

Clock configuration also determines:

  • UART baud rate accuracy — wrong clock frequency = wrong baud rate
  • Timer frequencies — affects delays, PWM frequencies
  • SPI/I2C maximum speeds — limited by APB bus frequency
  • ADC conversion accuracy — ADC clock must stay ≤ 36 MHz

Clock Sources: HSI, HSE, LSI, LSE

ClockTypeFrequencyAccuracyUse Case
HSIHigh-Speed Internal RC16 MHz±1% (calibrated) to ±5% over temperatureDefault boot clock. No external component needed. OK for non-critical applications.
HSEHigh-Speed External crystal4–26 MHz (8 MHz on Discovery)<0.001% (crystal)Best accuracy for PLL, UART, USB, Ethernet. Required for USB (needs 48 MHz precise).
LSILow-Speed Internal RC~32 kHz (±10%)PoorIndependent Watchdog (IWDG). RTC (inaccurate). Low power modes.
LSELow-Speed External crystal32.768 kHzExcellentRTC for accurate timekeeping. Very low power.

Complete Clock Tree (STM32F407 @ 168 MHz)

This is the complete path from oscillator to CPU and peripherals:

STM32F407 CLOCK TREE — Discovery Board Configuration
HSE Crystal
8 MHz
→ PLL Source Mux (PLLSRC=1) →
÷ PLLM
÷8 = 1 MHz
→
× PLLN
×336 = 336 MHz
→
÷ PLLP
÷2 = 168 MHz
PLLCLK
168 MHz
→ System Clock Mux (SW=10) →
SYSCLK
168 MHz
→
AHB Prescaler ÷1
HCLK = 168 MHz
→
CPU Core
168 MHz
HCLK
168 MHz
→
APB1 Prescaler ÷4
PCLK1 = 42 MHz
→
SPI2, SPI3
I2C1-3, UART4-5
USART2-3, TIM2-7
Max 42 MHz
HCLK
168 MHz
→
APB2 Prescaler ÷2
PCLK2 = 84 MHz
→
SPI1, USART1/6
TIM1/8-11, ADC
SYSCFG, SDIO
Max 84 MHz
PLLCLK 168MHz ÷PLLQ
÷7 = 48 MHz
→
USB OTG FS
SDIO, RNG
Must be 48 MHz
💡 Timer clocks are doubled: When APB prescaler ≠ 1, the timer clock = 2 × PCLK. So TIM2-7 get 42 × 2 = 84 MHz, and TIM1/8 get 84 × 2 = 168 MHz. This gives timers full HCLK speed regardless of APB prescaler.

PLL — Phase Locked Loop Explained

The PLL multiplies a lower-frequency input clock to a much higher stable frequency. It has several divisors and multipliers you configure via registers:

PLL Formula

VCO input = PLL source / PLLM (must be 1–2 MHz, ideally 2 MHz)
VCO output = VCO input × PLLN (must be 100–432 MHz)
PLLCLK = VCO output / PLLP (SYSCLK — CPU clock)
PLL48CK = VCO output / PLLQ (USB/SDIO/RNG clock — must = 48 MHz)

For 168 MHz from 8 MHz HSE:
PLLM=8 → VCO input = 8/8 = 1 MHz
PLLN=336 → VCO output = 1 × 336 = 336 MHz
PLLP=2 → PLLCLK = 336/2 = 168 MHz ✓
PLLQ=7 → PLL48CK = 336/7 = 48 MHz ✓ (exact for USB)

RCC_CR — Clock Control Register

Address: 0x40023800. Controls enabling/disabling of each clock source and reports their ready status.

RCC_CR (Address: 0x40023800) — Key bits shown
25
PLLRDY
24
PLLON
23:20
–
19
CSSON
18
HSEBYP
17
HSERDY
16
HSEON
15:8
HSICAL
7
HSITRIM
3:2
–
1
HSIRDY
0
HSION
Bit and field descriptions
Bit / FieldDescription
25 PLLRDYPLLRDY (bit 25)
24 PLLONPLLON (bit 24)
19 CSSONCSSON (bit 19)
18 HSEBYPHSEBYP (bit 18)
17 HSERDYHSERDY (bit 17)
16 HSEONHSEON (bit 16)
1 HSIRDYHSIRDY (bit 1)
0 HSIONHSION (bit 0)

RCC_PLLCFGR — PLL Configuration Register

Address: 0x40023804. This register must be configured BEFORE enabling the PLL. You cannot change it while the PLL is running.

RCC_PLLCFGR (Address: 0x40023804)
31
–
27:24
PLLQ
23:23
–
22
PLLSRC
21:18
–
17:16
PLLP
15:15
–
14:6
PLLN[8:0]
5
–
5:0
PLLM[5:0]
Bit and field descriptions
Bit / FieldDescription
27:24 PLLQPLLQ[3:0] bits 27:24
22 PLLSRCPLLSRC (bit 22)
17:16 PLLPPLLP[1:0] bits 17:16
14:6 PLLN[8:0]PLLN[8:0] bits 14:6
5:0 PLLM[5:0]PLLM[5:0] bits 5:0
⚠️ VCO frequency limits: The PLL VCO output (PLLN × VCO_input) must be between 100 MHz and 432 MHz. Going outside this range causes unstable operation. With PLLM=8 (1 MHz VCO input) and PLLN=336: VCO = 336 MHz ✓

RCC_CFGR — Clock Configuration Register

Address: 0x40023808. Selects the system clock source and sets AHB/APB prescalers. Also controls MCO (Master Clock Output) pins.

RCC_CFGR (Address: 0x40023808) — Key fields
31:30
MCO2
29:27
MCO2PRE
26:24
MCO1PRE
23
I2SSRC
22:21
MCO1
20:16
RTCPRE
15:13
PPRE2
12:10
PPRE1
9:8
–
7:4
HPRE
3:2
SWS
1:0
SW
Bit and field descriptions
Bit / FieldDescription
20:16 RTCPRERTCPRE[4:0] bits 20:16
15:13 PPRE2PPRE2[2:0] bits 15:13
12:10 PPRE1PPRE1[2:0] bits 12:10
7:4 HPREHPRE[3:0] bits 7:4
3:2 SWSSWS[1:0] bits 3:2
1:0 SWSW[1:0] bits 1:0

Enabling Peripheral Clocks

Every peripheral on the STM32F407 needs its clock enabled separately. Forgetting this is the #2 most common bug (after forgetting GPIO clock). Here are the key clock enable registers:

RegisterAddressBusControls
RCC_AHB1ENR0x40023830AHB1GPIO A-I, DMA1/2, CRC, BKPSRAM, Flash, CCMDATARAM, ETH, USB OTG HS
RCC_AHB2ENR0x40023834AHB2USB OTG FS, DCMI, Crypto, Hash, RNG
RCC_AHB3ENR0x40023838AHB3FSMC (external memory)
RCC_APB1ENR0x40023840APB1TIM2-7,12-14, WWDG, SPI2/3, USART2/3, UART4/5, I2C1/2/3, CAN1/2, PWR, DAC, RTC
RCC_APB2ENR0x40023844APB2TIM1,8-11, USART1/6, ADC1/2/3, SDIO, SPI1, SYSCFG, EXTI, SAI1
/* Common peripheral clock enables */
#define RCC_AHB1ENR  (*(volatile uint32_t*)0x40023830UL)
#define RCC_APB1ENR  (*(volatile uint32_t*)0x40023840UL)
#define RCC_APB2ENR  (*(volatile uint32_t*)0x40023844UL)

/* GPIO clocks (AHB1) */
RCC_AHB1ENR |= (1U<<0);  /* GPIOA */
RCC_AHB1ENR |= (1U<<1);  /* GPIOB */
RCC_AHB1ENR |= (1U<<2);  /* GPIOC */
RCC_AHB1ENR |= (1U<<3);  /* GPIOD */
RCC_AHB1ENR |= (1U<<4);  /* GPIOE */

/* DMA clocks */
RCC_AHB1ENR |= (1U<<21); /* DMA1 */
RCC_AHB1ENR |= (1U<<22); /* DMA2 */

/* APB1 peripherals */
RCC_APB1ENR |= (1U<<0);  /* TIM2 */
RCC_APB1ENR |= (1U<<1);  /* TIM3 */
RCC_APB1ENR |= (1U<<17); /* USART2 */
RCC_APB1ENR |= (1U<<21); /* I2C1 */
RCC_APB1ENR |= (1U<<25); /* CAN1 */
RCC_APB1ENR |= (1U<<29); /* DAC */

/* APB2 peripherals */
RCC_APB2ENR |= (1U<<0);  /* TIM1 */
RCC_APB2ENR |= (1U<<4);  /* USART1 */
RCC_APB2ENR |= (1U<<8);  /* ADC1 */
RCC_APB2ENR |= (1U<<12); /* SPI1 */
RCC_APB2ENR |= (1U<<14); /* SYSCFG (needed for EXTI) */

Full 168 MHz PLL Configuration Code

This is the complete function to configure the STM32F407 to run at 168 MHz using the external 8 MHz crystal. This is what the HAL library does internally — now you understand every step:

C — SystemInit for 168 MHz

#include <stdint.h>

#define RCC_BASE    0x40023800UL
#define FLASH_BASE  0x40023C00UL

#define RCC_CR      (*(volatile uint32_t*)(RCC_BASE + 0x00))
#define RCC_PLLCFGR (*(volatile uint32_t*)(RCC_BASE + 0x04))
#define RCC_CFGR    (*(volatile uint32_t*)(RCC_BASE + 0x08))
#define FLASH_ACR   (*(volatile uint32_t*)(FLASH_BASE + 0x00))
#define PWR_CR      (*(volatile uint32_t*)0x40007000UL)
#define RCC_APB1ENR (*(volatile uint32_t*)(RCC_BASE + 0x40))

void SystemClock_Config_168MHz(void) {

    /* ---- Step 1: Enable HSE oscillator ---- */
    RCC_CR |= (1U << 16);           /* HSEON = 1 */
    while (!(RCC_CR & (1U << 17)));  /* Wait HSERDY = 1 */

    /* ---- Step 2: Enable power controller & set voltage scale 1 ---- */
    RCC_APB1ENR |= (1U << 28);      /* Enable PWR clock */
    PWR_CR |= (0x3U << 14);         /* VOS = 11 (scale 1, needed for 168 MHz) */

    /* ---- Step 3: Configure Flash wait states for 168 MHz ---- */
    /* At 3.3V, 168 MHz requires 5 wait states (LATENCY = 5) */
    FLASH_ACR  = (0x5U)              /* LATENCY = 5 wait states */
               | (1U << 8)          /* PRFTEN  = prefetch enable */
               | (1U << 9)          /* ICEN    = instruction cache */
               | (1U << 10);        /* DCEN    = data cache */

    /* ---- Step 4: Configure PLL ---- */
    /* PLLM=8, PLLN=336, PLLP=÷2, PLLQ=7, Source=HSE */
    RCC_PLLCFGR = (8U  << 0)    /* PLLM = 8  → 8MHz/8 = 1 MHz VCO input */
                | (336U << 6)  /* PLLN = 336 → 1×336 = 336 MHz VCO out */
                | (0U   << 16) /* PLLP = 00 = ÷2 → 336/2 = 168 MHz */
                | (1U   << 22) /* PLLSRC = 1 = HSE selected */
                | (7U   << 24); /* PLLQ = 7  → 336/7 = 48 MHz for USB */

    /* ---- Step 5: Enable PLL ---- */
    RCC_CR |= (1U << 24);           /* PLLON = 1 */
    while (!(RCC_CR & (1U << 25)));  /* Wait PLLRDY = 1 */

    /* ---- Step 6: Set AHB/APB prescalers ---- */
    RCC_CFGR = (0x0U << 4)    /* HPRE  = 0000 = ÷1 → HCLK = 168 MHz */
             | (0x5U << 10)  /* PPRE1 = 101  = ÷4 → PCLK1 = 42 MHz */
             | (0x4U << 13); /* PPRE2 = 100  = ÷2 → PCLK2 = 84 MHz */

    /* ---- Step 7: Switch SYSCLK to PLL ---- */
    RCC_CFGR |= (0x2U << 0);        /* SW = 10 = PLL as SYSCLK */
    while((RCC_CFGR & (0x3U << 2)) != (0x2U << 2)); /* Wait SWS = 10 */

    /* System is now running at 168 MHz! */
}
Tutorial 06 Summary
  • STM32F407 boots at 16 MHz HSI — configure PLL for full 168 MHz speed
  • HSE (8 MHz crystal) → PLLM=8 → PLLN=336 → PLLP=÷2 = 168 MHz
  • PLL48CK: PLLQ=7 → 336/7 = 48 MHz for USB OTG (exact)
  • APB1 max = 42 MHz (÷4 from HCLK); APB2 max = 84 MHz (÷2 from HCLK)
  • Flash wait states must match frequency — 168 MHz @ 3.3V = 5 wait states
  • Every peripheral needs its own clock enabled in AHB1/APB1/APB2 ENR register
  • Wait for HSERDY and PLLRDY flags before using those sources

STM32F407 Discovery Tutorial Series — Tutorial 06: RCC Clock System

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *