Skip to content

Commit 381fec2

Browse files
committed
[examples] Add RTC example for NUCLEO boards
1 parent ec619af commit 381fec2

File tree

27 files changed

+194
-11
lines changed

27 files changed

+194
-11
lines changed

examples/generic/rtc/main.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// coding: utf-8
2+
/*
3+
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
4+
* Copyright (c) 2024, Niklas Hauser
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#include <modm/board.hpp>
15+
16+
int
17+
main()
18+
{
19+
Board::initialize();
20+
Board::Leds::setOutput();
21+
#ifdef MODM_BOARD_HAS_LOGGER
22+
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
23+
#endif
24+
const bool inited = Rtc::initialize<Board::SystemClock>();
25+
#ifdef MODM_BOARD_HAS_LOGGER
26+
if (not inited) { MODM_LOG_INFO << "RTC was already initialized." << modm::endl; }
27+
#endif
28+
29+
constexpr auto cdt = modm::DateTime::fromBuildTime();
30+
if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt);
31+
#ifdef MODM_BOARD_HAS_LOGGER
32+
MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl;
33+
MODM_LOG_INFO << "YMD: " << cdt.year_month_day() << modm::endl;
34+
MODM_LOG_INFO << "HMS: " << cdt.hh_mm_ss() << modm::endl;
35+
MODM_LOG_INFO << "Weekday: " << cdt.weekday() << modm::endl;
36+
#endif
37+
38+
39+
while (true)
40+
{
41+
const auto dt = Rtc::dateTime();
42+
#ifdef MODM_BOARD_HAS_LOGGER
43+
const auto now = Rtc::now();
44+
MODM_LOG_INFO << dt << " (" << dt.weekday() << ") = " << now << " since 1970" << modm::endl;
45+
modm::delay(1.1s);
46+
#else
47+
static uint8_t prev_second{};
48+
if (prev_second != dt.seconds().count())
49+
{
50+
prev_second = dt.seconds().count();
51+
Board::Leds::toggle();
52+
}
53+
modm::delay(10ms);
54+
#endif
55+
56+
}
57+
58+
return 0;
59+
}

examples/generic/rtc/openocd.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Replace this with your custom programmer
2+
source [find interface/stlink.cfg]

examples/generic/rtc/project.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<library>
2+
<extends>modm:nucleo-c031c6</extends>
3+
4+
<!-- <extends>modm:nucleo-g071rb</extends> -->
5+
6+
<!-- <extends>modm:stm32f030_demo</extends> -->
7+
<!-- <extends>modm:disco-f051r8</extends> -->
8+
<!-- <extends>modm:disco-f072rb</extends> -->
9+
<!-- <extends>modm:nucleo-f072rb</extends> -->
10+
<!-- <extends>modm:nucleo-f091rc</extends> -->
11+
12+
<!-- <extends>modm:nucleo-l053r8</extends> -->
13+
<!-- <extends>modm:nucleo-l152re</extends> -->
14+
<!-- <extends>modm:disco-l152rc</extends> -->
15+
<!-- <extends>modm:nucleo-l476rg</extends> -->
16+
<!-- <extends>modm:disco-l476vg</extends> -->
17+
18+
<!-- <extends>modm:disco-f303vc</extends> -->
19+
<!-- <extends>modm:nucleo-f334r8</extends> -->
20+
21+
<!-- <extends>modm:nucleo-g474re</extends> -->
22+
<options>
23+
<option name="modm:build:build.path">../../../build/generic/rtc</option>
24+
<!-- <option name="modm:build:openocd.cfg">openocd.cfg</option> -->
25+
</options>
26+
<modules>
27+
<module>modm:platform:rtc</module>
28+
<module>modm:build:scons</module>
29+
</modules>
30+
</library>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Replace this with your custom programmer
2-
source [find interface/stlink-v2.cfg]
2+
source [find interface/stlink.cfg]

src/modm/board/disco_f051r8/board.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ using namespace modm::literals;
2828
/// STM32F0 running at 48MHz generated from the internal 8MHz with PLL.
2929
struct SystemClock
3030
{
31-
static constexpr int Frequency = 48_MHz;
32-
static constexpr int Usart1 = Frequency;
33-
static constexpr int Usart2 = Frequency;
34-
static constexpr int Spi2 = Frequency;
31+
static constexpr uint32_t Frequency = 48_MHz;
32+
static constexpr uint32_t Usart1 = Frequency;
33+
static constexpr uint32_t Usart2 = Frequency;
34+
static constexpr uint32_t Spi2 = Frequency;
3535
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
36+
static constexpr uint32_t Rtc = Rcc::LsiFrequency;
3637

3738
static bool inline
3839
enable()
3940
{
41+
Rcc::enableLowSpeedInternalClock();
42+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);
43+
4044
// enable internal 8 MHz HSI RC clock
4145
Rcc::enableInternalClock();
4246
// (internal clock / 2) * 12 = 48MHz

src/modm/board/disco_f072rb/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ struct SystemClock
6060

6161
static constexpr uint32_t Usb = 48_MHz;
6262
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
63+
static constexpr uint32_t Rtc = Rcc::LsiFrequency;
6364

6465
static bool inline
6566
enable()
6667
{
68+
Rcc::enableLowSpeedInternalClock();
69+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);
70+
6771
// Enable the internal 48MHz clock
6872
Rcc::enableInternalClockMHz48();
6973
// set flash latency for 48MHz

src/modm/board/disco_f303vc/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ struct SystemClock
8585

8686
static constexpr uint32_t Usb = Ahb / 1.5;
8787
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
88+
static constexpr uint32_t Rtc = Rcc::LsiFrequency;
8889

8990
static bool inline
9091
enable()
9192
{
93+
Rcc::enableLowSpeedInternalClock();
94+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);
95+
9296
Rcc::enableExternalClock(); // 8MHz
9397
const Rcc::PllFactors pllFactors{
9498
.pllMul = 9,

src/modm/board/disco_l152rc/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ struct SystemClock
5858
static constexpr uint32_t Timer10 = Apb2Timer;
5959
static constexpr uint32_t Timer11 = Apb2Timer;
6060
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
61+
static constexpr uint32_t Rtc = 32.768_kHz;
6162

6263
static bool inline
6364
enable()
6465
{
66+
Rcc::enableLowSpeedExternalCrystal();
67+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
68+
6569
// Enable power scaling for 1.8V
6670
PWR->CR = (PWR->CR & ~PWR_CR_VOS) | PWR_CR_VOS_0;
6771
while(PWR->CSR & PWR_CSR_VOSF) ;

src/modm/board/disco_l476vg/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ struct SystemClock
4444
static constexpr uint32_t Usart4 = Apb1;
4545
static constexpr uint32_t Usart5 = Apb1;
4646
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
47+
static constexpr uint32_t Rtc = 32.768_kHz;
4748

4849
static bool inline
4950
enable()
5051
{
52+
Rcc::enableLowSpeedExternalCrystal();
53+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
54+
5155
// set flash latency first because system already runs from MSI
5256
Rcc::setFlashLatency<Frequency>();
5357

src/modm/board/nucleo_c031c6/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ struct SystemClock
5050
static constexpr uint32_t Timer16 = Apb;
5151
static constexpr uint32_t Timer17 = Apb;
5252
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
53+
static constexpr uint32_t Rtc = 32.768_kHz;
5354

5455
static bool inline
5556
enable()
5657
{
58+
Rcc::enableLowSpeedExternalCrystal();
59+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
60+
5761
// 48MHz generated from internal RC
5862
Rcc::enableInternalClock();
5963
Rcc::setHsiSysDivider(Rcc::HsiSysDivider::Div1);

src/modm/board/nucleo_f031k6/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ struct SystemClock
4949
static constexpr uint32_t Timer16 = Apb;
5050
static constexpr uint32_t Timer17 = Apb;
5151
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
52+
static constexpr uint32_t Rtc = 32.768_kHz;
5253

5354
static bool inline
5455
enable()
5556
{
57+
Rcc::enableLowSpeedExternalCrystal();
58+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
59+
5660
Rcc::enableInternalClock(); // 8MHz
5761
// (internal clock / 2) * 12 = 48MHz
5862
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_f042k6/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ struct SystemClock
5252
static constexpr uint32_t Timer16 = Apb;
5353
static constexpr uint32_t Timer17 = Apb;
5454
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
55+
static constexpr uint32_t Rtc = 32.768_kHz;
5556

5657
static bool inline
5758
enable()
5859
{
60+
Rcc::enableLowSpeedExternalCrystal();
61+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
62+
5963
Rcc::enableInternalClock(); // 8MHz
6064
// (internal clock / 2) * 12 = 48MHz
6165
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_f072rb/board.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace modm::literals;
3030
/// STM32f072rb running at 48MHz generated from the internal 8MHz crystal
3131
struct SystemClock
3232
{
33-
static constexpr int Frequency = 48_MHz;
33+
static constexpr uint32_t Frequency = 48_MHz;
3434
static constexpr uint32_t Ahb = Frequency;
3535
static constexpr uint32_t Apb = Frequency;
3636

@@ -60,10 +60,14 @@ struct SystemClock
6060

6161
static constexpr uint32_t Usb = 48_MHz;
6262
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
63+
static constexpr uint32_t Rtc = 32.768_kHz;
6364

6465
static bool inline
6566
enable()
6667
{
68+
Rcc::enableLowSpeedExternalCrystal();
69+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
70+
6771
// Enable the internal 48MHz clock
6872
Rcc::enableInternalClockMHz48();
6973
// set flash latency for 48MHz

src/modm/board/nucleo_f091rc/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ struct SystemClock
5959

6060
static constexpr uint32_t Usb = 48_MHz;
6161
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
62+
static constexpr uint32_t Rtc = 32.768_kHz;
6263

6364
static bool inline
6465
enable()
6566
{
67+
Rcc::enableLowSpeedExternalCrystal();
68+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
69+
6670
// Enable the internal 48MHz clock
6771
Rcc::enableInternalClockMHz48();
6872
// set flash latency for 48MHz

src/modm/board/nucleo_f303k8/board.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace Board
2828

2929
/// STM32F303K8 running at 64MHz generated from the internal 8MHz clock
3030
// Dummy clock for devices
31-
struct SystemClock {
31+
struct SystemClock
32+
{
3233
static constexpr uint32_t Frequency = 64_MHz;
3334
static constexpr uint32_t Ahb = Frequency;
3435
static constexpr uint32_t Apb1 = Frequency / 2;
@@ -60,10 +61,14 @@ struct SystemClock {
6061
static constexpr uint32_t Timer16 = Apb2Timer;
6162
static constexpr uint32_t Timer17 = Apb2Timer;
6263
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
64+
static constexpr uint32_t Rtc = Rcc::LsiFrequency;
6365

6466
static bool inline
6567
enable()
6668
{
69+
Rcc::enableLowSpeedInternalClock();
70+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);
71+
6772
Rcc::enableInternalClock(); // 8MHz
6873
// 8MHz / 2 * 16 = 64MHz
6974
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_f303re/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ struct SystemClock
6363
static constexpr uint32_t Timer16 = Apb2Timer;
6464
static constexpr uint32_t Timer17 = Apb2Timer;
6565
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
66+
static constexpr uint32_t Rtc = 32.768_kHz;
6667

6768
static bool inline
6869
enable()
6970
{
71+
Rcc::enableLowSpeedExternalCrystal();
72+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
73+
7074
Rcc::enableInternalClock(); // 8MHz
7175
// 8MHz / 2 * 16 = 64MHz
7276
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_f334r8/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ struct SystemClock
6060
static constexpr uint32_t Timer16 = Apb2Timer;
6161
static constexpr uint32_t Timer17 = Apb2Timer;
6262
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
63+
static constexpr uint32_t Rtc = 32.768_kHz;
6364

6465
static bool inline
6566
enable()
6667
{
68+
Rcc::enableLowSpeedExternalCrystal();
69+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
70+
6771
Rcc::enableInternalClock(); // 8MHz
6872
// 8MHz / 2 * 16 = 64MHz
6973
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_g071rb/board.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ using namespace modm::literals;
2929
struct SystemClock
3030
{
3131
static constexpr uint32_t Frequency = 64_MHz;
32-
static constexpr uint32_t Ahb = Frequency;
33-
static constexpr uint32_t Apb = Frequency;
32+
static constexpr uint32_t Ahb = Frequency;
33+
static constexpr uint32_t Apb = Frequency;
3434

3535
static constexpr uint32_t Aes = Ahb;
3636
static constexpr uint32_t Rng = Ahb;
@@ -72,16 +72,19 @@ struct SystemClock
7272
static constexpr uint32_t Spi2 = Apb;
7373
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
7474
static constexpr uint32_t Wwdg = Apb;
75-
static constexpr uint32_t Rtc = Apb;
7675
static constexpr uint32_t Timer14 = Apb;
7776
static constexpr uint32_t Timer7 = Apb;
7877
static constexpr uint32_t Timer6 = Apb;
7978
static constexpr uint32_t Timer3 = Apb;
8079
static constexpr uint32_t Timer2 = Apb;
80+
static constexpr uint32_t Rtc = 32.768_kHz;
8181

8282
static bool inline
8383
enable()
8484
{
85+
Rcc::enableLowSpeedExternalCrystal();
86+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
87+
8588
Rcc::enableInternalClock(); // 16MHz
8689
// (internal clock / 1_M) * 8_N / 2_R = 128MHz / 2 = 64MHz
8790
const Rcc::PllFactors pllFactors{

src/modm/board/nucleo_g474re/board.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ struct SystemClock
6666
static constexpr uint32_t I2c4 = I2c;
6767
static constexpr uint32_t Lptim = Apb1;
6868
static constexpr uint32_t Lpuart = Apb1;
69-
static constexpr uint32_t Rtc = Apb1;
7069
static constexpr uint32_t Spi2 = Apb1;
7170
static constexpr uint32_t Spi3 = Apb1;
7271
static constexpr uint32_t Uart4 = Apb1;
@@ -94,6 +93,8 @@ struct SystemClock
9493
static constexpr uint32_t Timer20 = Apb2Timer;
9594
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
9695

96+
static constexpr uint32_t Rtc = 32.768_kHz;
97+
9798
static bool inline
9899
enable()
99100
{
@@ -115,6 +116,9 @@ struct SystemClock
115116
// update frequencies for busy-wait delay functions
116117
Rcc::updateCoreFrequency<Frequency>();
117118

119+
Rcc::enableLowSpeedExternalCrystal();
120+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
121+
118122
Rcc::setCanClockSource(Rcc::CanClockSource::Pclk);
119123
return true;
120124
}

src/modm/board/nucleo_l031k6/board.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ struct SystemClock
5454

5555
static constexpr uint32_t Usart2 = Apb1;
5656
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
57+
static constexpr uint32_t Rtc = 32.768_kHz;
5758

5859
static bool inline
5960
enable()
6061
{
62+
Rcc::enableLowSpeedExternalCrystal();
63+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
64+
6165
Rcc::enableInternalClock(); // 16MHz
6266
// (internal clock / 1) * 4 / 2 = 32MHz
6367
const Rcc::PllFactors pllFactors{

0 commit comments

Comments
 (0)