SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_time_util.h
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#ifndef STK_TIME_UTIL_H_
11#define STK_TIME_UTIL_H_
12
16
17namespace stk {
18namespace time {
19
51{
52public:
62 PeriodicTrigger(uint32_t period, bool start = false) : m_next(0), m_period(period)
63 {
64 if (start)
65 {
66 Restart();
67 }
68 }
69
73 uint32_t GetPeriod() const
74 {
75 return m_period;
76 }
77
83 void SetPeriod(uint32_t period)
84 {
85 m_next = (m_next - static_cast<Ticks>(m_period)) + static_cast<Ticks>(period);
86 m_period = period;
87 }
88
93 void Restart()
94 {
95 m_next = GetTicks() + static_cast<Ticks>(m_period);
96 }
97
108 bool Poll()
109 {
110 STK_ASSERT(m_next > 0);
111
112 bool triggered = false;
113 const Ticks diff = GetTicks() - m_next;
114
115 if (diff >= 0)
116 {
117 m_next += static_cast<Ticks>(m_period);
118 triggered = true;
119 }
120
121 return triggered;
122 }
123
124protected:
126 uint32_t m_period;
127};
128
157{
158 static constexpr Cycles NOT_STARTED = 0ULL;
159
160public:
168
177 template <typename Func>
178 void Start(Func now_func)
179 {
180 m_prev = static_cast<Cycles>(now_func());
181 }
182
194 template <typename Func>
195 Cycles Update(Func now_func)
196 {
197 Cycles now = static_cast<Cycles>(now_func());
198
200 {
201 m_prev = now;
202 }
203
204 Cycles diff = now - m_prev;
205 m_prev = now;
206
207 return diff;
208 }
209
210protected:
212};
213
214} // namespace time
215} // namespace stk
216
217#endif /* STK_TIME_UTIL_H_ */
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:455
#define STK_UNLIKELY(x)
Provides a compiler hint that the given expression is highly unlikely to evaluate to true (typically ...
Definition stk_defs.h:673
Namespace of STK package.
int64_t Ticks
Ticks value.
Definition stk_common.h:155
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:435
uint64_t Cycles
Cycles value.
Definition stk_common.h:165
Time-related primitives.
Ticks m_next
Next trigger time in ticks.
uint32_t GetPeriod() const
Get currently configured trigger period.
void Restart()
Reset the trigger and start.
void SetPeriod(uint32_t period)
Change the trigger period while preserving phase.
uint32_t m_period
Trigger period in ticks. Modified only by SetPeriod(). Must be > 0.
bool Poll()
Check whether the scheduled trigger time has been reached.
PeriodicTrigger(uint32_t period, bool start=false)
Construct a PeriodicTrigger.
void Start(Func now_func)
Capture the start time.
Cycles m_prev
Counter value captured at the last Start() or Update() call.
static constexpr Cycles NOT_STARTED
Value of m_prev meaning that stopwatch is not started.
Cycles Update(Func now_func)
Return the number of cycles elapsed since the last Start() or Update().
Stopwatch()
Construct a Stopwatch.