14
14
#include < modm/driver/inertial/lis3dsh.hpp>
15
15
#include < modm/platform.hpp>
16
16
17
+ #include " rcc_prototype.hpp"
18
+
17
19
using namespace modm ::platform;
18
20
19
21
namespace Board
@@ -22,13 +24,49 @@ namespace Board
22
24
// / @{
23
25
using namespace modm ::literals;
24
26
25
- // / STM32F411 running at 96MHz generated from the external 8MHz crystal
27
+
26
28
struct SystemClock
27
29
{
28
- static constexpr uint32_t Frequency = 96_MHz;
29
- static constexpr uint32_t Ahb = Frequency;
30
- static constexpr uint32_t Apb1 = Frequency / 2 ;
31
- static constexpr uint32_t Apb2 = Frequency;
30
+ static constexpr uint32_t ExternalCrystalClock = 8_MHz;
31
+
32
+ static constexpr Rcc::PllFactors pllFactors{
33
+ .pllM = 8 ,
34
+ .pllN = 336 ,
35
+ .pllP = 4 ,
36
+ .pllQ = 7 ,
37
+ };
38
+ static constexpr Rcc::AhbPrescaler Ahb_pre = Rcc::AhbPrescaler::Div1;
39
+ static constexpr Rcc::Apb1Prescaler Apb1_pre = Rcc::Apb1Prescaler::Div2;
40
+ static constexpr Rcc::Apb2Prescaler Apb2_pre = Rcc::Apb2Prescaler::Div1;
41
+
42
+ // ------------------------------------------
43
+
44
+ static constexpr uint32_t MainPllClock = ExternalCrystalClock / pllFactors.pllM * pllFactors.pllN;
45
+ static constexpr uint32_t SystemFrequency = MainPllClock / pllFactors.pllP; // 96_Mhz
46
+
47
+ static constexpr uint32_t Ahb = SystemFrequency / RccProto::prescalerToValue<Ahb_pre>();
48
+ static constexpr uint32_t Apb1 = Ahb / RccProto::prescalerToValue<Apb1_pre>();
49
+ static constexpr uint32_t Apb2 = Ahb / RccProto::prescalerToValue<Apb2_pre>();
50
+
51
+ // @todo find a nice home for these
52
+ static_assert (SystemFrequency <= 100_MHz, " SystemFrequency has max. 100MHz!" );
53
+ static_assert (Apb1 <= 50_MHz, " Apb1 has max. 50MHz!" );
54
+ static_assert (Apb2 <= 100_MHz, " Apb2 has max. 100MHz!" );
55
+
56
+ // @todo is this correct?
57
+ // prescaler is one ? -> multiply by one, otherwise multiply by two
58
+ static constexpr uint32_t Apb1Timer = Apb1 * (RccProto::prescalerToValue<Apb1_pre>() == 1 ? 1 : 2 );
59
+ static constexpr uint32_t Apb2Timer = Apb2 * (RccProto::prescalerToValue<Apb2_pre>() == 1 ? 1 : 2 );
60
+
61
+ // static_assert(Ahb == 84_MHz, "Wrong");
62
+ // static_assert(Apb1 == 42_MHz, "Wrong");
63
+ // static_assert(Apb2 == 84_MHz, "Wrong");
64
+ // static_assert(Apb1Timer == 84_MHz, "Wrong");
65
+ // static_assert(Apb2Timer == 84_MHz, "Wrong");
66
+
67
+ // ------------------------------------------
68
+
69
+ static constexpr uint32_t Frequency = Ahb;
32
70
33
71
static constexpr uint32_t Adc = Apb2;
34
72
@@ -51,8 +89,6 @@ struct SystemClock
51
89
static constexpr uint32_t I2c2 = Apb1;
52
90
static constexpr uint32_t I2c3 = Apb1;
53
91
54
- static constexpr uint32_t Apb1Timer = Apb1 * 2 ;
55
- static constexpr uint32_t Apb2Timer = Apb2 * 2 ;
56
92
static constexpr uint32_t Timer1 = Apb2Timer;
57
93
static constexpr uint32_t Timer2 = Apb1Timer;
58
94
static constexpr uint32_t Timer3 = Apb1Timer;
@@ -62,27 +98,20 @@ struct SystemClock
62
98
static constexpr uint32_t Timer10 = Apb2Timer;
63
99
static constexpr uint32_t Timer11 = Apb2Timer;
64
100
65
- static constexpr uint32_t Usb = 48_MHz;
101
+ static constexpr uint32_t Usb = MainPllClock / pllFactors.pllQ; // 48_Mhz
66
102
67
103
static bool inline enable ()
68
104
{
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
- };
105
+ // / STM32F411 running at 84MHz generated from the external 8MHz crystal
106
+ Rcc::enableExternalCrystal ();
76
107
Rcc::enablePll (Rcc::PllSource::ExternalCrystal, pllFactors);
77
108
// set flash latency for 100MHz
78
109
Rcc::setFlashLatency<Frequency>();
79
110
// switch system clock to PLL output
80
111
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);
112
+ Rcc::setAhbPrescaler (Ahb_pre);
113
+ Rcc::setApb1Prescaler (Apb1_pre);
114
+ Rcc::setApb2Prescaler (Apb2_pre);
86
115
// update frequencies for busy-wait delay functions
87
116
Rcc::updateCoreFrequency<Frequency>();
88
117
@@ -188,7 +217,7 @@ initializeLis3()
188
217
lis3::Cs::setOutput (modm::Gpio::High);
189
218
190
219
lis3::SpiMaster::connect<lis3::Sck::Sck, lis3::Mosi::Mosi, lis3::Miso::Miso>();
191
- lis3::SpiMaster::initialize<SystemClock, 6_MHz >();
220
+ lis3::SpiMaster::initialize<SystemClock, 5 .25_MHz >();
192
221
lis3::SpiMaster::setDataMode (lis3::SpiMaster::DataMode::Mode3);
193
222
}
194
223
0 commit comments