# ethercat — On-the-Fly Industrial Fieldbus Datagrams (0x88a4) (`ethercat`)

> **FrameAssembly v3.3.0** · Protocol 18 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/ethercat

- **Full Protocol Name:** IEC 61158 EtherCAT Industrial Ethernet Fieldbus (0x88a4 / UDP 34980)
- **Keyword:** `ethercat`
- **OSI Layer & Transport:** Layer 2 · EtherType 0x88a4 (or UDP 34980)
- **Host Endpoint Rule:** MAC-Only or IP
- **Standards:** IEC 61158 Type 12, ETG.1000.4
- **Wireshark Display Filter:** `ecat`
- **Wire Encapsulation:** `Ethernet II -> EtherCAT (EtherType 0x88a4 (or UDP 34980))`

## Overview

Servo drives process `0x88a4` EtherCAT frames on the fly as the bits pass through the ASIC, incrementing the Working Counter (`wkc`) on each slave — `ethercat` packs the little-endian header and `transport udp` (`34980`) mode cleanly.

EtherCAT (`ethercat`, IEC 61158 Type 12 / ETG.1000.4) powers ultra-low-latency servo motion control, CNC machining, semiconductor wafer tools, and PLC I/O rings by processing Ethernet frames **on the fly** (`EtherType 0x88a4`). Every EtherCAT frame starts with a 2-byte little-endian EtherCAT Frame Header (`11-bit Length`, `Type = 1` DLPDU) followed by datagrams containing an 8-bit Command (`APRD`, `APWR`, `FPRD`, `FPWR`, `BRD`, `BWR`, `LRD`, `LWR`, `NOP`), 8-bit Index (`idx`), 32-bit Position/Node/Logical Address (`adp`+`ado` or `addr`), Length/Round-Trip flags, 16-bit Interrupt (`irq`), data bytes, and a 16-bit **Working Counter (`wkc`)** incremented by each slave ASIC.

FrameAssembly encodes native little-endian `0x88a4` frames over MAC-only or IP hosts—and switches to **EtherCAT over UDP (`dstport 34980`)** when `transport udp` is added.

## Minimal Quickstart (`ethercat_quickstart.frasm`)

```frameassembly
host twincat_master { mac 00:01:05:10:00:01 }
host servo_drive_01 { mac 00:01:05:10:00:02 }

compile {
    twincat_master -> servo_drive_01 ethercat read idx 1 adp 0x0000 ado 0x0130 wkc 0 wait 125us
    twincat_master <- servo_drive_01 ethercat read idx 1 adp 0x0001 ado 0x0130 wkc 1
}
```

### Automatic Wire Calculations

- Sets Ethernet II EtherType to `0x88a4` (or IPv4/IPv6 + UDP `dstport 34980` when `transport udp` is specified).
- Packs the little-endian 16-bit EtherCAT Frame Header (`Length | (1 << 12)`) and 10-byte Datagram Header + trailing 16-bit little-endian `wkc`.
- Writes little-endian 16-bit `adp` (`addr` alias, `0..=65535`) and 16-bit `ado` (`offset` alias, `0..=65535`) into the 32-bit datagram address slot.

## Practical Lab Example: 125us Isochronous Motion-Control Cycle: Broadcast AL Status + Logical Read/Write (`ethercat_servo_ring_cycle.frasm`)

Simulates an EtherCAT master polling 4 servo drives for `SAFEOP`/`OP` status (`ado 0x0130`), exchanging logical process-data (`LRW` at `addr 0x1000`), and sending an engineering mailbox check over `transport udp`.

```frameassembly
host plc_master { ip 10.120.1.10 mac 00:01:05:aa:00:01 }
host drive_ring { ip 10.120.1.20 mac ff:ff:ff:ff:ff:ff }

compile {
    // 1. Broadcast Read (BRD) of AL Status register 0x0130 outbound (wkc=0) and returning (wkc=4 drives)
    plc_master -> drive_ring ethercat cmd "BRD" idx 10 adp 0x0000 ado 0x0130 wkc 0 wait 60us
    plc_master <- drive_ring ethercat cmd "BRD" idx 10 adp 0x0004 ado 0x0130 wkc 4 wait 65us
    // 2. Logical Read/Write (LRW) process data exchange at logical base address 0x1000
    plc_master -> drive_ring ethercat cmd "LRW" idx 11 addr 0x1000 hexpayload "0f00e80300000000" wkc 0 wait 60us
    plc_master <- drive_ring ethercat cmd "LRW" idx 11 addr 0x1000 hexpayload "3702e80300000000" wkc 12 wait 200us
    // 3. Routed EtherCAT-over-UDP (port 34980) diagnostic read
    plc_master -> drive_ring ethercat cmd "FPRD" idx 12 adp 0x03e9 offset 0x0000 wkc 0 transport udp
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show ethercat_servo_ring_cycle.frasm

# 2. Compile .frasm -> classic .pcap, nanosecond .pcapng, or pipe via '-'
frameassembly ethercat_servo_ring_cycle.frasm ethercat_servo_ring_cycle.pcap
frameassembly ethercat_servo_ring_cycle.frasm ethercat_servo_ring_cycle.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse ethercat_servo_ring_cycle.pcap ethercat_servo_ring_cycle_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r ethercat_servo_ring_cycle.pcap
```

