ethercat-linux/vendor/ethercrab
Tony Cao ad312c0b00 feat(phase-ab): 4kHz timestamp stamping + rfm-sync-4k experiment + dc-align + L0b fresh watchdog
- Phase A: aOut1[264..271]=plc_ts / aIn1[264..279]=t_d2k_ns+plc_ts_echo (RTT p50=2.0ms p99=2.25ms, 7.7M samples)
- rfm-sync-4k chunked mode: implemented, measured infeasible (slot exec p50=295us > 250us budget)
- --dc-align + --sync-offset-ns/--dc-wphase-ns: DC-grid phase gating for FPRD/FPWR kicks;
  sweep showed net loss vs free-running async (p50 RTT 2.0->2.5-3.0ms), kept off in production
- L0b watchdog: fresh-stall >3s -> exit(42); fixes blind spot where bridge wedge freezes
  tx_seq and old echo watchdog condition stays false (2026-11-13 incident, cured by CX TwinCAT reset)
- ethercrab pdu_rx/pdu_loop patch: late/short/unknown-idx frames downgraded to Ignored
  instead of killing the tx_rx task (production robustness fix)
- plc_data_age metric: (dc_read - plc_ts) mod 250us, proved end-to-end DC grid lock
- production binary md5=72eb50fb deployed to D2000 + SSD p4
2026-11-16 17:19:17 +08:00
..
.cargo J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.circleci J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.github J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
benches J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
examples J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
src feat(phase-ab): 4kHz timestamp stamping + rfm-sync-4k experiment + dc-align + L0b fresh watchdog 2026-11-16 17:19:17 +08:00
tests J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.cargo_vcs_info.json J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.cargo-ok J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.gitattributes J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
.gitignore J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
Cargo.toml J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
Cargo.toml.orig J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
CHANGELOG.md J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
dc-align.plot J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
dc-pd.plot J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
ethercrab-logo-docsrs.svg J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
ethercrab-logo-solid-white.svg J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
ethercrab-logo.svg J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
FAQ.md J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
Justfile J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
README.md J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
README.tpl J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
release.toml J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
repo-stats.sh J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00
rust-toolchain.toml J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge 2026-07-22 08:55:41 +08:00

EtherCrab

Build Status Crates.io Docs.rs Matrix chat

A performant, async-first EtherCAT MainDevice written in pure Rust.

Crate features

  • std (enabled by default) - exposes the std module, containing helpers to run the TX/RX loop on desktop operating systems.
  • defmt - enable logging with the defmt crate.
  • log - enable logging with the log crate. This is enabled by default when the std feature is enabled.
  • serde - enable serde impls for some public items.
  • xdp - enable support for XDP on some (currently only Linux) systems.

For no_std targets, it is recommended to add this crate with

cargo add --no-default-features --features defmt

Examples

This example increments the output bytes of all detected SubDevices every tick. It is tested on an EK1100 with output modules but may work on other basic SubDevices.

Run with e.g.

Linux

RUST_LOG=debug cargo run --example ek1100 --release -- eth0

Windows

$env:RUST_LOG="debug" ; cargo run --example ek1100 --release -- '\Device\NPF_{FF0ACEE6-E8CD-48D5-A399-619CD2340465}'
use env_logger::Env;
use ethercrab::{
    error::Error, std::{ethercat_now, tx_rx_task}, MainDevice, MainDeviceConfig, PduStorage, Timeouts
};
use std::{sync::Arc, time::Duration};
use tokio::time::MissedTickBehavior;

/// Maximum number of SubDevices that can be stored. This must be a power of 2 greater than 1.
const MAX_SUBDEVICES: usize = 16;
/// Maximum PDU data payload size - set this to the max PDI size or higher.
const MAX_PDU_DATA: usize = 1100;
/// Maximum number of EtherCAT frames that can be in flight at any one time.
const MAX_FRAMES: usize = 16;
/// Maximum total PDI length.
const PDI_LEN: usize = 64;

static PDU_STORAGE: PduStorage<MAX_FRAMES, MAX_PDU_DATA> = PduStorage::new();

