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 7, 2024
1 parent 009bb06 commit 70fa1ce
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 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;
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 = 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<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 70fa1ce

Please sign in to comment.