From 0752317e36058cd8298c714be8d0cf8e193b57a6 Mon Sep 17 00:00:00 2001 From: Thomas Sommer Date: Tue, 5 Mar 2024 21:44:52 +0100 Subject: [PATCH] quickly noted the concept in disco_f411ve/board.hpp --- src/modm/board/disco_f411ve/board.hpp | 43 +++++++++++++++++---------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/modm/board/disco_f411ve/board.hpp b/src/modm/board/disco_f411ve/board.hpp index 3e8885b0c1..8bb466eab9 100644 --- a/src/modm/board/disco_f411ve/board.hpp +++ b/src/modm/board/disco_f411ve/board.hpp @@ -22,14 +22,34 @@ namespace Board /// @{ using namespace modm::literals; -/// STM32F411 running at 84MHz generated from the external 8MHz crystal struct SystemClock { - static constexpr uint32_t Frequency = 84_MHz; + static constexpr uint32_t ExternalCrystalClock = 8_MHz; + static constexpr Rcc::PllFactors pllFactors{ + .pllM = 8, + .pllN = 336, + .pllP = 4, + .pllQ = 7, + }; + static constexpr uint32_t MainPllClock = ExternalCrystalClock / pllFactors.pllM * pllFactors.pllN; + + static constexpr Rcc::AhbPrescaler Ahb_prescaler = Rcc::AhbPrescaler::Div1; + static constexpr Rcc::Apb1Prescaler Apb1_prescaler = Rcc::Apb1Prescaler::Div2; + static constexpr Rcc::Apb2Prescaler Apb2_prescaler = Rcc::Apb2Prescaler::Div1; + + // ------------------------------------------ + + static constexpr uint32_t Frequency = MainPllClock / pllFactors.pllP; // 84_Mhz + static constexpr uint32_t Usb = MainPllClock / pllFactors.pllQ; // 48_Mhz + static constexpr uint32_t Ahb = Frequency; static constexpr uint32_t Apb1 = Frequency / 2; static constexpr uint32_t Apb2 = Frequency; + // @todo find the right place + static_assert(Apb1 <= 50_MHz, "Apb1 has max. 50MHz!"); + static_assert(Apb2 <= 100_MHz, "Apb2 has max. 100MHz!"); + static constexpr uint32_t Adc = Apb2; static constexpr uint32_t Spi1 = Apb2; @@ -62,27 +82,18 @@ struct SystemClock static constexpr uint32_t Timer10 = Apb2Timer; static constexpr uint32_t Timer11 = Apb2Timer; - static constexpr uint32_t Usb = 48_MHz; - static bool inline enable() { - Rcc::enableExternalCrystal(); // 8MHz - const Rcc::PllFactors pllFactors{ - .pllM = 8, // 8MHz / M=8 -> 1MHz - .pllN = 336, // 1MHz * N=336 -> 84MHz - .pllP = 4, // 336MHz / P=4 -> 100MHz = F_cpu - .pllQ = 7, // 336MHz / P=7 -> 100MHz = F_cpu - }; + /// STM32F411 running at 84MHz generated from the external 8MHz crystal + Rcc::enableExternalCrystal(); Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors); // set flash latency for 100MHz Rcc::setFlashLatency(); // switch system clock to PLL output Rcc::enableSystemClock(Rcc::SystemClockSource::Pll); - Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1); - // APB1 has max. 50MHz - // APB2 has max. 100MHz - Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2); - Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1); + Rcc::setAhbPrescaler(Ahb_prescaler); + Rcc::setApb1Prescaler(Apb1_prescaler); + Rcc::setApb2Prescaler(Apb2_prescaler); // update frequencies for busy-wait delay functions Rcc::updateCoreFrequency();