Skip to content

Commit

Permalink
[board] Add RTC clock source support to all BSPs
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Dec 30, 2024
1 parent 5d8123f commit 42f3599
Show file tree
Hide file tree
Showing 50 changed files with 215 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/modm/board/black_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/black_pill_f411/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/blue_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/devebox_stm32f4xx/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ struct SystemClock
static constexpr uint32_t Timer13 = Apb1Timer;
static constexpr uint32_t Timer14 = Apb1Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/devebox_stm32h750vb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz; // From PLL3Q
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 25MHz
Rcc::setVoltageScaling(Rcc::VoltageScaling::Scale0); // required for 400MHz/480MHz
const Rcc::PllFactors pllFactors1{
Expand Down
12 changes: 8 additions & 4 deletions src/modm/board/disco_f051r8/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ using namespace modm::literals;
/// STM32F0 running at 48MHz generated from the internal 8MHz with PLL.
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr int Usart1 = Frequency;
static constexpr int Usart2 = Frequency;
static constexpr int Spi2 = Frequency;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Usart1 = Frequency;
static constexpr uint32_t Usart2 = Frequency;
static constexpr uint32_t Spi2 = Frequency;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

// enable internal 8 MHz HSI RC clock
Rcc::enableInternalClock();
// (internal clock / 2) * 12 = 48MHz
Expand Down
6 changes: 5 additions & 1 deletion src/modm/board/disco_f072rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace modm::literals;
/// STM32F072 running at 48MHz generated from the internal 48MHz clock
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

Expand Down Expand Up @@ -60,10 +60,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

// Enable the internal 48MHz clock
Rcc::enableInternalClockMHz48();
// set flash latency for 48MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f100rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ struct SystemClock
static constexpr uint32_t Timer16 = Apb2Timer;
static constexpr uint32_t Timer17 = Apb2Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllMul = 3,
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f303vc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalClock(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllMul = 9,
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f401vc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f407vg/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
8 changes: 7 additions & 1 deletion src/modm/board/disco_f411ve/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ struct SystemClock
static constexpr uint32_t Timer11 = Apb2Timer;

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline enable()
static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 7, // 8MHz / M=7 -> ~1.14MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f429zi/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8 MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f469ni/board.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8 MHz
const Rcc::PllFactors pllFactors{
.pllM = 8, // 8MHz / M=8 -> 1MHz !!! Must be 1 MHz for PLLSAI !!!
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f746ng/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalClock(); // 25 MHz
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f769ni/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ struct SystemClock
static constexpr uint32_t Timer13 = Apb1Timer;
static constexpr uint32_t Timer14 = Apb1Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalClock(); // 25 MHz
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_l152rc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ struct SystemClock
static constexpr uint32_t Timer10 = Apb2Timer;
static constexpr uint32_t Timer11 = Apb2Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

// Enable power scaling for 1.8V
PWR->CR = (PWR->CR & ~PWR_CR_VOS) | PWR_CR_VOS_0;
while(PWR->CSR & PWR_CSR_VOSF) ;
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_l476vg/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ struct SystemClock
static constexpr uint32_t Usart4 = Apb1;
static constexpr uint32_t Usart5 = Apb1;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

// set flash latency first because system already runs from MSI
Rcc::setFlashLatency<Frequency>();

Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_c031c6/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ struct SystemClock
static constexpr uint32_t Timer16 = Apb;
static constexpr uint32_t Timer17 = Apb;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

// 48MHz generated from internal RC
Rcc::enableInternalClock();
Rcc::setHsiSysDivider(Rcc::HsiSysDivider::Div1);
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f031k6/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ struct SystemClock
static constexpr uint32_t Timer16 = Apb;
static constexpr uint32_t Timer17 = Apb;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableInternalClock(); // 8MHz
// (internal clock / 2) * 12 = 48MHz
const Rcc::PllFactors pllFactors{
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f042k6/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ struct SystemClock
static constexpr uint32_t Timer16 = Apb;
static constexpr uint32_t Timer17 = Apb;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableInternalClock(); // 8MHz
// (internal clock / 2) * 12 = 48MHz
const Rcc::PllFactors pllFactors{
Expand Down
6 changes: 5 additions & 1 deletion src/modm/board/nucleo_f072rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace modm::literals;
/// STM32f072rb running at 48MHz generated from the internal 8MHz crystal
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

Expand Down Expand Up @@ -60,10 +60,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

// Enable the internal 48MHz clock
Rcc::enableInternalClockMHz48();
// set flash latency for 48MHz
Expand Down
6 changes: 5 additions & 1 deletion src/modm/board/nucleo_f091rc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace modm::literals;
/// STM32F091RC running at 48MHz generated from the internal 8MHz crystal
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

Expand Down Expand Up @@ -59,10 +59,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

// Enable the internal 48MHz clock
Rcc::enableInternalClockMHz48();
// set flash latency for 48MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f103rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ struct SystemClock
static constexpr uint32_t Timer7 = Apb1Timer;
static constexpr uint32_t Timer8 = Apb2Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableInternalClock(); // 8MHz
// internal clock / 2 * 16 = 64MHz, => 64/1.5 = 42.6 => bad for USB
const Rcc::PllFactors pllFactors{
Expand Down
Loading

0 comments on commit 42f3599

Please sign in to comment.