µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_CHRONO_CLOCKS_H_
14#define CMSIS_PLUS_CHRONO_CLOCKS_H_
15
16// ----------------------------------------------------------------------------
17
18#include <cmsis-plus/rtos/os.h>
19
20#include <chrono>
21
22// ----------------------------------------------------------------------------
23
24#pragma GCC diagnostic push
25#if defined(__clang__)
26#pragma clang diagnostic ignored "-Wc++98-compat"
27#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
28#endif
29
30// ----------------------------------------------------------------------------
31
32namespace os
33{
34 namespace estd
35 {
36 namespace chrono
37 {
38
39 // ======================================================================
40
41 template<typename T, typename = void>
42 struct has_sleep_for : std::false_type
43 {
44 };
45
50 template<typename T>
51 struct has_sleep_for<T,
52 decltype(std::declval<T>().has_sleep_for, void())> : std::true_type
53 {
54 };
55
65 // ======================================================================
66 // µOS++ SysTick clock.
68 {
69 public:
70
75 using period = std::ratio<1, os::rtos::clock_systick::frequency_hz>;
77 using duration = std::chrono::duration<rep, period>;
78 using sleep_duration = std::chrono::duration<sleep_rep, period>;
80 using time_point = std::chrono::time_point<systick_clock>;
81 static constexpr bool is_steady
82 { true };
83 static constexpr bool has_sleep_for
84 { true };
85
89 static time_point
90 now () noexcept;
91
92 // --------------------------------------------------------------------
93 // Extension to ISO
94
95 static void
96 sleep_for (sleep_rep ticks);
97
98 };
99
100 // Define a duration type, to be used in timeout expressions.
102
103 //constexpr systicks
104 //operator "" systicks (Ssstick_clock::rep); // C++14
105
106#pragma GCC diagnostic push
107#if defined(__clang__)
108#pragma clang diagnostic ignored "-Wc++98-compat"
109// error: 'long long' is incompatible with C++98 [-Werror,-Wc++98-compat-pedantic]
110#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
111#pragma clang diagnostic ignored "-Wreserved-identifier"
112#elif defined(__GNUC__)
113#pragma GCC diagnostic ignored "-Waggregate-return"
114#pragma GCC diagnostic ignored "-Wuseless-cast"
115#endif
116
117 constexpr systicks
118 operator"" _ticks (unsigned long long t)
119 {
120 return systicks (static_cast<systick_clock::rep> (t));
121 }
122
123#pragma GCC diagnostic pop
124
125 // ======================================================================
126
127 // µOS++ RTC.
128
130 {
131 public:
132
135
137 using period = std::ratio<1, 1>;
139 using duration = std::chrono::duration<rep, period>;
140 using sleep_duration = std::chrono::duration<sleep_rep, period>;
141
142 using time_point = std::chrono::time_point<realtime_clock>;
143
144 // Non-monotonic, may be adjusted back in time.
145 static constexpr const bool is_steady
146 { false };
147 static constexpr bool has_sleep_for
148 { true };
149
150 static time_point
151 now () noexcept;
152
153 // --------------------------------------------------------------------
154 // Extension to ISO
155
156 static void
157 sleep_for (sleep_rep);
158
159 // Number of seconds from epoch (1 January 1970 00:00:00 UTC)
160 // Must be initialised during startup with the value of now().
161 // Realtime_clock::startup_time_point = Realtime_clock::now();
162 static time_point startup_time_point;
163 };
164
165 // ======================================================================
166
167 // The system_clock is basically derived from the SysTick counts.
168 // The counter is monotonic, and normally should not be adjusted,
169 // so the clock is steady.
170
172 {
173 public:
174
175 using duration = std::chrono::microseconds;
176 using rep = duration::rep;
177 using period = duration::period;
178 using time_point = std::chrono::time_point<system_clock>;
179
180 // Monotonic, never adjusted back in time.
181 static constexpr const bool is_steady
182 { true };
183
184 static time_point
185 now () noexcept;
186
187 // It is the only C++ clock that has the ability to map its
188 // time points to C-style time, and, therefore, to be displayed.
189 static time_t
190 to_time_t (const time_point& tp) noexcept;
191 static time_point
192 from_time_t (time_t t) noexcept;
193
194 };
195
196 // ======================================================================
197
198 // To simplify things, we assumed the system clock is already steady.
200
201 // ======================================================================
202
203 // The high resolution clock is also based on SysTick, but also uses
204 // the counter instant value, which gives 1 CPU cycle resolution.
205
207 {
208 public:
209
210 using duration = std::chrono::nanoseconds;
211 using rep = duration::rep;
212 using period = duration::period;
213 using time_point = std::chrono::time_point<high_resolution_clock>;
214
215 // Monotonic, never adjusted back in time.
216 static constexpr const bool is_steady
217 { true };
218
219 static time_point
220 now () noexcept;
221 };
222
227 } /* namespace chrono */
228 } /* namespace estd */
229} /* namespace os */
230
231// ----------------------------------------------------------------------------
232// Inline & template implementations.
233
234namespace os
235{
236 namespace estd
237 {
238 namespace chrono
239 {
240
241 // ======================================================================
242
243 inline void
244 systick_clock::sleep_for (sleep_rep ticks)
245 {
246 rtos::sysclock.sleep_for (ticks);
247 }
248
249 // ======================================================================
250
251 inline void
252 realtime_clock::sleep_for (sleep_rep secs)
253 {
254 rtos::rtclock.sleep_for (secs);
255 }
256
257 // ======================================================================
258
259#pragma GCC diagnostic push
260#if defined(__clang__)
261#pragma clang diagnostic ignored "-Wreserved-identifier"
262#elif defined(__GNUC__)
263#pragma GCC diagnostic ignored "-Waggregate-return"
264#endif
265
266 template<class _To, class Rep_T, class Period_T>
267 constexpr typename std::enable_if<
268 std::chrono::__is_duration<_To>::value, _To>::type
269 ceil (std::chrono::duration<Rep_T, Period_T> d)
270 {
271 using namespace std::chrono;
272 _To r = std::chrono::duration_cast<_To> (d);
273 if (r < d)
274 {
275 ++r;
276 }
277 return r;
278 }
279
280#pragma GCC diagnostic pop
281
282 // ----------------------------------------------------------------------
283
284 } /* namespace chrono */
285 } /* namespace estd */
286} /* namespace os */
287
288#pragma GCC diagnostic pop
289
290// ----------------------------------------------------------------------------
291
292#endif /* CMSIS_PLUS_CHRONO_CLOCKS_H_ */
std::chrono::time_point< high_resolution_clock > time_point
Definition chrono:213
std::chrono::nanoseconds duration
Definition chrono:210
rtos::clock_rtc::duration_t sleep_rep
Definition chrono:134
std::chrono::duration< sleep_rep, period > sleep_duration
Definition chrono:140
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:139
std::ratio< 1, 1 > period
std::ratio type representing the tick period of the clock, in seconds
Definition chrono:137
std::chrono::time_point< realtime_clock > time_point
Definition chrono:142
rtos::clock_rtc::timestamp_t rep
Definition chrono:133
std::chrono::time_point< system_clock > time_point
Definition chrono:178
std::chrono::microseconds duration
Definition chrono:175
duration::period period
Definition chrono:177
std::ratio< 1, os::rtos::clock_systick::frequency_hz > period
std::ratio type representing the tick period of the clock, in seconds
Definition chrono:75
static void sleep_for(sleep_rep ticks)
Definition chrono:244
static constexpr bool is_steady
Definition chrono:82
rtos::clock_systick::duration_t sleep_rep
Definition chrono:73
std::chrono::time_point< systick_clock > time_point
basic time_point type of clock
Definition chrono:80
rtos::clock_systick::timestamp_t rep
type of variable holding the tick counter
Definition chrono:72
static time_point now() noexcept
Definition chrono.cpp:49
std::chrono::duration< sleep_rep, period > sleep_duration
Definition chrono:78
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:77
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:76
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition os-clocks.h:85
systick_clock::duration systicks
Definition chrono:101
constexpr std::enable_if< std::chrono::__is_duration< _To >::value, _To >::type ceil(std::chrono::duration< Rep_T, Period_T > d)
Definition chrono:269
System namespace.
Standard std namespace.
Single file µOS++ RTOS definitions.