Skip to main content

DC configuration

Master-level DC configuration. Call it directly through master.

Properties​

CategoryMethodTypeR/WDescription
DChas_dc()Option<bool>Read-onlyWhether the network has a slave that supports DC (None = the master structure cannot be read, which does not mean there is no DC)

Methods​

configure_dc_all()​

pub fn configure_dc_all(&self, sync0_ns: u32, sync1_ns: u32) -> Result<i32>

Configures DC synchronization for all DC slaves. All parameters are in nanoseconds (ns). The following is done automatically by default:

  1. Propagation delay measurement — reads the receive time of each port and computes the delay
  2. Phase calculation — resolves the phase of each slave by ESI explicit / user explicit / default 70% (pure phase; the propagation delay is corrected in hardware by the ESC via 0x0928 and is not merged into the phase)
  3. Applying DC — writes the SYNC0/SYNC1 cycle and start time to each slave

Parameters:

  • sync0_ns — SYNC0 cycle (nanoseconds); 0 disables DC
  • sync1_ns — SYNC1 delta time (nanoseconds); 0 means SYNC0 only

Returns:

  • Result<i32> — number of DC slaves that landed and were accepted = armed + PreOp ready (never equivalent to the armed count); <0 = failure

Example:

master.configure_dc_all(1_000_000, 0)?;           // SYNC0 = 1ms
master.configure_dc_all(125_000, 0)?; // SYNC0 = 125 µs
master.configure_dc_all(1_000_000, 500_000)?; // SYNC0=1ms, SYNC1=500 µs
Recommended configuration

125 µs high-performance configuration (recommended for real-time driver mode):

master.config().set_loop_cycle(125_000);    // PDO exchange cycle 125 µs
master.configure_dc_all(125_000, 0)?; // SYNC0 = 125 µs

1ms standard configuration (general scenarios):

master.config().set_loop_cycle(1_000_000);  // PDO exchange cycle 1ms
master.configure_dc_all(1_000_000, 0)?; // SYNC0 = 1ms
  • SYNC0 and LoopCycle — should be identical or an integer multiple of it; synchronization of data that has not arrived is meaningless
  • Automatic phase (recommended) — pass None for shift_ns and the master resolves the phase by priority; the propagation delay is measured as usual and written to 0x0928
  • Manual shift — you can also set a high fixed value, such as 0.8 × sync0_cycle_ns
  • Oversampling exception — with oversampling devices SYNC0 < LoopCycle, which requires dedicated support from the device hardware and the PDO mapping
About SYNC1

SYNC1 is a delta time relative to SYNC0. For example, SYNC0=1,000,000ns, SYNC1=500,000ns means SYNC1 triggers 500 µs after each SYNC0.

note

The DC synchronization mode is configured explicitly by the user. After restoring a snapshot, slaves that need DC should be confirmed to have entered DcSynchron / DcSynchron01.

Difference from slave-level configuration
  • master.configure_dc_all() — configures all DC slaves at once
  • slave.dc().configure() — configures a single slave

Use the slave-level method only when individual slaves need different parameters.

enable_continuous_measurement()​

pub fn enable_continuous_measurement(&self, enable: bool, interval_sec: u32)

Enables or disables continuous propagation delay measurement (ETG.1500 5.13.2). Once enabled, the master re-measures the propagation delay periodically; this suits scenarios where temperature drift and similar effects change the delay.

Parameters:

  • enable — true enables, false disables
  • interval_sec — measurement interval (seconds); 0 means use the default interval

Example:

master.enable_continuous_measurement(true, 60);  // re-measure every 60 seconds
master.enable_continuous_measurement(false, 0); // disable
note

By default the DC shift is calculated once during configure_dc_all() (single-slave configuration goes through slave.dc().configure()). Enable this feature only for long-running systems where the ambient temperature varies a lot.

Sync window monitoring​

The master checks the time deviation of each DC slave automatically once per second. When a slave's deviation exceeds the sync_window_threshold() threshold, the on_dc_sync_lost event fires.

// threshold: 1000ns by default, adjustable
master.diagnostics_info().set_sync_window_threshold(500); // set to 500ns

// event: fires once when a slave goes from "in sync" to "out of sync" (does not fire repeatedly)
master.events().on_dc_sync_lost(|master_idx, slave_idx, diff_ns| {
println!("从站 {} DC 同步丢失: 偏差 {}ns", slave_idx, diff_ns); // Slave {} DC sync lost: deviation {}ns
});

max_sync_difference()​

pub fn max_sync_difference(&self) -> i32

Gets the maximum time deviation among all DC slaves (nanoseconds).

This entry point returns i32 (an underlying summary) and does not distinguish "read failed" from "no DC-active slave". The C# SDK has a separate int? three-state entry point; this language has no equivalent three-state.

Example:

let max_diff = master.max_sync_difference();
println!("最大同步偏差: {}ns", max_diff); // Maximum sync deviation: {}ns

is_all_slaves_in_sync()​

pub fn is_all_slaves_in_sync(&self) -> bool

Checks whether all slaves with SYNC0 DC enabled are inside the sync window (deviation <= sync_window_threshold()). FreeRun on all slaves is not "DC synchronized".

Returns:

  • bool — true when all SYNC0 DC slaves are inside the window; FreeRun must not be read as synchronized

Example:

if !master.is_all_slaves_in_sync() {
println!("存在从站同步偏差过大"); // A slave has a sync deviation that is too large
}

reset_all_sync_window_stats()​

pub fn reset_all_sync_window_stats(&self)

