Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stm32] Add RTC driver #1242

Merged
merged 5 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,31 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">✕</td>
<td align="center">✕</td>
</tr><tr>
<td align="left">RTC</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">○</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
</tr><tr>
<td align="left">SPI</td>
<td align="center">✅</td>
<td align="center">✅</td>
Expand Down
59 changes: 59 additions & 0 deletions examples/generic/rtc/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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();
Board::Leds::setOutput();
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "Initialize RTC" << modm::endl;
#endif
const bool inited = Rtc::initialize<Board::SystemClock>();
#ifdef MODM_BOARD_HAS_LOGGER
if (not inited) { MODM_LOG_INFO << "RTC was already initialized." << modm::endl; }
#endif

constexpr auto cdt = modm::DateTime::fromBuildTime();
if (Rtc::dateTime() < cdt) Rtc::setDateTime(cdt);
#ifdef MODM_BOARD_HAS_LOGGER
MODM_LOG_INFO << "Compile DateTime: " << cdt << modm::endl;
MODM_LOG_INFO << "YMD: " << cdt.year_month_day() << modm::endl;
MODM_LOG_INFO << "HMS: " << cdt.hh_mm_ss() << modm::endl;
MODM_LOG_INFO << "Weekday: " << cdt.weekday() << modm::endl;
#endif


while (true)
{
const auto dt = Rtc::dateTime();
#ifdef MODM_BOARD_HAS_LOGGER
const auto now = Rtc::now();
MODM_LOG_INFO << dt << " (" << dt.weekday() << ") = " << now << " since 1970" << modm::endl;
modm::delay(1.1s);
#else
static uint8_t prev_second{};
if (prev_second != dt.seconds().count())
{
prev_second = dt.seconds().count();
Board::Leds::toggle();
}
modm::delay(10ms);
#endif

}

return 0;
}
2 changes: 2 additions & 0 deletions examples/generic/rtc/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink.cfg]
48 changes: 48 additions & 0 deletions examples/generic/rtc/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<library>
<!-- <extends>modm:nucleo-c031c6</extends> -->

<!-- <extends>modm:nucleo-g071rb</extends> -->

<!-- <extends>modm:stm32f030_demo</extends> -->
<!-- <extends>modm:disco-f051r8</extends> -->
<!-- <extends>modm:disco-f072rb</extends> -->
<!-- <extends>modm:nucleo-f072rb</extends> -->
<!-- <extends>modm:nucleo-f091rc</extends> -->

<!-- <extends>modm:nucleo-l053r8</extends> -->
<!-- <extends>modm:nucleo-l152re</extends> -->
<!-- <extends>modm:disco-l152rc</extends> -->
<!-- <extends>modm:nucleo-l476rg</extends> -->
<!-- <extends>modm:disco-l476vg</extends> -->
<extends>modm:nucleo-l432kc</extends>

<!-- <extends>modm:disco-f303vc</extends> -->
<!-- <extends>modm:nucleo-f303k8</extends> -->
<!-- <extends>modm:nucleo-f334r8</extends> -->


<!-- <extends>modm:black-pill-f401</extends> -->
<!-- <extends>modm:nucleo-f401re</extends> -->
<!-- <extends>modm:disco-f407vg</extends> -->
<!-- <extends>modm:black-pill-f411</extends> -->
<!-- <extends>modm:nucleo-f411re</extends> -->
<!-- <extends>modm:nucleo-f429zi</extends> -->
<!-- <extends>modm:disco-f429zi</extends> -->
<!-- <extends>modm:nucleo-f446re</extends> -->
<!-- <extends>modm:disco-f469ni</extends> -->

<!-- <extends>modm:nucleo-g474re</extends> -->

<!-- <extends>modm:disco-f746ng</extends> -->
<!-- <extends>modm:nucleo-h723zg</extends> -->
<!-- <extends>modm:nucleo-h743zi</extends> -->
<!-- <extends>modm:nucleo-u575zi-q</extends> -->
<options>
<option name="modm:build:build.path">../../../build/generic/rtc</option>
<!-- <option name="modm:build:openocd.cfg">openocd.cfg</option> -->
</options>
<modules>
<module>modm:platform:rtc</module>
<module>modm:build:scons</module>
</modules>
</library>
4 changes: 4 additions & 0 deletions src/modm/board/black_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/black_pill_f411/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/blue_pill_f103/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal();

