Skip to main content

CiA 402 -- Servo drives

CiA 402 (IEC 61800-7-204) is the CoE-based device protocol for servo drives.

note

CiA 402 runs on top of CoE (SDO) and is available only when the slave supports CoE.

CSP / synchronized axis hard rules
  • Cyclic synchronous position mode is fixed at 0x6060 = 8; under CSP do not write 0x001F (bit4 is the PP/HM NewSetpoint).
  • Use (status_word & 0x6F) for state checks: == 0x27 = OperationEnabled. To detect a fault use (status_word & 0x4F) == 0x08 (0x4F only looks at faults and cannot be used to judge the operating state).
  • The enable chain is fixed at 0x0006 → 0x0007 → 0x000F and advances one cycle at a time; writing 0x000F directly from SwitchOnDisabled does not skip a step. CiA402_Enable() walks this chain; do not skip steps when writing the control word by hand.
  • While not enabled, during a handshake and during a group stop, the target position always equals the actual position, to avoid jumps.
  • Fault reset is the rising edge of bit7, and bit2 (Quick Stop, 1 = quick stop not triggered) must be 1; complete it in two cycles as 0x0006 → 0x0086. Do not Sleep inside the PDO callback, and do not write the same 0x0080 repeatedly (there is no edge, so the drive ignores it).
  • Issue all axes from the same PDO callback (RegisterProcessDataCyclicCallbackSync), one axis after another; do not start a thread per axis. Axis order must match the configured slave index / PDO offset; do not guess an axis from array indices inside the callback.
  • Before issuing commands each cycle, check that axis's 0x6041 bit13 (Following Error) and the deviation between target and actual position: if any axis reports a following error, do not silently skip it and keep sending the other axes on the synchronized tick; follow the stop procedure and record the axis number.
  • The 0x1F of single-axis PP must not be copied to CSP / synchronized axes; synchronized axes use CSP + DC only.

State enum​

typedef enum {
CIA402_NOT_READY = 0, /* not ready */
CIA402_SWITCH_ON_DISABLED = 1, /* switch on disabled */
CIA402_READY_TO_SWITCH_ON = 2, /* ready to switch on */
CIA402_SWITCHED_ON = 3, /* switched on */
CIA402_OPERATION_ENABLED = 4, /* operation enabled */
CIA402_QUICK_STOP_ACTIVE = 5, /* quick stop active */
CIA402_FAULT_REACTION = 6, /* fault reaction */
CIA402_FAULT = 7, /* fault */
CIA402_UNKNOWN = 99 /* unknown */
} cia402_state_t;

Operation mode enum​

typedef enum {
CIA402_PP = 1, /* profile position mode */
CIA402_VL = 2, /* velocity mode */
CIA402_PV = 3, /* profile velocity mode */
CIA402_PT = 4, /* profile torque mode */
CIA402_HM = 6, /* homing mode */
CIA402_IP = 7, /* interpolated position mode */
CIA402_CSP = 8, /* cyclic synchronous position mode */
CIA402_CSV = 9, /* cyclic synchronous velocity mode */
CIA402_CST = 10, /* cyclic synchronous torque mode */
CIA402_CSTCA = 11 /* cyclic synchronous torque + commutation angle mode */
} cia402_mode_t;

PDO offset scan​

For the CiA 402 standard objects (ControlWord / StatusWord / PositionActual and so on), the byte offsets in the IOmap are scanned by cia402_scan_pdo() and filled into cia402_pdo_map_t. Call it once after entering OP; every later cia402_read_* / cia402_write_* call then accesses PDOs through those offsets, and falls back to SDO when the object is not mapped to a PDO. For a zero-copy high-frequency path, still call GetIO once after entering OP to get the process image pointer.

State control​

CiA402_Enable()​

int CiA402_Enable(uint16_t master, uint16_t slave, int max_retries);

Enables in one call by advancing through the state machine step by step (Shutdown → Switch On → Enable Operation), for at most max_retries attempts (0 or a negative value is treated as 20 attempts). It requires 0x6040 / 0x6041 to be mapped to process data; on a fault, only 0x603F == 0x7500 (communication class) is reset automatically and enable continues, while any other fault code returns failure directly and needs manual handling.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index
  • max_retries (int) — maximum number of retries

Return value:

  • int — a value other than 0 (TRUE) on success, 0 (FALSE) on failure (same semantics as CiA402_FaultReset / CiA402_SetMode / CiA402_WriteControlWord)

Example:

