DC configuration
- For the DC configuration of a single slave see the slave's
ConfigureDC()method
Master-level DC configuration
ConfigureDC()
int ConfigureDC(uint32_t sync0CycleNs, uint32_t sync1CycleNs = 0);
Configures DC synchronization for all DC slaves. All parameter units: nanoseconds (ns). By default it does the following automatically:
- Propagation delay measurement — reads the receive time of each port and computes the delay
- Phase calculation — resolves each slave's phase by ESI explicit / user explicit / default 70% (phase only; the propagation delay is corrected by the ESC in hardware through
0x0928and is not folded into the phase) - DC application — writes the SYNC0/SYNC1 cycle and start time to each slave
Parameters:
sync0CycleNs(uint32_t) — SYNC0 cycle (nanoseconds); 0 disables DCsync1CycleNs(uint32_t) — SYNC1 delta time (nanoseconds); 0 means SYNC0 only
Returns:
int— the number of DC slaves applied and accepted = armed + prepared in PreOp (never equivalent to the armed count; on the first PRE_OP only 0x1C32:01 + 0x09A0 are written, and 0x0980 is armed by the runtime before SafeOp/OP); <0 = failure
Example:
// SYNC0 = 1 ms, SYNC0 only
master.ConfigureDC(1000000);
// SYNC0 = 1ms, SYNC1 = 500 µs
master.ConfigureDC(1000000, 500000);
// SYNC0 = 125 µs (recommended for real-time drives)
master.ConfigureDC(125000);
125 µs high-performance configuration (recommended for the real-time driver):
master.LoopCycle(125000);
master.ConfigureDC(125000);
1ms standard configuration (general use):
master.LoopCycle(1000000);
master.ConfigureDC(1000000);
- SYNC0 and LoopCycle — keep them equal or an integer multiple of each other; synchronizing on data that has not arrived is meaningless
- Automatic phase (recommended) —
ConfigureDC()takes no phase, and the master resolves it by priority; the propagation delay is still measured and written to0x0928 - Oversampling exception — on oversampling devices SYNC0 < LoopCycle, which needs dedicated device hardware and PDO mapping support
SYNC1 is the delta time relative to SYNC0. For example SYNC0=1,000,000ns, SYNC1=500,000ns means SYNC1 triggers 500 µs after each SYNC0.
The DC synchronization mode is configured explicitly by the user. After restoring a snapshot, slaves that need DC should confirm they have entered DcSynchron / DcSynchron01.
Slave-level DC configuration
Slave-level ConfigureDC() accepts a DcSyncMode parameter (implemented through the SlaveDC namespace) and can set the synchronization mode for a single slave.
DcSyncMode enum:
enum class DcSyncMode : int {
FreeRun = 0, // Free run (local clock, not DC synchronisation)
SmSynchron = 1, // SM synchronisation mode
DcSynchron = 2, // DC synchronisation (SYNC0 only)
DcSynchron01 = 3 // DC synchronisation (SYNC0 + SYNC1)
};
using namespace darra;
auto& s = master.GetSlave(1);
// Configure by synchronisation mode through the SlaveDC namespace
SlaveDC::ConfigureDC(s.Dll(), master.MasterNumber(), 1,
DcSyncMode::DcSynchron, 1000000); // DC synchronisation SYNC0 = 1 ms
SlaveDC::ConfigureDC(s.Dll(), master.MasterNumber(), 1,
DcSyncMode::SmSynchron); // SM synchronisation mode
SlaveDC::ConfigureDC(s.Dll(), master.MasterNumber(), 1,
DcSyncMode::FreeRun); // Disable DC synchronisation
master.ConfigureDC()— configures all DC slaves in one goSlaveDC::ConfigureDC()— configures a single slave
Use the slave-level method only when individual slaves need different parameters.
Slave-level shortcut methods
auto& s = master.GetSlave(1);
// Set the DC synchronisation parameters of a single slave
s.ConfigureDC(1000000); // SYNC0 = 1ms
s.ConfigureDC(1000000, 500000); // SYNC0 = 1ms, SYNC1 = 500 µs
s.ConfigureDC(125000, 0, 100000); // SYNC0 = 125 µs, manual offset 100 µs
// Disable DC
s.DisableDC();
Continuous propagation delay measurement
EnableContinuousMeasurement()
// Call through the SlaveDC namespace
SlaveDC::EnableContinuousMeasurement(dll, masterIndex, enable, intervalSec);
Enables or disables continuous propagation delay measurement (ETG.1500 5.13.2). Once it is enabled, the master re-measures the propagation delay periodically; this suits cases where temperature drift and the like change the delay.
Parameters:
enable(bool) —trueenables,falsedisablesintervalSec(uint32_t) — measurement interval (seconds); 0 means the default interval
Example:
// Re-measure every 60 seconds
SlaveDC::EnableContinuousMeasurement(master.Dll(), master.MasterNumber(), true, 60);
// Disabled
SlaveDC::EnableContinuousMeasurement(master.Dll(), master.MasterNumber(), false);
By default the DC offset is calculated once during ConfigureDC(). Enable this feature only for long-running systems with large ambient temperature changes.
Propagation delay
Slave propagation delay
auto& s = master.GetSlave(1);
int delay = s.PropagationDelay();
printf("从站 1 传播延迟: %d ns\n", delay); // Slave 1 propagation delay: %d ns
Sync window monitoring
The master automatically checks the time deviation of every DC slave once per second. When a slave's deviation exceeds the threshold, the DCSyncLost event fires.
// Threshold configuration
darra::ethercat::master_other::set_sync_window_threshold(master, 500); // Set it to 500 ns
// Event: fired once when a slave goes from "in sync" to "out of sync" (no repeat; register with OnDCSyncLost)
master.Events().OnDCSyncLost([](uint16_t mi, uint16_t si, int diff) {
printf("从站 %d DC 同步丢失: 偏差 %d ns\n", si, diff); // Slave %d lost DC sync: deviation %d ns
});
GetMaxSyncDifference()
// Call through the SlaveDC namespace
int SlaveDC::GetMaxSyncDifference(dll_t& dll, uint16_t mi);
Gets the largest time deviation among all DC slaves (nanoseconds).
Returns:
int— the largest time deviation (nanoseconds); -1 on failure
The C++ layer keeps the underlying integer summary:
-1carries both "read failed" and "no DC-active slave", which cannot be told apart. For a strict three-state result use the C# SDK'smaster.GetMaxSyncDifference()/MaxSyncDifferenceNs(int?).
Example:
int maxDiff = SlaveDC::GetMaxSyncDifference(master.Dll(), master.MasterNumber());
printf("最大同步偏差: %d ns\n", maxDiff); // Max sync deviation: %d ns
IsAllSlavesInSync()
// Call through the SlaveDC namespace
bool SlaveDC::IsAllSlavesInSync(dll_t& dll, uint16_t mi, int thresholdNs = 1000);
Checks whether all slaves with SYNC0 DC enabled are inside the sync window. All slaves running in FreeRun does not mean "DC synchronized".
Returns:
bool—truewhen all SYNC0 DC slaves are inside the window; FreeRun must not be read as synchronized
Example:
if (!SlaveDC::IsAllSlavesInSync(master.Dll(), master.MasterNumber()))
printf("存在从站同步偏差过大\n"); // Some slaves have a sync deviation that is too large
ResetAllSyncWindowStats()
// Call through the SlaveDC namespace
void SlaveDC::ResetAllSyncWindowStats(dll_t& dll, uint16_t mi);
Resets the sync window statistics of all slaves (maximum/minimum time difference, out-of-sync count and so on).
Example:
SlaveDC::ResetAllSyncWindowStats(master.Dll(), master.MasterNumber());
Slave sync window status
auto& s = master.GetSlave(1);
auto status = s.GetSyncWindowStatus();
if (status) {
printf("从站 1 同步状态:\n"); // Slave 1 sync status:
printf(" 当前偏差: %d ns\n", status->DiffNs); // Current deviation: %d ns
printf(" 最大偏差: %d ns\n", status->MaxDiffNs); // Max deviation: %d ns
printf(" 最小偏差: %d ns\n", status->MinDiffNs); // Min deviation: %d ns
printf(" 同步状态: %s\n", status->InSync ? "正常" : "异常"); // Sync status: %s (OK / abnormal)
printf(" 失步次数: %u\n", status->OutOfSyncCount); // Out-of-sync count: %u
}
For the detailed sync window status of a single slave use slave.GetSyncWindowStatus(), which includes DiffNs, MaxDiffNs, MinDiffNs, InSync, OutOfSyncCount.
DC sync lost callback
Register with OnDCSyncLost() (multiple subscribers; do not assign with =).
master.Events().OnDCSyncLost([](uint16_t mi, uint16_t si, int diff) {
printf("从站 %d DC 同步丢失: 偏差 %d ns\n", si, diff); // Slave %d lost DC sync: deviation %d ns
});
DC drift compensation
EnableDriftCompensation()
// Configure through MasterConfig
auto& config = master.GetConfig();
config.DriftCompensation(bool enable, int32_t thresholdNs, int32_t gain);
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, preventing synchronization accuracy from degrading after a long run.
Parameters:
enable(bool) —trueenables,falsedisablesthresholdNs(int32_t) — deviation threshold that triggers compensation (nanoseconds); no correction is made while the deviation is below this valuegain(int32_t) — compensation gain, controlling the correction rate (a larger value corrects faster but may introduce oscillation)
The single-argument overload config.DriftCompensation(true) uses a default threshold of 1000ns and a gain of 100.
Example:
auto& config = master.GetConfig();
// Enable with defaults (thresholdNs=1000, gain=100)
config.DriftCompensation(true);
// Custom parameters: 500 ns threshold, lower gain (a smoother correction)
config.DriftCompensation(true, 500, 256);
// Disabled
config.DriftCompensation(false);
It can also be called directly through the SlaveDC namespace (that path defaults to thresholdNs=1000, gain=512):
SlaveDC::EnableDriftCompensation(master.Dll(), master.MasterNumber(), true, 1000, 512);
For long-running systems (more than 24 hours), enabling drift compensation is recommended. The default parameters suit most cases; adjust the threshold and gain only when finer control is needed.
Complete example
125 µs high performance (real-time driver)
#include "ethercat.hpp"
using namespace darra;
int main() {
try {
EtherCATMaster master(dll);
master.SetNetwork("\\Device\\NPF_{...}");
master.Build();
// 1. Configure the cycle time 125 µs
master.LoopCycle(125000);
// 2. Configure DC (SYNC0 = 125 µs)
master.ConfigureDC(125000);
// 3. Drift compensation
master.GetConfig().DriftCompensation(true, 1000, 512);
// 4. Listen for DC synchronisation loss
master.Events().OnDCSyncLost([](uint16_t mi, uint16_t si, int diff) {
printf("从站 %d DC 同步丢失: %d ns\n", si, diff); // Slave %d lost DC sync: %d ns
});
// 5. Switch to OP
master.SetState(EcState::OP);
master.Start();
printf("运行中... 按 Enter 停止\n"); // Running... press Enter to stop
getchar();
} catch (const ethercat::DarraException& e) {
printf("错误: %s\n", e.what()); // Error: %s
return -1;
}
return 0;
}
Continuous measurement and sync monitoring
// Long-running: enable continuous propagation delay measurement (for temperature drift compensation)
SlaveDC::EnableContinuousMeasurement(master.Dll(), master.MasterNumber(), true, 60);
// Sync window monitoring
if (!SlaveDC::IsAllSlavesInSync(master.Dll(), master.MasterNumber())) {
int maxDiff = SlaveDC::GetMaxSyncDifference(master.Dll(), master.MasterNumber());
printf("同步偏差过大: %d ns\n", maxDiff); // Sync deviation too large: %d ns
}
// Reset the statistics
SlaveDC::ResetAllSyncWindowStats(master.Dll(), master.MasterNumber());
1ms standard configuration
EtherCATMaster master(dll);
master.SetNetwork("\\Device\\NPF_{...}");
master.Build();
// Configure a 1 ms cycle
master.LoopCycle(1000000);
master.ConfigureDC(1000000);
// Enable drift compensation
master.GetConfig().DriftCompensation(true, 1000, 512);
// Switch to OP
master.SetState(EcState::OP);
master.Start();
// Check the sync status periodically
for (int i = 1; i <= master.SlaveCount(); ++i) {
auto status = master.GetSlave(i).GetSyncWindowStatus();
if (status && !status->InSync) {
printf("从站 %d 同步偏差: %d ns\n", i, status->DiffNs); // Slave %d sync deviation: %d ns
}
}
CurrentDcSyncMode is obtained through SlaveDC::GetCurrentDcSyncMode(), which returns the slave's current DC synchronization mode.
Master DC time queries
The master provides two read-only interfaces for querying DC time and reference clock information: getting the current master-side DC timestamp (for correlating logs and aligning event timelines), and querying which slave is selected as the reference clock source.
GetMasterDCTime()
class EtherCATMaster {
public:
int64_t GetMasterDCTime() const;
};
Gets the master's current DC timestamp (nanoseconds), based on 2000-01-01 00:00:00 UTC (defined by the EtherCAT DC protocol). Internally it reads the master's DC-compensated local clock; the accuracy is related to the sync window threshold.
Returns:
int64_t— nanoseconds since 2000-01-01; returns 0 when the master is not initialized or DC is not enabled
Example:
using namespace std::chrono;
int64_t dcNs = master.GetMasterDCTime();
// Convert to time_point (2000-01-01 + dcNs)
constexpr auto ec_epoch = sys_days{2000y / January / 1};
auto tp = ec_epoch + nanoseconds{dcNs};
auto t = system_clock::to_time_t(system_clock::time_point{tp.time_since_epoch()});
std::cout << "DC 时间: " << std::put_time(std::localtime(&t), "%F %T") // DC time:
<< " (+" << (dcNs % 1'000'000'000) << " ns)\n";
GetReferenceClockSlaveIndex()
int GetReferenceClockSlaveIndex() const;
Gets the index of the slave selected as the DC reference clock. The reference clock is the first slave on the bus with DC actually active (real SYNC0) (skipping couplers that support DC but run in FreeRun); when no slave has DC active it falls back to the first DC-capable slave in the chain. The SDK selects it automatically during ConfigureDC().
Returns:
int— reference clock slave index (1-based); returns0when there is no DC slave, or when it is not configured or not initialized
Example:
int refSlave = master.GetReferenceClockSlaveIndex();
if (refSlave > 0) {
auto& s = master.GetSlave(refSlave);
std::cout << "参考时钟: 从站 " << refSlave // Reference clock: slave
<< " (" << s.Name() << ")\n";
}
- Log time alignment: replace master log timestamps with DC time, so they can be correlated precisely with slave-side hardware timestamps (such as IO sampling times)
- Event timeline: alarms, diagnostics and PDO anomaly events all use the DC timestamp, which makes aggregating data across sites easier
- Reference clock monitoring: if the reference clock slave goes offline, DC reconfiguration must be triggered manually
ETG.1000 Part 4 §6.8.3 distributed clock reference clock mechanism; ETG.1020 §6.4.