paper_trail icon indicating copy to clipboard operation
paper_trail copied to clipboard

Calculate STM32 clocks from the roots

Open TomSaw opened this issue 11 months ago • 2 comments

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

TomSaw avatar Mar 05 '24 20:03 TomSaw

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.

salkinium avatar Mar 10 '24 22:03 salkinium

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.

TomSaw avatar Mar 11 '24 09:03 TomSaw