if (CiA402_Enable(master, slave, 50) == 0)
printf("使能失败: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Enable failed: 0x603F=0x%04X */

Disable and stop​

The C SDK has no separate Disable / QuickStop entry point; disable and stop are performed uniformly by writing the corresponding control word value through CiA402_WriteControlWord(). That entry point takes effect only when the slave is at AL=OP and 0x6040 is mapped to RxPDO; otherwise it returns failure directly (it does not fall back to SDO for the control word):

CiA402_WriteControlWord(master, slave, 0x0000);   /* full disable (Disable Voltage) */
CiA402_WriteControlWord(master, slave, 0x0007); /* back to Switched On: holds power, no torque output */
CiA402_WriteControlWord(master, slave, 0x0002); /* Quick Stop: decelerate and stop per the 0x605A option code */
Leaving QuickStopActive

After writing 0x0002 to enter QuickStopActive, you cannot write 0x0006 directly (Shutdown is valid only from SwitchOnDisabled): first write 0x0000 (Disable Voltage) to return to SwitchOnDisabled, then walk the enable chain 0x0006 → 0x0007 → 0x000F. While stuck in QuickStopActive, writing any value of the enable chain will not bring the drive back into operation.

CiA402_FaultReset()​

int CiA402_FaultReset(uint16_t master, uint16_t slave);

Fault reset; generates the ControlWord Bit 7 rising edge automatically. Requires the slave to be in Fault, AL=OP, and 0x6040 / 0x6041 mapped to process data; if any condition is not met it returns failure directly. Only a return value other than 0 means the fault has been cleared; when it returns 0, read CiA402_ReadErrorCode() to confirm the residual fault instead of retrying in a loop.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index

Return value:

  • int — a value other than 0 (TRUE) on success, 0 (FALSE) on failure

CiA402_WriteControlWord()​

int CiA402_WriteControlWord(uint16_t master, uint16_t slave, uint16_t cw);

Writes the control word (0x6040). For values see "Control word bit definitions and command constants" below.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index
  • cw (uint16_t) — control word value

Return value:

  • int — a value other than 0 (TRUE) on success, 0 (FALSE) on failure

State readout​

Status word keys: Bit10 (TargetReached), Bit12 (Set-point Ack / HomingAttained) and Bit13 (Following Error / HomingError) are the key PP / HM handshake bits. Control word keys: Bit4 (NewSetpoint / StartHoming), Bit6 (PP relative/absolute) and Bit8 (Halt) are the bits applications commonly use. Full bit definitions are in "Status word bitmask constants" and "Control word bit definitions and command constants" below.

CiA402_ReadStatusWord()​

uint16_t CiA402_ReadStatusWord(uint16_t master, uint16_t slave);

Reads the status word (0x6041); each call performs one SDO read, so do not put it in a high-frequency loop.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index

Return value:

  • uint16_t — StatusWord value (the return value may not be trustworthy, see below)
Read failures look like real values

A failed slave read returns 0, and 0 also makes CiA402_ParseState() return CIA402_NOT_READY, so the return value alone cannot separate "read failed" from "the drive really is not ready". When the runtime is not ready or the entry point is missing it returns 0xFFFF, which parses as CIA402_UNKNOWN. Neither value is a trustworthy status word.

For the cyclic path use the PDO snapshot cia402_read_statusword(master, slave, &map, &sw) (returns 0 on success / -1 on failure); for a single tri-state query use cia402_read_object_observable(master, slave, 0x6041, 0, &result) (both in ethercat_advanced.h).

CiA402_ParseState()​

cia402_state_t CiA402_ParseState(uint16_t statusword);

Parses the current drive state from the StatusWord and returns cia402_state_t.

Parameters:

  • statusword (uint16_t) — StatusWord value

Return value:

  • cia402_state_t — drive state (see "State enum"; CIA402_UNKNOWN when it cannot be parsed)

CiA402_GetEnableCommand()​

uint16_t CiA402_GetEnableCommand(uint16_t statusword);

Computes the ControlWord value needed for the next enable step from the current StatusWord. Use it to work out which control word to write when advancing the state machine manually.

Parameters:

  • statusword (uint16_t) — current StatusWord value

Return value:

  • uint16_t — ControlWord value to write for the next step

Example:

uint16_t sw = CiA402_ReadStatusWord(master, slave);
cia402_state_t state = CiA402_ParseState(sw);

if (state == CIA402_FAULT)
CiA402_FaultReset(master, slave);
else if (state != CIA402_OPERATION_ENABLED)
CiA402_WriteControlWord(master, slave, CiA402_GetEnableCommand(sw));

Operation modes​

CiA402_SetMode()​

int CiA402_SetMode(uint16_t master, uint16_t slave, int8_t mode);

Sets the operation mode (0x6060). Requires AL to be PreOp / SafeOp / OP; CSP / CSV / CST / CSTCA additionally require DC to be enabled on that slave, otherwise it returns failure directly. After writing the target mode it reads the mode display object (0x6061) to confirm the device followed it; it returns failure when the device does not follow.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index
  • mode (int8_t) — operation mode (cia402_mode_t)

Return value:

  • int — a value other than 0 (TRUE) on success (0x6061 confirmed the mode), 0 (FALSE) on failure (including the mode not being followed)

CiA402_GetMode()​

int8_t CiA402_GetMode(uint16_t master, uint16_t slave);

Reads the current operation mode (0x6061) and returns cia402_mode_t. A failed slave read is collapsed to 0 (the same value as CIA402_PP), and a runtime that is not ready or a missing entry point returns -1; neither value is a trustworthy mode. For a tri-state result use cia402_read_operation_mode_display(master, slave, &result) (see ethercat_advanced.h).

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index

Return value:

  • int8_t — current operation mode (cia402_mode_t); 0 / -1 are failure collapse values, not trustworthy modes

PDO mapping scan (ethercat_advanced.h)​

cia402_pdo_map_t​

typedef struct {
int controlword; /* 0x6040 RxPDO byte offset (-1 = not mapped) */
int target_position; /* 0x607A */
int target_velocity; /* 0x60FF */
int target_torque; /* 0x6071 */
int modes_of_operation; /* 0x6060 */
int statusword; /* 0x6041 TxPDO byte offset (-1 = not mapped) */
int position_actual; /* 0x6064 */
int velocity_actual; /* 0x606C */
int torque_actual; /* 0x6077 */
int modes_display; /* 0x6061 */
} cia402_pdo_map_t;

cia402_scan_pdo()​

int cia402_scan_pdo(uint16_t master, uint16_t slave, cia402_pdo_map_t* map);

Scans the CiA 402 PDO mapping and locates the byte offset of each object in the IOmap automatically. A struct field of -1 means that object is not mapped.

Parameters:

  • master (uint16_t) — master index
  • slave (uint16_t) — slave index
  • map (cia402_pdo_map_t*) — output argument, filled with each object's byte offset

Return value:

  • int — 0 = at least one known object is mapped; -1 = nothing is mapped or the arguments are invalid

PDO mapping read and write​

int cia402_read_statusword      (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, uint16_t* out);
int cia402_write_controlword (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, uint16_t val);
int cia402_read_position (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int32_t* out);
int cia402_write_target_position(uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int32_t val);
int cia402_read_velocity (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int32_t* out);
int cia402_write_target_velocity(uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int32_t val);
int cia402_read_torque (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int16_t* out);
int cia402_write_target_torque (uint16_t master, uint16_t slave, const cia402_pdo_map_t* map, int16_t val);

Read calls take a snapshot of the input frame; write calls merge into the output frame and commit it. Objects that are not mapped to a PDO fall back to SDO automatically.

Parameters:

  • master / slave (uint16_t) — master index / slave index
  • map (const cia402_pdo_map_t*) — offset map obtained from cia402_scan_pdo()
  • out — output argument, filled with the value read (uint16_t status word / int32_t position and velocity / int16_t torque)
  • val — target value to write (same types as above)

Return value:

  • int — 0 on success, -1 on failure

Example:

#include "ethercat_advanced.h"

cia402_pdo_map_t map;
if (cia402_scan_pdo(master, 1, &map) == 0) {
uint16_t sw;
if (cia402_read_statusword(master, 1, &map, &sw) == 0)
printf("StatusWord: 0x%04X, 偏移=%d\n", sw, map.statusword); /* StatusWord: 0x%04X, offset=%d */
else
printf("StatusWord 读取失败\n"); /* StatusWord read failed */

if (cia402_write_target_position(master, 1, &map, 100000) != 0)
printf("目标位置写入失败\n"); /* Target position write failed */
}

Motion parameters (SDO access)​

The following parameters are read and written over SDO, which suits parameter configuration; use PDO mappings for high-frequency control.

CategoryObjectTypeRead/writeDescription
Position0x6064int32read (sdo_read_typed_ex)Actual position
Position0x607Aint32write (sdo_write_typed_ex)Target position
Position0x6091:01 / :02uint32 / uint32write (sdo_write_typed_ex)Gear ratio: :01 motor resolution / :02 shaft resolution (basis for position unit conversion)
Position0x60B0int32write (sdo_write_typed_ex)Position offset (CSP additive)
Velocity0x606Cint32read (sdo_read_typed_ex)Actual velocity
Velocity0x60FFint32write (sdo_write_typed_ex)Target velocity
Velocity0x60B1int32write (sdo_write_typed_ex)Velocity offset (CSV additive)
Torque0x6077int16read (sdo_read_typed_ex)Actual torque (thousandths of rated)
Torque0x6071int16write (sdo_write_typed_ex)Target torque
Torque0x6072uint16write (sdo_write_typed_ex)Maximum torque (thousandths of rated)
Torque0x6076uint32write (sdo_write_typed_ex)Motor rated torque (mNm)
Torque0x60E0 / 0x60E1uint16write (sdo_write_typed_ex)Positive / negative torque limit (thousandths of rated)
Profile0x6081 / 0x6083 / 0x6084uint32write (sdo_write_typed_ex)Profile velocity / acceleration / deceleration
Profile0x6085uint32write (sdo_write_typed_ex)Quick stop deceleration
Profile0x6086int16write (sdo_write_typed_ex)Motion profile type (0 = trapezoidal, 1 = S-curve)
Polarity0x607Euint8write (sdo_write_typed_ex)Polarity (Bit6 = velocity inversion, Bit7 = position inversion)
Interpolation0x60C2:01 / :02uint8 / int8write (sdo_write_typed_ex)Interpolation time period value / exponent (period = value × 10^exponent seconds)
SDO read and write interfaces

Prefer the typed sdo_read_* / sdo_write_* calls; use sdo_read_typed_ex / sdo_write_typed_ex when you need the Abort Code / WKC. The C SDK SDO interfaces have no separate timeout parameter (timeouts follow the ETG fallback).

Units and interpolation period
  • Position: the 0x607A target position and the 0x6064 actual position must use the same position unit (the drive user unit, derived from the 0x6091 gear ratio and the 0x6092 feed constant, not a fixed count or mm). If the two conversions disagree, positions are scaled up or down by a fixed ratio.
  • Velocity: 0x60FF / 0x606C use user units per second, from the same source as position; do not put rpm in directly.
  • Torque: 0x6071 / 0x6077 / 0x6072 / 0x60E0 / 0x60E1 are all thousandths of the rated torque.
  • Interpolation period: 0x60C2:01 is the period value and :02 the exponent (period = value × 10^exponent seconds). Under CSP / CSV / CST it should match the master bus PDO cycle (1 ms bus → :01 = 1, :02 = -3); otherwise the drive resamples on its own interpolation tick and a fixed tick offset appears between target values and actual feedback.

Option codes and error codes (0x605A–0x605E / 0x603F)​

The five drive option codes determine the deceleration/stop behavior on each state transition; the error code reflects the drive's current fault. The following objects are read and written directly through the typed SDO interfaces:

CategoryObjectTypeRead/writeDescription
Option code0x605Aint16read/write (sdo_read_i16 / sdo_write_i16)Quick Stop Option Code
Option code0x605Bint16read/write (sdo_read_i16 / sdo_write_i16)Shutdown Option Code
Option code0x605Cint16read/write (sdo_read_i16 / sdo_write_i16)Disable Operation Option Code
Option code0x605Dint16read/write (sdo_read_i16 / sdo_write_i16)Halt Option Code
Option code0x605Eint16read/write (sdo_read_i16 / sdo_write_i16)Fault Reaction Option Code
Error code0x603Fuint16read (CiA402_ReadErrorCode / CiA402_GetErrorCode)Error Code; 0xFFFF = read failure or missing entry point, distinguish with out_cause (NULL allowed)

Read failure sentinels (none is a legal value: legal option codes are small values including 0, and error code 0x0000 means no error):

ConstantValueDescription
DARRA_READ_FAIL_I16-32768 (INT16_MIN)Signed 16-bit option code read failure; test with DARRA_IS_READ_FAIL_I16(v)
DARRA_READ_FAIL_U160xFFFFError code read failure; test with DARRA_IS_READ_FAIL_U16(v)
DARRA_READ_FAIL_INT-1Read failure of a counting interface that returns int; test with DARRA_IS_READ_FAIL_INT(v)

Always treat sentinel values as read failures, never as real values. The out_cause of CiA402_ReadErrorCode() splits 0xFFFF into three states: DARRAECAT_SENTINEL_VALUE (real value) / DARRAECAT_SENTINEL_READ_FAIL (slave read failed) / DARRAECAT_SENTINEL_ENTRY_MISSING (runtime entry not ready); when you only need the value, pass NULL or use the fail-closed CiA402_GetErrorCode() instead. int8 method numbers (0x60E3 list entries, -128..-1 are vendor-defined methods) are real values and are not sentinels.

Example:

int16_t qs = 0;
if (sdo_read_i16(m, 1, 0x605A, 0, &qs) != 0) {
/* read failure, not a real option code */
}

uint16_t ec = CiA402_ReadErrorCode(m, 1, NULL); /* the third argument out_cause can be NULL */
if (DARRA_IS_READ_FAIL_U16(ec)) {
/* read failure sentinel, not a real error code */
}

Aggregate reads that return int​

The two aggregate read entry points use int to express both how many entries were read and whether the read failed:

int DarraEcat_CiA402_GetErrorHistory(uint16_t master, uint16_t slave,
uint32_t* out_codes, int max_count); /* 0x1003 */
int DarraEcat_CiA402_GetSupportedHomingMethods(uint16_t master, uint16_t slave,
int8_t* out_methods, int max_count); /* 0x60E3 */
Return valueMeaning
> 0Read succeeded; the actual number of entries (the first n entries of the array are valid)
0Read succeeded and the set really is empty (no error history / no homing methods supported)
-1Read failed (timeout / communication error / abort / runtime entry unavailable); test with DARRA_IS_READ_FAIL_INT(v)
  • 0 and -1 must be handled separately: 0 is a real answer (the device explicitly answered "none"), -1 is "could not read"; an empty set does not mean "confirmed none".
  • max_count must be > 0; a return value greater than max_count means the return is untrustworthy, do not index by it.
  • Never cast to unsigned before testing, otherwise -1 becomes 0xFFFFFFFF and is no longer comparable with any count.

Example:

uint32_t codes[32];
int n = DarraEcat_CiA402_GetErrorHistory(m, 1, codes, 32);
if (DARRA_IS_READ_FAIL_INT(n))
printf("读失败 —— 这不是「没有错误历史」\n"); /* Read failed — this is not the same as "no error history" */
else if (n == 0)
printf("读成功:驱动器错误历史确实是空的\n"); /* Read succeeded: the drive error history really is empty */
else
printf("最近 %d 条错误,[0]=0x%04X\n", n, (unsigned)(codes[0] & 0xFFFF)); /* Most recent %d errors, [0]=0x%04X */

Homing parameters​

CategoryObjectTypeRead/writeDescription
Homing0x6098int8writeHoming method
Homing0x607Cint32writeHoming offset
Homing0x6099:01uint32writeHoming search velocity
Homing0x6099:02uint32writeHoming crawl velocity
Homing0x609Auint32writeHoming acceleration

Touch probe​

/* Configure the Touch Probe function word through the SDO write to 0x60B8 */
uint16_t fn = 0x0001; /* Bit0 = enable probe 1, Bit1 = edge selection */
sdo_write_u16(master, slave, 0x60B8, 0, fn);
CategoryObjectTypeRead/writeDescription
Touch probe0x60B8uint16writeTouch Probe function control (Bit0 = enable, Bit1 = edge selection)
Touch probe0x60B9uint16readTouch Probe status
Touch probe0x60BAint32readPositive edge captured position
Touch probe0x60BBint32readNegative edge captured position

Digital IO​

CategoryObjectTypeRead/writeDescription
Digital IO0x60FDuint32readDigital inputs (32-bit bitmap)
Digital IO0x60FE:01uint32read/writeDigital outputs (32-bit bitmap)

Software position limits​

CategoryObjectTypeRead/writeDescription
Soft limit0x607D:01int32writeMinimum position limit
Soft limit0x607D:02int32writeMaximum position limit
Per-cycle checks on the command side (position / direction / limits)
  • 0x607D:01/:02 are drive-side soft limits and constrain only the drive itself; the master side must still check every cycle, before writing 0x607A, that the target position is inside the process window, and must not rely on the drive rejecting it (rejection only happens when the drive implementation is strict, and status readback is delayed).
  • Position is a signed quantity: zero is established by homing (0x6098 / 0x607C), and the direction is decided jointly by 0x607E Bit6 (velocity inversion) / Bit7 (position inversion) and the 0x6091 conversion. After changing polarity or gear ratio, re-confirm that 0x6064 and 0x607A have the same sign, otherwise the position loop chases in the wrong direction.

Fast motion control​

NewSetpoint (control word bit)​

Sending a new positioning command in PP mode: write the target position to 0x607A and trigger Controlword Bit4 (NewSetpoint). With relative=TRUE, set Bit6 (Relative) as well.

ClearNewSetpoint (control word bit)​

Clears the NewSetpoint flag in PP mode (Controlword Bit4=0). Call it after TargetReached (Statusword Bit10=1) to complete the Set-point Ack handshake.

StartHoming (control word bit)​

Starting homing in HM mode: after configuring 0x6098 (HomingMethod) / 0x607C (HomeOffset) over SDO, set Controlword Bit4=1 to start. HomingAttained (Statusword Bit12) means done and HomingError (Bit13) means an error.

PP / HM handshakes are performed by the application through Controlword operations over PDO/SDO. Typical flow:

/* PP mode: send a new setpoint */
uint16_t cw = 0x000F; /* Operation Enable */
cw |= 0x0010; /* Bit4: New Setpoint */
if (relative) cw |= 0x0040; /* Bit6: Relative */
CiA402_WriteControlWord(master, slave, cw);
/* clear Bit4 after TargetReached: */
CiA402_WriteControlWord(master, slave, cw & ~0x0010);

/* HM start: after configuring 0x6098/0x607C over SDO, set Controlword Bit4=1 to start */

Homing-related parameters:

  • 0x6098 (int8) — homing method (HomingMethod)
  • 0x607C (int32) — homing offset (HomeOffset)

Homing status bits (Statusword):

  • Bit 12 — HomingAttained
  • Bit 13 — HomingError

Drive information​

SupportedDriveModes​

Reads the supported drive mode bitmask over SDO 0x6502 (uint32): Bit0=PP, Bit1=VL, Bit2=PV, Bit3=PT, Bit5=HM, Bit6=IP, Bit7=CSP, Bit8=CSV, Bit9=CST, Bit10=CSTCA. The public interface is sdo_read_u32(master, slave, 0x6502, 0, &mask).

IsModeSupported​

Bit number = mode enum value - 1: ((mask >> (mode - 1)) & 1) != 0. The mode enum value is not the bit number (CIA402_CSP = 8 corresponds to Bit7), so shifting by mask >> mode directly is off by one bit.

SupportedHomingMethods​

Reads the list of supported homing method numbers over SDO 0x60E3: subindex 0 is the UINT8 list length (use sdo_read_u8()), 1..N are INT8 method numbers (read them item by item with sdo_read_i8()):

uint8_t count = 0;
if (sdo_read_u8(master, slave, 0x60E3, 0, &count) != 0) {
/* failure or 0x60E3 not supported */
} else if (count == 0) {
/* empty set: the read succeeded and the slave really returned 0 entries */
} else {
int8_t methods[32];
int n = count > 32 ? 32 : count;
for (int i = 0; i < n; i++) {
if (sdo_read_i8(master, slave, 0x60E3, (uint8_t)(i + 1), &methods[i]) != 0)
printf("回零方法 %d 读取失败\n", i + 1); /* Homing method %d read failed */
}
}

What the three cases return: failure (timeout / communication error / another abort) and unsupported (no 0x60E3) both return failure, which is not the same as supporting 0 homing methods; never treat a failure as an empty set. Entries are int8: -128..-1 are vendor-defined methods, 0 is unselected, and 1..35 and 37 are ETG.6010 standard methods.

The whole list can also be read in one call: DarraEcat_CiA402_GetSupportedHomingMethods() (see "Aggregate reads that return int" above) returns the count directly — -1 = read failed, 0 = read succeeded and the list really is empty; the two must not be conflated. The per-item SDO loop above suits cases where you need to know which item failed.

TxPDO data validity​

Read 0x603E over SDO to determine whether the TxPDO data is valid. A non-zero value means the drive's TxPDO data is not trustworthy; for example, position values are meaningless while the encoder is not ready. Likewise, do not use position values when the read fails or the slave does not support the object (the example pre-sets valid to 1 and only proceeds when the read succeeds and returns 0):

uint8_t valid = 1;
if (sdo_read_u8(master, slave, 0x603E, 0, &valid) == 0 && valid == 0) {
/* data is valid and safe to use */
int32_t pos;
if (cia402_read_position(master, slave, &map, &pos) == 0)
printf("实际位置: %d\n", pos); /* Actual position: %d */
}

Synchronization​

CategoryObjectTypeRead/writeDescription
Synchronization0x60D9:01uint16read/writeSynchronization settings (SynchronizationSettings), synchronization enable bitmask
Synchronization0x60DAuint16readDrive sync status (DriveSyncStatus), indicates whether the drive has synchronized to the master clock
/* read the drive sync status */
uint16_t sync_status = 0;
if (sdo_read_u16(master, slave, 0x60DA, 0, &sync_status) == 0) {
printf("同步状态: 0x%04X\n", sync_status); /* Synchronization status: 0x%04X */
}

Control word bit definitions and command constants​

Control word bit definitions for writing CiA402_WriteControlWord() by hand:

ConstantValueDescription
Bit 0 Switch On0x0001Power-on command bit
Bit 1 Enable Voltage0x0002Enable voltage
Bit 2 Quick Stop0x00041 = quick stop not triggered; writing 0 triggers quick stop
Bit 3 Enable Operation0x0008Enable operation
Bit 4 New Setpoint / Start Homing0x0010PP send new setpoint / HM start homing (must not be written under CSP)
Bit 6 Relative0x0040PP relative move (otherwise absolute)
Bit 7 Fault Reset0x0080Rising edge resets the fault
Bit 8 Halt0x0100Decelerate and stop the axis per the Halt option code (0x605D)

Common command values (enable chain 0x0006 → 0x0007 → 0x000F, fault reset in two phases as 0x0006 → 0x0086):

ConstantValueDescription
Disable Voltage0x0000Disable voltage (back to SwitchOnDisabled)
Quick Stop0x0002Quick stop (decelerate per the 0x605A option code)
Shutdown0x0006Shutdown (enters Ready to Switch On)
Switch On0x0007Switch on (enters Switched On, holds power without torque output)
Enable Operation0x000FEnable operation (enters Operation Enabled)
Fault Reset0x0080Fault reset (bit7 rising edge; writing 0x0080 alone has no edge and is ignored, see "CSP / synchronized axis hard rules")

Status word bitmask constants​

Bitmasks for interpreting the value returned by CiA402_ReadStatusWord():

ConstantValueDescription
Bit 0 Ready to Switch On0x0001Ready to power on
Bit 1 Switched On0x0002Powered on
Bit 2 Operation Enabled0x0004Operation enabled
Bit 3 Fault0x0008Fault
Bit 4 Voltage Enabled0x0010Voltage enabled
Bit 5 Quick Stop0x00201 = quick stop not triggered
Bit 7 Warning0x0080Warning
Bit 9 Remote0x0200Remote
Bit 10 Target Reached0x0400Target reached (PP handshake)
Bit 12 Homing Attained / Set-point Ack0x1000Homing attained / set-point acknowledged
Bit 13 Homing Error / Following Error0x2000Homing error / following error

Full example​

#include "ethercat.h"
#include "ethercat_advanced.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
if (argc < 2) return 1; /* argv[1] = NIC name */

uint16_t master = Initialize();
if (master == 0 || master == (uint16_t)-1) return 1; /* failure returns (uint16_t)-1 */
uint16_t slaves_found = SetNetwork(master, argv[1], "");
if (slaves_found == 0 || slaves_found == (uint16_t)-1) return 1; /* >0 = number of slaves found */
if (!SetStateSequence(master, EC_STATE_OPERATIONAL, 15000)) return 1;
if (!Start(master)) return 1;

uint16_t slave = 1;

/* fault reset + set CSP mode: both return values must be checked */
if (CiA402_ParseState(CiA402_ReadStatusWord(master, slave)) == CIA402_FAULT
&& CiA402_FaultReset(master, slave) == 0)
printf("故障未清除: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Fault not cleared: 0x603F=0x%04X */
if (CiA402_SetMode(master, slave, CIA402_CSP) != 0)
printf("0x6060 未确认切到 CSP\n"); /* 0x6060 did not confirm the switch to CSP */

/* one-step enable, then issue one target position on success */
if (CiA402_Enable(master, slave, 50) != 0) {
cia402_pdo_map_t map;
if (cia402_scan_pdo(master, slave, &map) != 0) {
printf("PDO 映射扫描失败\n"); /* PDO mapping scan failed */
} else {
int32_t actual;
if (cia402_read_position(master, slave, &map, &actual) == 0
&& cia402_write_target_position(master, slave, &map, actual + 1000) == 0)
printf("目标位置: %d\n", actual + 1000); /* Target position: %d */
else
printf("位置读写失败\n"); /* Position read/write failed */
}
} else {
printf("使能失败: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Enable failed: 0x603F=0x%04X */
}

getchar();
Stop(master);
Dispose(master);
return 0;
}

PP mode -- profile position control​

uint16_t slave = 1;

if (CiA402_SetMode(master, slave, CIA402_PP) != 0)
printf("0x6060 未确认切到 PP, 先不要下发目标位置\n"); /* 0x6060 did not confirm the switch to PP, do not send the target position yet */

/* profile parameters go over SDO: check every return value; do not enter the enable chain if any fails */
if (sdo_write_u32(master, slave, 0x6081, 0, 10000) != 0 || /* profile velocity */
sdo_write_u32(master, slave, 0x6083, 0, 50000) != 0 || /* acceleration */
sdo_write_u32(master, slave, 0x6084, 0, 50000) != 0) /* deceleration */
printf("轮廓参数写入失败\n"); /* Profile parameters write failed */

cia402_pdo_map_t map;
if (cia402_scan_pdo(master, slave, &map) != 0)
printf("PDO 映射扫描失败\n"); /* PDO mapping scan failed */
else if (CiA402_Enable(master, slave, 50) != 0) {
if (sdo_write_i32(master, slave, 0x607A, 0, 100000) != 0 || /* target position */
!CiA402_WriteControlWord(master, slave, 0x001F)) /* Enable + NewSetpoint */
printf("目标位置下发失败\n"); /* Failed to send the target position */

uint16_t sw = 0;
int retries = 500; /* bounded wait; never wait forever */
int sw_read_failed = 0;
do {
if (cia402_read_statusword(master, slave, &map, &sw) != 0) {
sw_read_failed = 1;
break;
}
} while ((sw & 0x0400) == 0 && --retries > 0); /* read once per bus cycle, wait for TargetReached (Bit10) */

if (sw_read_failed)
printf("StatusWord 读取失败\n"); /* StatusWord read failed */
else if ((sw & 0x0400) == 0)
printf("等 TargetReached 超时: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Timed out waiting for TargetReached: 0x603F=0x%04X */
else
CiA402_WriteControlWord(master, slave, 0x000F); /* clear NewSetpoint */
} else {
printf("使能失败: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Enable failed: 0x603F=0x%04X */
}

HM mode -- homing​

uint16_t slave = 1;

if (CiA402_SetMode(master, slave, CIA402_HM) != 0)
printf("0x6060 未确认切到 HM\n"); /* 0x6060 did not confirm the switch to HM */

/* homing parameters go over SDO: method 35 = home on current position; check every return value */
if (sdo_write_i8(master, slave, 0x6098, 0, 35) != 0 || /* homing method */
sdo_write_i32(master, slave, 0x607C, 0, 0) != 0 || /* homing offset */
sdo_write_u32(master, slave, 0x6099, 1, 5000) != 0 || /* search velocity */
sdo_write_u32(master, slave, 0x6099, 2, 500) != 0 || /* crawl velocity */
sdo_write_u32(master, slave, 0x609A, 0, 10000) != 0) /* homing acceleration */
printf("回零参数写入失败\n"); /* Homing parameters write failed */

if (CiA402_Enable(master, slave, 50) != 0) {
/* start homing: Enable Operation + Bit4 = Start Homing */
if (!CiA402_WriteControlWord(master, slave, 0x001F))
printf("回零启动失败\n"); /* Failed to start homing */

uint16_t sw = 0;
int retries = 5000; /* bounded wait; never wait forever */
do {
sw = CiA402_ReadStatusWord(master, slave);
if (sw & 0x2000) break; /* Bit13: HomingError */
} while ((sw & 0x1000) == 0 && --retries > 0); /* Bit12: HomingAttained */

if (sw & 0x2000)
printf("回零错误: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Homing error: 0x603F=0x%04X */
else if (sw & 0x1000)
printf("回零完成\n"); /* Homing complete */
else
printf("等 HomingAttained 超时\n"); /* Timed out waiting for HomingAttained */
} else {
printf("使能失败: 0x603F=0x%04X\n", CiA402_ReadErrorCode(master, slave, NULL)); /* Enable failed: 0x603F=0x%04X */
}

Mode and status code query entry points​

The detailed family documentation for modes, AL status codes and SDO abort codes (return types, read/write semantics, every entry) is on Protocol codes and diagnostic strings.

CategoryFunctionReturn typeRead/writeDescription
CiA 402 modesCiA402Modes_ModeToSupportedBit(mode)intread-onlyMaps a mode to its bit index in 0x6502; -1 means there is no corresponding bit
CiA402Modes_IsModeSupportedInMask(supported_mask, mode)intread-onlyWhether the 0x6502 mask supports the given mode
CiA402Modes_ExpandSupportedMask(supported_mask, modes, capacity)intread/writeExpands the 0x6502 mask into a list of modes (written into a caller array)
CiA402Modes_GetModeName(mode)const char*read-onlyMode name (Chinese + English abbreviation)
CiA402Modes_GetModeNameEn(mode)const char*read-onlyMode name (full English name)
CiA402Modes_GetModeDescription(mode)const char*read-onlyWhat the mode is used for
CiA402Modes_IsCyclicSyncMode(mode)intread-onlyWhether it is a cyclic synchronous PDO mode
CiA402Modes_RequiresDC(mode)intread-onlyWhether DC synchronization is required
CiA402Modes_IsStandardHomingMethod(method)intread-onlyWhether it is a standard ETG.6010 homing method
CiA402Modes_GetHomingMethodName(method)const char*read-onlyHoming method description
CiA402Modes_GetHomingTrigger(method)homing_trigger_tread-onlyTrigger source of the homing method
CiA402Modes_GetHomingDirection(method)intread-onlyMovement direction of the homing method (-1 = negative, 0 = no movement, +1 = positive)
CiA402Modes_ListStandardHomingMethods(array, capacity)intread/writeOutputs all standard homing method numbers (written into a caller array)
AL status codeAL_StatusCode_GetSeverity(code)al_status_severity_tread-onlySeverity of the status code
AL_StatusCode_GetRecoveryHint(code)const char*read-onlyRecommended action
AL_StatusCode_IsVendorSpecific(code)intread-onlyWhether it is a vendor-specific error (0x8000-0xFFFF)
SDO abortdx_sdo_abort_get_category(abort_code)sdo_abort_category_tread-onlyAbort code category
dx_sdo_abort_get_description(abort_code)const char*read-onlyAbort code description (Chinese / English)
dx_sdo_abort_get_hint(abort_code)const char*read-onlyGuidance for the engineer
dx_sdo_abort_is_retryable(abort_code)intread-onlyWhether it can be retried (1 = retryable, 0 = not retryable)