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 22, 2024
1 parent 91ee5c9 commit 25e4529
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 2 deletions.
109 changes: 109 additions & 0 deletions examples/nucleo_g071rb/rtc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// coding: utf-8
/*
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
*
* 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>

#include <charconv>
#include <string_view>

class CompileDateTime
{
static consteval uint16_t to_uint(uint8_t offset, uint8_t length)
{
auto const str = timestamp().substr(offset, length);
int integer;
auto result = std::from_chars(str.begin(), str.end(), integer);
return uint16_t(integer);
}
static constexpr std::string_view weekdays[]
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static constexpr std::string_view months[]
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
public:
static consteval std::string_view timestamp()
{ return __TIMESTAMP__; }

static consteval std::chrono::year year()
{ return std::chrono::year{to_uint(20, 4)}; }

static consteval std::chrono::month month()
{
for (uint8_t i = 0; i < 12; ++i)
if (months[i] == timestamp().substr(4, 3))
return std::chrono::month{i+1};
return std::chrono::month{0};
}

static consteval std::chrono::day day()
{ return std::chrono::day{to_uint(8, 2)}; }

static consteval std::chrono::weekday weekday()
{
for (uint8_t i = 0; i < 7; ++i)
if (weekdays[i] == timestamp().substr(0, 3))
return std::chrono::weekday{i};
return std::chrono::weekday{0};
}

static consteval std::chrono::hours hour()
{ return std::chrono::hours{to_uint(11, 2)}; }

static consteval std::chrono::minutes minute()
{ return std::chrono::minutes{to_uint(14, 2)}; }

static consteval std::chrono::seconds second()
{ return std::chrono::seconds{to_uint(17, 2)}; }
};

#include <inttypes.h>
#include <modm/io/iostream.hpp>

namespace modm
{

/// @ingroup modm_platform_rtc
inline modm::IOStream&
operator << (modm::IOStream& s, const CompileDateTime& dt)
{
// 2024-12-22 18:39:21
s.printf("%04" PRIu16 "-%02" PRIu8 "-%02" PRIu8 " %02" PRIu8 ":%02" PRIu8 ":%02" PRIu8 ".000",
uint16_t(int(dt.year())), uint8_t(unsigned(dt.month())), uint8_t(unsigned(dt.day())),
uint8_t(dt.hour().count()), uint8_t(dt.minute().count()), uint8_t(dt.second().count()));
return s;
}

} // modm namespace

int
main()
{
Board::initialize();
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
Rtc::initialize<Board::SystemClock>();

const auto cdt = CompileDateTime();
MODM_LOG_INFO << "__TIMESTAMP__: " << __TIMESTAMP__ << modm::endl;
MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl;
// MODM_LOG_INFO << "Compile Weekday: " << cdt.weekday().c_encoding() << modm::endl;

Rtc::setDateTime(cdt);

while (true)
{
const auto& dt = Rtc::dateTime();
MODM_LOG_INFO << dt << modm::endl;
// MODM_LOG_INFO << "Weekday: " << dt.weekday().c_encoding() << modm::endl;
modm::delay(1s);
}

return 0;
}
10 changes: 10 additions & 0 deletions examples/nucleo_g071rb/rtc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:nucleo-g071rb</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g071rb/rtc</option>
</options>
<modules>
<module>modm:platform:rtc</module>
<module>modm:build:scons</module>
</modules>
</library>
32 changes: 32 additions & 0 deletions examples/nucleo_g474re/rtc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// coding: utf-8
/*
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen
*
* 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();
Rtc::initialize<Board::SystemClock>();

while (true)
{
const auto& dt = Rtc::read();
MODM_LOG_INFO << "Date: " << unsigned(dt.day()) << "-" << unsigned(dt.month()) << "-" << int(dt.year()) << modm::endl;
MODM_LOG_INFO << "Weekday: " << dt.weekday().c_encoding() << modm::endl;
MODM_LOG_INFO << "Time: " << dt.hour().count() << ":" << dt.minute().count() << ":" << dt.second().count() << modm::endl;
MODM_LOG_INFO << "Sub Second: " << dt.millisecond().count() << modm::endl;
modm::delay(1s);
}

return 0;
}
12 changes: 12 additions & 0 deletions examples/nucleo_g474re/rtc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:nucleo-g474re</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/rtc</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:rtc</module>
<module>modm:processing:timer</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 25e4529

Please sign in to comment.