From 70fa1ce10c548f647e12dc4e98a8585884910c9b 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 0e4b470a2b..ff27fb58d8 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 96MHz generated from the external 8MHz crystal struct SystemClock { - static constexpr uint32_t Frequency = 96_MHz; + static constexpr uint32_t ExternalCrystalClock = 8_MHz; + static constexpr Rcc::PllFactors pllFactors{ + .pllM = 7, + .pllN = 336, + .pllP = 4, + .pllQ = 8, + }; + 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; // 96_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 = 7, // 8MHz / M=7 -> ~1.14MHz - .pllN = 336, // 1.14MHz * N=336 -> 384MHz - .pllP = 4, // 384MHz / P=4 -> 96MHz = F_cpu - .pllQ = 8, // 384MHz / P=8 -> 48MHz = F_usb - }; + /// 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();