µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
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 namespace os
40 {
41  namespace estd
42  {
43  namespace chrono
44  {
45 
46  // ======================================================================
47 
48  template<typename T, typename = void>
49  struct has_sleep_for : std::false_type
50  {
51  };
52 
57  template<typename T>
58  struct has_sleep_for<T,
59  decltype(std::declval<T>().has_sleep_for, void())> : std::true_type
60  {
61  };
62 
72  // ======================================================================
73  // µOS++ SysTick clock.
75  {
76  public:
77 
82  using period = std::ratio<1, os::rtos::clock_systick::frequency_hz>;
84  using duration = std::chrono::duration<rep, period>;
85  using sleep_duration = std::chrono::duration<sleep_rep, period>;
87  using time_point = std::chrono::time_point<systick_clock>;
88  static constexpr bool is_steady
89  { true };
90  static constexpr bool has_sleep_for
91  { true };
92 
96  static time_point
97  now () noexcept;
98 
99  // --------------------------------------------------------------------
100  // Extension to ISO
101 
102  static void
103  sleep_for (sleep_rep ticks);
104 
105  };
106 
107  // Define a duration type, to be used in timeout expressions.
109 
110  //constexpr systicks
111  //operator "" systicks (Ssstick_clock::rep); // C++14
112 
113 #pragma GCC diagnostic push
114 #pragma GCC diagnostic ignored "-Waggregate-return"
115 
116  constexpr systicks
117  operator"" _ticks (unsigned long long t)
118  {
119  return systicks (static_cast<systick_clock::rep> (t));
120  }
121 
122 #pragma GCC diagnostic pop
123 
124  // ======================================================================
125 
126  // µOS++ RTC.
127 
129  {
130  public:
131 
134 
136  using period = std::ratio<1, 1>;
138  using duration = std::chrono::duration<rep, period>;
139  using sleep_duration = std::chrono::duration<sleep_rep, period>;
140 
141  using time_point = std::chrono::time_point<realtime_clock>;
142 
143  // Non-monotonic, may be adjusted back in time.
144  static constexpr const bool is_steady
145  { false };
146  static constexpr bool has_sleep_for
147  { true };
148 
149  static time_point
150  now () noexcept;
151 
152  // --------------------------------------------------------------------
153  // Extension to ISO
154 
155  static void
157 
158  // Number of seconds from epoch (1 January 1970 00:00:00 UTC)
159  // Must be initialised during startup with the value of now().
160  // Realtime_clock::startup_time_point = Realtime_clock::now();
162  };
163 
164  // ======================================================================
165 
166  // The system_clock is basically derived from the SysTick counts.
167  // The counter is monotonic, and normally should not be adjusted,
168  // so the clock is steady.
169 
171  {
172  public:
173 
174  using duration = std::chrono::microseconds;
175  using rep = duration::rep;
176  using period = duration::period;
177  using time_point = std::chrono::time_point<system_clock>;
178 
179  // Monotonic, never adjusted back in time.
180  static constexpr const bool is_steady
181  { true };
182 
183  static time_point
184  now () noexcept;
185 
186  // It is the only C++ clock that has the ability to map its
187  // time points to C-style time, and, therefore, to be displayed.
188  static time_t
189  to_time_t (const time_point& tp) noexcept;
190  static time_point
191  from_time_t (time_t t) noexcept;
192 
193  };
194 
195  // ======================================================================
196 
197  // To simplify things, we assumed the system clock is already steady.
199 
200  // ======================================================================
201 
202  // The high resolution clock is also based on SysTick, but also uses
203  // the counter instant value, which gives 1 CPU cycle resolution.
204 
206  {
207  public:
208 
209  using duration = std::chrono::nanoseconds;
210  using rep = duration::rep;
211  using period = duration::period;
212  using time_point = std::chrono::time_point<high_resolution_clock>;
213 
214  // Monotonic, never adjusted back in time.
215  static constexpr const bool is_steady
216  { true };
217 
218  static time_point
219  now () noexcept;
220  };
221 
226  } /* namespace chrono */
227  } /* namespace estd */
228 } /* namespace os */
229 
230 // ----------------------------------------------------------------------------
231 // Inline & template implementations.
232 
233 namespace os
234 {
235  namespace estd
236  {
237  namespace chrono
238  {
239 
240  // ======================================================================
241 
242  inline void
244  {
245  rtos::sysclock.sleep_for (ticks);
246  }
247 
248  // ======================================================================
249 
250  inline void
252  {
253  rtos::rtclock.sleep_for (secs);
254  }
255 
256  // ======================================================================
257 
258 #pragma GCC diagnostic push
259 #pragma GCC diagnostic ignored "-Waggregate-return"
260 
261  template<class _To, class Rep_T, class Period_T>
262 constexpr typename std::enable_if<
263  std::chrono::__is_duration<_To>::value, _To>::type
264  ceil (std::chrono::duration<Rep_T, Period_T> d)
265  {
266  using namespace std::chrono;
267  _To r = std::chrono::duration_cast<_To> (d);
268  if (r < d)
269  {
270  ++r;
271  }
272  return r;
273  }
274 
275 #pragma GCC diagnostic pop
276 
277  // ----------------------------------------------------------------------
278  ;
279  // Avoid formatter bug
280  } /* namespace chrono */
281  } /* namespace estd */
282 } /* namespace os */
283 
284 // ----------------------------------------------------------------------------
285 
286 #endif /* CMSIS_PLUS_CHRONO_CLOCKS_H_ */
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: chrono:138
rtos::clock_systick::duration_t sleep_rep
Definition: chrono:80
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition: os-clocks.h:81
std::chrono::time_point< system_clock > time_point
Definition: chrono:177
rtos::clock_rtc::timestamp_t rep
Definition: chrono:132
std::ratio< 1, 1 > period
std::ratio type representing the tick period of the clock, in seconds
Definition: chrono:136
std::chrono::time_point< realtime_clock > time_point
Definition: chrono:141
std::chrono::duration< sleep_rep, period > sleep_duration
Definition: chrono:139
constexpr void sleep_for(const std::chrono::duration< Rep_T, Period_T > &rel_time)
Sleep for a given duration.
System namespace.
systick_clock::duration systicks
Definition: chrono:108
constexpr std::enable_if< std::chrono::__is_duration< _To >::value, _To >::type ceil(std::chrono::duration< Rep_T, Period_T > d)
Definition: chrono:264
clock_rtc rtclock
The real time clock object instance.
Definition: os-clocks.cpp:660
rtos::clock_rtc::duration_t sleep_rep
Definition: chrono:133
result_t sleep_for(duration_t duration)
Sleep for a relative duration.
Definition: os-clocks.cpp:201
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition: os-clocks.h:72
Single file µOS++ RTOS definitions.
clock_systick sysclock
The system clock object instance.
Definition: os-clocks.cpp:551
std::chrono::time_point< systick_clock > time_point
basic time_point type of clock
Definition: chrono:87
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition: chrono:84
std::chrono::time_point< high_resolution_clock > time_point
Definition: chrono:212
std::chrono::duration< sleep_rep, period > sleep_duration
Definition: chrono:85
rtos::clock_systick::timestamp_t rep
type of variable holding the tick counter
Definition: chrono:79
std::chrono::microseconds duration
Definition: chrono:174
std::chrono::nanoseconds duration
Definition: chrono:209
static void sleep_for(sleep_rep)
Definition: chrono:251
static time_point startup_time_point
Definition: chrono:161
duration::period period
Definition: chrono:176
std::ratio< 1, os::rtos::clock_systick::frequency_hz > period
std::ratio type representing the tick period of the clock, in seconds
Definition: chrono:82
static void sleep_for(sleep_rep ticks)
Definition: chrono:243