#[tokio::main]
async fn main() -> Result<(), Error> {
    env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

    let interface = std::env::args()
        .nth(1)
        .expect("Provide network interface as first argument.");

    log::info!("Starting EK1100 demo...");
    log::info!("Ensure an EK1100 is the first SubDevice, with any number of modules connected after");
    log::info!("Run with RUST_LOG=ethercrab=debug or =trace for debug information");

    let (tx, rx, pdu_loop) = PDU_STORAGE.try_split().expect("can only split once");

    let maindevice = Arc::new(MainDevice::new(
        pdu_loop,
        Timeouts {
            wait_loop_delay: Duration::from_millis(2),
            mailbox_response: Duration::from_millis(1000),
            ..Default::default()
        },
        MainDeviceConfig::default(),
    ));

    tokio::spawn(tx_rx_task(&interface, tx, rx).expect("spawn TX/RX task"));

    let mut group = maindevice
        .init_single_group::<MAX_SUBDEVICES, PDI_LEN>(ethercat_now)
        .await
        .expect("Init");

    log::info!("Discovered {} SubDevices", group.len());

    for subdevice in group.iter(&maindevice) {
        // Special case: if an EL3004 module is discovered, it needs some specific config during
        // init to function properly
        if subdevice.name() == "EL3004" {
            log::info!("Found EL3004. Configuring...");

            subdevice.sdo_write(0x1c12, 0, 0u8).await?;
            subdevice.sdo_write(0x1c13, 0, 0u8).await?;

            subdevice.sdo_write(0x1c13, 1, 0x1a00u16).await?;
            subdevice.sdo_write(0x1c13, 2, 0x1a02u16).await?;
            subdevice.sdo_write(0x1c13, 3, 0x1a04u16).await?;
            subdevice.sdo_write(0x1c13, 4, 0x1a06u16).await?;
            subdevice.sdo_write(0x1c13, 0, 4u8).await?;
        }
    }

    let mut group = group.into_op(&maindevice).await.expect("PRE-OP -> OP");

    for subdevice in group.iter(&maindevice) {
        let io = subdevice.io_raw();

        log::info!(
            "-> SubDevice {:#06x} {} inputs: {} bytes, outputs: {} bytes",
            subdevice.configured_address(),
            subdevice.name(),
            io.inputs().len(),
            io.outputs().len()
        );
    }

    let mut tick_interval = tokio::time::interval(Duration::from_millis(5));
    tick_interval.set_missed_tick_behavior(MissedTickBehavior::Skip);

    loop {
        group.tx_rx(&maindevice).await.expect("TX/RX");

        // Increment every output byte for every SubDevice by one
        for mut subdevice in group.iter(&maindevice) {
            let mut io = subdevice.io_raw_mut();

            for byte in io.outputs().iter_mut() {
                *byte = byte.wrapping_add(1);
            }
        }

        tick_interval.tick().await;
    }
}

Community

We're on Matrix!

Current and future features

  • async API
  • Usable in no_std contexts with no allocator required, as long as an async executor is available.
  • Autoconfigure SubDevices from their EEPROM (SII) data during startup
    • Supports configuration using CoE data
  • Safely usable in multi-threaded Linux systems with e.g. smol, tokio or std::thread and block_on.
  • Support for io_uring on Linux systems to improve performance and latency
  • Support for SDO read/writes to configure SubDevices
  • Distributed clocks
    • Detection of delays between SubDevices in topology
    • Static drift compensation on startup
    • Cyclic synchronisation during OP
  • Basic support for CiA402/DS402 drives
    • A higher level DS402 API for torque, position and velocity control of common servo drives in a more abstract way.
  • Integration with LinuxCNC as a HAL component using the linuxcnc-hal crate.
  • Load SubDevice configurations from ESI XML files

Sponsors

GitHub Sponsors

Thank you to everyone who has donated test equipment, time or money to the EtherCrab project! Would you like to be in this list? Then please consider becoming a Github sponsor!

  • @nealsjoe generously donated an EK1100 with several IO modules for testing with.
  • Trisk Bio generously donated some additional Beckhoff modules and some optical ethernet gear.
  • Smark sent a $200 one time donation. Thank you!

License

Licensed under either of

at your option.