Skip to content

Commit

Permalink
[examples] Add RTC example on NUCLEO-G071RB/G474RE
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Dec 29, 2024
1 parent c2016bf commit 883f839
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
42 changes: 42 additions & 0 deletions examples/generic/rtc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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();
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
const bool inited = Rtc::initialize<Board::SystemClock>();
if (not inited) { MODM_LOG_INFO << "RTC was already initialized." << modm::endl; }

constexpr auto cdt = modm::DateTime::fromBuildTime();
if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt);
MODM_LOG_INFO << "Compile DateTime: " << cdt << " weekday: " << cdt.weekday().c_encoding() << modm::endl;


while (true)
{
const auto dt = Rtc::dateTime();
const auto now = Rtc::now();

const char *const wd = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
MODM_LOG_INFO << dt << " (" << (wd + dt.weekday().c_encoding()*4) << ") = ";
MODM_LOG_INFO << now.time_since_epoch().count() << "ms since 1970" << modm::endl;

modm::delay(1.1s);
}

return 0;
}
11 changes: 11 additions & 0 deletions examples/generic/rtc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-g071rb</extends>
<!-- <extends>modm:nucleo-g474re</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/rtc</option>
</options>
<modules>
<module>modm:platform:rtc</module>
<module>modm:build:scons</module>
</modules>
</library>
6 changes: 5 additions & 1 deletion src/modm/board/nucleo_g071rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ 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

0 comments on commit 883f839

Please sign in to comment.