Skip to content

Commit 4d8e790

Browse files
committed
[examples] Add RTC example on NUCLEO-G071RB/G474RE
1 parent 6a30c5c commit 4d8e790

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

examples/generic/rtc/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
21+
Rtc::initialize<Board::SystemClock>();
22+
23+
constexpr auto cdt = modm::DateTime::fromBuildTime();
24+
MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl;
25+
// MODM_LOG_INFO << "Compile Weekday: " << cdt.weekday().c_encoding() << modm::endl;
26+
27+
if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt);
28+
29+
while (true)
30+
{
31+
const auto dt = Rtc::dateTime();
32+
const auto now = Rtc::now();
33+
MODM_LOG_INFO << dt << modm::endl;
34+
MODM_LOG_INFO << now.time_since_epoch().count() << modm::endl;
35+
// MODM_LOG_INFO << "Weekday: " << dt.weekday().c_encoding() << modm::endl;
36+
modm::delay(0.9s);
37+
}
38+
39+
return 0;
40+
}

examples/generic/rtc/project.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<library>
2+
<extends>modm:nucleo-g071rb</extends>
3+
<!-- <extends>modm:nucleo-g474re</extends> -->
4+
<options>
5+
<option name="modm:build:build.path">../../../build/generic/rtc</option>
6+
</options>
7+
<modules>
8+
<module>modm:platform:rtc</module>
9+
<module>modm:build:scons</module>
10+
</modules>
11+
</library>

src/modm/board/nucleo_g071rb/board.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,20 @@ 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;
8180

81+
static constexpr uint32_t Rtc = 32.768_kHz;
82+
8283
static bool inline
8384
enable()
8485
{
86+
Rcc::enableLowSpeedExternalCrystal();
87+
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);
88+
8589
Rcc::enableInternalClock(); // 16MHz
8690
// (internal clock / 1_M) * 8_N / 2_R = 128MHz / 2 = 64MHz
8791
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
}

0 commit comments

Comments
 (0)