µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
chrono
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_CHRONO_CLOCKS_H_
29#define CMSIS_PLUS_CHRONO_CLOCKS_H_
30
31// ----------------------------------------------------------------------------
32
33#include <cmsis-plus/rtos/os.h>
34
35#include <chrono>
36
37// ----------------------------------------------------------------------------
38
39#pragma GCC diagnostic push
40
41#if defined(__clang__)
42#pragma clang diagnostic ignored "-Wc++98-compat"
43#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
44#endif
45
46// ----------------------------------------------------------------------------
47
48namespace os
49{
50 namespace estd
51 {
52 namespace chrono
53 {
54
55 // ======================================================================
56
57 template<typename T, typename = void>
58 struct has_sleep_for : std::false_type
59 {
60 };
61
66 template<typename T>
67 struct has_sleep_for<T,
68 decltype(std::declval<T>().has_sleep_for, void())> : std::true_type
69 {
70 };
71
81 // ======================================================================
82 // µOS++ SysTick clock.
84 {
85 public:
86
91 using period = std::ratio<1, os::rtos::clock_systick::frequency_hz>;
93 using duration = std::chrono::duration<rep, period>;
94 using sleep_duration = std::chrono::duration<sleep_rep, period>;
96 using time_point = std::chrono::time_point<systick_clock>;
97 static constexpr bool is_steady
98 { true };
99 static constexpr bool has_sleep_for
100 { true };
101
105 static time_point
106 now () noexcept;
107
108 // --------------------------------------------------------------------
109 // Extension to ISO
110
111 static void
112 sleep_for (sleep_rep ticks);
113
114 };
115
116 // Define a duration type, to be used in timeout expressions.
118
119 //constexpr systicks
120 //operator "" systicks (Ssstick_clock::rep); // C++14
121
122#pragma GCC diagnostic push
123#pragma GCC diagnostic ignored "-Waggregate-return"
124#if defined(__clang__)
125#pragma clang diagnostic ignored "-Wc++98-compat"
126// error: 'long long' is incompatible with C++98 [-Werror,-Wc++98-compat-pedantic]
127#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
128#endif
129
130 constexpr systicks
131 operator"" _ticks (unsigned long long t)
132 {
133 return systicks (static_cast<systick_clock::rep> (t));
134 }
135
136#pragma GCC diagnostic pop
137
138 // ======================================================================
139
140 // µOS++ RTC.
141
143 {
144 public:
145
148
150 using period = std::ratio<1, 1>;
152 using duration = std::chrono::duration<rep, period>;
153 using sleep_duration = std::chrono::duration<sleep_rep, period>;
154
155 using time_point = std::chrono::time_point<realtime_clock>;
156
157 // Non-monotonic, may be adjusted back in time.
158 static constexpr const bool is_steady
159 { false };
160 static constexpr bool has_sleep_for
161 { true };
162
163 static time_point
164 now () noexcept;
165
166 // --------------------------------------------------------------------
167 // Extension to ISO
168
169 static void
170 sleep_for (sleep_rep);
171
172 // Number of seconds from epoch (1 January 1970 00:00:00 UTC)
173 // Must be initialised during startup with the value of now().
174 // Realtime_clock::startup_time_point = Realtime_clock::now();
175 static time_point startup_time_point;
176 };
177
178 // ======================================================================
179
180 // The system_clock is basically derived from the SysTick counts.
181 // The counter is monotonic, and normally should not be adjusted,
182 // so the clock is steady.
183
185 {
186 public:
187
188 using duration = std::chrono::microseconds;
189 using rep = duration::rep;
190 using period = duration::period;
191 using time_point = std::chrono::time_point<system_clock>;
192
193 // Monotonic, never adjusted back in time.
194 static constexpr const bool is_steady
195 { true };
196
197 static time_point
198 now () noexcept;
199
200 // It is the only C++ clock that has the ability to map its
201 // time points to C-style time, and, therefore, to be displayed.
202 static time_t
203 to_time_t (const time_point& tp) noexcept;
204 static time_point
205 from_time_t (time_t t) noexcept;
206
207 };
208
209 // ======================================================================
210
211 // To simplify things, we assumed the system clock is already steady.
213
214 // ======================================================================
215
216 // The high resolution clock is also based on SysTick, but also uses
217 // the counter instant value, which gives 1 CPU cycle resolution.
218
220 {
221 public:
222
223 using duration = std::chrono::nanoseconds;
224 using rep = duration::rep;
225 using period = duration::period;
226 using time_point = std::chrono::time_point<high_resolution_clock>;
227
228 // Monotonic, never adjusted back in time.
229 static constexpr const bool is_steady
230 { true };
231
232 static time_point
233 now () noexcept;
234 };
235
240 } /* namespace chrono */
241 } /* namespace estd */
242} /* namespace os */
243
244// ----------------------------------------------------------------------------
245// Inline & template implementations.
246
247namespace os
248{
249 namespace estd
250 {
251 namespace chrono
252 {
253
254 // ======================================================================
255
256 inline void
257 systick_clock::sleep_for (sleep_rep ticks)
258 {
259 rtos::sysclock.sleep_for (ticks);
260 }
261
262 // ======================================================================
263
264 inline void
265 realtime_clock::sleep_for (sleep_rep secs)
266 {
267 rtos::rtclock.sleep_for (secs);
268 }
269
270 // ======================================================================
271
272#pragma GCC diagnostic push
273#pragma GCC diagnostic ignored "-Waggregate-return"
274
275 template<class _To, class Rep_T, class Period_T>
276constexpr typename std::enable_if<
277 std::chrono::__is_duration<_To>::value, _To>::type
278 ceil (std::chrono::duration<Rep_T, Period_T> d)
279 {
280 using namespace std::chrono;
281 _To r = std::chrono::duration_cast<_To> (d);
282 if (r < d)
283 {
284 ++r;
285 }
286 return r;
287 }
288
289#pragma GCC diagnostic pop
290
291 // ----------------------------------------------------------------------
292
293 } /* namespace chrono */
294 } /* namespace estd */
295} /* namespace os */
296
297#pragma GCC diagnostic pop
298
299// ----------------------------------------------------------------------------
300
301#endif /* CMSIS_PLUS_CHRONO_CLOCKS_H_ */
std::chrono::time_point< high_resolution_clock > time_point
Definition chrono:226
std::chrono::nanoseconds duration
Definition chrono:223
rtos::clock_rtc::duration_t sleep_rep
Definition chrono:147
std::chrono::duration< sleep_rep, period > sleep_duration
Definition chrono:153
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:152
std::ratio< 1, 1 > period
std::ratio type representing the tick period of the clock, in seconds
Definition chrono:150
std::chrono::time_point< realtime_clock > time_point
Definition chrono:155
rtos::clock_rtc::timestamp_t rep
Definition chrono:146
std::chrono::time_point< system_clock > time_point
Definition chrono:191
std::chrono::microseconds duration
Definition chrono:188
duration::period period
Definition chrono:190
std::ratio< 1, os::rtos::clock_systick::frequency_hz > period
std::ratio type representing the tick period of the clock, in seconds
Definition chrono:91
static void sleep_for(sleep_rep ticks)
Definition chrono:257
static constexpr bool is_steady
Definition chrono:98
rtos::clock_systick::duration_t sleep_rep
Definition chrono:89
std::chrono::time_point< systick_clock > time_point
basic time_point type of clock
Definition chrono:96
rtos::clock_systick::timestamp_t rep
type of variable holding the tick counter
Definition chrono:88
static time_point now() noexcept
Definition chrono.cpp:61
std::chrono::duration< sleep_rep, period > sleep_duration
Definition chrono:94
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:93
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:83
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition os-clocks.h:92
systick_clock::duration systicks
Definition chrono:117
constexpr std::enable_if< std::chrono::__is_duration< _To >::value, _To >::type ceil(std::chrono::duration< Rep_T, Period_T > d)
Definition chrono:278
System namespace.
Standard std namespace.
Single file µOS++ RTOS definitions.