Skip to content

Commit 45f0685

Browse files
committed
[rcc] Move STM32F1 RCC to new API
1 parent a9e4bc2 commit 45f0685

File tree

7 files changed

+520
-107
lines changed

7 files changed

+520
-107
lines changed

src/modm/board/black_pill_f103/board.hpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ using namespace modm::literals;
2727
/// STM32F103 running at 72MHz generated from the external 8MHz crystal
2828
struct SystemClock
2929
{
30-
static constexpr uint32_t Frequency = 72_MHz;
30+
static constexpr uint32_t Hse = 8_MHz;
31+
static constexpr Rcc::PllConfig pll{.mul = 9, .usbdiv = Rcc::UsbPrescaler::Div1_5};
32+
static constexpr uint32_t Pll = Hse * pll.mul;
33+
static constexpr uint32_t Frequency = Pll;
34+
static_assert(Frequency == Rcc::MaxFrequency);
35+
3136
static constexpr uint32_t Ahb = Frequency;
3237
static constexpr uint32_t Apb1 = Frequency / 2;
3338
static constexpr uint32_t Apb2 = Frequency;
@@ -56,42 +61,26 @@ struct SystemClock
5661
static constexpr uint32_t Timer3 = Apb1Timer;
5762
static constexpr uint32_t Timer4 = Apb1Timer;
5863

59-
static constexpr uint32_t Usb = Ahb / 1.5;
64+
static constexpr uint32_t Usb = Ahb * 2 / 3;
6065
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
6166
static constexpr uint32_t Rtc = 32.768_kHz;
6267

6368
static bool inline
6469
enable()
6570
{
66-
Rcc::enableLowSpeedExternalCrystal();
67-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
68-
69-
Rcc::enableExternalCrystal();
70-
71-
// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
72-
const Rcc::PllFactors pllFactors{
73-
.pllMul = 9,
74-
.usbPrediv = Rcc::UsbPrescaler::Div1_5
75-
};
76-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
71+
Rcc::enableLseCrystal();
72+
Rcc::enableHseClock();
7773

78-
// set flash latency for 72MHz
7974
Rcc::setFlashLatency<Frequency>();
75+
Rcc::updateCoreFrequency<Frequency>();
8076

81-
// switch system clock to PLL output
82-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
83-
84-
// AHB has max 72MHz
8577
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
78+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
79+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
8680

87-
// APB1 has max. 36MHz
88-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
89-
90-
// APB2 has max. 72MHz
91-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
92-
93-
// update frequencies for busy-wait delay functions
94-
Rcc::updateCoreFrequency<Frequency>();
81+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
82+
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
83+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
9584

9685
return true;
9786
}

src/modm/board/blue_pill_f103/board.hpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ using namespace modm::literals;
3131
/// STM32F103 running at 72MHz generated from the external 8MHz crystal
3232
struct SystemClock
3333
{
34-
static constexpr uint32_t Frequency = 72_MHz;
34+
static constexpr uint32_t Hse = 8_MHz;
35+
static constexpr Rcc::PllConfig pll{.mul = 9, .usbdiv = Rcc::UsbPrescaler::Div1_5};
36+
static constexpr uint32_t Pll = Hse * pll.mul;
37+
static constexpr uint32_t Frequency = Pll;
38+
static_assert(Frequency == Rcc::MaxFrequency);
39+
3540
static constexpr uint32_t Ahb = Frequency;
3641
static constexpr uint32_t Apb1 = Frequency / 2;
3742
static constexpr uint32_t Apb2 = Frequency;
@@ -60,42 +65,26 @@ struct SystemClock
6065
static constexpr uint32_t Timer3 = Apb1Timer;
6166
static constexpr uint32_t Timer4 = Apb1Timer;
6267

63-
static constexpr uint32_t Usb = Ahb / 1.5;
68+
static constexpr uint32_t Usb = Ahb * 2 / 3;
6469
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
6570
static constexpr uint32_t Rtc = 32.768_kHz;
6671

6772
static bool inline
6873
enable()
6974
{
70-
Rcc::enableLowSpeedExternalCrystal();
71-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
72-
73-
Rcc::enableExternalCrystal();
74-
75-
// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
76-
const Rcc::PllFactors pllFactors{
77-
.pllMul = 9,
78-
.usbPrediv = Rcc::UsbPrescaler::Div1_5
79-
};
80-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
75+
Rcc::enableLseCrystal();
76+
Rcc::enableHseClock();
8177

82-
// set flash latency for 72MHz
8378
Rcc::setFlashLatency<Frequency>();
79+
Rcc::updateCoreFrequency<Frequency>();
8480

85-
// switch system clock to PLL output
86-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
87-
88-
// AHB has max 72MHz
8981
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
82+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
83+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
9084

91-
// APB1 has max. 36MHz
92-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
93-
94-
// APB2 has max. 72MHz
95-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
96-
97-
// update frequencies for busy-wait delay functions
98-
Rcc::updateCoreFrequency<Frequency>();
85+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
86+
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
87+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
9988

10089
return true;
10190
}

src/modm/board/disco_f100rb/board.hpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ using namespace modm::literals;
3232
/// supplied by the on-board st-link
3333
struct SystemClock
3434
{
35-
static constexpr uint32_t Frequency = 24_MHz;
35+
static constexpr uint32_t Hse = 8_MHz;
36+
static constexpr Rcc::PllConfig pll{.mul = 3};
37+
static constexpr uint32_t Pll = Hse * pll.mul / pll.prediv1;
38+
static constexpr uint32_t Frequency = Pll;
39+
static_assert(Frequency == Rcc::MaxFrequency);
40+
3641
static constexpr uint32_t Ahb = Frequency;
3742
static constexpr uint32_t Apb1 = Frequency;
3843
static constexpr uint32_t Apb2 = Frequency;
@@ -77,26 +82,20 @@ struct SystemClock
7782
static bool inline
7883
enable()
7984
{
80-
Rcc::enableLowSpeedExternalCrystal();
81-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
82-
83-
Rcc::enableExternalCrystal(); // 8MHz
84-
const Rcc::PllFactors pllFactors{
85-
.pllMul = 3,
86-
.pllPrediv = 1
87-
};
88-
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
89-
// set flash latency for 24MHz
85+
Rcc::enableLseCrystal();
86+
Rcc::enableHseClock();
87+
9088
Rcc::setFlashLatency<Frequency>();
91-
// switch system clock to PLL output
92-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
93-
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
94-
// APB1 has max. 24MHz
95-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div1);
96-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
97-
// update frequencies for busy-wait delay functions
9889
Rcc::updateCoreFrequency<Frequency>();
9990

91+
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
92+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div1);
93+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
94+
95+
Rcc::enablePll(Rcc::PllSource::Hse, pll);
96+
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
97+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
98+
10099
return true;
101100
}
102101
};

