Skip to content

Commit

Permalink
quickly noted the concept in disco_f411ve/board.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Mar 5, 2024
1 parent 7e0b471 commit 0752317
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/modm/board/disco_f411ve/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Frequency>();
// 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<Frequency>();

Expand Down

0 comments on commit 0752317

Please sign in to comment.