## All `ethercat` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ethercat [nop | read | write | cmd "<text>"] [idx <0..255>] [adp|addr <0..65535>] [ado|offset <0..65535>] [irq <0..65535>] [wkc <0..65535>] [transport udp]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `nop \| read \| write \| cmd "<text>"` | Command keyword or ETG mnemonic ("APRD","APWR","FPRD","FPWR","BRD","BWR","LRD","LWR","LRW") | `read (APRD 0x01)` | Sets the 8-bit EtherCAT Datagram Command byte (`0x00` NOP, `0x01` APRD, `0x02` APWR, `0x04` FPRD, `0x05` FPWR, `0x07` BRD, `0x0a` LRD, `0x0b` LWR, `0x0c` LRW). |
| `idx <0..255>` | 8-bit Datagram Index | `0` | Sets the 1-byte master correlation `Index` in byte 1 of the datagram header. |
| `adp <0..65535> \| addr <0..65535>` | 16-bit Slave Position / Configured / Logical Base Address | `0` | Sets the little-endian 16-bit `ADP` / low address word (`addr` is validated `0..=65535` at compile time). |
| `ado <0..65535> \| offset <0..65535>` | 16-bit ESC Register / High Word Offset (e.g. 0x0130) | `0` | Sets the little-endian 16-bit `ADO` physical memory/register offset (`offset` is an exact alias). |
| `irq <0..65535>` | 16-bit EtherCAT Event Request mask | `0` | Sets the little-endian 16-bit `IRQ` field ahead of the datagram payload. |
| `wkc <0..65535>` | 16-bit Working Counter | `0` | Writes the trailing 16-bit little-endian Working Counter incremented by slaves on the wire. |
| `transport udp` | Optional UDP 34980 encapsulation (IP hosts required) | `Raw Ethernet 0x88a4` | Encapsulates the EtherCAT DLPDU inside IPv4/IPv6 + UDP `dstport 34980`. |

## Cross-Layer Options Valid with `ethercat`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `comment "<text>"` | Quoted UTF-8 annotation | `Omitted` | Attaches an Enhanced Packet Block `opt_comment` annotation when compiling to `.pcapng` (Wireshark `pkt_comment` filter). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"`, `"de:ad:be:ef"`, or `"\x01\x02"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType 0..65535 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or standalone Layer 2 16-bit EtherType. |
| `fcs \| badfcs` | Ethernet CRC-32 trailer flags (choose one) | `Omitted` | `fcs` appends a valid IEEE 802.3 4-byte CRC-32 Frame Check Sequence trailer; `badfcs` XOR-corrupts the CRC-32 for NIC/tap negative tests. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Repeatable label stack (0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255) | `Unlabeled (auto bos=0 on outer labels, bos=1 on bottom)` | Prepends one or more 32-bit MPLS label shims (`0x8847`, e.g. `mpls 16000 mpls 24010`) ahead of Layer 2 EoMPLS or Layer 3 payloads. |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), EtherType 0x88a4 (or UDP 34980)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport udp` | Optional UDP encapsulation override | `Ethernet 0x88a4 (UDP 34980 when transport udp is set)` | Wraps the protocol frame inside an IPv4/IPv6 + UDP header instead of raw Ethernet or raw IP encapsulation. |

## Common Mistakes & Quick Fixes

### Declare IP Addresses on Hosts When Using ethercat transport udp

- **Avoid:** `host m { mac 00:01:05:00:00:01 }
m -> s ethercat read transport udp`
- **Valid v3.3.0 Syntax:** `host m { ip 10.120.1.10 mac 00:01:05:00:00:01 }
m -> s ethercat read transport udp`

Pure Layer 2 `ethercat` (`0x88a4`) compiles on MAC-only hosts, whereas `transport udp` builds an outer IP + UDP `34980` header and requires `ip` on both hosts.

### Keep adp/addr and ado/offset Within 16-Bit Bounds (0..65535)

- **Avoid:** `m -> s ethercat cmd "LRW" addr 0x10000 wkc 0`
- **Valid v3.3.0 Syntax:** `m -> s ethercat cmd "LRW" addr 0x1000 offset 0x0001 wkc 0`

Semantic validation enforces 16-bit bounds (`0..=65535`) on `adp`/`addr` and `ado`/`offset`; split upper 16-bit words into `ado` / `offset`.
