@@ -22,14 +22,34 @@ namespace Board
22
22
// / @{
23
23
using namespace modm ::literals;
24
24
25
- // / STM32F411 running at 84MHz generated from the external 8MHz crystal
26
25
struct SystemClock
27
26
{
28
- static constexpr uint32_t Frequency = 84_MHz;
27
+ static constexpr uint32_t ExternalCrystalClock = 8_MHz;
28
+ static constexpr Rcc::PllFactors pllFactors{
29
+ .pllM = 8 ,
30
+ .pllN = 336 ,
31
+ .pllP = 4 ,
32
+ .pllQ = 7 ,
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; // 84_Mhz
43
+ static constexpr uint32_t Usb = MainPllClock / pllFactors.pllQ; // 48_Mhz
44
+
29
45
static constexpr uint32_t Ahb = Frequency;
30
46
static constexpr uint32_t Apb1 = Frequency / 2 ;
31
47
static constexpr uint32_t Apb2 = Frequency;
32
48
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
+
33
53
static constexpr uint32_t Adc = Apb2;
34
54
35
55
static constexpr uint32_t Spi1 = Apb2;
@@ -62,27 +82,18 @@ struct SystemClock
62
82
static constexpr uint32_t Timer10 = Apb2Timer;
63
83
static constexpr uint32_t Timer11 = Apb2Timer;
64
84
65
- static constexpr uint32_t Usb = 48_MHz;
66
-
67
85
static bool inline enable ()
68
86
{
69
- Rcc::enableExternalCrystal (); // 8MHz
70
- const Rcc::PllFactors pllFactors{
71
- .pllM = 8 , // 8MHz / M=8 -> 1MHz
72
- .pllN = 336 , // 1MHz * N=336 -> 84MHz
73
- .pllP = 4 , // 336MHz / P=4 -> 100MHz = F_cpu
74
- .pllQ = 7 , // 336MHz / P=7 -> 100MHz = F_cpu
75
- };
87
+ // / STM32F411 running at 84MHz generated from the external 8MHz crystal
88
+ Rcc::enableExternalCrystal ();
76
89
Rcc::enablePll (Rcc::PllSource::ExternalCrystal, pllFactors);
77
90
// set flash latency for 100MHz
78
91
Rcc::setFlashLatency<Frequency>();
79
92
// switch system clock to PLL output
80
93
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);
86
97
// update frequencies for busy-wait delay functions
87
98
Rcc::updateCoreFrequency<Frequency>();
88
99
0 commit comments