# EtherCAT

Run an EtherCAT segment: read inputs and drive outputs every cycle

- Category: Industrial
- Capabilities: trellis, Streams, Assets, Commands, Entities
- Runs on: Linux x64, Linux ARM64, macOS Apple silicon, macOS Intel

Run an EtherCAT segment from this computer: Phloem becomes the segment's main
device, reads every subdevice's inputs every cycle, and drives its outputs as
commands that return to a safe value on any fault.

## What you get

- **Every PDO entry as a channel**: each input and each output of each
  subdevice, named from your ESI files (or the subdevice's own EEPROM),
  sampled every cycle.
- **Cyclic process data** from 125 µs to 100 ms (1 ms by default), on the
  segment's **distributed clocks** when the subdevices support them, so each
  sample is stamped with the SYNC0 pulse it was latched on.
- **Outputs as commands**, each with a **safe value**. Every output goes to its
  safe value when anything goes wrong and when the input stops.
- **Losses stated, never filled in.** A cycle whose frame did not come back,
  or whose working counter says not every subdevice answered, carries no
  samples, and the hole is recorded with its reason. A subdevice that leaves
  OP has its inputs marked missing for as long as it is out.

## Before you start

**A dedicated network port.** Cable the segment's first subdevice straight to
a port on this computer. Use a port that carries nothing else: the connector
sends raw Ethernet frames on it, and a switch or other traffic on the same
port gets in the way.

**Permission to send raw Ethernet.**

| Computer | What it needs |
|---|---|
| Linux | The connector binary needs the `CAP_NET_RAW` capability (and `CAP_NET_ADMIN` for promiscuous mode): `sudo setcap cap_net_raw,cap_net_admin+ep <path to the ethercat connector>`, or run Phloem as root. |
| macOS | Read and write access to `/dev/bpf*`. Installing Wireshark's ChmodBPF (the `access_bpf` group) gives it; so does running as root. |
| Windows | Not supported in this release. |

**ESI files (recommended).** Your subdevices' vendor publishes an ESI file (an
XML device description). Point the connector at the file, or at a folder of
them, to get the vendor's channel names and data types. Without one, names
come from each subdevice's EEPROM, and an entry the EEPROM does not name is
called by its object index (`0x6000_11`). A vendor's whole folder is fine: the
connector lists what each file describes when it starts (a few seconds for a
folder of about a thousand megabytes) and reads in full only the devices on
your segment. A device whose description has an error is reported when that
device is on the segment, and is then described from its EEPROM.

## Set up

1. Add an **EtherCAT** input.
2. **Network port**: the interface the segment is cabled to (`eth1`, `en7`).
3. **ESI files**: a file or a folder.
4. The setup table scans the segment and lists every PDO entry of every
   subdevice, inputs and outputs, with its PDO, object index, data type and
   where its name came from. Untick what you do not want streamed.
5. Under **More settings**:
   - **Cycle (µs)**: how often process data is exchanged.
   - **Distributed clocks**: on by default. Subdevices whose description
     offers a DC mode are synchronized to SYNC0 at the cycle period.
   - **Safe values**: one line per output, `path = value`. An output with no
     line is safe at 0.
   - **Units**: one line per channel, `path = unit [scale] [offset]`.
     The connector streams the raw value times scale plus offset in that unit.

Scanning brings the subdevices to PRE-OP to read their PDO assignment and
returns them to INIT. Checking the connection only counts the subdevices
that answer and changes nothing on the segment.

## Channels

Paths are `/ethercat/<slot>/<PDO>/<entry>`:

- **slot** is the subdevice's position on the segment and its type name
  (`003_EL3102`). When the subdevice has a station alias set, the alias is
  used instead (`a1001_EL3102`), so re-cabling does not rename it.
- **PDO** and **entry** are the names from the ESI file, or the EEPROM.

Each channel's data type comes from the ESI file or the EEPROM. A one-bit
entry is shown as an on/off state. An entry no description gives a type for
is read as an unsigned integer of its width, and the setup table says so.
Integers wider than 53 bits are not exact: they are carried as 64-bit
floating point. An entry wider than 64 bits (a byte array or a string mapped
whole into a PDO) is not a number, so it is not a channel; the device's
details in the setup table list each one.

