21#define MIN(a, b) (((a) < (b)) ? (a) : (b))
27#define MAX(a, b) (((a) > (b)) ? (a) : (b))
30#define S (gPtpCoreState)
40 if (!
nonZeroI(&S.network.meanPathDelay)) {
45 TimestampI d, syncMa, syncSl, delReqSl, delReqMa;
48 syncMa = S.slave.scd.t[
T1];
49 syncSl = S.slave.scd.t[
T2];
50 delReqSl = S.slave.scd.t[
T3];
51 delReqMa = S.slave.scd.t[
T4];
55 CLILOG(S.logging.timestamps,
57 "T1: %d.%09d <- Sync TX (master)\n"
58 "T2: %d.%09d <- Sync RX (slave) \n"
59 "T3: %d.%09d <- Del_Req TX (slave) \n"
60 "T4: %d.%09d <- Del_Req RX (master)\n\n",
61 (uint32_t)S.slave.messaging.sequenceID,
66 }
else if (S.profile.delayMechanism ==
PTP_DM_P2P) {
67 CLILOG(S.logging.timestamps,
69 "T1: %d.%09d <- Sync TX (master)\n"
70 "T2: %d.%09d <- Sync RX (slave)\n"
71 "t1: %d.%09d <- PDel_Req TX (our clock)\n"
72 "t2: %d.%09d <- PDel_Req RX (their clock)\n"
73 "t3: %d.%09d <- PDel_Resp TX (their clock)\n"
74 "t4: %d.%09d <- PDel_Resp RX (our clock)\n\n",
75 (uint32_t)S.slave.messaging.sequenceID,
76 (int32_t)S.slave.scd.t[0].sec, S.slave.scd.t[0].nanosec,
77 (int32_t)S.slave.scd.t[1].sec, S.slave.scd.t[1].nanosec,
78 (int32_t)S.slave.scd.t[2].sec, S.slave.scd.t[2].nanosec,
79 (int32_t)S.slave.scd.t[3].sec, S.slave.scd.t[3].nanosec,
80 (int32_t)S.slave.scd.t[4].sec, S.slave.scd.t[4].nanosec,
81 (int32_t)S.slave.scd.t[5].sec, S.slave.scd.t[5].nanosec);
86 nsToTsI(&cf, S.slave.scd.cf[
T1] + S.slave.scd.cf[
T2]);
90 subTime(&d, &d, &S.network.meanPathDelay);
94 subTime(&d, &d, &S.hwoptions.offset);
103 int64_t d_ns =
nsI(&d);
104 if (llabs(d_ns) > S.slave.coarseLimit) {
107 CLILOG(S.logging.info,
"Time difference has exceeded the coarse correction threshold [%ldns], executing coarse correction!\n", d_ns);
109 S.slave.prevSyncMa = syncMa;
115 double measSyncPeriod_ns;
118 measSyncPeriod_ns = S.slave.messaging.syncPeriodMs * 1E+06;
121 subTime(&measSyncPeriod, &syncMa, &(S.slave.prevSyncMa));
122 measSyncPeriod_ns =
nsI(&measSyncPeriod);
125 PtpServoAuxInput saux = {S.slave.
scd, S.slave.messaging.logSyncPeriod, S.slave.messaging.syncPeriodMs, measSyncPeriod_ns};
132 S.hwclock.addend =
MIN(compAddend, 0xFFFFFFFF);
144 S.hwclock.addend, corr_ppb,
nsI(&S.network.meanPathDelay), (uint64_t)measSyncPeriod_ns);
147 if (S.slave.syncCb != NULL) {
148 S.slave.syncCb(
nsI(&d), &S.slave.scd, S.hwclock.addend);
151 S.slave.prevSyncMa = syncMa;
170 subTime(&d, &S.slave.scd.t[
T2], &S.slave.scd.t[
T1]);
192 S.slave.messaging.lastRespondedDelReqId = pdelRespSeqId;
206 switch (S.slave.messaging.m2sState) {
218 S.slave.scd.t[
T2] = pRawMsg->
ts;
221 S.slave.messaging.sequenceID = pHeader->
sequenceID;
231 S.slave.scd.cf[
T2] = 0;
245 if (pHeader->
sequenceID == S.slave.messaging.sequenceID) {
262 S.slave.messaging.m2sState =
SIdle;
274 if (pHeader->
sequenceID == S.slave.messaging.delay_reqSequenceID) {
292 S.slave.messaging.lastRespondedDelReqId = pHeader->
sequenceID;
308 uint64_t *cf = &S.slave.scd.cf[2];
310 pT[
T4] = pRawMsg->
ts;
316 pT[
T3] = pT[
T2] = zeroTs;
336 uint64_t *cf = &S.slave.scd.cf[2];
371 S.slave.enabled =
false;
375 S.slave.prevSyncMa = zeroTs;
382 if (!S.slave.enabled) {
388 if (++S.slave.delReqTmr > S.slave.delReqTickPeriod) {
389 S.slave.delReqTmr = 0;
393 if (S.slave.messaging.delay_reqSequenceID != S.slave.messaging.lastRespondedDelReqId) {
394 CLILOG(S.logging.info,
"(P)Del_Req #%d: no response received!\n", S.slave.messaging.delay_reqSequenceID);
409 S.slave.enabled =
true;
417 S.slave.enabled =
false;
void ptp_compute_mean_path_delay_p2p(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
void ptp_compute_mean_path_delay_e2e(const TimestampI *pTs, const uint64_t *pCf, TimestampI *pMPD)
void ptp_send_delay_req_message()
This module defines messaging functions for both the slave and master modules.
@ PTP_UEV_DELAY_RESP_RECVED
A Delay_Resp had been received (slave)
@ PTP_UEV_DELAY_REQ_SENT
A Delay_Req had been sent (slave)
@ PTP_UEV_PDELAY_RESP_FOLLOW_UP_RECVED
A PDelay_Resp_Follow_Up had been received (master/slave)
@ PTP_UEV_PDELAY_REQ_SENT
A PDelay_Req had been sent (master/slave)
@ PTP_UEV_PDELAY_RESP_RECVED
A PDelay_Resp had been received (master/slave)
@ PTP_UEV_SYNC_RECVED
A Sync message has been received (slave)
@ PTP_UEV_FOLLOW_UP_RECVED
A Follow_Up message has been received (slave)
@ PTP_UEV_NETWORK_ERROR
Indication of lost messages or the absence of expected responses.
#define PTP_SET_CLOCK(s, ns)
#define PTP_SET_ADDEND(addend)
#define PTP_SERVO_RUN(d, pscd)
void ptp_extract_timestamps(TimestampI *ts, void *pPayload, uint8_t n)
void ptp_read_delay_resp_id_data(PtpDelay_RespIdentification *pDRData, void *pPayload)
This module defines functions that deal with actual PTP messages; they can extract or insert headers,...
Core of the PTP implementation. Defines functions for message processing, clock tuning,...
In here reside a multitude of fundamental PTP-related constants and definitions.
#define PTP_HEARTBEAT_TICKRATE_MS
Heartbeat ticking period.
#define PTP_CLOCK_TICK_FREQ_HZ
Rated clock tick frequency.
#define PTP_ADDEND_CORR_PER_PPB_F
Addend/ppb ratio.
#define PTP_COLOR_BYELLOW
Bright yellow.
#define PTP_PORT_ID
PTP port ID on the device.
#define PTP_DEFAULT_COARSE_TRIGGER_NS
Coarse correction kick-in threshold.
#define PTP_COLOR_RESET
Reset colors.
This module defines the fundamental PTP message and state machine type, flags, bitfields and the PTP ...
@ PTP_LOGPER_SYNCMATCHED
Messaging occurs whenever a Sync arrives.
@ SWaitFollowUp
Waiting for a Follow Up message.
PtpMessageType
PTP packet type enumeration.
@ PTP_MT_Delay_Resp
Delay Response.
@ PTP_MT_PDelay_Resp
Peer Delay Response.
@ PTP_MT_PDelay_Resp_Follow_Up
Peer Delay Response Follow Up.
@ PTP_MT_Follow_Up
Follow Up.
PtpDelayMechanism
PTP Delay mechanism enumeration.
@ PTP_DM_E2E
End-to-End Delay Mechanism.
@ PTP_DM_P2P
Peer-to-Peer Delay Mechanism.
void ptp_set_coarse_threshold(uint64_t ns)
This module features functions to tweak around the PTP engine's almost every property.
static void ptp_commence_p2p_correction(uint32_t pdelRespSeqId)
void ptp_slave_process_message(RawPtpMessage *pRawMsg, PtpHeader *pHeader)
static void ptp_commence_e2e_correction()
static void ptp_perform_correction()
This module implements the slave clock functionality.
void ptp_collect_stats(int64_t d)
This is the statistics module that gathers data of the operating PTP-engine.
Identification carrying Delay_Resp message.
uint64_t requestingSourceClockIdentity
Requesting Source Clock Identity.
uint16_t requestingSourcePortIdentity
Requesting Source Port Identity.
bool PTP_TWO_STEP
Two Step.
PTP slave messaging state structure.
PTP synchronization cycle data.
Raw PTP message structure.
uint8_t data[(128)]
raw packet data
int32_t nanosec
nanoseconds
The entry point of the whole PTP-implementation. Calling reg_task_ptp() initializes the PTP-engine,...
void normTime(TimestampI *t)
int64_t tsToTick(const TimestampI *ts, uint32_t tps)
TimestampI * nsToTsI(TimestampI *r, int64_t ns)
TimestampI * subTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
bool nonZeroI(const TimestampI *a)
int64_t nsI(const TimestampI *t)
This module defines storage classes for timestamps and operations on time values.