SatCat5
timeref.h
Go to the documentation of this file.
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
34 
35 #pragma once
36 
37 #include <satcat5/types.h>
38 #include <satcat5/utils.h>
39 
40 namespace satcat5 {
41  namespace util {
48  struct TimeVal {
50  u32 tval;
51 
52  // Explicitly declare default constructor and assignment methods.
53  TimeVal() = delete;
54  TimeVal(const TimeVal& t) = default;
55  TimeVal& operator=(const TimeVal& t) = default;
56 
65  unsigned elapsed_tick() const;
67  unsigned elapsed_usec() const;
69  unsigned elapsed_msec() const;
70 
88  unsigned increment_usec();
89 
93  unsigned increment_msec();
94 
109  bool interval_tick(u32 ticks);
112  bool interval_usec(unsigned usec);
115  bool interval_msec(unsigned msec);
116 
131  bool checkpoint_elapsed();
132  };
133 
142  class TimeRef {
143  protected:
146  explicit constexpr TimeRef(u64 ticks_per_sec)
147  : m_msec_per_tick(div_ceil<u64>( 1000ull << 32, ticks_per_sec))
148  , m_usec_per_tick(div_ceil<u64>(1000000ull << 32, ticks_per_sec))
149  , m_tick_per_msec(div_round<u64>(ticks_per_sec << 32, 1000ull))
150  , m_tick_per_usec(div_round<u64>(ticks_per_sec << 32, 1000000ull))
151  {} // No other initialization required.
152  ~TimeRef() {}
153 
154  private:
155  friend satcat5::util::TimeVal;
156 
157  // Fixed-point scaling for conversion to/from engineering units.
158  // (Internal use only, format is subject to change at any time.)
159  u64 const m_msec_per_tick; // 2^32 * 1K / FT
160  u64 const m_usec_per_tick; // 2^32 * 1M / FT
161  u64 const m_tick_per_msec; // 2^32 * FT / 1K
162  u64 const m_tick_per_usec; // 2^32 * FT / 1M
163 
164  public:
169  virtual u32 raw() = 0;
170 
172  TimeVal now();
173 
177  inline u32 ticks_per_sec() const
178  { return u32((1000 * m_tick_per_msec) >> 32); }
179  inline u32 ticks_per_msec() const
180  { return u32(m_tick_per_msec >> 32); }
181  inline u32 ticks_per_usec() const
182  { return u32(m_tick_per_usec >> 32); }
184 
189  TimeVal checkpoint_usec(unsigned usec);
192  TimeVal checkpoint_msec(unsigned msec);
193 
196  void busywait_usec(unsigned usec);
197  };
198 
201  public:
202  constexpr NullTimer() : TimeRef(1) {}
203  u32 raw() override {return 0;}
204  };
205 
209  class TimeRegister final : public satcat5::util::TimeRef {
210  public:
211  constexpr TimeRegister(volatile u32* reg, u32 clkref_hz)
212  : TimeRef(clkref_hz), m_reg(reg) {}
213  ~TimeRegister() {}
214 
215  u32 raw() override; // Return counter register value
216 
217  private:
218  volatile u32* m_reg; // Pointer to the counter register
219  };
220  }
221 }
Placeholder used if no timer is available.
Definition: timeref.h:200
u32 raw() override
Read current time in arbitrary "ticks".
Definition: timeref.h:203
The TimeRef API provides access to a monotonic time-counter.
Definition: timeref.h:142
TimeVal checkpoint_msec(unsigned msec)
Create an oven-timer, set N milliseconds from now.
Definition: timeref.cc:73
u32 ticks_per_msec() const
Stable accessors for unit conversion.
Definition: timeref.h:179
virtual u32 raw()=0
Read current time in arbitrary "ticks".
TimeVal now()
Create a TimeVal object using the tick-count from raw().
Definition: timeref.cc:65
u32 ticks_per_usec() const
Stable accessors for unit conversion.
Definition: timeref.h:181
TimeVal checkpoint_usec(unsigned usec)
Create an oven-timer, set N microseconds from now.
Definition: timeref.cc:69
constexpr TimeRef(u64 ticks_per_sec)
Constructor accepts a scale-factor for conversion to real-time.
Definition: timeref.h:146
u32 ticks_per_sec() const
Stable accessors for unit conversion.
Definition: timeref.h:177
void busywait_usec(unsigned usec)
If timer resolution allows, busywait for X microseconds.
Definition: timeref.cc:93
Implement TimeRef API using a memory-mapped performance counter.
Definition: timeref.h:209
u32 raw() override
Read current time in arbitrary "ticks".
Definition: timeref.cc:100
Timestamp for measuring elapsed time.
Definition: timeref.h:48
unsigned elapsed_usec() const
Elapsed time in microseconds.
Definition: timeref.cc:25
bool interval_usec(unsigned usec)
Test an interval measured in microseconds.
Definition: timeref.cc:55
unsigned elapsed_tick() const
Measure elapsed time in ticks.
Definition: timeref.cc:19
TimeRef * clk
Pointer to the parent time reference.
Definition: timeref.h:49
bool checkpoint_elapsed()
Test if an oven-timer checkpoint has elapsed.
Definition: timeref.cc:77
u32 tval
The value of this timestamp, measured in ticks.
Definition: timeref.h:50
bool interval_tick(u32 ticks)
Test an interval measured in ticks.
Definition: timeref.cc:45
unsigned increment_msec()
Measure elapsed time in milliseconds, then increment by the returned quantized value.
Definition: timeref.cc:39
unsigned elapsed_msec() const
Elapsed time in milliseconds.
Definition: timeref.cc:29
bool interval_msec(unsigned msec)
Test an interval measured in milliseconds.
Definition: timeref.cc:60
unsigned increment_usec()
Measure elapsed time in microseconds, then increment by the returned quantized value.
Definition: timeref.cc:33
Basic type aliases and prototypes used throughout SatCat5.
Miscellaneous mathematical utility functions.
constexpr T div_ceil(T a, T b)
Integer division functions with various rounding options:
Definition: utils.h:265