@@ -22,14 +22,34 @@ namespace Board
2222// / @{
2323using  namespace  modm ::literals; 
2424
25- // / STM32F411 running at 96MHz generated from the external 8MHz crystal
2625struct  SystemClock 
2726{
28- 	static  constexpr  uint32_t  Frequency = 96_MHz;
27+ 	static  constexpr  uint32_t  ExternalCrystalClock = 8_MHz;
28+ 	static  constexpr  Rcc::PllFactors pllFactors{
29+ 		.pllM  = 7 ,
30+ 		.pllN  = 336 ,
31+ 		.pllP  = 4 ,
32+ 		.pllQ  = 8 ,
33+ 	};
34+ 	static  constexpr  uint32_t  MainPllClock = ExternalCrystalClock / pllFactors.pllM * pllFactors.pllN;
35+ 
36+ 	static  constexpr  Rcc::AhbPrescaler Ahb_prescaler = Rcc::AhbPrescaler::Div1;
37+ 	static  constexpr  Rcc::Apb1Prescaler Apb1_prescaler = Rcc::Apb1Prescaler::Div2;
38+ 	static  constexpr  Rcc::Apb2Prescaler Apb2_prescaler = Rcc::Apb2Prescaler::Div1;
39+ 
40+ 	//  ------------------------------------------
41+ 
42+ 	static  constexpr  uint32_t  Frequency = MainPllClock / pllFactors.pllP; //  96_Mhz
43+ 	static  constexpr  uint32_t  Usb = MainPllClock / pllFactors.pllQ; //  48_Mhz
44+ 
2945	static  constexpr  uint32_t  Ahb = Frequency;
3046	static  constexpr  uint32_t  Apb1 = Frequency / 2 ;
3147	static  constexpr  uint32_t  Apb2 = Frequency;
3248
49+ 	//  @todo find the right place
50+ 	static_assert (Apb1 <= 50_MHz, " Apb1 has max. 50MHz!" 
51+ 	static_assert (Apb2 <= 100_MHz, " Apb2 has max. 100MHz!" 
52+ 
3353	static  constexpr  uint32_t  Adc = Apb2;
3454
3555	static  constexpr  uint32_t  Spi1 = Apb2;
@@ -62,27 +82,18 @@ struct SystemClock
6282	static  constexpr  uint32_t  Timer10 = Apb2Timer;
6383	static  constexpr  uint32_t  Timer11 = Apb2Timer;
6484
65- 	static  constexpr  uint32_t  Usb = 48_MHz;
66- 
6785	static  bool  inline  enable ()
6886	{
69- 		Rcc::enableExternalCrystal ();  //  8MHz
70- 		const  Rcc::PllFactors pllFactors{
71- 			.pllM  = 7 ,    //  8MHz / M=7 -> ~1.14MHz
72- 			.pllN  = 336 ,  //  1.14MHz * N=336 -> 384MHz
73- 			.pllP  = 4 ,    //  384MHz / P=4 -> 96MHz = F_cpu
74- 			.pllQ  = 8 ,    //  384MHz / P=8 -> 48MHz = F_usb
75- 		};
87+ 		// / STM32F411 running at 84MHz generated from the external 8MHz crystal
88+ 		Rcc::enableExternalCrystal ();
7689		Rcc::enablePll (Rcc::PllSource::ExternalCrystal, pllFactors);
7790		//  set flash latency for 100MHz
7891		Rcc::setFlashLatency<Frequency>();
7992		//  switch system clock to PLL output
8093		Rcc::enableSystemClock (Rcc::SystemClockSource::Pll);
81- 		Rcc::setAhbPrescaler (Rcc::AhbPrescaler::Div1);
82- 		//  APB1 has max. 50MHz
83- 		//  APB2 has max. 100MHz
84- 		Rcc::setApb1Prescaler (Rcc::Apb1Prescaler::Div2);
85- 		Rcc::setApb2Prescaler (Rcc::Apb2Prescaler::Div1);
94+ 		Rcc::setAhbPrescaler (Ahb_prescaler);
95+ 		Rcc::setApb1Prescaler (Apb1_prescaler);
96+ 		Rcc::setApb2Prescaler (Apb2_prescaler);
8697		//  update frequencies for busy-wait delay functions
8798		Rcc::updateCoreFrequency<Frequency>();
8899
0 commit comments