Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/modm/board/black_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ using namespace modm::literals;
/// STM32F103 running at 72MHz generated from the external 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 72_MHz;
static constexpr uint32_t Hse = 8_MHz;
static constexpr Rcc::PllConfig pll{.mul = 9, .usbdiv = Rcc::UsbPrescaler::Div1_5};
static constexpr uint32_t Pll = Hse * pll.mul;
static constexpr uint32_t Frequency = Pll;
static_assert(Frequency == Rcc::MaxFrequency);

static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
static constexpr uint32_t Apb2 = Frequency;
Expand Down Expand Up @@ -56,42 +61,26 @@ struct SystemClock
static constexpr uint32_t Timer3 = Apb1Timer;
static constexpr uint32_t Timer4 = Apb1Timer;

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Usb = Ahb * 2 / 3;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
const Rcc::PllFactors pllFactors{
.pllMul = 9,
.usbPrediv = Rcc::UsbPrescaler::Div1_5
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
Rcc::enableLseCrystal();
Rcc::enableHseClock();

// set flash latency for 72MHz
Rcc::setFlashLatency<Frequency>();
Rcc::updateCoreFrequency<Frequency>();

// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);

// AHB has max 72MHz
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

// APB1 has max. 36MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);

// APB2 has max. 72MHz
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);

// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();
Rcc::enablePll(Rcc::PllSource::Hse, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
Expand Down
41 changes: 15 additions & 26 deletions src/modm/board/blue_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ using namespace modm::literals;
/// STM32F103 running at 72MHz generated from the external 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 72_MHz;
static constexpr uint32_t Hse = 8_MHz;
static constexpr Rcc::PllConfig pll{.mul = 9, .usbdiv = Rcc::UsbPrescaler::Div1_5};
static constexpr uint32_t Pll = Hse * pll.mul;
static constexpr uint32_t Frequency = Pll;
static_assert(Frequency == Rcc::MaxFrequency);

static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
static constexpr uint32_t Apb2 = Frequency;
Expand Down Expand Up @@ -60,42 +65,26 @@ struct SystemClock
static constexpr uint32_t Timer3 = Apb1Timer;
static constexpr uint32_t Timer4 = Apb1Timer;

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Usb = Ahb * 2 / 3;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
const Rcc::PllFactors pllFactors{
.pllMul = 9,
.usbPrediv = Rcc::UsbPrescaler::Div1_5
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
Rcc::enableLseCrystal();
Rcc::enableHseClock();

// set flash latency for 72MHz
Rcc::setFlashLatency<Frequency>();
Rcc::updateCoreFrequency<Frequency>();

// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);

// AHB has max 72MHz
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

// APB1 has max. 36MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);

// APB2 has max. 72MHz
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);

// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();
Rcc::enablePll(Rcc::PllSource::Hse, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
Expand Down
35 changes: 17 additions & 18 deletions src/modm/board/disco_f100rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ using namespace modm::literals;
/// supplied by the on-board st-link
struct SystemClock
{
static constexpr uint32_t Frequency = 24_MHz;
static constexpr uint32_t Hse = 8_MHz;
static constexpr Rcc::PllConfig pll{.mul = 3};
static constexpr uint32_t Pll = Hse * pll.mul / pll.prediv1;
static constexpr uint32_t Frequency = Pll;
static_assert(Frequency == Rcc::MaxFrequency);

static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency;
static constexpr uint32_t Apb2 = Frequency;
Expand Down Expand Up @@ -77,26 +82,20 @@ struct SystemClock
static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllMul = 3,
.pllPrediv = 1
};
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
// set flash latency for 24MHz
Rcc::enableLseCrystal();
Rcc::enableHseClock();

Rcc::setFlashLatency<Frequency>();
// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
// APB1 has max. 24MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div1);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();

Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div1);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

Rcc::enablePll(Rcc::PllSource::Hse, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
};
Expand Down
34 changes: 16 additions & 18 deletions src/modm/board/nucleo_f103rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ using namespace modm::literals;
/// STM32F103RB running at 64MHz generated from the internal 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 64_MHz;
static constexpr Rcc::PllConfig pll{.mul = 16};
static constexpr uint32_t Pll = (Rcc::HsiFrequency / 2) * pll.mul;
static_assert(Pll == 64_MHz);
static constexpr uint32_t Frequency = Pll;

static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
static constexpr uint32_t Apb2 = Frequency;
Expand Down Expand Up @@ -68,26 +72,20 @@ struct SystemClock
static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableInternalClock(); // 8MHz
// internal clock / 2 * 16 = 64MHz, => 64/1.5 = 42.6 => bad for USB
const Rcc::PllFactors pllFactors{
.pllMul = 16,
};
Rcc::enablePll(Rcc::PllSource::InternalClock, pllFactors);
// set flash latency for 64MHz
Rcc::enableLsiClock();
Rcc::enableHsiClock();

Rcc::setFlashLatency<Frequency>();
// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
// APB1 has max. 36MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();

Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

Rcc::enablePll(Rcc::PllSource::HsiDiv2, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lsi);

return true;
}
};
Expand Down
34 changes: 16 additions & 18 deletions src/modm/board/olimexino_stm32/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ using namespace modm::literals;
/// STM32F103RB running at 64MHz generated from the internal 8MHz crystal
struct SystemClock
{
static constexpr uint32_t Frequency = 64_MHz;
static constexpr Rcc::PllConfig pll{.mul = 16};
static constexpr uint32_t Pll = (Rcc::HsiFrequency / 2) * pll.mul;
static_assert(Pll == 64_MHz);
static constexpr uint32_t Frequency = Pll;

static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
static constexpr uint32_t Apb2 = Frequency;
Expand Down Expand Up @@ -68,26 +72,20 @@ struct SystemClock
static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableInternalClock(); // 8MHz
// internal clock / 2 * 16 = 64MHz, => 64/1.5 = 42.6 => bad for USB
const Rcc::PllFactors pllFactors{
.pllMul = 16,
};
Rcc::enablePll(Rcc::PllSource::InternalClock, pllFactors);
// set flash latency for 64MHz
Rcc::enableLseCrystal();
Rcc::enableHsiClock();

Rcc::setFlashLatency<Frequency>();
// switch system clock to PLL output
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
// APB1 has max. 36MHz
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);
// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();

Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::ApbPrescaler::Div2);
Rcc::setApb2Prescaler(Rcc::ApbPrescaler::Div1);

Rcc::enablePll(Rcc::PllSource::HsiDiv2, pll);
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
Rcc::setRealTimeClockSource(Rcc::RealTimeClockSource::Lse);

return true;
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/modm/platform/clock/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def build(env):
ids += device.get_driver(driver)["instance"]
return list(map(int, ids))

if t.family in ["h5", "u0", "g0", "u3", "c0", "f0", "l0"]:
if t.family in ["h5", "u0", "g0", "u3", "c0", "f0", "l0", "f1"]:
p["uart_ids"] = instances("usart", "uart")
p["lpuart_ids"] = instances("lpuart")
p["spi_ids"] = instances("spi")
Expand Down
Loading
Loading