Skip to content

Commit

Permalink
[examples] Add RTC example for NUCLEO boards
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Dec 30, 2024
1 parent ec619af commit 381fec2
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 11 deletions.
59 changes: 59 additions & 0 deletions examples/generic/rtc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// coding: utf-8
/*
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
* Copyright (c) 2024, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>

int
main()
{
Board::initialize();
Board::Leds::setOutput();
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
#endif
const bool inited = Rtc::initialize<Board::SystemClock>();
#ifdef MODM_BOARD_HAS_LOGGER
if (not inited) { MODM_LOG_INFO << "RTC was already initialized." << modm::endl; }
#endif

constexpr auto cdt = modm::DateTime::fromBuildTime();
if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt);
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl;
MODM_LOG_INFO << "YMD: " << cdt.year_month_day() << modm::endl;
MODM_LOG_INFO << "HMS: " << cdt.hh_mm_ss() << modm::endl;
MODM_LOG_INFO << "Weekday: " << cdt.weekday() << modm::endl;
#endif


while (true)
{
const auto dt = Rtc::dateTime();
#ifdef MODM_BOARD_HAS_LOGGER
const auto now = Rtc::now();
MODM_LOG_INFO << dt << " (" << dt.weekday() << ") = " << now << " since 1970" << modm::endl;
modm::delay(1.1s);
#else
static uint8_t prev_second{};
if (prev_second != dt.seconds().count())
{
prev_second = dt.seconds().count();
Board::Leds::toggle();
}
modm::delay(10ms);
#endif

}

return 0;
}
2 changes: 2 additions & 0 deletions examples/generic/rtc/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink.cfg]
30 changes: 30 additions & 0 deletions examples/generic/rtc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<library>
<extends>modm:nucleo-c031c6</extends>

<!-- <extends>modm:nucleo-g071rb</extends> -->

<!-- <extends>modm:stm32f030_demo</extends> -->
<!-- <extends>modm:disco-f051r8</extends> -->
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:nucleo-f072rb</extends> -->
<!-- <extends>modm:nucleo-f091rc</extends> -->

<!-- <extends>modm:nucleo-l053r8</extends> -->
<!-- <extends>modm:nucleo-l152re</extends> -->
<!-- <extends>modm:disco-l152rc</extends> -->
<!-- <extends>modm:nucleo-l476rg</extends> -->
<!-- <extends>modm:disco-l476vg</extends> -->

<!-- <extends>modm:disco-f303vc</extends> -->
<!-- <extends>modm:nucleo-f334r8</extends> -->

<!-- <extends>modm:nucleo-g474re</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/rtc</option>
<!-- <option name="modm:build:openocd.cfg">openocd.cfg</option> -->
</options>
<modules>
<module>modm:platform:rtc</module>
<module>modm:build:scons</module>
</modules>
</library>
2 changes: 1 addition & 1 deletion examples/stm32f030f4p6_demo_board/blink/openocd.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]
source [find interface/stlink.cfg]
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
4 changes: 4 additions & 0 deletions src/modm/board/disco_f072rb/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 = 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_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_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 = 32.768_kHz;

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

// 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
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f091rc/board.hpp
Original file line number Diff line number Diff line change
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
7 changes: 6 additions & 1 deletion src/modm/board/nucleo_f303k8/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace Board

/// STM32F303K8 running at 64MHz generated from the internal 8MHz clock
// Dummy clock for devices
struct SystemClock {
struct SystemClock
{
static constexpr uint32_t Frequency = 64_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb1 = Frequency / 2;
Expand Down Expand Up @@ -60,10 +61,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 = Rcc::LsiFrequency;

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

Rcc::enableInternalClock(); // 8MHz
// 8MHz / 2 * 16 = 64MHz
const Rcc::PllFactors pllFactors{
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f303re/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,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::enableInternalClock(); // 8MHz
// 8MHz / 2 * 16 = 64MHz
const Rcc::PllFactors pllFactors{
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_f334r8/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,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::enableInternalClock(); // 8MHz
// 8MHz / 2 * 16 = 64MHz
const Rcc::PllFactors pllFactors{
Expand Down
9 changes: 6 additions & 3 deletions src/modm/board/nucleo_g071rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ using namespace modm::literals;
struct SystemClock
{
static constexpr uint32_t Frequency = 64_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

static constexpr uint32_t Aes = Ahb;
static constexpr uint32_t Rng = Ahb;
Expand Down Expand Up @@ -72,16 +72,19 @@ struct SystemClock
static constexpr uint32_t Spi2 = Apb;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Wwdg = Apb;
static constexpr uint32_t Rtc = Apb;
static constexpr uint32_t Timer14 = Apb;
static constexpr uint32_t Timer7 = Apb;
static constexpr uint32_t Timer6 = Apb;
static constexpr uint32_t Timer3 = Apb;
static constexpr uint32_t Timer2 = Apb;
static constexpr uint32_t Rtc = 32.768_kHz;

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

Rcc::enableInternalClock(); // 16MHz
// (internal clock / 1_M) * 8_N / 2_R = 128MHz / 2 = 64MHz
const Rcc::PllFactors pllFactors{
Expand Down
6 changes: 5 additions & 1 deletion src/modm/board/nucleo_g474re/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ struct SystemClock
static constexpr uint32_t I2c4 = I2c;
static constexpr uint32_t Lptim = Apb1;
static constexpr uint32_t Lpuart = Apb1;
static constexpr uint32_t Rtc = Apb1;
static constexpr uint32_t Spi2 = Apb1;
static constexpr uint32_t Spi3 = Apb1;
static constexpr uint32_t Uart4 = Apb1;
Expand Down Expand Up @@ -94,6 +93,8 @@ struct SystemClock
static constexpr uint32_t Timer20 = Apb2Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;

static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Expand All @@ -115,6 +116,9 @@ struct SystemClock
// update frequencies for busy-wait delay functions
Rcc::updateCoreFrequency<Frequency>();

Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::setCanClockSource(Rcc::CanClockSource::Pclk);
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/nucleo_l031k6/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ struct SystemClock

static constexpr uint32_t Usart2 = 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);

Rcc::enableInternalClock(); // 16MHz
// (internal clock / 1) * 4 / 2 = 32MHz
const Rcc::PllFactors pllFactors{
Expand Down
Loading

0 comments on commit 381fec2

Please sign in to comment.