File tree 3 files changed +49
-10
lines changed
subsys/net/l2/ethernet/gptp
3 files changed +49
-10
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ K_FIFO_DEFINE(gptp_rx_queue);
35
35
static k_tid_t tid ;
36
36
static struct k_thread gptp_thread_data ;
37
37
struct gptp_domain gptp_domain ;
38
+ struct gptp_domain_data gptp_domain_data ;
38
39
39
40
int gptp_get_port_number (struct net_if * iface )
40
41
{
@@ -911,6 +912,18 @@ int gptp_get_port_data(struct gptp_domain *domain,
911
912
return 0 ;
912
913
}
913
914
915
+ double gptp_servo_pi (int64_t nanosecond_diff )
916
+ {
917
+ double kp = 0.7 ;
918
+ double ki = 0.3 ;
919
+ double ppb ;
920
+
921
+ gptp_domain_data .pi_drift += ki * nanosecond_diff ;
922
+ ppb = kp * nanosecond_diff + gptp_domain_data .pi_drift ;
923
+
924
+ return ppb ;
925
+ }
926
+
914
927
static void init_ports (void )
915
928
{
916
929
net_if_foreach (gptp_add_port , & gptp_domain .default_ds .nb_ports );
@@ -929,5 +942,8 @@ void net_gptp_init(void)
929
942
{
930
943
gptp_domain .default_ds .nb_ports = 0U ;
931
944
945
+ gptp_domain_data .domain = & gptp_domain ;
946
+ gptp_domain_data .pi_drift = 0.0 ;
947
+
932
948
init_ports ();
933
949
}
Original file line number Diff line number Diff line change @@ -786,14 +786,12 @@ static void gptp_update_local_port_clock(void)
786
786
nanosecond_diff = - (int64_t )NSEC_PER_SEC + nanosecond_diff ;
787
787
}
788
788
789
- ptp_clock_rate_adjust (clk , port_ds -> neighbor_rate_ratio );
790
-
791
789
/* If time difference is too high, set the clock value.
792
790
* Otherwise, adjust it.
793
791
*/
794
792
if (second_diff || (second_diff == 0 &&
795
- (nanosecond_diff < -5000 ||
796
- nanosecond_diff > 5000 ))) {
793
+ (nanosecond_diff < -50000000 ||
794
+ nanosecond_diff > 50000000 ))) {
797
795
bool underflow = false;
798
796
799
797
key = irq_lock ();
@@ -837,13 +835,17 @@ static void gptp_update_local_port_clock(void)
837
835
skip_clock_set :
838
836
irq_unlock (key );
839
837
} else {
840
- if (nanosecond_diff < -200 ) {
841
- nanosecond_diff = -200 ;
842
- } else if (nanosecond_diff > 200 ) {
843
- nanosecond_diff = 200 ;
844
- }
838
+ double ppb = gptp_servo_pi (nanosecond_diff );
845
839
846
- ptp_clock_adjust (clk , nanosecond_diff );
840
+ ptp_clock_rate_adjust (clk , 1.0 + (ppb / 1000000000.0 ));
841
+ /* This prints continuous messages but can be enabled to see
842
+ * the real time convergence of synchronization offset.
843
+ * This is what users care about.
844
+ */
845
+ if (0 ) {
846
+ NET_INFO ("sync offset %9" PRId64 " ns, freq offset %f ppb" ,
847
+ nanosecond_diff , ppb );
848
+ }
847
849
}
848
850
}
849
851
#endif /* CONFIG_NET_GPTP_USE_DEFAULT_CLOCK_UPDATE */
Original file line number Diff line number Diff line change @@ -30,6 +30,18 @@ extern "C" {
30
30
#define GPTP_STATS_INC (port , var )
31
31
#endif
32
32
33
+ /**
34
+ * @brief gPTP domain all data.
35
+ */
36
+ struct gptp_domain_data {
37
+ /** gptp_domain pointer */
38
+ struct gptp_domain * domain ;
39
+ /** pi control drift value */
40
+ double pi_drift ;
41
+ };
42
+
43
+ extern struct gptp_domain_data gptp_domain_data ;
44
+
33
45
/**
34
46
* @brief Is a slave acting as a slave.
35
47
*
@@ -117,6 +129,15 @@ static inline uint64_t gptp_timestamp_to_nsec(struct net_ptp_time *ts)
117
129
return (ts -> second * NSEC_PER_SEC ) + ts -> nanosecond ;
118
130
}
119
131
132
+ /**
133
+ * @brief gPTP PI servo.
134
+ *
135
+ * @param nanosecond_diff nanosecond offset.
136
+ *
137
+ * @return ppb value to adjust.
138
+ */
139
+ double gptp_servo_pi (int64_t nanosecond_diff );
140
+
120
141
/**
121
142
* @brief Change the port state
122
143
*
You can’t perform that action at this time.
0 commit comments