SatCat5
ptp_simclock.h
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Tools for simulation and unit testing of PTP clocks
6 
7 #pragma once
8 
9 #include <hal_test/sim_utils.h>
10 #include <satcat5/ptp_filters.h>
12 #include <satcat5/ptp_time.h>
13 #include <satcat5/ptp_tracking.h>
14 
15 namespace satcat5 {
16  namespace ptp {
19  public:
24  static constexpr unsigned TICK_SCALE_NSEC = 40;
25  static constexpr u64 TICKS_PER_SUBNS =
26  (1ull << TICK_SCALE_NSEC) / u64(satcat5::ptp::SUBNS_PER_NSEC);
27  static constexpr double TICKS_PER_SEC =
28  double(TICKS_PER_SUBNS) * double(satcat5::ptp::SUBNS_PER_SEC);
30 
34  SimulatedClock(double nominal_hz, double actual_hz);
35 
37  inline bool ok() const
38  { return m_scale_nominal.ok(); }
39 
42  inline unsigned num_coarse() const
43  { return m_count_coarse; }
46  inline unsigned num_fine() const
47  { return m_count_fine; }
48 
50  inline double mean() const
51  { return m_stats.mean(); }
52 
53  // Implement the required TrackingClock API.
55  const satcat5::ptp::Time& amount) override;
56  satcat5::ptp::Time clock_now() override;
57  void clock_rate(s64 offset) override;
58  double clock_offset_ppm() const;
59  void clock_set(const satcat5::ptp::Time& t);
60 
62  void run(const satcat5::ptp::Time& dt);
63 
64  private:
65  const satcat5::ptp::RateConversion m_scale_nominal;
66  const double m_rate_actual;
67  const s64 m_nco_rate;
68  satcat5::util::uint128_t m_nco_accum;
69  unsigned m_count_coarse;
70  unsigned m_count_fine;
71  satcat5::ptp::Time m_rtc;
73  };
74 
79  public:
81 
84  {return &m_timer;}
85 
87  void run(const satcat5::ptp::Time& dt);
88 
89  protected:
90  u32 m_treg;
92  };
93  }
94 }
Convert normalized frequency offset to ticks-per-clock.
Definition: ptp_filters.h:545
bool ok() const
Is the scale coefficient large enough to mitigate rounding error?
Definition: ptp_filters.h:553
Simulated clock mimics operation of "ptp_realtime.vhd".
Definition: ptp_simclock.h:18
SimulatedClock(double nominal_hz, double actual_hz)
Constructor allows user to set nominal vs actual frequency.
Definition: ptp_simclock.cc:16
bool ok() const
Confirm initial configuration is valid.
Definition: ptp_simclock.h:37
static constexpr double TICKS_PER_SEC
Convert from internal ticks to engineering units.
Definition: ptp_simclock.h:27
static constexpr u64 TICKS_PER_SUBNS
Convert from internal ticks to engineering units.
Definition: ptp_simclock.h:25
double mean() const
Mean of all inputs to clock_rate(...)
Definition: ptp_simclock.h:50
satcat5::ptp::Time clock_now() override
Return the current time if available, TIME_ZERO otherwise.
Definition: ptp_simclock.cc:28
unsigned num_fine() const
Report the number of fine adjustments.
Definition: ptp_simclock.h:46
void run(const satcat5::ptp::Time &dt)
Run simulation for X seconds.
Definition: ptp_simclock.cc:53
static constexpr unsigned TICK_SCALE_NSEC
Convert from internal ticks to engineering units.
Definition: ptp_simclock.h:24
void clock_rate(s64 offset) override
Adjust rate by a normalized frequency offset.
Definition: ptp_simclock.cc:43
satcat5::ptp::Time clock_adjust(const satcat5::ptp::Time &amount) override
Make a one-time adjustment of the specified magnitude.
Definition: ptp_simclock.cc:32
unsigned num_coarse() const
Report the number of coarse adjustments.
Definition: ptp_simclock.h:42
Helper object for tracking simulation time.
Definition: ptp_simclock.h:78
satcat5::util::TimeRef * get_timer()
Accessor for a SatCat5 coarse timer.
Definition: ptp_simclock.h:83
void run(const satcat5::ptp::Time &dt)
Advance simulation by X seconds.
Definition: ptp_simclock.cc:76
A source for ptp::Measurement events, usually a ptp::Client.
Definition: ptp_source.h:19
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
Generic interface to a numerically-controlled reference clock.
Definition: ptp_tracking.h:52
Measure various statistics of a discrete-time series.
Definition: sim_utils.h:293
double mean() const
Mean of all data points.
Definition: sim_utils.cc:338
The TimeRef API provides access to a monotonic time-counter.
Definition: timeref.h:142
Implement TimeRef API using a memory-mapped performance counter.
Definition: timeref.h:209
Chainable filters for use with ptp::TrackingController.
Define the data structure for a two-way time transfer handshake.
High-precision "Time" object for use with PTP / IEEE1588.
Closed-loop time-tracking clocks and tracking control.
Miscellaneous simulation and test helper functions.