Skip to content

Commit e27bf55

Browse files
f-brooli-obk
authored andcommitted
Improve documentation
1 parent 912eeba commit e27bf55

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/system_clock.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,21 @@ pub fn init(rcc: &mut Rcc, pwr: &mut Pwr, flash: &mut Flash) {
5050
rcc.cr.update(|r| r.set_pllon(false));
5151
while rcc.cr.read().pllrdy() {}
5252
// Configure the main PLL clock source, multiplication and division factors.
53+
// HSE is used as clock source. HSE runs at 25 MHz.
54+
// PLLM = 25: Division factor for the main PLLs (PLL, PLLI2S and PLLSAI) input clock
55+
// VCO input frequency = PLL input clock frequency / PLLM with 2 ≤ PLLM ≤ 63
56+
// => VCO input frequency = 25_000 kHz / 25 = 1_000 kHz = 1 MHz
57+
// PPLM = 432: Main PLL (PLL) multiplication factor for VCO
58+
// VCO output frequency = VCO input frequency × PLLN with 50 ≤ PLLN ≤ 432
59+
// => VCO output frequency 1 Mhz * 432 = 432 MHz
60+
// PPLQ = 0 =^= division factor 2: Main PLL (PLL) division factor for main system clock
61+
// PLL output clock frequency = VCO frequency / PLLP with PLLP = 2, 4, 6, or 8
62+
// => PLL output clock frequency = 432 MHz / 2 = 216 MHz
5363
rcc.pllcfgr.update(|r| {
5464
r.set_pllsrc(true); // HSE
5565
r.set_pllm(25);
5666
r.set_plln(432); // 400 for 200 MHz, 432 for 216 MHz(don't forget to update `get_frequency`)
57-
r.set_pllp(0); // 0 == division factor 2
67+
r.set_pllp(0); // 0 =^= division factor 2
5868
r.set_pllq(9); // 8 for 200 MHz, 9 for 216 MHz
5969
});
6070
// enable main PLL
@@ -78,6 +88,8 @@ pub fn init(rcc: &mut Rcc, pwr: &mut Pwr, flash: &mut Flash) {
7888
const SYSTEM_CLOCK_PLL: u8 = 0b10;
7989

8090
// HCLK Configuration
91+
// HPRE = system clock not divided: AHB prescaler
92+
// => AHB clock frequency = system clock / 1 = 216 MHz / 1 = 216 MHz
8193
rcc.cfgr.update(|r| r.set_hpre(NO_DIVIDE));
8294
// SYSCLK Configuration
8395
rcc.cfgr.update(|r| r.set_sw(SYSTEM_CLOCK_PLL));
@@ -87,8 +99,14 @@ pub fn init(rcc: &mut Rcc, pwr: &mut Pwr, flash: &mut Flash) {
8799
const DIVIDE_4: u8 = 0b101;
88100

89101
// PCLK1 Configuration
102+
// PPRE1: APB Low-speed prescaler (APB1)
103+
// => APB low-speed clock frequency = 216 Mhz / 4 = 54 MHz
104+
// FIXME: Frequency should not exceed 45 MHz
90105
rcc.cfgr.update(|r| r.set_ppre1(DIVIDE_4));
91106
// PCLK2 Configuration
107+
// PPRE2: APB high-speed prescaler (APB2)
108+
// => APB high-speed clock frequency = 216 Mhz / 2 = 108 MHz
109+
// FIXME: Frequency should not exceed 90 MHz
92110
rcc.cfgr.update(|r| r.set_ppre2(DIVIDE_2));
93111

94112

@@ -98,7 +116,9 @@ pub fn init(rcc: &mut Rcc, pwr: &mut Pwr, flash: &mut Flash) {
98116
let pllm = u32::from(pll_cfgr.pllm());
99117
let plln = u32::from(pll_cfgr.plln());
100118
let pllp = u32::from(pll_cfgr.pllp() + 1) * 2;
101-
systick.rvr.write(25 * 1000 / pllm * plln / pllp - 1); // hse runs at 25 MHz
119+
// SysTick Reload Value Register = ((25000/25) * 432) / 2 - 1 = 215_999
120+
// => SysTick interrupt tiggers every 1 ms
121+
systick.rvr.write((((25 * 1000) / pllm) * plln) / pllp - 1); // hse runs at 25 MHz
102122
systick.cvr.write(0); // clear
103123
systick.csr.write(0b111); // CLKSOURCE | TICKINT | ENABLE
104124

0 commit comments

Comments
 (0)