**Units.** EtherCAT descriptions do not carry units, and a raw count (an
analog terminal's 16-bit value) is not volts until its range is known. The
connector never guesses one: add a line under **Units** for each channel you
want in engineering units.

## Timing

Every cycle the connector exchanges the whole process image with the segment
and reads each subdevice's state.

- **With distributed clocks**, each sample's time is the SYNC0 pulse the
  subdevice latched its inputs on, on the segment's own clock. A cycle that
  read the same pulse twice adds nothing; a skipped pulse is a stated hole.
- **Without them**, inputs are latched as the frame passes each subdevice.
  Each sample is stamped with its cycle's scheduled time on this computer's
  clock. An exchange that finished more than one cycle after its slot is
  dropped and stated, rather than stamped with a time it was not taken at.

A frame that does not come back is waited for, then sent once more. A cycle
that still fails is stated as lost.

## Outputs and safety

Each output entry is a command (a boolean for one-bit entries, an integer or
number for the rest, within its data type's range). A write is answered once
a cycle has carried it to the subdevice and the working counter confirmed it.
A write to a subdevice that is not in OP is refused.

The output image returns to the safe values, and every earlier command is
dropped, when:

- 50 ms pass with no good exchange (a pulled cable, a powered-down segment);
- a subdevice with outputs leaves OP;
- the input stops (the safe image is sent for five cycles, then every
  subdevice is taken to INIT, where it applies its own safe state).

After a fault, outputs stay at their safe values until they are commanded
again.

Each subdevice's own sync manager watchdog is the last guard. If cycles stop
reaching it (the computer stops, the cable is cut), it drops its outputs to
their hardware safe state by itself.

## Recovery

- **A subdevice drops to SAFE-OP** (its watchdog fired, it reported an
  error): the connector records its AL status code, acknowledges it, and
  asks it back to OP once a second.
- **The segment stops answering for a second**, or a subdevice stops
  answering at its address (it rebooted): the connector scans the segment
  again. It resumes only when the same subdevices with the same PDOs come
  back. Until then it records what it found and keeps waiting.
- **Bring-up fails**: the input stops with the step that failed, each
  subdevice's state, and the AL status code and its meaning
  (`subdevice 2 (0x1001) in SAFE-OP with error 0x0030 (invalid DC SYNC
  configuration)`).

## What has not run on real hardware

Every behaviour in this guide was tested against virtual subdevices that
implement the subdevice controller's registers, EEPROM interface, mailbox
(CoE) and distributed clocks. That includes 64 subdevices with 2,048 entries
at 1 ms, an hour of cycling with cable pulls, and every fault above. None of
it has run against physical EtherCAT hardware yet.

| | Status |
|---|---|
| Raw Ethernet on Linux | Built; not yet run against a physical segment |
| Raw Ethernet on macOS | Built; not yet run against a physical segment |
| Windows | Not supported in this release |
| Real subdevices (terminals, drives) | Not yet run |
| Cycle timing on this computer | Measured only on a virtual segment; a real segment adds its wire time, about 1 µs per subdevice plus 80 ns per byte |
| Changing a subdevice's PDO assignment | Not offered: the connector streams the assignment the subdevice has |

## Troubleshooting

| You see | What it means |
|---|---|
| `no network interface named …` | The port name is wrong; the message lists this computer's interfaces. |
| `no permission to open … for raw Ethernet` | See **Before you start**. |
| `no subdevice answered on the segment` | The cable to the first subdevice, its power, or the port. |
| `the mapped image is … bytes but its PDO assignment describes …` | The subdevice reports a PDO assignment that does not match its own process-data size. The connector refuses to stream it rather than read the wrong bits. |
| Many `too late to place on the cycle grid` holes | This computer cannot keep the cycle. Use a longer cycle, distributed clocks, or a quieter computer. |
| An output write is refused with `not confirmed` | A subdevice with outputs is not in OP, or cycles are being lost; the input's activity line says which. |

## Supports

- Any EtherCAT subdevice: identity and PDOs read from its own EEPROM, and its PDO assignment over CoE
- Channel names and data types from your ESI files (a file or a folder of them)
- Cyclic process data from 125 µs to 100 ms, with distributed clocks (SYNC0) where the subdevices support them
- Outputs as commands, each with a safe value it returns to on any fault and when the input stops
- Raw Ethernet with CAP_NET_RAW (linux)
- Raw Ethernet through BPF (macos)

## Connection fields

| Field | Type | Required | Notes |
|---|---|---|---|
| Network port | Text | Yes |  |
| ESI files | Path | No |  |
| Cycle (µs) | Number | No |  |
| Distributed clocks | On / off | No |  |
| PDO entries | Text | No |  |
| Safe values | Text | No |  |
| Units | Text | No |  |
