Mbed OS Reference
Loading...
Searching...
No Matches
RealTimeClock.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef MBED_REALTIMECLOCK_H
18#define MBED_REALTIMECLOCK_H
19
20#include <chrono>
21#include "hal/rtc_api.h"
22
23namespace mbed {
24/**
25 * \defgroup drivers_RealTimeClock RealTimeClock class
26 * \ingroup drivers-public-api-ticker
27 * @{
28 */
29
30/**
31 * An implementation of a C++11 Clock representing the HAL real-time clock.
32 *
33 * This is intended to be usable as a substitute for @c std::chrono::system_clock,
34 * but is provided as a separate class, due to inconsistencies in retargetting
35 * support between toolchains.
36 *
37 * This is only a thin wrapper around the HAL RTC API, and as such it requires the
38 * same use of initialization.
39 *
40 */
42public:
43 using duration = std::chrono::seconds;
44 using rep = duration::rep;
45 using period = duration::period;
46 using time_point = std::chrono::time_point<RealTimeClock>;
47 static const bool is_steady = false;
48
49 /** Initialize the RTC peripheral
50 *
51 * Power up the RTC in preparation for access. This function must be called
52 * before any other RealTimeClock methods are called. This does not change the state
53 * of the RTC. It just enables access to it.
54 *
55 * @see ::rtc_init
56 */
57 static void init()
58 {
59 return rtc_init();
60 }
61
62 /** Deinitialize RTC
63 *
64 * Powerdown the RTC in preparation for sleep, powerdown or reset. That should only
65 * affect the CPU domain and not the time keeping logic.
66 * After this function is called no other RealTimeClock methods should be called
67 * except for RealTimeClock::init.
68 *
69 * @see ::rtc_free
70 */
71 static void free()
72 {
73 return rtc_free();
74 }
75
76 /** Check if the RTC has been initialized and is counting
77 *
78 * @retval false The time reported by the RTC is not valid
79 * @retval true The RTC has been initialized and is counting
80 *
81 * @note In versions of Mbed before 7.0, this function claimed to return false "if the time had not been set."
82 * However, this was flawed because, in most implementations, it only returned true if the time had been
83 * set *on this boot*. There wasn't, and isn't, a way to detect whether the time was set correctly by a previous
84 * boot. To answer that question, you may wish to check if the time is approximately valid (in the 21st
85 * century), or add another flag at the application level.
86 *
87 * @see ::rtc_isenabled
88 */
89 static bool isenabled() noexcept
90 {
91 return bool(rtc_isenabled());
92 }
93
94 /** Get the current time from the RTC peripheral
95 *
96 * @return The current time in seconds
97 *
98 * @see ::rtc_read
99 */
100 static time_point now() noexcept
101 {
102 return from_time_t(rtc_read());
103 }
104
105 /** Write the current time in seconds to the RTC peripheral
106 *
107 * @param t The current time to be set in seconds.
108 *
109 * @see ::rtc_write
110 */
111 static void write(time_point t) noexcept
112 {
114 }
115
116 /** Convert a C time_t to C++ Chrono time_point
117 *
118 * @param t a @c time_t object
119 * @return a RealTimeClock::time_point object that represents the same point in time as @p t.
120 */
121 static time_point from_time_t(time_t t) noexcept
122 {
123 return time_point{std::chrono::duration_cast<duration>(std::chrono::duration<time_t>{t})};
124 }
125
126 /** Convert a C++ Chrono time_point to a C time_t
127 *
128 * @param t a RealTimeClock::time_point object
129 * @return a @c time_t object that represents the same point in time as @p t.
130 */
131 static time_t to_time_t(const time_point &t) noexcept
132 {
133 return std::chrono::duration_cast<std::chrono::duration<time_t>>(t.time_since_epoch()).count();
134 }
135
136 /** Lock the clock to ensure it stays running; dummy for API compatibility with HighResClock */
137 static void lock()
138 {
139 }
140
141 /** Unlock the clock, allowing it to stop during power saving; dummy for API compatibility with HighResClock */
142 static void unlock()
143 {
144 }
145};
146
147/** @}*/
148
149}
150#endif /* MBED_TICKERCLOCK_H */
An implementation of a C++11 Clock representing the HAL real-time clock.
static time_t to_time_t(const time_point &t) noexcept
Convert a C++ Chrono time_point to a C time_t.
static void unlock()
Unlock the clock, allowing it to stop during power saving; dummy for API compatibility with HighResCl...
static time_point from_time_t(time_t t) noexcept
Convert a C time_t to C++ Chrono time_point.
static void lock()
Lock the clock to ensure it stays running; dummy for API compatibility with HighResClock.
static time_point now() noexcept
Get the current time from the RTC peripheral.
static void free()
Deinitialize RTC.
static bool isenabled() noexcept
Check if the RTC has been initialized and is counting.
static void write(time_point t) noexcept
Write the current time in seconds to the RTC peripheral.
static void init()
Initialize the RTC peripheral.
time_t rtc_read(void)
Get the current time from the RTC peripheral.
int rtc_isenabled(void)
Check if the RTC has been initialized and is counting.
void rtc_write(time_t t)
Write the current time in seconds to the RTC peripheral.
void rtc_init(void)
Initialize the RTC peripheral.
void rtc_free(void)
Deinitialize RTC.