StateOS
Loading...
Searching...
No Matches
osclock.h
1/******************************************************************************
2
3 @file StateOS: osclock.h
4 @author Rajmund Szymanski
5 @date 10.07.2022
6 @brief This file implements steady clock for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_CLK_H
33#define __STATEOS_CLK_H
34
35#include "oskernel.h"
36
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
55
56cnt_t sys_time( void );
57
59__STATIC_INLINE
60cnt_t sys_timeISR( void ) { return sys_time(); }
61
62#ifdef __cplusplus
63}
64#endif
65
66/* -------------------------------------------------------------------------- */
67
68#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
69
70#if __cplusplus >= 201402L
71#include <chrono>
72#endif
73
74namespace stateos {
75
79
80struct Clock
81{
82 using rep = cnt_t;
83#if __cplusplus >= 201402L
84 using period = std::ratio<1, OS_FREQUENCY>;
85 using duration = std::chrono::duration<rep, period>;
86 using time_point = std::chrono::time_point<Clock>;
87
88 static constexpr bool is_steady = true;
89
91 static
93 {
95 }
96
97private:
98 // rounded up to whole ticks: a delay is never shorter than requested
99 template<class _Rep, class _Period> static constexpr
100 intmax_t ticks( const std::chrono::duration<_Rep, _Period>& _time )
101 {
102 return std::chrono::duration_cast<std::chrono::duration<intmax_t, period>>(_time).count() +
103 (std::chrono::duration_cast<std::chrono::duration<intmax_t, period>>(_time) < _time ? 1 : 0);
104 }
105
106public:
108 template<class _Rep, class _Period> static constexpr
109 rep count( const std::chrono::duration<_Rep, _Period>& _delay )
110 {
111 return ticks(_delay) <= 0 ? IMMEDIATE : static_cast<uintmax_t>(ticks(_delay)) >= INFINITE ? INFINITE : static_cast<rep>(ticks(_delay));
112 }
113
115 template<class _Duration> static constexpr
116 rep until( const std::chrono::time_point<Clock, _Duration>& _time )
117 {
118 return static_cast<rep>(ticks(_time.time_since_epoch()));
119 }
120#endif
121 static constexpr
123 rep count( const rep _delay ) { return _delay; }
124
125 static constexpr
127 rep until( const rep _time ) { return _time; }
128};
129
130} // namespace
131#endif//__cplusplus
132
133/* -------------------------------------------------------------------------- */
134
136
137#endif//__STATEOS_CLK_H
#define IMMEDIATE
no waiting
Definition osbase.h:258
#define INFINITE
infinite waiting
Definition osbase.h:261
cnt_t sys_time(void)
return current value of system counter
cnt_t sys_timeISR(void)
ISR alias of sys_time().
Definition osclock.h:60
steady clock
Definition osclock.h:81
cnt_t rep
type of the system time (cnt_t)
Definition osclock.h:82
static time_point now()
current system time (sys_time())
Definition osclock.h:92
std::chrono::duration< rep, period > duration
duration in ticks of the system timer
Definition osclock.h:85
std::ratio< 1, OS_FREQUENCY > period
tick of the system timer (1 / OS_FREQUENCY s)
Definition osclock.h:84
std::chrono::time_point< Clock > time_point
time point of the system time
Definition osclock.h:86
static constexpr rep until(const rep _time)
time point in ticks
Definition osclock.h:127
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:109
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:116
static constexpr rep count(const rep _delay)
delay in ticks
Definition osclock.h:123
static constexpr bool is_steady
the system time is monotonic
Definition osclock.h:88