modm-io / modm

modm: a C++23 library generator for AVR and ARM Cortex-M devices
https://modm.io
Mozilla Public License 2.0
746 stars 132 forks source link

Calculate STM32 clocks from the roots #1137

Open TomSaw opened 7 months ago

TomSaw commented 7 months ago

There are hardcoded clock constants in each of **/board.hpp althought the required information to calculate the truth is available!

salkinium commented 7 months ago

Hm, so what I would want is to move the common pattern of

// Enable clock source
Rcc::enableExternalCrystal();
// Enable PLL
Rcc::enablePll(Rcc::PllSource::ExternalCrystal, pllFactors);
Rcc::setFlashLatency<Frequency>();
Rcc::enableSystemClock(Rcc::SystemClockSource::Pll);
// Set Bus Prescaler
Rcc::setAhbPrescaler(Rcc::AhbPrescaler::Div1);
Rcc::setApb1Prescaler(Rcc::Apb1Prescaler::Div2);
Rcc::setApb2Prescaler(Rcc::Apb2Prescaler::Div1);

into a helper class defined in modm and configured by the user:

template< OneConfigStruct? >
struct BaseSystemClock
{
// Computes the basic frequencies with static_assert < max freq

// Adds all the peripheral frequencies via modm-devices?

// has a basic enable() function that does the above always correctly
};

The BSP would then inherit from that for their SystemClock and perhaps overwrite the one of the other thing for customization.

// simple case
using SystemClock = BaseSystemClock< ConfigStruct >;
// advances case
struct SystemClock : public BaseSystemClock< ConfigStruct >;
{
// customize
};

This would probably cover 90% of use-cases. Maaaany (8) years ago I tried something like that before, but I did it way too complicated. So please do it simpler if you're interested in that.

TomSaw commented 7 months ago

That't some good guidiance! You're way more convenient with the STM architecture than me, so i blindly follow your suggestions in this topic.

// simple case
using SystemClock = BaseSystemClock< ConfigStruct >;
// advances case
struct SystemClock : public BaseSystemClock< ConfigStruct >;
{
// customize
};

Thats simple and users can opt in to vary their clocks per application. I go for this.