// external clock * 9 = 72MHz, => 72/1.5 = 48 => good for USB
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/devebox_stm32f4xx/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ struct SystemClock
static constexpr uint32_t Timer13 = Apb1Timer;
static constexpr uint32_t Timer14 = Apb1Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/devebox_stm32h750vb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz; // From PLL3Q
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 25MHz
Rcc::setVoltageScaling(Rcc::VoltageScaling::Scale0); // required for 400MHz/480MHz
const Rcc::PllFactors pllFactors1{
Expand Down
12 changes: 8 additions & 4 deletions src/modm/board/disco_f051r8/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ using namespace modm::literals;
/// STM32F0 running at 48MHz generated from the internal 8MHz with PLL.
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr int Usart1 = Frequency;
static constexpr int Usart2 = Frequency;
static constexpr int Spi2 = Frequency;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Usart1 = Frequency;
static constexpr uint32_t Usart2 = Frequency;
static constexpr uint32_t Spi2 = Frequency;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

// enable internal 8 MHz HSI RC clock
Rcc::enableInternalClock();
// (internal clock / 2) * 12 = 48MHz
Expand Down
6 changes: 5 additions & 1 deletion src/modm/board/disco_f072rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using namespace modm::literals;
/// STM32F072 running at 48MHz generated from the internal 48MHz clock
struct SystemClock
{
static constexpr int Frequency = 48_MHz;
static constexpr uint32_t Frequency = 48_MHz;
static constexpr uint32_t Ahb = Frequency;
static constexpr uint32_t Apb = Frequency;

Expand Down Expand Up @@ -60,10 +60,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

// Enable the internal 48MHz clock
Rcc::enableInternalClockMHz48();
// set flash latency for 48MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f100rb/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ struct SystemClock
static constexpr uint32_t Timer16 = Apb2Timer;
static constexpr uint32_t Timer17 = Apb2Timer;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllMul = 3,
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f303vc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ struct SystemClock

static constexpr uint32_t Usb = Ahb / 1.5;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalClock(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllMul = 9,
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f401vc/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f407vg/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M=4 -> 2MHz
Expand Down
8 changes: 7 additions & 1 deletion src/modm/board/disco_f411ve/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ struct SystemClock
static constexpr uint32_t Timer11 = Apb2Timer;

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline enable()
static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8MHz
const Rcc::PllFactors pllFactors{
.pllM = 7, // 8MHz / M=7 -> ~1.14MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f429zi/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = Rcc::LsiFrequency;

static bool inline
enable()
{
Rcc::enableLowSpeedInternalClock();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::Lsi);

Rcc::enableExternalCrystal(); // 8 MHz
const Rcc::PllFactors pllFactors{
.pllM = 4, // 8MHz / M -> 2MHz
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f469ni/board.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalCrystal(); // 8 MHz
const Rcc::PllFactors pllFactors{
.pllM = 8, // 8MHz / M=8 -> 1MHz !!! Must be 1 MHz for PLLSAI !!!
Expand Down
4 changes: 4 additions & 0 deletions src/modm/board/disco_f746ng/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ struct SystemClock

static constexpr uint32_t Usb = 48_MHz;
static constexpr uint32_t Iwdg = Rcc::LsiFrequency;
static constexpr uint32_t Rtc = 32.768_kHz;

static bool inline
enable()
{
Rcc::enableLowSpeedExternalCrystal();
Rcc::enableRealTimeClock(Rcc::RealTimeClockSource::LowSpeedExternalCrystal);

Rcc::enableExternalClock(); // 25 MHz
const Rcc::PllFactors pllFactors{
.pllM = 25, // 25MHz / M=25 -> 1MHz
Expand Down
Loading
Loading