Skip to content

Commit f402893

Browse files
committed
[example] Add generic RTC for most STM32 boards
1 parent 42f3599 commit f402893

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
<extends>modm:nucleo-l432kc</extends>
18+
19+
<!-- <extends>modm:disco-f303vc</extends> -->
20+
<!-- <extends>modm:nucleo-f303k8</extends> -->
21+
<!-- <extends>modm:nucleo-f334r8</extends> -->
22+
23+
24+
<!-- <extends>modm:black-pill-f401</extends> -->
25+
<!-- <extends>modm:nucleo-f401re</extends> -->
26+
<!-- <extends>modm:disco-f407vg</extends> -->
27+
<!-- <extends>modm:black-pill-f411</extends> -->
28+
<!-- <extends>modm:nucleo-f411re</extends> -->
29+
<!-- <extends>modm:nucleo-f429zi</extends> -->
30+
<!-- <extends>modm:disco-f429zi</extends> -->
31+
<!-- <extends>modm:nucleo-f446re</extends> -->
32+
<!-- <extends>modm:disco-f469ni</extends> -->
33+
34+
<!-- <extends>modm:nucleo-g474re</extends> -->
35+
36+
<!-- <extends>modm:disco-f746ng</extends> -->
37+
<!-- <extends>modm:nucleo-h723zg</extends> -->
38+
<!-- <extends>modm:nucleo-h743zi</extends> -->
39+
<!-- <extends>modm:nucleo-u575zi-q</extends> -->
40+
<options>
41+
<option name="modm:build:build.path">../../../build/generic/rtc</option>
42+
<!-- <option name="modm:build:openocd.cfg">openocd.cfg</option> -->
43+
</options>
44+
<modules>
45+
<module>modm:platform:rtc</module>
46+
<module>modm:build:scons</module>
47+
</modules>
48+
</library>

0 commit comments

Comments
 (0)