ethercat-linux/vendor/ethercrab/examples/ek1100.rs
Tony Cao 21b2d3bf99 J1900 <-> TwinCAT 1 kHz verification over EL6695 bridge
Re-implementation of rustbootd's ecat_el6695_* examples as a single binary
with fixes found in review and on hardware:

- DC-follow PLL hardened against period-2 hunting: slew-limited anchor
  (+/-50us/cycle), bistable-trap snap re-anchor, re-prime on stale deadline
- drift-free absolute-grid ticker mode; probe mode for timestamp forensics
- bounded-memory online stats (histograms), graceful SIGINT/SIGTERM shutdown
  with full report, error-streak abort
- timestamp plausibility filter comparing against the previous raw sample
  (avoids the deadlock after a startup outlier)
- XFC scope waveform options: --el2202 (with --el2202-dual), --el2262,
  --el1252 latch timestamp readback with per-channel edge statistics
- register access unified in regs.rs: named bit constants everywhere,
  read-modify-write for enable/activation bytes
- vendored patched ethercrab 0.7.1 (sdo_write_complete, send_raw_coe)

Verified on J1900 (PREEMPT_RT 6.6.135): 600k cycles/600s exact 1 kHz,
tx/rx zero errors, phase_err p50=164us std=5us; EL2202<->EL1252 loopback
edge interval mean 2000.24us std=24.65us.
2026-07-22 08:55:41 +08:00

171 lines
5.4 KiB
Rust

//! Demonstrate setting outputs using a Beckhoff EK1100/EK1501 and modules.
//!
//! Run with e.g.
//!
//! Linux
//!
//! ```bash
//! RUST_LOG=debug cargo run --example ek1100 --release -- eth0
//! ```
//!
//! Windows
//!
//! ```ps
//! $env:RUST_LOG="debug" ; cargo run --example ek1100 --release -- '\Device\NPF_{FF0ACEE6-E8CD-48D5-A399-619CD2340465}'
//! ```
use env_logger::Env;
use ethercrab::{
MainDevice, MainDeviceConfig, PduStorage, Timeouts, error::Error, std::ethercat_now,
};
use futures_lite::StreamExt;
use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
/// 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 = PduStorage::element_size(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();
fn main() -> Result<(), Error> {
smol::block_on(async {
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/EK1501 demo...");
log::info!(
"Ensure an EK1100 or EK1501 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(),
));
#[cfg(target_os = "windows")]
std::thread::spawn(move || {
ethercrab::std::tx_rx_task_blocking(
&interface,
tx,
rx,
ethercrab::std::TxRxTaskConfig { spinloop: false },
)
.expect("TX/RX task")
});
#[cfg(not(target_os = "windows"))]
smol::spawn(ethercrab::std::tx_rx_task(&interface, tx, rx).expect("spawn TX/RX task"))
.detach();
let 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) {
if subdevice.name() == "EL3004" {
log::info!("Found EL3004. Configuring...");
subdevice.sdo_write(0x1c12, 0, 0u8).await?;
subdevice
.sdo_write_array(0x1c13, &[0x1a00u16, 0x1a02, 0x1a04, 0x1a06])
.await?;
// The `sdo_write_array` call above is equivalent to the following
// 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 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 = smol::Timer::interval(Duration::from_millis(5));
let shutdown = Arc::new(AtomicBool::new(false));
signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&shutdown))
.expect("Register hook");
loop {
// Graceful shutdown on Ctrl + C
if shutdown.load(Ordering::Relaxed) {
log::info!("Shutting down...");
break;
}
group.tx_rx(&maindevice).await.expect("TX/RX");
// Increment every output byte for every SubDevice by one
for subdevice in group.iter(&maindevice) {
let mut o = subdevice.outputs_raw_mut();
for byte in o.iter_mut() {
*byte = byte.wrapping_add(1);
}
}
tick_interval.next().await;
}
let group = group
.into_safe_op(&maindevice)
.await
.expect("OP -> SAFE-OP");
log::info!("OP -> SAFE-OP");
let group = group
.into_pre_op(&maindevice)
.await
.expect("SAFE-OP -> PRE-OP");
log::info!("SAFE-OP -> PRE-OP");
let _group = group.into_init(&maindevice).await.expect("PRE-OP -> INIT");
log::info!("PRE-OP -> INIT, shutdown complete");
Ok(())
})
}