C/C++ API Reference
Loading...
Searching...
No Matches
system_clock.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <stddef.h>
17#include <stdint.h>
18
19#include "pw_preprocessor/util.h"
20
21// The backend implements this header to provide the following SystemClock
22// parameters, for more detail on the parameters see the SystemClock usage of
23// them below:
24// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR
25// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR
26// constexpr pw::chrono::Epoch pw::chrono::backend::kSystemClockEpoch;
27// constexpr bool pw::chrono::backend::kSystemClockFreeRunning;
28// constexpr bool pw::chrono::backend::kSystemClockNmiSafe;
29#include "pw_chrono_backend/system_clock_config.h"
30
31#ifdef __cplusplus
32
33#include <chrono>
34#include <ratio>
35
36#include "pw_chrono/virtual_clock.h"
37
39namespace pw::chrono {
40
42
43namespace backend {
44
51
52} // namespace backend
53
85 using rep = int64_t;
87 using period = std::ratio<PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR,
88 PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR>;
90 using duration = std::chrono::duration<rep, period>;
91 using time_point = std::chrono::time_point<SystemClock>;
93 static constexpr Epoch epoch = backend::kSystemClockEpoch;
94
98 static constexpr bool is_monotonic = true;
99 static constexpr bool is_steady = false;
100
103 static constexpr bool is_free_running = backend::kSystemClockFreeRunning;
104
106 static constexpr bool is_stopped_in_halting_debug_mode = true;
107
109 static constexpr bool is_always_enabled = true;
110
114 static constexpr bool is_nmi_safe = backend::kSystemClockNmiSafe;
115
117 static time_point now() noexcept {
118 return time_point(duration(backend::GetSystemClockTickCount()));
119 }
120
124 template <class Rep, class Period>
125 static constexpr duration for_at_least(std::chrono::duration<Rep, Period> d) {
126 return std::chrono::ceil<duration>(d);
127 }
128
137 static time_point TimePointAfterAtLeast(duration after_at_least) {
138 return now() + after_at_least + duration(1);
139 }
140};
141
142// NOTE: VirtualClock here is specialized on SystemClock in order to provide
143// the `RealClock` function.
144//
145// SystemClock is defined in this file, so there's no risk of an ODR violation
146// as other libraries are unable to spell VirtualClock<SystemClock> unless
147// they have first included this file.
148
178template <>
180 public:
183
184 virtual ~VirtualClock() = default;
185
187 virtual SystemClock::time_point now() = 0;
188};
189
191
192} // namespace pw::chrono
193
194// The backend can opt to include an inlined implementation of the following:
195// int64_t GetSystemClockTickCount();
196#if __has_include("pw_chrono_backend/system_clock_inline.h")
197#include "pw_chrono_backend/system_clock_inline.h"
198#endif // __has_include("pw_chrono_backend/system_clock_inline.h")
199
200#endif // __cplusplus
201
202PW_EXTERN_C_START
203
204// C API Users should not create pw_chrono_SystemClock_Duration's directly,
205// instead it is strongly recommended to use macros which express the duration
206// in time units, instead of non-portable ticks.
207//
208// The following macros round up just like std::chrono::ceil, this is the
209// recommended rounding to maintain the "at least" contract of timeouts and
210// deadlines (note the *_CEIL macros are the same only more explicit):
211// PW_SYSTEM_CLOCK_MS(milliseconds)
212// PW_SYSTEM_CLOCK_S(seconds)
213// PW_SYSTEM_CLOCK_MIN(minutes)
214// PW_SYSTEM_CLOCK_H(hours)
215// PW_SYSTEM_CLOCK_MS_CEIL(milliseconds)
216// PW_SYSTEM_CLOCK_S_CEIL(seconds)
217// PW_SYSTEM_CLOCK_MIN_CEIL(minutes)
218// PW_SYSTEM_CLOCK_H_CEIL(hours)
219//
220// The following macros round down like std::chrono::{floor,duration_cast},
221// these are discouraged but sometimes necessary:
222// PW_SYSTEM_CLOCK_MS_FLOOR(milliseconds)
223// PW_SYSTEM_CLOCK_S_FLOOR(seconds)
224// PW_SYSTEM_CLOCK_MIN_FLOOR(minutes)
225// PW_SYSTEM_CLOCK_H_FLOOR(hours)
226#include "pw_chrono/internal/system_clock_macros.h"
227
228typedef struct {
229 int64_t ticks;
231
232typedef struct {
233 pw_chrono_SystemClock_Duration duration_since_epoch;
235typedef int64_t pw_chrono_SystemClock_Nanoseconds;
236
237// Returns the current time, see SystemClock::now() for more detail.
238pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now(void);
239
240// Returns the change in time between the current_time - last_time.
241pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed(
244
245// For lossless time unit conversion, the seconds per tick ratio that is
246// numerator/denominator should be used:
247// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR
248// PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR
249
250// Warning, this may be lossy due to the use of std::chrono::floor,
251// rounding towards zero.
252pw_chrono_SystemClock_Nanoseconds pw_chrono_SystemClock_DurationToNsFloor(
254
255PW_EXTERN_C_END
Definition: system_clock.h:179
Definition: virtual_clock.h:31
static constexpr Epoch epoch
The epoch must be provided by the backend.
Definition: system_clock.h:93
static constexpr bool is_nmi_safe
Definition: system_clock.h:114
int64_t GetSystemClockTickCount()
static constexpr bool is_monotonic
Definition: system_clock.h:98
static constexpr duration for_at_least(std::chrono::duration< Rep, Period > d)
Definition: system_clock.h:125
virtual SystemClock::time_point now()=0
Returns the current time.
static constexpr bool is_stopped_in_halting_debug_mode
The clock must stop while in halting debug mode.
Definition: system_clock.h:106
static time_point now() noexcept
This is thread and IRQ safe. This must be provided by the backend.
Definition: system_clock.h:117
static VirtualClock< SystemClock > & RealClock()
Returns a reference to the real system clock to aid instantiation.
static time_point TimePointAfterAtLeast(duration after_at_least)
Definition: system_clock.h:137
static constexpr bool is_free_running
Definition: system_clock.h:103
std::ratio< PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_NUMERATOR, PW_CHRONO_SYSTEM_CLOCK_PERIOD_SECONDS_DENOMINATOR > period
The period must be provided by the backend.
Definition: system_clock.h:88
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
static constexpr bool is_always_enabled
The now() function can be invoked at any time.
Definition: system_clock.h:109
Portable std::chrono library for embedded.
Definition: simulated_system_clock.h:22
Definition: system_clock.h:84
Definition: system_clock.h:228
Definition: system_clock.h:232