µ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.cpp
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#include <cmsis-plus/rtos/os.h>
15
16// ----------------------------------------------------------------------------
17
18#if defined(__clang__)
19#pragma clang diagnostic ignored "-Wc++98-compat"
20#endif
21
22// ----------------------------------------------------------------------------
23
24namespace os
25{
26 namespace estd
27 {
28 namespace chrono
29 {
30
31 // ----------------------------------------------------------------------
32
33 using namespace os;
34
35 // Number of seconds from epoch (1 January 1970 00:00:00 UTC)
36 // when the system was started.
37 // Must be set during startup by reading the RTC.
38 // uint64_t startup_absolute_seconds;
39
40#pragma GCC diagnostic push
41#if defined(__clang__)
42#elif defined(__GNUC__)
43#pragma GCC diagnostic ignored "-Waggregate-return"
44#endif
45
46 // ======================================================================
47
50 {
51 const auto ticks = rtos::sysclock.now ();
52 return time_point
53 { duration
54 { ticks } };
55 }
56
57 // ======================================================================
58
61 {
62 const auto secs = rtos::rtclock.now ();
63 return time_point
64 { duration
65 { secs } };
66 }
67
69
70 // ======================================================================
71
74 {
75 const auto ticks = rtos::sysclock.now ();
76 return time_point
77 { duration
78 { systicks
79 { ticks } + realtime_clock::startup_time_point.time_since_epoch () //
80 } //
81 };
82 }
83
84#pragma GCC diagnostic push
85#if defined(__clang__)
86#elif defined(__GNUC__)
87#pragma GCC diagnostic ignored "-Wuseless-cast"
88#endif
89
90 time_t
92 {
93 return time_t (
94 std::chrono::duration_cast<std::chrono::seconds> (
95 t.time_since_epoch ()).count ());
96 }
97
98#pragma GCC diagnostic pop
99
101 system_clock::from_time_t (time_t t) noexcept
102 {
103 return system_clock::time_point (std::chrono::seconds (t));
104 }
105
106 // ======================================================================
107
110 {
111 auto cycles = rtos::hrclock.now ();
112
113#pragma GCC diagnostic push
114#if defined(__clang__)
115// error: 'long long' is incompatible with C++98 [-Werror,-Wc++98-compat-pedantic]
116#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
117#endif
118 // The duration is the number of sum of SysTick ticks plus the current
119 // count of CPU cycles (computed from the SysTick counter).
120 // Notice: a more exact solution would be to compute
121 // ticks * divisor + cycles, but this severely reduces the
122 // range of ticks.
123 return time_point
124 { duration
125 { duration
126 { cycles * 1000000000ULL
128 + realtime_clock::startup_time_point.time_since_epoch () } //
129 };
130#pragma GCC diagnostic pop
131 }
132
133#pragma GCC diagnostic pop
134
135 // ------------------------------------------------------------------------
136
137 } /* namespace chrono */
138 } /* namespace estd */
139} /* namespace os */
140
141// ----------------------------------------------------------------------------
std::chrono::time_point< high_resolution_clock > time_point
Definition chrono:213
static time_point now() noexcept
Definition chrono.cpp:109
std::chrono::nanoseconds duration
Definition chrono:210
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:139
std::chrono::time_point< realtime_clock > time_point
Definition chrono:142
static time_point startup_time_point
Definition chrono:162
static time_point now() noexcept
Definition chrono.cpp:60
static time_point from_time_t(time_t t) noexcept
Definition chrono.cpp:101
std::chrono::time_point< system_clock > time_point
Definition chrono:178
std::chrono::microseconds duration
Definition chrono:175
static time_t to_time_t(const time_point &tp) noexcept
Definition chrono.cpp:91
static time_point now() noexcept
Definition chrono.cpp:73
std::chrono::time_point< systick_clock > time_point
basic time_point type of clock
Definition chrono:80
static time_point now() noexcept
Definition chrono.cpp:49
std::chrono::duration< rep, period > duration
basic duration type of clock
Definition chrono:77
virtual timestamp_t now(void) override
Tell the current time adjusted for epoch.
uint32_t input_clock_frequency_hz(void)
Definition os-clocks.h:932
virtual timestamp_t now(void) override
Tell the current time.
virtual timestamp_t now(void)
Tell the current time, possibly adjusted for epoch.
clock_highres hrclock
The high resolution clock object instance.
clock_rtc rtclock
The real time clock object instance.
clock_systick sysclock
The system clock object instance.
systick_clock::duration systicks
Definition chrono:101
System namespace.
Single file µOS++ RTOS definitions.