@@ -27,7 +27,12 @@ using namespace modm::literals;
2727// / STM32F103 running at 72MHz generated from the external 8MHz crystal
2828struct SystemClock
2929{
30- static constexpr uint32_t Frequency = 72_MHz;
30+ static constexpr uint32_t Hse = 8_MHz;
31+ static constexpr Rcc::PllConfig pll{.mul = 9 , .usbdiv = Rcc::UsbPrescaler::Div1_5};
32+ static constexpr uint32_t Pll = Hse * pll.mul;
33+ static constexpr uint32_t Frequency = Pll;
34+ static_assert (Frequency == Rcc::MaxFrequency);
35+
3136 static constexpr uint32_t Ahb = Frequency;
3237 static constexpr uint32_t Apb1 = Frequency / 2 ;
3338 static constexpr uint32_t Apb2 = Frequency;
@@ -56,42 +61,26 @@ struct SystemClock
5661 static constexpr uint32_t Timer3 = Apb1Timer;
5762 static constexpr uint32_t Timer4 = Apb1Timer;
5863
59- static constexpr uint32_t Usb = Ahb / 1.5 ;
64+ static constexpr uint32_t Usb = Ahb * 2 / 3 ;
6065 static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
6166 static constexpr uint32_t Rtc = 32 .768_kHz;
6267
6368 static bool inline
6469 enable ()
6570 {
66- Rcc::enableLowSpeedExternalCrystal ();
67- Rcc::enableRealTimeClock (Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
68-
69- Rcc::enableExternalCrystal ();
70-
71- // external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
72- const Rcc::PllFactors pllFactors{
73- .pllMul = 9 ,
74- .usbPrediv = Rcc::UsbPrescaler::Div1_5
75- };
76- Rcc::enablePll (Rcc::PllSource::ExternalCrystal, pllFactors);
71+ Rcc::enableLseCrystal ();
72+ Rcc::enableHseClock ();
7773
78- // set flash latency for 72MHz
7974 Rcc::setFlashLatency<Frequency>();
75+ Rcc::updateCoreFrequency<Frequency>();
8076
81- // switch system clock to PLL output
82- Rcc::enableSystemClock (Rcc::SystemClockSource::Pll);
83-
84- // AHB has max 72MHz
8577 Rcc::setAhbPrescaler (Rcc::AhbPrescaler::Div1);
78+ Rcc::setApb1Prescaler (Rcc::ApbPrescaler::Div2);
79+ Rcc::setApb2Prescaler (Rcc::ApbPrescaler::Div1);
8680
87- // APB1 has max. 36MHz
88- Rcc::setApb1Prescaler (Rcc::Apb1Prescaler::Div2);
89-
90- // APB2 has max. 72MHz
91- Rcc::setApb2Prescaler (Rcc::Apb2Prescaler::Div1);
92-
93- // update frequencies for busy-wait delay functions
94- Rcc::updateCoreFrequency<Frequency>();
81+ Rcc::enablePll (Rcc::PllSource::Hse, pll);
82+ Rcc::enableSystemClock (Rcc::SystemClockSource::Pll);
83+ Rcc::setRealTimeClockSource (Rcc::RealTimeClockSource::Lse);
9584
9685 return true ;
9786 }
0 commit comments