6 #include <hal_test/ptp_simclock.h>
16 SimulatedClock::SimulatedClock(
double nominal_hz,
double actual_hz)
17 : m_scale_nominal(nominal_hz, TICK_SCALE_NSEC)
18 , m_rate_actual(actual_hz)
19 , m_nco_rate(round_s64(TICKS_PER_SEC / nominal_hz))
20 , m_nco_accum(satcat5::util::UINT128_ZERO)
38 void SimulatedClock::clock_set(
const Time& t) {
49 double SimulatedClock::clock_offset_ppm()
const {
50 return double(m_offset) / double(satcat5::ptp::RATE_ONE_PPM);
56 u64 num_clocks = round_u64(dt_secs * m_rate_actual);
59 s64 delta = m_scale_nominal.
convert(m_offset);
60 const uint128_t incr(num_clocks);
61 const uint128_t rate(u64(m_nco_rate + delta));
62 m_nco_accum += incr * rate;
66 m_rtc +=
Time(s64(m_nco_accum / scale));
67 m_nco_accum = m_nco_accum % scale;
70 SimulatedTimer::SimulatedTimer()
71 : m_treg(0), m_timer(&m_treg, 1000000)
73 satcat5::poll::timekeeper.
set_clock(&m_timer);
void request_poll()
Call this method to request polling at a later time.
void set_clock(satcat5::util::TimeRef *timer)
Immediately set the system time reference.
s64 convert(s64 offset) const
Forward conversion (normalized rate ==> ticks-per-clock)
Simulated clock mimics operation of "ptp_realtime.vhd".
static constexpr u64 TICKS_PER_SUBNS
Convert from internal ticks to engineering units.
satcat5::ptp::Time clock_now() override
Return the current time if available, TIME_ZERO otherwise.
void run(const satcat5::ptp::Time &dt)
Run simulation for X seconds.
void clock_rate(s64 offset) override
Adjust rate by a normalized frequency offset.
satcat5::ptp::Time clock_adjust(const satcat5::ptp::Time &amount) override
Make a one-time adjustment of the specified magnitude.
Helper object for tracking simulation time.
void run(const satcat5::ptp::Time &dt)
Advance simulation by X seconds.
High-precision timestamp for use with PTP / IEEE1588.
s64 delta_subns() const
Convert time-differences to the designated unit.
s64 delta_usec() const
Convert time-differences to the designated unit.
void add(double x)
Add a new data point.
constexpr s64 SUBNS_PER_SEC
Define commonly used scaling factors.
Miscellaneous mathematical utility functions.
constexpr u64 round_u64(T x)
Round a floating-point value to the nearest integer.
constexpr s64 round_s64(T x)
Round a floating-point value to the nearest integer.