From 621b2616172fee6d139d18b18bd48acb70f7da25 Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Fri, 27 Dec 2024 22:56:11 +0100 Subject: [PATCH] [examples] Add RTC example on NUCLEO-G071RB/G474RE --- examples/nucleo_g071rb/rtc/main.cpp | 40 ++++++++++++++++++++++++++ examples/nucleo_g071rb/rtc/project.xml | 10 +++++++ examples/nucleo_g474re/rtc/main.cpp | 39 +++++++++++++++++++++++++ examples/nucleo_g474re/rtc/project.xml | 12 ++++++++ src/modm/board/nucleo_g071rb/board.hpp | 6 +++- src/modm/board/nucleo_g474re/board.hpp | 6 +++- 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 examples/nucleo_g071rb/rtc/main.cpp create mode 100644 examples/nucleo_g071rb/rtc/project.xml create mode 100644 examples/nucleo_g474re/rtc/main.cpp create mode 100644 examples/nucleo_g474re/rtc/project.xml diff --git a/examples/nucleo_g071rb/rtc/main.cpp b/examples/nucleo_g071rb/rtc/main.cpp new file mode 100644 index 0000000000..ae02b578f6 --- /dev/null +++ b/examples/nucleo_g071rb/rtc/main.cpp @@ -0,0 +1,40 @@ +// 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 + +int +main() +{ + Board::initialize(); + MODM_LOG_INFO << "Initialize RTC" << modm::endl; + Rtc::initialize(); + + constexpr auto cdt = modm::DateTime::fromBuildTime(); + MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl; + // MODM_LOG_INFO << "Compile Weekday: " << cdt.weekday().c_encoding() << modm::endl; + + if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt); + + while (true) + { + const auto dt = Rtc::dateTime(); + const auto now = Rtc::now(); + MODM_LOG_INFO << dt << modm::endl; + MODM_LOG_INFO << now.time_since_epoch().count() << modm::endl; + // MODM_LOG_INFO << "Weekday: " << dt.weekday().c_encoding() << modm::endl; + modm::delay(0.9s); + } + + return 0; +} diff --git a/examples/nucleo_g071rb/rtc/project.xml b/examples/nucleo_g071rb/rtc/project.xml new file mode 100644 index 0000000000..1d85980e88 --- /dev/null +++ b/examples/nucleo_g071rb/rtc/project.xml @@ -0,0 +1,10 @@ + + modm:nucleo-g071rb + + + + + modm:platform:rtc + modm:build:scons + + diff --git a/examples/nucleo_g474re/rtc/main.cpp b/examples/nucleo_g474re/rtc/main.cpp new file mode 100644 index 0000000000..97e6ac8154 --- /dev/null +++ b/examples/nucleo_g474re/rtc/main.cpp @@ -0,0 +1,39 @@ +// 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 + +int +main() +{ + Board::initialize(); + MODM_LOG_INFO << "Initialize RTC" << modm::endl; + Rtc::initialize(); + + constexpr auto cdt = modm::DateTime::fromBuildTime(); + MODM_LOG_INFO << "__TIMESTAMP__: " << modm::DateTime::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(0.9s); + } + + return 0; +} diff --git a/examples/nucleo_g474re/rtc/project.xml b/examples/nucleo_g474re/rtc/project.xml new file mode 100644 index 0000000000..f2a8865c21 --- /dev/null +++ b/examples/nucleo_g474re/rtc/project.xml @@ -0,0 +1,12 @@ + + modm:nucleo-g474re + + + + + modm:debug + modm:platform:rtc + modm:processing:timer + modm:build:scons + + diff --git a/src/modm/board/nucleo_g071rb/board.hpp b/src/modm/board/nucleo_g071rb/board.hpp index 8fd2ac808e..80ed1f92ca 100644 --- a/src/modm/board/nucleo_g071rb/board.hpp +++ b/src/modm/board/nucleo_g071rb/board.hpp @@ -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{ diff --git a/src/modm/board/nucleo_g474re/board.hpp b/src/modm/board/nucleo_g474re/board.hpp index 31005e0eb4..4c5ac64d37 100644 --- a/src/modm/board/nucleo_g474re/board.hpp +++ b/src/modm/board/nucleo_g474re/board.hpp @@ -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; @@ -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() { @@ -115,6 +116,9 @@ struct SystemClock // update frequencies for busy-wait delay functions Rcc::updateCoreFrequency(); + Rcc::enableLowSpeedExternalCrystal(); + Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal); + Rcc::setCanClockSource(Rcc::CanClockSource::Pclk); return true; }