@@ -30,7 +30,11 @@ using namespace modm::literals;
3030// / STM32F303RE running at 64MHz generated from the internal 8MHz clock
3131struct SystemClock
3232{
33- static constexpr uint32_t Frequency = 64_MHz;
33+ static constexpr Rcc::PllConfig pll{.Mul = 16 };
34+ static constexpr uint32_t Pll = Rcc::HsiFrequency / 2 * pll.Mul;
35+ static_assert (Pll <= Rcc::MaxFrequency);
36+
37+ static constexpr uint32_t Frequency = Pll;
3438 static constexpr uint32_t Ahb = Frequency;
3539 static constexpr uint32_t Apb1 = Frequency / 2 ;
3640 static constexpr uint32_t Apb2 = Frequency;
@@ -48,9 +52,9 @@ struct SystemClock
4852 static constexpr uint32_t Usart3 = Apb1;
4953
5054 // I2C clock source is HSI by default
51- static constexpr uint32_t I2c1 = 8_MHz ;
52- static constexpr uint32_t I2c2 = 8_MHz ;
53- static constexpr uint32_t I2c3 = 8_MHz ;
55+ static constexpr uint32_t I2c1 = Rcc::HsiFrequency ;
56+ static constexpr uint32_t I2c2 = Rcc::HsiFrequency ;
57+ static constexpr uint32_t I2c3 = Rcc::HsiFrequency ;
5458
5559 static constexpr uint32_t Apb1Timer = Apb1 * 2 ;
5660 static constexpr uint32_t Apb2Timer = Apb2 * 1 ;
@@ -68,27 +72,21 @@ struct SystemClock
6872 static bool inline
6973 enable ()
7074 {
71- Rcc::enableLowSpeedExternalCrystal ();
72- Rcc::enableRealTimeClock (Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
73-
74- Rcc::enableInternalClock (); // 8MHz
75- // 8MHz / 2 * 16 = 64MHz
76- const Rcc::PllFactors pllFactors{
77- .pllMul = 16 ,
78- .pllPrediv = 2 ,
79- };
80- Rcc::enablePll (Rcc::PllSource::InternalClock, pllFactors);
81- // set flash latency for 64MHz
75+ Rcc::enableLseCrystal ();
76+ Rcc::enableHsiClock ();
77+
8278 Rcc::setFlashLatency<Frequency>();
83- // switch system clock to PLL output
84- Rcc::enableSystemClock (Rcc::SystemClockSource::Pll);
85- Rcc::setAhbPrescaler (Rcc::AhbPrescaler::Div1);
86- // APB1 has max. 36MHz
87- Rcc::setApb1Prescaler (Rcc::Apb1Prescaler::Div2);
88- Rcc::setApb2Prescaler (Rcc::Apb2Prescaler::Div1);
89- // update frequencies for busy-wait delay functions
9079 Rcc::updateCoreFrequency<Frequency>();
9180
81+ Rcc::setAhbPrescaler (Rcc::AhbPrescaler::Div1);
82+ Rcc::setApb1Prescaler (Rcc::ApbPrescaler::Div2);
83+ Rcc::setApb2Prescaler (Rcc::ApbPrescaler::Div1);
84+
85+ Rcc::enablePll (Rcc::PllSource::HsiDiv2, pll);
86+ Rcc::enableSystemClock (Rcc::SystemClockSource::Pll);
87+ Rcc::setRealTimeClockSource (Rcc::RealTimeClockSource::Lse);
88+
89+
9290 return true ;
9391 }
9492};
0 commit comments