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