Skip to content

Commit 01ddcd5

Browse files
committed
[rcc] Move STM32F4 RCC to new API
1 parent 06a1618 commit 01ddcd5

15 files changed

Lines changed: 1704 additions & 476 deletions

File tree

src/modm/board/black_pill_f411/board.hpp

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,56 @@ using namespace modm::literals;
2626
/// STM32F4x1 running at 84MHz generated from the external 25MHz crystal
2727
struct SystemClock
2828
{
29-
static constexpr uint32_t Frequency = 84_MHz;
29+
static constexpr uint32_t Hse = 25_MHz;
30+
static constexpr uint32_t Lse = 32.768_kHz;
31+
static constexpr Rcc::PllConfig pll
32+
{
33+
.M = 25, // 25 MHz / 25 = 1 MHz
34+
.N = 336, // 1 MHz * 336 = 336 MHz
35+
.P = 4, // 336 MHz / 4 = 84 MHz = F_cpu
36+
.Q = 7, // 336 MHz / 7 = 48 MHz = F_usb
37+
};
38+
static constexpr uint32_t PllP = Hse / pll.M * pll.N / pll.P;
39+
static constexpr uint32_t PllQ = Hse / pll.M * pll.N / pll.Q;
40+
static_assert(PllP == Rcc::MaxFrequency);
41+
42+
static constexpr uint32_t Frequency = PllP;
3043
static constexpr uint32_t Ahb = Frequency;
3144
static constexpr uint32_t Apb1 = Frequency / 2;
3245
static constexpr uint32_t Apb2 = Frequency;
33-
34-
static constexpr uint32_t Adc = Apb2;
35-
36-
static constexpr uint32_t Spi1 = Apb2;
46+
static constexpr uint32_t Ahb1 = Ahb;
47+
48+
static constexpr uint32_t Crc = Ahb1;
49+
static constexpr uint32_t Dma1 = Ahb1;
50+
static constexpr uint32_t Dma2 = Ahb1;
51+
static constexpr uint32_t Flash = Ahb1;
52+
53+
static constexpr uint32_t I2c1 = Apb1;
54+
static constexpr uint32_t I2c2 = Apb1;
55+
static constexpr uint32_t I2c3 = Apb1;
56+
static constexpr uint32_t I2s2Ext = Apb1;
57+
static constexpr uint32_t I2s3Ext = Apb1;
58+
static constexpr uint32_t IwdgBus = Apb1;
59+
static constexpr uint32_t Pwr = Apb1;
60+
static constexpr uint32_t RtcBus = Apb1;
3761
static constexpr uint32_t Spi2 = Apb1;
3862
static constexpr uint32_t Spi3 = Apb1;
63+
static constexpr uint32_t Usart2 = Apb1;
64+
static constexpr uint32_t Wwdg = Apb1;
65+
66+
static constexpr uint32_t Adc = Apb2;
67+
static constexpr uint32_t Adc1Common = Apb2;
68+
static constexpr uint32_t Exti = Apb2;
69+
static constexpr uint32_t Sdio = Apb2;
70+
static constexpr uint32_t Spi1 = Apb2;
3971
static constexpr uint32_t Spi4 = Apb2;
4072
static constexpr uint32_t Spi5 = Apb2;
41-
73+
static constexpr uint32_t Syscfg = Apb2;
4274
static constexpr uint32_t Usart1 = Apb2;
43-
static constexpr uint32_t Usart2 = Apb1;
4475
static constexpr uint32_t Usart6 = Apb2;
4576

46-
static constexpr uint32_t I2c1 = Apb1;
47-
static constexpr uint32_t I2c2 = Apb1;
48-
static constexpr uint32_t I2c3 = Apb1;
49-
5077
static constexpr uint32_t Apb1Timer = Apb1 * 2;
51-
static constexpr uint32_t Apb2Timer = Apb2 * 1;
78+
static constexpr uint32_t Apb2Timer = Apb2;
5279
static constexpr uint32_t Timer1 = Apb2Timer;
5380
static constexpr uint32_t Timer2 = Apb1Timer;
5481
static constexpr uint32_t Timer3 = Apb1Timer;
@@ -58,37 +85,26 @@ struct SystemClock
5885
static constexpr uint32_t Timer10 = Apb2Timer;
5986
static constexpr uint32_t Timer11 = Apb2Timer;
6087

61-
static constexpr uint32_t Usb = 48_MHz;
88+
static constexpr uint32_t Usb = PllQ;
6289
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
63-
static constexpr uint32_t Rtc = 32.768_kHz;
90+
static constexpr uint32_t Rtc = Lse;
6491

6592
static bool inline
6693
enable()
6794
{
68-
Rcc::enableLowSpeedExternalCrystal();
69-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
70-
71-
Rcc::enableExternalCrystal();
72-
const Rcc::PllFactors pllFactors{
73-
.pllM = 25, // 25MHz / M=25 -> 1MHz
74-
.pllN = 336, // 1MHz * N=336 -> 336MHz
75-
.pllP = 4, // 336MHz / P=4 -> 84MHz = F_cpu
76-
.pllQ = 7, // 336MHz / Q=7 -> 48MHz = F_usb
77-
};
78-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
79-
80-
// set flash latency
81-
Rcc::setFlashLatency<Frequency>();
95+
Rcc::enableLseCrystal();
96+
Rcc::enableHseCrystal();
8297

83-
// switch system clock to PLL output
84-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
98+
Rcc::setFlashLatency<Frequency>();
99+
Rcc::updateCoreFrequency<Frequency>();
85100

86101
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
87-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
88-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
102+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
103+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
89104

90-
// update frequencies for busy-wait delay functions
91-
Rcc::updateCoreFrequency<Frequency>();
105+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
106+
Rcc::enableSystemClock(Rcc::SystemClockSource::PllP);
107+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
92108

93109
return true;
94110
}

src/modm/board/devebox_stm32f4xx/board.hpp

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,76 @@ using namespace modm::literals;
3131
/// STM32F407 running at 168MHz generated from the external 8MHz crystal
3232
struct SystemClock
3333
{
34-
static constexpr uint32_t Frequency = 168_MHz;
34+
static constexpr uint32_t Hse = 8_MHz;
35+
static constexpr uint32_t Lse = 32.768_kHz;
36+
static constexpr Rcc::PllConfig pll
37+
{
38+
.M = 4, // 8 MHz / 4 = 2 MHz
39+
.N = 168, // 2 MHz * 168 = 336 MHz
40+
.P = 2, // 336 MHz / 2 = 168 MHz = F_cpu
41+
.Q = 7, // 336 MHz / 7 = 48 MHz
42+
};
43+
static constexpr uint32_t PllP = Hse / pll.M * pll.N / pll.P;
44+
static constexpr uint32_t PllQ = Hse / pll.M * pll.N / pll.Q;
45+
46+
static constexpr uint32_t Frequency = PllP;
47+
static_assert(Frequency == Rcc::MaxFrequency);
48+
3549
static constexpr uint32_t Ahb = Frequency;
3650
static constexpr uint32_t Apb1 = Frequency / 4;
3751
static constexpr uint32_t Apb2 = Frequency / 2;
52+
static constexpr uint32_t Ahb1 = Ahb;
53+
static constexpr uint32_t Ahb2 = Ahb;
54+
55+
static constexpr uint32_t Crc = Ahb1;
56+
static constexpr uint32_t Dma1 = Ahb1;
57+
static constexpr uint32_t Dma2 = Ahb1;
58+
static constexpr uint32_t Eth = Ahb1;
59+
static constexpr uint32_t Flash = Ahb1;
60+
static constexpr uint32_t GpioA = Ahb1;
61+
static constexpr uint32_t GpioB = Ahb1;
62+
static constexpr uint32_t GpioC = Ahb1;
63+
static constexpr uint32_t GpioD = Ahb1;
64+
static constexpr uint32_t GpioE = Ahb1;
65+
static constexpr uint32_t GpioF = Ahb1;
66+
static constexpr uint32_t GpioG = Ahb1;
67+
static constexpr uint32_t GpioH = Ahb1;
68+
static constexpr uint32_t GpioI = Ahb1;
69+
static constexpr uint32_t RccBus = Ahb1;
70+
71+
static constexpr uint32_t Dcmi = Ahb2;
72+
static constexpr uint32_t Rng = Ahb2;
73+
74+
static constexpr uint32_t Can1 = Apb1;
75+
static constexpr uint32_t Can2 = Apb1;
76+
static constexpr uint32_t Dac = Apb1;
77+
static constexpr uint32_t I2c1 = Apb1;
78+
static constexpr uint32_t I2c2 = Apb1;
79+
static constexpr uint32_t I2c3 = Apb1;
80+
static constexpr uint32_t I2s2Ext = Apb1;
81+
static constexpr uint32_t I2s3Ext = Apb1;
82+
static constexpr uint32_t IwdgBus = Apb1;
83+
static constexpr uint32_t Pwr = Apb1;
84+
static constexpr uint32_t RtcBus = Apb1;
85+
static constexpr uint32_t Spi2 = Apb1;
86+
static constexpr uint32_t Spi3 = Apb1;
87+
static constexpr uint32_t Usart2 = Apb1;
88+
static constexpr uint32_t Usart3 = Apb1;
89+
static constexpr uint32_t Uart4 = Apb1;
90+
static constexpr uint32_t Uart5 = Apb1;
91+
static constexpr uint32_t Wwdg = Apb1;
3892

3993
static constexpr uint32_t Adc = Apb2;
40-
41-
static constexpr uint32_t Can1 = Apb1;
42-
static constexpr uint32_t Can2 = Apb1;
43-
44-
static constexpr uint32_t Spi1 = Apb2;
45-
static constexpr uint32_t Spi2 = Apb1;
46-
static constexpr uint32_t Spi3 = Apb1;
47-
static constexpr uint32_t Spi4 = Apb2;
48-
static constexpr uint32_t Spi5 = Apb2;
49-
static constexpr uint32_t Spi6 = Apb2;
50-
94+
static constexpr uint32_t Adc1 = Apb2;
95+
static constexpr uint32_t Adc2 = Apb2;
96+
static constexpr uint32_t Adc3 = Apb2;
97+
static constexpr uint32_t Adc123Common = Apb2;
98+
static constexpr uint32_t Exti = Apb2;
99+
static constexpr uint32_t Sdio = Apb2;
100+
static constexpr uint32_t Spi1 = Apb2;
101+
static constexpr uint32_t Syscfg = Apb2;
51102
static constexpr uint32_t Usart1 = Apb2;
52-
static constexpr uint32_t Usart2 = Apb1;
53-
static constexpr uint32_t Usart3 = Apb1;
54-
static constexpr uint32_t Uart4 = Apb1;
55-
static constexpr uint32_t Uart5 = Apb1;
56103
static constexpr uint32_t Usart6 = Apb2;
57-
static constexpr uint32_t Uart7 = Apb1;
58-
static constexpr uint32_t Uart8 = Apb1;
59-
60-
static constexpr uint32_t I2c1 = Apb1;
61-
static constexpr uint32_t I2c2 = Apb1;
62-
static constexpr uint32_t I2c3 = Apb1;
63104

64105
static constexpr uint32_t Apb1Timer = Apb1 * 2;
65106
static constexpr uint32_t Apb2Timer = Apb2 * 2;
@@ -77,34 +118,28 @@ struct SystemClock
77118
static constexpr uint32_t Timer12 = Apb1Timer;
78119
static constexpr uint32_t Timer13 = Apb1Timer;
79120
static constexpr uint32_t Timer14 = Apb1Timer;
121+
122+
static constexpr uint32_t Usb = PllQ;
80123
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
81-
static constexpr uint32_t Rtc = 32.768_kHz;
124+
static constexpr uint32_t Rtc = Lse;
82125

83126
static bool inline
84127
enable()
85128
{
86-
Rcc::enableLowSpeedExternalCrystal();
87-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
88-
89-
Rcc::enableExternalCrystal(); // 8MHz
90-
const Rcc::PllFactors pllFactors{
91-
.pllM = 4, // 8MHz / M=4 -> 2MHz
92-
.pllN = 168, // 2MHz * N=168 -> 336MHz
93-
.pllP = 2 // 336MHz / P=2 -> 168MHz = F_cpu
94-
};
95-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
96-
// set flash latency for 168MHz
129+
Rcc::enableLseCrystal();
130+
Rcc::enableHseCrystal();
131+
97132
Rcc::setFlashLatency<Frequency>();
98-
// switch system clock to PLL output
99-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
100-
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
101-
// APB1 has max. 42MHz
102-
// APB2 has max. 84MHz
103-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div4);
104-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div2);
105-
// update frequencies for busy-wait delay functions
106133
Rcc::updateCoreFrequency<Frequency>();
107134

135+
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
136+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div4);
137+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div2);
138+
139+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
140+
Rcc::enableSystemClock(Rcc::SystemClockSource::PllP);
141+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
142+
108143
return true;
109144
}
110145
};

