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 the time set and is counting
77 *
78 * @retval false The time reported by the RTC is not valid
79 * @retval true The time has been set and the RTC is counting
80 *
81 * @see ::rtc_isenabled
82 */
83 static bool isenabled() noexcept
84 {
85 return bool(rtc_isenabled());
86 }
87
88 /** Get the current time from the RTC peripheral
89 *
90 * @return The current time in seconds
91 *
92 * @see ::rtc_read
93 */
94 static time_point now() noexcept
95 {
96 return from_time_t(rtc_read());
97 }
98
99 /** Write the current time in seconds to the RTC peripheral
100 *
101 * @param t The current time to be set in seconds.
102 *
103 * @see ::rtc_write
104 */
105 static void write(time_point t) noexcept
106 {
108 }
109
110 /** Convert a C time_t to C++ Chrono time_point
111 *
112 * @param t a @c time_t object
113 * @return a RealTimeClock::time_point object that represents the same point in time as @p t.
114 */
115 static time_point from_time_t(time_t t) noexcept
116 {
117 return time_point{std::chrono::duration_cast<duration>(std::chrono::duration<time_t>{t})};
118 }
119
120 /** Convert a C++ Chrono time_point to a C time_t
121 *
122 * @param t a RealTimeClock::time_point object
123 * @return a @c time_t object that represents the same point in time as @p t.
124 */
125 static time_t to_time_t(const time_point &t) noexcept
126 {
127 return std::chrono::duration_cast<std::chrono::duration<time_t>>(t.time_since_epoch()).count();
128 }
129
130 /** Lock the clock to ensure it stays running; dummy for API compatibility with HighResClock */
131 static void lock()
132 {
133 }
134
135 /** Unlock the clock, allowing it to stop during power saving; dummy for API compatibility with HighResClock */
136 static void unlock()
137 {
138 }
139};
140
141/** @}*/
142
143}
144#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 the time set 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 the time set 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.