🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →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.
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
| Clock | Type | Frequency | Accuracy | Use Case |
|---|---|---|---|---|
| HSI | High-Speed Internal RC | 16 MHz | ±1% (calibrated) to ±5% over temperature | Default boot clock. No external component needed. OK for non-critical applications. |
| HSE | High-Speed External crystal | 4–26 MHz (8 MHz on Discovery) | <0.001% (crystal) | Best accuracy for PLL, UART, USB, Ethernet. Required for USB (needs 48 MHz precise). |
| LSI | Low-Speed Internal RC | ~32 kHz (±10%) | Poor | Independent Watchdog (IWDG). RTC (inaccurate). Low power modes. |
| LSE | Low-Speed External crystal | 32.768 kHz | Excellent | RTC for accurate timekeeping. Very low power. |
Complete Clock Tree (STM32F407 @ 168 MHz)
This is the complete path from oscillator to CPU and peripherals:
8 MHz
÷8 = 1 MHz
×336 = 336 MHz
÷2 = 168 MHz
168 MHz
168 MHz
HCLK = 168 MHz
168 MHz
168 MHz
PCLK1 = 42 MHz
I2C1-3, UART4-5
USART2-3, TIM2-7
Max 42 MHz
168 MHz
PCLK2 = 84 MHz
TIM1/8-11, ADC
SYSCFG, SDIO
Max 84 MHz
÷7 = 48 MHz
SDIO, RNG
Must be 48 MHz
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:
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.
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 25 PLLRDY | PLLRDY (bit 25) |
| 24 PLLON | PLLON (bit 24) |
| 19 CSSON | CSSON (bit 19) |
| 18 HSEBYP | HSEBYP (bit 18) |
| 17 HSERDY | HSERDY (bit 17) |
| 16 HSEON | HSEON (bit 16) |
| 1 HSIRDY | HSIRDY (bit 1) |
| 0 HSION | HSION (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.
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 27:24 PLLQ | PLLQ[3:0] bits 27:24 |
| 22 PLLSRC | PLLSRC (bit 22) |
| 17:16 PLLP | PLLP[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 |
RCC_CFGR — Clock Configuration Register
Address: 0x40023808. Selects the system clock source and sets AHB/APB prescalers. Also controls MCO (Master Clock Output) pins.
Bit and field descriptions
| Bit / Field | Description |
|---|---|
| 20:16 RTCPRE | RTCPRE[4:0] bits 20:16 |
| 15:13 PPRE2 | PPRE2[2:0] bits 15:13 |
| 12:10 PPRE1 | PPRE1[2:0] bits 12:10 |
| 7:4 HPRE | HPRE[3:0] bits 7:4 |
| 3:2 SWS | SWS[1:0] bits 3:2 |
| 1:0 SW | SW[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:
| Register | Address | Bus | Controls |
|---|---|---|---|
| RCC_AHB1ENR | 0x40023830 | AHB1 | GPIO A-I, DMA1/2, CRC, BKPSRAM, Flash, CCMDATARAM, ETH, USB OTG HS |
| RCC_AHB2ENR | 0x40023834 | AHB2 | USB OTG FS, DCMI, Crypto, Hash, RNG |
| RCC_AHB3ENR | 0x40023838 | AHB3 | FSMC (external memory) |
| RCC_APB1ENR | 0x40023840 | APB1 | TIM2-7,12-14, WWDG, SPI2/3, USART2/3, UART4/5, I2C1/2/3, CAN1/2, PWR, DAC, RTC |
| RCC_APB2ENR | 0x40023844 | APB2 | TIM1,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! */
}
- 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

2 Comments