ethercat-linux/vendor/ethercrab/src/command/reads.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

138 lines
3.8 KiB
Rust

use crate::{MainDevice, error::Error, pdu_loop::ReceivedPdu};
use ethercrab_wire::{EtherCrabWireRead, EtherCrabWireSized};
/// Read commands that send no data.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum Reads {
/// APRD.
Aprd {
/// Auto increment counter.
address: u16,
/// Memory location to read from.
register: u16,
},
/// FPRD.
Fprd {
/// Configured station address.
address: u16,
/// Memory location to read from.
register: u16,
},
/// Broadcast Read (BRD).
Brd {
/// Autoincremented by each SubDevice visited.
address: u16,
/// Memory location to read from.
register: u16,
},
/// LRD.
Lrd {
/// Logical address.
address: u32,
},
/// FRMW.
Frmw {
/// Configured station address.
address: u16,
/// Memory location to read from.
register: u16,
},
}
impl Reads {
/// Wrap this command with a MainDevice to make it sendable over the wire.
pub fn wrap(self) -> WrappedRead {
WrappedRead::new(self)
}
}
/// A wrapped version of a [`Reads`] exposing a builder API used to send/receive data over the wire.
#[derive(Debug, Copy, Clone)]
pub struct WrappedRead {
/// EtherCAT command.
pub command: Reads,
/// Expected working counter.
wkc: Option<u16>,
}
impl WrappedRead {
pub(crate) fn new(command: Reads) -> Self {
Self {
command,
wkc: Some(1),
}
}
/// Do not return an error if the working counter is different from the expected value.
///
/// The default value is `1` and can be overridden with [`with_wkc`](WrappedRead::with_wkc).
pub fn ignore_wkc(self) -> Self {
Self { wkc: None, ..self }
}
/// Change the expected working counter from its default of `1`.
pub fn with_wkc(self, wkc: u16) -> Self {
Self {
wkc: Some(wkc),
..self
}
}
/// Receive data and decode into a `T`.
pub async fn receive<'maindevice, T>(
self,
maindevice: &'maindevice MainDevice<'maindevice>,
) -> Result<T, Error>
where
T: EtherCrabWireRead + EtherCrabWireSized,
{
self.common(maindevice, T::PACKED_LEN as u16)
.await?
.maybe_wkc(self.wkc)
.and_then(|data| Ok(T::unpack_from_slice(&data)?))
}
/// Receive a given number of bytes and return it as a slice.
pub async fn receive_slice<'maindevice>(
self,
maindevice: &'maindevice MainDevice<'maindevice>,
len: u16,
) -> Result<ReceivedPdu<'maindevice>, Error> {
self.common(maindevice, len).await?.maybe_wkc(self.wkc)
}
/// Receive only the working counter.
///
/// Any expected working counter value will be ignored when calling this method, regardless of
/// any value set by [`with_wkc`](WrappedRead::with_wkc).
///
/// `T` determines the length of the read, which is required for valid reads. It is otherwise
/// ignored.
pub(crate) async fn receive_wkc<'maindevice, T>(
&self,
maindevice: &'maindevice MainDevice<'maindevice>,
) -> Result<u16, Error>
where
T: EtherCrabWireRead + EtherCrabWireSized,
{
self.common(maindevice, T::PACKED_LEN as u16)
.await
.map(|res| res.working_counter)
}
// Some manual monomorphisation
fn common<'maindevice>(
&self,
maindevice: &'maindevice MainDevice<'maindevice>,
len: u16,
) -> impl core::future::Future<Output = Result<ReceivedPdu<'maindevice>, Error>> {
maindevice.single_pdu(self.command.into(), (), Some(len))
}
}