-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
541 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
# -*- 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/. | ||
# ----------------------------------------------------------------------------- | ||
|
||
def init(module): | ||
module.name = ":platform:rtc" | ||
module.description = "Real Time Clock (RTC)" | ||
|
||
def prepare(module, options): | ||
device = options[":target"] | ||
if not device.has_driver("rtc:stm32*") or device.identifier.family in ["f1"]: | ||
return False | ||
|
||
module.depends( | ||
":cmsis:device", | ||
":platform:rcc", | ||
":architecture:register", | ||
":math:calendar", | ||
) | ||
|
||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/platform/rtc" | ||
target = env[":target"].identifier | ||
env.substitutions = { | ||
# F1, F2, L1 do not have the RTC->SSR register. | ||
# (Some L1 device do have a SSR field, but the CMSIS headers are inconsistent). | ||
"with_ssr": target.family not in ["f1", "f2", "l1"], | ||
# F2, L1 have a smaller PREDIV_S register field. | ||
"bits_prediv_s": 13 if target.family in ["f2", "l1"] else 15, | ||
} | ||
env.template("rtc.hpp.in") | ||
env.template("rtc_impl.hpp.in") | ||
env.copy("rtc.cpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* 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 "rtc.hpp" | ||
|
||
extern "C" int | ||
_gettimeofday(struct timeval *tp, void *) | ||
{ | ||
*tp = modm::platform::Rtc::timeval(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#ifndef MODM_STM32_RTC_HPP | ||
#define MODM_STM32_RTC_HPP | ||
|
||
#include <chrono> | ||
#include <ctime> | ||
#include <sys/time.h> | ||
|
||
#include <modm/architecture.hpp> | ||
#include <modm/math/calendar/date_time.hpp> | ||
|
||
namespace modm::platform | ||
{ | ||
|
||
/** | ||
* Real Time Clock (RTC) control for STM32 devices | ||
* | ||
* @author Niklas Hauser | ||
* @author Rasmus Kleist Hørlyck Sørensen | ||
* @ingroup modm_platform_rtc | ||
*/ | ||
class Rtc : public modm::PeripheralDriver | ||
{ | ||
public: | ||
%% if with_ssr | ||
using duration = std::chrono::milliseconds; | ||
%% else | ||
using duration = std::chrono::seconds; | ||
%% endif | ||
using rep = duration::rep; | ||
using period = duration::period; | ||
using time_point = std::chrono::time_point<Rtc, duration>; | ||
static constexpr bool is_steady = true; | ||
|
||
static time_point | ||
now() noexcept; | ||
|
||
static std::time_t | ||
to_time_t(const time_point& t) noexcept | ||
{ | ||
return std::time_t(duration_cast<std::chrono::seconds>(t.time_since_epoch()).count()); | ||
} | ||
|
||
static time_point | ||
from_time_t(std::time_t t) noexcept | ||
{ | ||
using from_t = std::chrono::time_point<Rtc, std::chrono::seconds>; | ||
return time_point_cast<duration>(from_t(std::chrono::seconds(t))); | ||
} | ||
|
||
public: | ||
// Optimized version that returns seconds since epoch | ||
static std::time_t | ||
time_t(); | ||
|
||
static struct timeval | ||
timeval(); | ||
|
||
static modm::DateTime | ||
dateTime(); | ||
|
||
static void | ||
setDateTime(const modm::DateTime &dt); | ||
|
||
public: | ||
static void | ||
enable(); | ||
|
||
static void | ||
disable(); | ||
|
||
template< class SystemClock > | ||
static bool | ||
initialize(); | ||
|
||
/** | ||
* Synchronized to a remote clock with a high degree of precision | ||
* | ||
* @tparam Rep | ||
* an arithmetic type representing the number of ticks | ||
* @tparam Period | ||
* a std::ratio representing the tick period (i.e. the number of second's fractions per tick) | ||
* | ||
* @param delay The amount of time to delay (or advance) the | ||
* @param waitCycle Number of cycles to wait for the INITF bit to be set. (default = 2048) | ||
* | ||
* @return True on success | ||
*/ | ||
// template< typename Rep, typename Period > | ||
// static bool | ||
// synchronize(std::chrono::duration<Rep, Period> delay, uint32_t waitCycles = 2048); | ||
|
||
private: | ||
/// Unlock RTC register write protection | ||
static void | ||
unlock(); | ||
|
||
/// Lock RTC register write protection | ||
static void | ||
lock(); | ||
%# | ||
%% if with_ssr | ||
static uint16_t | ||
%% else | ||
static void | ||
%% endif | ||
read(); | ||
|
||
static void | ||
update_cache(); | ||
|
||
struct Data | ||
{ | ||
union | ||
{ | ||
struct | ||
{ | ||
uint8_t second; | ||
uint8_t minute; | ||
uint8_t hour; | ||
} modm_packed; | ||
uint32_t time32; | ||
}; | ||
union | ||
{ | ||
struct | ||
{ | ||
uint8_t weekday; | ||
uint8_t day; | ||
uint8_t month; | ||
uint8_t year; | ||
} modm_packed; | ||
uint32_t date32; | ||
}; | ||
}; | ||
|
||
static inline Data data{}; | ||
%# | ||
%% if with_ssr | ||
static inline uint64_t cache_time_milliseconds{}; | ||
%% endif | ||
static inline uint32_t cache_time_seconds{}; | ||
static inline uint32_t cache_date_seconds{}; | ||
static inline uint32_t cache_time{}; | ||
static inline uint32_t cache_date{}; | ||
%# | ||
%% if with_ssr | ||
static inline uint32_t (*t2ms)(uint32_t) = [](uint32_t) { return 0ul; }; | ||
static inline uint32_t (*ms2t)(uint32_t) = [](uint32_t) { return 0ul; }; | ||
%% endif | ||
%# | ||
static constexpr uint16_t epoch{1970}; | ||
}; | ||
|
||
} // namespace modm::platform | ||
|
||
#include "rtc_impl.hpp" | ||
|
||
#endif // MODM_STM32_RTC_HPP |
Oops, something went wrong.