src/modm/board/disco_f401vc/board.hpp

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,59 @@ using namespace modm::literals;
3737
struct SystemClock
3838
{
3939
static constexpr uint32_t Hse = 8_MHz;
40-
static constexpr uint32_t Frequency = 84_MHz;
40+
static constexpr Rcc::PllConfig pll
41+
{
42+
.M = 4, // 8 MHz / 4 = 2 MHz
43+
.N = 168, // 2 MHz * 168 = 336 MHz
44+
.P = 4, // 336 MHz / 4 = 84 MHz = F_cpu
45+
.Q = 7, // 336 MHz / 7 = 48 MHz
46+
};
47+
static constexpr uint32_t PllP = Hse / pll.M * pll.N / pll.P;
48+
static constexpr uint32_t PllQ = Hse / pll.M * pll.N / pll.Q;
49+
50+
static constexpr uint32_t Frequency = PllP;
51+
static_assert(Frequency == Rcc::MaxFrequency);
52+
4153
static constexpr uint32_t Ahb = Frequency;
4254
static constexpr uint32_t Apb1 = Frequency / 4;
4355
static constexpr uint32_t Apb2 = Frequency / 2;
56+
static constexpr uint32_t Ahb1 = Ahb;
57+
58+
static constexpr uint32_t Crc = Ahb1;
59+
static constexpr uint32_t Dma1 = Ahb1;
60+
static constexpr uint32_t Dma2 = Ahb1;
61+
static constexpr uint32_t Flash = Ahb1;
62+
static constexpr uint32_t GpioA = Ahb1;
63+
static constexpr uint32_t GpioB = Ahb1;
64+
static constexpr uint32_t GpioC = Ahb1;
65+
static constexpr uint32_t GpioD = Ahb1;
66+
static constexpr uint32_t GpioE = Ahb1;
67+
static constexpr uint32_t GpioH = Ahb1;
68+
static constexpr uint32_t RccBus = Ahb1;
69+
70+
static constexpr uint32_t I2c1 = Apb1;
71+
static constexpr uint32_t I2c2 = Apb1;
72+
static constexpr uint32_t I2c3 = Apb1;
73+
static constexpr uint32_t I2s2Ext = Apb1;
74+
static constexpr uint32_t I2s3Ext = Apb1;
75+
static constexpr uint32_t IwdgBus = Apb1;
76+
static constexpr uint32_t Pwr = Apb1;
77+
static constexpr uint32_t RtcBus = Apb1;
78+
static constexpr uint32_t Spi2 = Apb1;
79+
static constexpr uint32_t Spi3 = Apb1;
80+
static constexpr uint32_t Usart2 = Apb1;
81+
static constexpr uint32_t Wwdg = Apb1;
4482

4583
static constexpr uint32_t Adc = Apb2;
46-
47-
static constexpr uint32_t Spi1 = Apb2;
48-
static constexpr uint32_t Spi2 = Apb1;
49-
static constexpr uint32_t Spi3 = Apb1;
50-
static constexpr uint32_t Spi4 = Apb2;
51-
84+
static constexpr uint32_t Adc1 = Apb2;
85+
static constexpr uint32_t Adc1Common = Apb2;
86+
static constexpr uint32_t Exti = Apb2;
87+
static constexpr uint32_t Sdio = Apb2;
88+
static constexpr uint32_t Spi1 = Apb2;
89+
static constexpr uint32_t Spi4 = Apb2;
90+
static constexpr uint32_t Syscfg = Apb2;
5291
static constexpr uint32_t Usart1 = Apb2;
53-
static constexpr uint32_t Usart2 = Apb1;
54-
static constexpr uint32_t Usart6 = Apb1;
55-
56-
static constexpr uint32_t I2c1 = Apb1;
57-
static constexpr uint32_t I2c2 = Apb1;
58-
static constexpr uint32_t I2c3 = Apb1;
92+
static constexpr uint32_t Usart6 = Apb2;
5993

6094
static constexpr uint32_t Apb1Timer = Apb1 * 2;
6195
static constexpr uint32_t Apb2Timer = Apb2 * 2;
@@ -71,35 +105,26 @@ struct SystemClock
71105
static constexpr uint32_t Timer10 = Apb2Timer;
72106
static constexpr uint32_t Timer11 = Apb2Timer;
73107

74-
static constexpr uint32_t Usb = 48_MHz;
108+
static constexpr uint32_t Usb = PllQ;
75109
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
76110
static constexpr uint32_t Rtc = Hse / 25;
77111

78112
static bool inline
79113
enable()
80114
{
81-
Rcc::enableExternalCrystal(); // 8MHz
82-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::ExternalClock, 25);
83-
84-
const Rcc::PllFactors pllFactors{
85-
.pllM = 4, // 8MHz / M=4 -> 2MHz
86-
.pllN = 168, // 2MHz * N=168 -> 336MHz
87-
.pllP = 4, // 336MHz / P=4 -> 84MHz = F_cpu
88-
.pllQ = 7 // 336MHz / Q=7 -> 48MHz = F_usb
89-
};
90-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
91-
// set flash latency for 84MHz
115+
Rcc::enableHseCrystal();
116+
92117
Rcc::setFlashLatency<Frequency>();
93-
// switch system clock to PLL output
94-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
95-
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
96-
// APB1 has max. 42MHz
97-
// APB2 has max. 84MHz
98-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div4);
99-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div2);
100-
// update frequencies for busy-wait delay functions
101118
Rcc::updateCoreFrequency<Frequency>();
102119

120+
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
121+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div4);
122+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div2);
123+
124+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
125+
Rcc::enableSystemClock(Rcc::SystemClockSource::PllP);
126+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Hse, 25);
127+
103128
return true;
104129
}
105130
};

0 commit comments

Comments
 (0)