Resets the sync window statistics of all slaves (maximum/minimum time difference, out-of-sync count and so on).

Example:

master.reset_all_sync_window_stats();
Single slave

For the detailed sync window state of a single slave use slave.dc().sync_window_status(), which includes diff_ns, max_diff_ns, min_diff_ns, in_sync, out_of_sync_count.

DC drift compensation​

enable_drift_compensation()​

pub fn enable_drift_compensation(&self, enable: bool,
threshold_ns: i32, gain: i32)

Enables or disables DC drift compensation. Drift compensation continuously corrects the deviation between the system clock and the DC reference clock on the master side, which prevents synchronization accuracy from degrading over long runs.

Parameters:

  • enable — true enables, false disables
  • threshold_ns — deviation threshold that triggers compensation (nanoseconds); no correction is made below this value, default 1000ns
  • gain — compensation gain, which controls the correction rate (larger values correct faster but may introduce oscillation), default 512

Example:

// enable drift compensation with default parameters
master.enable_drift_compensation(true, 1000, 512);

// custom parameters: 500ns threshold, lower gain (smoother correction)
master.enable_drift_compensation(true, 500, 256);

// disable drift compensation
master.enable_drift_compensation(false, 0, 0);
Recommendation

For long-running systems (>24 hours), enabling drift compensation is recommended. The default parameters suit most scenarios; adjust the threshold and gain only when finer control is needed.

CurrentDcSyncMode

current_dc_sync_mode() is a method on the slave DC sub-object; access it through slave.dc().current_dc_sync_mode().

Full example​

125 µs high performance (real-time driver mode)​

let mut master = EtherCATMaster::new()?;
master.set_network(r"\Device\NPF_{...}", "")?;

// 1. configure the cycle — 125 µs (recommended value for real-time driver mode)
master.config().set_loop_cycle(125_000);

// 2. switch to SafeOp
master.set_state(EcState::SafeOp)?;

// 3. configure DC (automatic shift) — keep SYNC0 consistent with LoopCycle
// has_dc() returns Option<bool>: configure DC only on an explicit Some(true); an unreadable value does not count as supported
if master.has_dc() == Some(true) {
master.configure_dc_all(125_000, 0)?;
}

std::thread::sleep(std::time::Duration::from_secs(1));

// 4. switch to Op
master.set_state(EcState::Operational)?;

// listen for DC synchronization loss events
master.events().on_dc_sync_lost(|_, slave_idx, diff_ns| {
println!("从站 {} DC 同步丢失: 偏差 {}ns", slave_idx, diff_ns); // Slave {} DC sync lost: deviation {}ns
});

Continuous measurement and sync monitoring​

// long-running: enable continuous propagation delay measurement (temperature drift compensation)
master.enable_continuous_measurement(true, 60);

// sync window monitoring
if !master.is_all_slaves_in_sync() {
let max_diff = master.max_sync_difference();
println!("同步偏差过大: {}ns", max_diff); // Sync deviation too large: {}ns
}

// reset statistics
master.reset_all_sync_window_stats();

// drift compensation (recommended for long-running systems)
master.enable_drift_compensation(true, 1000, 512);
SM synchronization type configuration

The SM synchronization type is configured automatically through the slave-level slave.dc().configure_mode(mode, ...) method; there is no need to call it manually.

DC time queries​

At runtime you can query the current DC system time and the reference clock slave index through the master instance, for scenarios such as log printing, timestamp alignment and clock comparison across masters.

master_dc_time()​

pub fn master_dc_time(&self) -> u64

The 64-bit DC system time most recently retrieved (nanoseconds); the epoch is 2000-01-01 00:00:00. A return of 0 means:

  • no DC slaves
  • configure_dc_all() has not been called
  • DC is not active

Example:

let now_ns = master.master_dc_time();
if now_ns == 0 {
println!("DC 尚未激活"); // DC not active yet
} else {
// nanoseconds -> seconds since 2000-01-01
let sec = now_ns / 1_000_000_000;
println!("DC 时间: {} ns ({} s 自 2000-01-01)", now_ns, sec); // DC time: {} ns ({} s since 2000-01-01)
}

reference_clock_slave_index()​

pub fn reference_clock_slave_index(&self) -> i32

Index of the slave currently serving as the DC reference clock (1-based).

Returns:

  • > 0 — index of the slave serving as the reference clock
  • 0 — no DC slaves in the network

Example:

match master.reference_clock_slave_index() {
0 => println!("无 DC 参考时钟 (网络中无 DC 从站)"), // No DC reference clock (no DC slaves on the network)
idx => println!("参考时钟从站: slave[{}]", idx), // Reference clock slave: slave[{}]
}

Example: annotating logs with DC timestamps​

use darra_ethercat::slave::dc::MasterDC;

let ref_idx = master.reference_clock_slave_index();
let t0 = master.master_dc_time();
println!("启动时 DC 时间 = {} ns, 参考时钟 = slave[{}]", t0, ref_idx); // DC time at startup = {} ns, reference clock = slave[{}]

// the callback must be 'static and cannot capture master (EtherCATMaster is not Clone and implements Drop).
// build a master_idx inside the callback from the MasterDC parameter (its ::new takes a bare u16).
master.events().on_pdo_cyclic_sync(|master_idx| {
let t = MasterDC::new(master_idx).master_dc_time();
// annotate the cyclic frame with the reference clock slave time
println!("[dc={} ns] cycle", t);
});
References
  • ETG.1000.6 §5.3 Distributed Clocks
  • ETG.1500 §5.13 propagation delay / drift compensation