From f0e09e43fb0957ba3739c400b270963f2b877b4b Mon Sep 17 00:00:00 2001 From: orosiferenc3 Date: Tue, 4 Mar 2025 14:27:32 +0100 Subject: [PATCH] synchronize CLOCK_REALTIME to NMEA sentences --- ts2phc.c | 12 +++++++++++- ts2phc_pps_sink.c | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ts2phc.c b/ts2phc.c index 39d31b64..541e6827 100644 --- a/ts2phc.c +++ b/ts2phc.c @@ -480,6 +480,16 @@ static void ts2phc_synchronize_clocks(struct ts2phc_private *priv, int autocfg) double adj; tmv_t ts; + if (c->clkid == CLOCK_REALTIME) { + struct timespec real_time_ts; + if (clock_gettime(CLOCK_REALTIME, &real_time_ts) == -1) { + return; + } + c->last_ts.ns = tmv_to_nanoseconds(timespec_to_tmv(real_time_ts)); + c->is_ts_available = true; + c->is_target = true; + } + if (!c->is_target) continue; @@ -813,7 +823,7 @@ int main(int argc, char *argv[]) pr_err("poll failed"); break; } - if (err > 0) { + if (err >= 0) { err = ts2phc_collect_pps_source_tstamp(&priv); if (err) { pr_err("failed to collect PPS source tstamp"); diff --git a/ts2phc_pps_sink.c b/ts2phc_pps_sink.c index af34e39f..7b53c63a 100644 --- a/ts2phc_pps_sink.c +++ b/ts2phc_pps_sink.c @@ -187,6 +187,9 @@ static struct ts2phc_pps_sink *ts2phc_pps_sink_create(struct ts2phc_private *pri pr_debug("PPS sink %s has ptp index %d", device, sink->clock->phc_index); + if (sink->clock->clkid == CLOCK_REALTIME) + return sink; + if (phc_number_pins(sink->clock->clkid) > 0) { err = phc_pin_setfunc(sink->clock->clkid, &sink->pin_desc); if (err < 0) { @@ -228,12 +231,16 @@ static void ts2phc_pps_sink_destroy(struct ts2phc_pps_sink *sink) { struct ptp_extts_request extts; + if (sink->clock->clkid == CLOCK_REALTIME) + goto destroy; + memset(&extts, 0, sizeof(extts)); extts.index = sink->pin_desc.chan; extts.flags = 0; if (ioctl(sink->clock->fd, PTP_EXTTS_REQUEST2, &extts)) { pr_err(PTP_EXTTS_REQUEST_FAILED); } +destroy: ts2phc_clock_destroy(sink->clock); free(sink->name); free(sink); @@ -343,6 +350,8 @@ int ts2phc_pps_sink_arm(struct ts2phc_private *priv) memset(&extts, 0, sizeof(extts)); STAILQ_FOREACH(sink, &priv->sinks, list) { + if (sink->clock->clkid == CLOCK_REALTIME) + continue; extts.index = sink->pin_desc.chan; extts.flags = sink->polarity | PTP_ENABLE_FEATURE; err = ioctl(sink->clock->fd, PTP_EXTTS_REQUEST2, &extts);