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