src/modm/board/nucleo_f103rb/board.hpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ using namespace modm::literals;
3030
/// STM32F103RB running at 64MHz generated from the internal 8MHz crystal
3131
struct SystemClock
3232
{
33-
static constexpr uint32_t Frequency = 64_MHz;
33+
static constexpr Rcc::PllConfig pll{.mul = 16};
34+
static constexpr uint32_t Pll = (Rcc::HsiFrequency / 2) * pll.mul;
35+
static_assert(Pll == 64_MHz);
36+
static constexpr uint32_t Frequency = Pll;
37+
3438
static constexpr uint32_t Ahb = Frequency;
3539
static constexpr uint32_t Apb1 = Frequency / 2;
3640
static constexpr uint32_t Apb2 = Frequency;
@@ -68,26 +72,20 @@ struct SystemClock
6872
static bool inline
6973
enable()
7074
{
71-
Rcc::enableLowSpeedInternalClock();
72-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);
73-
74-
Rcc::enableInternalClock(); // 8MHz
75-
// internal clock / 2 * 16 = 64MHz, => 64/1.5 = 42.6 => bad for USB
76-
const Rcc::PllFactors pllFactors{
77-
.pllMul = 16,
78-
};
79-
Rcc::enablePll(Rcc::PllSource::InternalClock, pllFactors);
80-
// set flash latency for 64MHz
75+
Rcc::enableLsiClock();
76+
Rcc::enableHsiClock();
77+
8178
Rcc::setFlashLatency<Frequency>();
82-
// switch system clock to PLL output
83-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
84-
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
85-
// APB1 has max. 36MHz
86-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
87-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
88-
// update frequencies for busy-wait delay functions
8979
Rcc::updateCoreFrequency<Frequency>();
9080

81+
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
82+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
83+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
84+
85+
Rcc::enablePll(Rcc::PllSource::HsiDiv2, pll);
86+
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
87+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lsi);
88+
9189
return true;
9290
}
9391
};

src/modm/board/olimexino_stm32/board.hpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ using namespace modm::literals;
2828
/// STM32F103RB running at 64MHz generated from the internal 8MHz crystal
2929
struct SystemClock
3030
{
31-
static constexpr uint32_t Frequency = 64_MHz;
31+
static constexpr Rcc::PllConfig pll{.mul = 16};
32+
static constexpr uint32_t Pll = (Rcc::HsiFrequency / 2) * pll.mul;
33+
static_assert(Pll == 64_MHz);
34+
static constexpr uint32_t Frequency = Pll;
35+
3236
static constexpr uint32_t Ahb = Frequency;
3337
static constexpr uint32_t Apb1 = Frequency / 2;
3438
static constexpr uint32_t Apb2 = Frequency;
@@ -68,26 +72,20 @@ struct SystemClock
6872
static bool inline
6973
enable()
7074
{
71-
Rcc::enableLowSpeedExternalCrystal();
72-
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
73-
74-
Rcc::enableInternalClock(); // 8MHz
75-
// internal clock / 2 * 16 = 64MHz, => 64/1.5 = 42.6 => bad for USB
76-
const Rcc::PllFactors pllFactors{
77-
.pllMul = 16,
78-
};
79-
Rcc::enablePll(Rcc::PllSource::InternalClock, pllFactors);
80-
// set flash latency for 64MHz
75+
Rcc::enableLseCrystal();
76+
Rcc::enableHsiClock();
77+
8178
Rcc::setFlashLatency<Frequency>();
82-
// switch system clock to PLL output
83-
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
84-
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
85-
// APB1 has max. 36MHz
86-
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
87-
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
88-
// update frequencies for busy-wait delay functions
8979
Rcc::updateCoreFrequency<Frequency>();
9080

81+
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
82+
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
83+
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);
84+
85+
Rcc::enablePll(Rcc::PllSource::HsiDiv2, pll);
86+
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
87+
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);
88+
9189
return true;
9290
}
9391
};

src/modm/platform/clock/stm32/module.lb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def build(env):
146146
ids += device.get_driver(driver)["instance"]
147147
return list(map(int, ids))
148148

149-
if t.family in ["h5", "u0", "g0", "u3", "c0", "f0", "l0"]:
149+
if t.family in ["h5", "u0", "g0", "u3", "c0", "f0", "l0", "f1"]:
150150
p["uart_ids"] = instances("usart", "uart")
151151
p["lpuart_ids"] = instances("lpuart")
152152
p["spi_ids"] = instances("spi")

0 commit comments

Comments
 (0)