Mbed OS Reference
Loading...
Searching...
No Matches
rtc_api.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 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
18/** \addtogroup hal */
19/** @{*/
20
21#ifndef MBED_RTC_API_H
22#define MBED_RTC_API_H
23
24#include "device.h"
25
26#include <time.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * \defgroup hal_rtc RTC hal
34 *
35 * The RTC hal provides a low level interface to the Real Time Counter (RTC) of a
36 * target.
37 *
38 * # Defined behaviour
39 * * The function ::rtc_init is safe to call repeatedly - Verified by test rtc_init_test.
40 * * RTC accuracy is at least 10% - Verified by test rtc_accuracy_test.
41 * * Init/free doesn't stop RTC from counting - Verified by test rtc_persist_test.
42 * * Software reset doesn't stop RTC from counting - Verified by test rtc_reset_test.
43 * * Sleep modes don't stop RTC from counting - Verified by test rtc_sleep_test.
44 * * Shutdown mode doesn't stop RTC from counting - Not verified.
45 * * The functions ::rtc_write/::rtc_read provides availability to set/get RTC time
46 * - Verified by test rtc_write_read_test.
47 * * The function ::rtc_isenabled returns 1 if the RTC has been initialized and is counting and 0 otherwise - Verified by test rtc_enabled_test.
48 * * ::rtc_read may be called before rtc_write. If the RTC time has not been set, this will return the
49 * time since some arbitrary epoch. If the RTC time was set on a previous boot, this will return time
50 * based on what was set then.
51 *
52 * # Undefined behaviour
53 * * Calling any function other than ::rtc_init before the initialisation of the RTC
54 *
55 * # Potential bugs
56 * * Incorrect overflow handling - Verified by rtc_range_test
57 * * Glitches due to ripple counter - Verified by rtc_glitch_test
58 *
59 * @see hal_rtc_tests
60 *
61 * @{
62 */
63
64/**
65 * \defgroup hal_rtc_tests RTC hal tests
66 * The RTC test validate proper implementation of the RTC hal.
67 *
68 * To run the RTC hal tests use the command:
69 *
70 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-rtc*
71 */
72
73
74/** Initialize the RTC peripheral
75 *
76 * Powerup the RTC in perpetration for access. This function must be called
77 * before any other RTC functions ares called. This does not change the state
78 * of the RTC. It just enables access to it.
79 *
80 * @note This function is safe to call repeatedly - Tested by rtc_init_test
81 *
82 * Example Implementation Pseudo Code:
83 * @code
84 * void rtc_init()
85 * {
86 * // Enable clock gate so processor can read RTC registers
87 * POWER_CTRL |= POWER_CTRL_RTC_Msk;
88 *
89 * // See if the RTC is already setup
90 * if (!(RTC_STATUS & RTC_STATUS_COUNTING_Msk)) {
91 *
92 * // Setup the RTC clock source
93 * RTC_CTRL |= RTC_CTRL_CLK32_Msk;
94 * }
95 * }
96 * @endcode
97 */
98void rtc_init(void);
99
100/** Deinitialize RTC
101 *
102 * Powerdown the RTC in preparation for sleep, powerdown or reset. That should only
103 * affect the CPU domain and not the time keeping logic.
104 * After this function is called no other RTC functions should be called
105 * except for ::rtc_init.
106 *
107 * @note This function does not stop the RTC from counting - Tested by rtc_persist_test
108 *
109 * Example Implementation Pseudo Code:
110 * @code
111 * void rtc_free()
112 * {
113 * // Disable clock gate since processor no longer needs to read RTC registers
114 * POWER_CTRL &= ~POWER_CTRL_RTC_Msk;
115 * }
116 * @endcode
117 *
118 * @note Implementations are allowed to not implement this function if it's impossible or there is
119 * no significant benefit to disabling the RTC. As such, the RTC is allowed to remain initialized
120 * after this function is called if freeing the RTC is not implemented.
121 */
122void rtc_free(void);
123
124/** Check if the RTC has been initialized and is counting
125 *
126 * @retval 0 The time reported by the RTC is not valid
127 * @retval 1 The RTC has been initialized and is counting
128 *
129 * @note In versions of Mbed before 7.0, this function claimed to return false "if the time had not been set."
130 * However, this was flawed because, in most implementations, it only returned true if the time had been
131 * set *on this boot*. There wasn't, and isn't, a way to detect whether the time was set correctly by a previous
132 * boot. To answer that question, you may wish to check if the time is approximately valid (in the 21st
133 * century), or add another flag at the application level.
134 *
135 * Example Implementation Pseudo Code:
136 * @code
137 * int rtc_isenabled()
138 * {
139 * if (RTC_STATUS & RTC_STATUS_COUNTING_Msk) {
140 * return 1;
141 * } else {
142 * return 0;
143 * }
144 * }
145 * @endcode
146 */
148
149/** Get the current time from the RTC peripheral
150 *
151 * @return The current time in seconds
152 *
153 * @note Some RTCs are not synchronized with the main clock. If
154 * this is the case with your RTC then you must read the RTC time
155 * in a loop to prevent reading the wrong time due to a glitch.
156 * The test rtc_glitch_test is intended to catch this bug.
157 *
158 * Example implementation for an unsynchronized ripple counter:
159 * @code
160 * time_t rtc_read()
161 * {
162 * uint32_t val;
163 * uint32_t last_val;
164 *
165 * // Loop until the same value is read twice
166 * val = RTC_SECONDS;
167 * do {
168 * last_val = val;
169 * val = RTC_SECONDS;
170 * } while (last_val != val);
171 *
172 * return (time_t)val;
173 * }
174 * @endcode
175 */
176time_t rtc_read(void);
177
178/** Write the current time in seconds to the RTC peripheral
179 *
180 * @param t The current time to be set in seconds.
181 *
182 * Example Implementation Pseudo Code:
183 * @code
184 * void rtc_write(time_t t)
185 * {
186 * RTC_SECONDS = t;
187 * }
188 * @endcode
189 */
190void rtc_write(time_t t);
191
192/**@}*/
193
194#ifdef __cplusplus
195}
196#endif
197
198#endif
199
200/** @}*/
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.