DC configuration
Properties
| Category | Function | Type | Read/write | Description |
|---|---|---|---|---|
| DC and cycle | GetSlaveHasDC(mi, si) | BOOL | Read-only | Whether a single slave supports DC |
| get_master_dc_time(mi) | int64_t | Read-only | Master DC system time (ns, 2000-01-01 epoch) | |
| get_reference_clock_slave(mi) | uint16_t | Read-only | Reference clock slave index (1-based); 0 = no DC |
Methods
ConfigureDCAll()
int ConfigureDCAll(uint16_t master_index, uint32_t sync0_ns, uint32_t sync1_ns);
int AutoCalculateDCShift(uint16_t master_index);
void SetMasterDCCycleTime(uint16_t master_index, uint32_t time_ns);
void SetMasterLoopCycleTime(uint16_t master_index, uint32_t time_ns);
ConfigureDCAll() configures DC synchronization for every DC slave (parameters in nanoseconds). It performs the propagation delay measurement, the phase resolution and the SYNC0/SYNC1 writes, and returns the number of slaves that landed and were accepted = armed + prepared in PreOp (never equal to the number armed; on the first PRE_OP pass it writes only 0x1C32:01 + 0x09A0, and the runtime arms 0x0980 before SafeOp/OP). sync0_ns=0 disables DC, and sync1_ns is an incremental time relative to SYNC0. AutoCalculateDCShift() resolves the phase of each DC slave from the ESI explicit value, the user explicit value or the 70% default; the propagation delay is corrected in hardware by the ESC from 0x0928 and is not folded into the phase.
Example:
SetMasterLoopCycleTime(master, 125000); /* PDO cycle 125 µs (recommended in real-time mode) */
int n = ConfigureDCAll(master, 125000, 0);
AutoCalculateDCShift(master);
printf("配置了 %d 个 DC 从站\n", n); /* Configured %d DC slaves */
- SYNC0 and LoopCycle should be equal or an integer multiple of each other
- Oversampling exception — oversampling devices use a SYNC0 cycle shorter than LoopCycle and need dedicated device hardware and PDO mapping support
SetSyncBySlaveIndex()
void SetSyncBySlaveIndex(uint16_t master_index, uint16_t slave_index,
uint32_t sync0_ns, uint32_t sync1_ns, int32_t shift_ns);
Configures the DC synchronization parameters of a single slave (shift_ns is the phase offset). Use it only when individual slaves need different parameters. sync0_ns = 0 disarms SYNC0 (back to FreeRun); shift_ns is tri-state: DC_SHIFT_UNSPECIFIED = not specified (resolved by precedence), 0 = a literal 0 phase, a negative value = an explicit negative phase. The parameter semantics are the same as in slave DC configuration.
EnableContinuousMeasurement()
void EnableContinuousMeasurement(uint16_t master_index, BOOL enable, uint32_t interval_sec);
Enables or disables continuous propagation delay measurement (ETG.1500 5.13.2). Once enabled, the master re-measures the propagation delay periodically; this suits cases where temperature drift and similar effects change the delay. interval_sec=0 uses the default interval.
Example:
EnableContinuousMeasurement(master, TRUE, 60); /* re-measure every 60 seconds */
By default the DC offset is computed once, at ConfigureDCAll() time. Enable this only for long-running systems where the ambient temperature varies widely.
The DC sync mode is configured explicitly by the user (SetDcSyncMode() / GetDcSyncMode()). After a snapshot restore, slaves that need DC should be confirmed to be in DC_SYNCHRON / DC_SYNCHRON_01 (dc_sync_mode_t).
Propagation delay
int UpdatePropagationDelays(uint16_t master_index);
int32_t GetSlavePropagationDelay(uint16_t master_index, uint16_t slave_index);
int32_t GetMaxPropagationDelay(uint16_t master_index);
UpdatePropagationDelays() triggers one network-wide propagation delay measurement. GetSlavePropagationDelay() returns the delay of a single slave (ns), and GetMaxPropagationDelay() returns the largest delay in the network.
Sync window monitoring
The master checks the time deviation of every DC slave once per second. When a slave's deviation exceeds the SyncWindowThreshold threshold, the DCSyncLost callback fires.
void SetSyncWindowThreshold(uint16_t master_index, int threshold_ns);
int GetSyncWindowThreshold(uint16_t master_index);
The threshold defaults to 1000ns.
GetMaxSyncDifference()
int GetMaxSyncDifference(uint16_t master_index);
Returns the largest time deviation among all DC slaves (ns); returns -1 on failure.
The C layer keeps an integer summary:
-1carries both "read failed" and "no DC-active slave", and the two cannot be told apart. When you need a strict tri-state, usemaster.GetMaxSyncDifference()/MaxSyncDifferenceNs(int?) from the C# SDK.
IsAllSlavesInSync()
BOOL IsAllSlavesInSync(uint16_t master_index);
Checks whether every slave with SYNC0 DC enabled is inside the sync window (deviation ≤ SyncWindowThreshold). An all-FreeRun network is not "DC synchronized". Under FreeRun, in_sync must not be read as in sync.
GetSlaveSyncWindowStatus()
BOOL GetSlaveSyncWindowStatus(uint16_t master_index, uint16_t slave_index,
int* diff_ns, int* max_diff_ns, int* min_diff_ns,
BOOL* in_sync, uint32_t* out_of_sync_count);
Reads detailed DC sync window status for a slave: current deviation, historical maximum/minimum deviation, whether it is inside the sync window, and the cumulative out-of-sync count.
ResetSlaveSyncWindowStats()
void ResetSlaveSyncWindowStats(uint16_t master_index, uint16_t slave_index);
Resets the sync window statistics of one slave. Calling it for every slave resets all of them.
DC drift compensation
EnableDriftCompensation()
void EnableDriftCompensation(uint16_t master_index, BOOL enable,
int32_t threshold_ns, int32_t gain);
Enables or disables DC drift compensation. Drift compensation continuously corrects the difference between the system clock and the DC reference clock on the master side, so synchronization accuracy does not degrade over long runs.
Parameters:
threshold_ns— deviation threshold that triggers compensation (ns); nothing is corrected below it; 1000 is recommendedgain— compensation gain (controls the correction rate; larger corrects faster but may introduce oscillation); 512 is recommended
/* recommended parameters */
EnableDriftCompensation(master, TRUE, 1000, 512);
/* custom: 500ns threshold, lower gain (smoother) */
EnableDriftCompensation(master, TRUE, 500, 256);
/* disabled */
EnableDriftCompensation(master, FALSE, 0, 0);
The C SDK has no GetDriftCompensation interface: the getter is a caller-side cache, so keep your own BOOL drift_on after calling EnableDriftCompensation.
For long-running systems (>24 hours), enable drift compensation. The default parameters suit most scenarios.
Querying the master's current DC time
Every PDO cycle the master reads the 64-bit DC system time back from the reference clock slave with FRMW (Feed-Read-Multiple-Write). The SDK exposes that timestamp to the application layer for:
- Log time alignment — application logs can be stamped with DC time and line up exactly with the timeline of slave-side events (EMCY / Diagnosis History)
- Cross-slave event correlation — when several slaves report events at once, DC time tells you the absolute order
- Drift diagnosis — compare against the host wall clock or an external NTP source to watch the drift trend
DC time is in nanoseconds and starts at 2000-01-01 00:00:00 UTC (as defined by the ETG specification). It is offset from the UNIX timestamp by 30 years, so take care when converting.
get_master_dc_time()
int64_t get_master_dc_time(uint16_t master_index);
uint16_t get_reference_clock_slave(uint16_t master_index);
get_master_dc_time() returns the master's current 64-bit DC system time (ns, signed int64_t); 0 means DC is not active yet or there is no reference clock. get_reference_clock_slave() returns the reference clock slave index (1-based) chosen by the SDK in the configdc phase: the first slave on the line that has DC active (a real SYNC0) (skipping couplers that are DC capable but run in FreeRun); when no slave has DC active it falls back to the first DC-capable slave on the line.
Example:
uint16_t ref = get_reference_clock_slave(master);
if (ref == 0) {
printf("网络无 DC 从站\n"); /* No DC slaves on the network */
} else {
int64_t now = get_master_dc_time(master);
printf("参考时钟: 从站 %u, DC 时间 = %lld ns\n", /* Reference clock: slave %u, DC time = %lld ns */
ref, (long long)now);
}
It is updated once per PDO cycle by the FRMW frame, so the resolution equals LoopCycle. The master returns the last value it read back and never extrapolates. For actual event timestamps, prefer the slave's local latch registers.
Full example
125 µs high performance (real-time mode)
#include "ethercat.h"
#include <stdio.h>
static void on_dc_lost(uint16_t mi, uint16_t si, int diff) {
printf("从站 %d DC 同步丢失: 偏差 %dns\n", si, diff); /* Slave %d lost DC sync: deviation %dns */
}
int main(void) {
uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{GUID}", "");
/* 1. configure the cycle + DC */
SetMasterLoopCycleTime(master, 125000);
int dc_count = ConfigureDCAll(master, 125000, 0);
AutoCalculateDCShift(master);
printf("配置了 %d 个 DC 从站\n", dc_count); /* Configured %d DC slaves */
/* 2. sync window threshold + monitoring */
SetSyncWindowThreshold(master, 500);
SetDCSyncLostCallback(on_dc_lost);
/* 3. start up to OP */
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
/* 4. check the sync status */
printf("所有从站同步: %s\n", /* All slaves in sync: %s */
IsAllSlavesInSync(master) ? "是" : "否");
printf("最大同步偏差: %d ns\n", /* Max sync deviation: %d ns */
GetMaxSyncDifference(master));
getchar();
Stop(master);
Dispose(master);
return 0;
}
Continuous measurement and drift compensation
/* long run: enable continuous propagation delay measurement (temperature drift compensation) */
EnableContinuousMeasurement(master, TRUE, 60);
/* enable drift compensation */
EnableDriftCompensation(master, TRUE, 1000, 512);
/* sync window monitoring */
if (!IsAllSlavesInSync(master)) {
int max_diff = GetMaxSyncDifference(master);
printf("同步偏差过大: %d ns\n", max_diff); /* Sync deviation too large: %d ns */
}