Skip to content

Commit 6181c54

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

File tree

20 files changed

+1781
-717
lines changed

20 files changed

+1781
-717
lines changed

src/modm/board/black_pill_f401/module.lb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ def build(env):
5050
env.outbasepath = "modm/src/modm/board"
5151
env.substitutions = {
5252
"with_logger": False,
53-
"with_assert": env.has_module(":architecture:assert")
53+
"with_assert": env.has_module(":architecture:assert"),
54+
"target": env[":target"].identifier,
5455
}
5556
env.template("../board.cpp.in", "board.cpp")
56-
env.copy('../black_pill_f411/board.hpp', 'board.hpp')
57+
env.template("../black_pill_f411/board.hpp.in", "board.hpp")
5758

5859
env.outbasepath = "modm/openocd/modm/board/"
5960
env.template(repopath("tools/openocd/modm/stm32_swd.cfg.in"), "board.cfg",

src/modm/board/black_pill_f411/board.hpp renamed to src/modm/board/black_pill_f411/board.hpp.in

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
*/
1010
// ----------------------------------------------------------------------------
1111

12-
#ifndef MODM_STM32_F4X1CEU_MINI_F4X1_HPP
13-
#define MODM_STM32_F4X1CEU_MINI_F4X1_HPP
12+
#pragma once
1413

1514
#include <modm/platform.hpp>
1615
#include <modm/architecture/interface/clock.hpp>
@@ -19,36 +18,69 @@ using namespace modm::platform;
1918

2019
namespace Board
2120
{
22-
/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
21+
/// @ingroup modm_board_black_pill_f4{{target.name}}
2322
/// @{
2423
using namespace modm::literals;
2524

26-
/// STM32F4x1 running at 84MHz generated from the external 25MHz crystal
25+
/// STM32F4{{target.name}} running at {% if target.name == "11" %}96{% else %}84{% endif %}MHz generated from the external 25MHz crystal
2726
struct SystemClock
2827
{
29-
static constexpr uint32_t Frequency = 84_MHz;
28+
static constexpr uint32_t Hse = 25_MHz;
29+
static constexpr uint32_t Lse = 32.768_kHz;
30+
static constexpr Rcc::PllConfig pll
31+
{
32+
.M = 25, // 25 MHz / 25 = 1 MHz
33+
%% if target.name == "11"
34+
.N = 384, // 1 MHz * 384 = 384 MHz
35+
.P = 4, // 384 MHz / 4 = 96 MHz = F_cpu
36+
.Q = 8, // 384 MHz / 8 = 48 MHz = F_usb
37+
%% else
38+
.N = 336, // 1 MHz * 336 = 336 MHz
39+
.P = 4, // 336 MHz / 4 = 84 MHz = F_cpu
40+
.Q = 7, // 336 MHz / 7 = 48 MHz = F_usb
41+
%% endif
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+
static_assert(PllP == {% if target.name == "11" %}96_MHz{% else %}Rcc::MaxFrequency{% endif %});
46+
47+
static constexpr uint32_t Frequency = PllP;
3048
static constexpr uint32_t Ahb = Frequency;
3149
static constexpr uint32_t Apb1 = Frequency / 2;
3250
static constexpr uint32_t Apb2 = Frequency;
33-
34-
static constexpr uint32_t Adc = Apb2;
35-
36-
static constexpr uint32_t Spi1 = Apb2;
51+
static constexpr uint32_t Ahb1 = Ahb;
52+
53+
static constexpr uint32_t Crc = Ahb1;
54+
static constexpr uint32_t Dma1 = Ahb1;
55+
static constexpr uint32_t Dma2 = Ahb1;
56+
static constexpr uint32_t Flash = Ahb1;
57+
58+
static constexpr uint32_t I2c1 = Apb1;
59+
static constexpr uint32_t I2c2 = Apb1;
60+
static constexpr uint32_t I2c3 = Apb1;
61+
static constexpr uint32_t I2s2Ext = Apb1;
62+
static constexpr uint32_t I2s3Ext = Apb1;
63+
static constexpr uint32_t IwdgBus = Apb1;
64+
static constexpr uint32_t Pwr = Apb1;
65+
static constexpr uint32_t RtcBus = Apb1;
3766
static constexpr uint32_t Spi2 = Apb1;
3867
static constexpr uint32_t Spi3 = Apb1;
68+
static constexpr uint32_t Usart2 = Apb1;
69+
static constexpr uint32_t Wwdg = Apb1;
70+
71+
static constexpr uint32_t Adc = Apb2;
72+
static constexpr uint32_t Adc1Common = Apb2;
73+
static constexpr uint32_t Exti = Apb2;
74+
static constexpr uint32_t Sdio = Apb2;
75+
static constexpr uint32_t Spi1 = Apb2;
3976
static constexpr uint32_t Spi4 = Apb2;
4077
static constexpr uint32_t Spi5 = Apb2;
41-
78+
static constexpr uint32_t Syscfg = Apb2;
4279
static constexpr uint32_t Usart1 = Apb2;
43-
static constexpr uint32_t Usart2 = Apb1;
4480
static constexpr uint32_t Usart6 = Apb2;
4581

46-
static constexpr uint32_t I2c1 = Apb1;
47-
static constexpr uint32_t I2c2 = Apb1;
48-
static constexpr uint32_t I2c3 = Apb1;
49-
5082
static constexpr uint32_t Apb1Timer = Apb1 * 2;
51-
static constexpr uint32_t Apb2Timer = Apb2 * 1;
83+
static constexpr uint32_t Apb2Timer = Apb2 * 2;
5284
static constexpr uint32_t Timer1 = Apb2Timer;
5385
static constexpr uint32_t Timer2 = Apb1Timer;
5486
static constexpr uint32_t Timer3 = Apb1Timer;
@@ -58,37 +90,26 @@ struct SystemClock
5890
static constexpr uint32_t Timer10 = Apb2Timer;
5991
static constexpr uint32_t Timer11 = Apb2Timer;
6092

61-
static constexpr uint32_t Usb = 48_MHz;
93+
static constexpr uint32_t Usb = PllQ;
6294
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
63-
static constexpr uint32_t Rtc = 32.768_kHz;
95+
static constexpr uint32_t Rtc = Lse;
6496

6597
static bool inline
6698
enable()
6799
{
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>();
100+
Rcc::enableLseCrystal();
101+
Rcc::enableHseCrystal();
82102

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

86106
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
87-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
88-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
107+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
108+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
89109

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

93114
return true;
94115
}
@@ -97,7 +118,7 @@ struct SystemClock
97118

98119
namespace usb
99120
{
100-
/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
121+
/// @ingroup modm_board_black_pill_f4{{target.name}}
101122
/// @{
102123
using Id = GpioA10;
103124
using Dm = GpioA11;
@@ -107,7 +128,7 @@ using Device = UsbFs;
107128
/// @}
108129
}
109130

110-
/// @ingroup modm_board_black_pill_f401 modm_board_black_pill_f411
131+
/// @ingroup modm_board_black_pill_f4{{target.name}}
111132
/// @{
112133
using Led = GpioInverted< GpioOutputC13 >;
113134
using Leds = SoftwareGpioPort< Led >;
@@ -145,5 +166,3 @@ initializeUsbFs(uint8_t priority=3)
145166
/// @}
146167

147168
} // Board namespace
148-
149-
#endif // MODM_STM32_F4X1CEU_MINI_F4X1_HPP

src/modm/board/black_pill_f411/module.lb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ def build(env):
5050
env.outbasepath = "modm/src/modm/board"
5151
env.substitutions = {
5252
"with_logger": False,
53-
"with_assert": env.has_module(":architecture:assert")
53+
"with_assert": env.has_module(":architecture:assert"),
54+
"target": env[":target"].identifier,
5455
}
5556
env.template("../board.cpp.in", "board.cpp")
56-
env.copy('.')
57+
env.template("board.hpp.in")
5758

5859
env.outbasepath = "modm/openocd/modm/board/"
5960
env.template(repopath("tools/openocd/modm/stm32_swd.cfg.in"), "board.cfg",

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
};

0 commit comments

Comments
 (0)