Skip to content

Commit 07e1de3

Browse files
yangbolu1991kartben
authored andcommitted
net: ptp: adjust only frequency for continuous synchronization
Current clock synchronization was always stepping clock. This was causing large offset, and discontiguous ptp hardware clock time. For TSN hardware, discontiguous ptp hardware clock time was not able to be used for other TSN protocols. This patch is to convert to frequency adjustment with a basic PI control algorithm. Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
1 parent e9efff6 commit 07e1de3

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

subsys/net/lib/ptp/clock.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct ptp_clock {
5353
uint64_t t3;
5454
uint64_t t4;
5555
} timestamp; /* latest timestamps in nanoseconds */
56+
double pi_drift;
5657
};
5758

5859
__maybe_unused static struct ptp_clock ptp_clk = { 0 };
@@ -506,8 +507,21 @@ int ptp_clock_management_msg_process(struct ptp_port *port, struct ptp_msg *msg)
506507
return state_decision_required;
507508
}
508509

510+
static double ptp_servo_pi(int64_t nanosecond_diff)
511+
{
512+
double kp = 0.7;
513+
double ki = 0.3;
514+
double ppb;
515+
516+
ptp_clk.pi_drift += ki * nanosecond_diff;
517+
ppb = kp * nanosecond_diff + ptp_clk.pi_drift;
518+
519+
return ppb;
520+
}
521+
509522
void ptp_clock_synchronize(uint64_t ingress, uint64_t egress)
510523
{
524+
double ppb;
511525
int64_t offset;
512526
int64_t delay = ptp_clk.current_ds.mean_delay >> 16;
513527

@@ -543,13 +557,15 @@ void ptp_clock_synchronize(uint64_t ingress, uint64_t egress)
543557
current.nanosecond = (uint32_t)dest_nsec;
544558

545559
ptp_clock_set(ptp_clk.phc, &current);
560+
LOG_WRN("Set clock time: %"PRIu64".%09u", current.second, current.nanosecond);
546561
return;
547562
}
548563

549564
LOG_DBG("Offset %lldns", offset);
550565
ptp_clk.current_ds.offset_from_tt = clock_ns_to_timeinterval(offset);
551566

552-
ptp_clock_adjust(ptp_clk.phc, -offset);
567+
ppb = ptp_servo_pi(-offset);
568+
ptp_clock_rate_adjust(ptp_clk.phc, 1.0 + (ppb / 1000000000.0));
553569
}
554570

555571
void ptp_clock_delay(uint64_t egress, uint64_t ingress)

0 commit comments

Comments
 (0)