Adeeb Robotics

EtherCAT Master C API

C API reference for the Adeeb Robotics EtherCAT Master library — PDO/SDO communication, DC sync, and CoE operations.

Getting Started

The Adeeb Robotics EtherCAT Master library provides support for PDO and SDO EtherCAT communication via easy-to-use C APIs. The library also supports DC (Distributed Clocks) sync, EtherCAT state transitions, and CoE object reads and writes.

Software Release Version: 0.0.6

Requirements

Before using this library, ensure the following:

  1. All connected slaves are EtherCAT devices implementing the CiA 402 motion control profile.
  2. The TxPDO (slave-to-master) mapping includes at minimum:
    • Status Word: 0x6041
    • Position Actual Value: 0x6064
  3. The RxPDO (master-to-slave) mapping includes at minimum:
    • Control Word: 0x6040
    • Target Position: 0x607A

Initial Setup

Ethernet Interface Configuration

Update the interface_name field in the ethernet section of the app_config.yml file to match the Ethernet adapter used for EtherCAT communication.

If you’re unsure of the interface name, simply run the library and it will display a list of available network interfaces.

Cycle Time Configuration

Set your desired EtherCAT cycle time in the app_config.yml file.

ESI Files

Add the EtherCAT Slave Information (ESI) files for your devices to the ESI/ directory.

Building & Running

Linux / macOS

To rebuild and run the example application:

./rebuild_and_rerun_app.sh

To run the example application only:

sudo ./ethercat_master_example <path_to_config_file>

Windows

To rebuild and run the example application (use the x64 Native Tools Command Prompt for Visual Studio 2022 or later):

rebuild_and_rerun_app.bat

To run the example code only:

ethercat_master_example <path_to_config_file>

The default configuration file is app_config.yml. To list available network interfaces, run ifconfig.

Required Privileges

On Unix-based systems, root privileges (sudo) are required as the library performs real-time optimizations and direct access to the network interface. You can persist sudo privileges by running:

sudo -s

Sample Application

The main.c file contains a basic application that powers on a servo motor and jogs it at a slow constant speed. It serves as a reference for using the EtherCAT API.

Caution: The sample application will immediately power on the first motor and move it slowly. Ensure your hardware is safe and ready before running.

This library includes several OS- and system-level optimizations to help achieve high-performance real-time control.

Configuration File

Ethernet

FieldTypeDescription
interface_nameStringEthernet interface name
prioritize_usr_space_eth_drvBooleanIf true, use ethernet driver optimization if possible. Only supported on select hardware running Linux. See Appendix B.

EtherCAT

FieldTypeDescriptionUnit
esi_file_pathStringESI file pathN/A
prioritize_esi_configBooleanIf true, prioritize obtaining slave configuration from ESI file; otherwise use slave EEPROM SSI dataN/A
cycle_timeu32EtherCAT cycle time, only multiples of 250μs supportedμs
shift_timeu32DC master frame send to sync0 offset% of cycle_time
rx_frame_timeoutu32Receive frame timeout, only supported on Linux and macOSμs

Real-Time Cyclic Thread APIs

All APIs below are non-blocking except for thrd_rt_pre_opt_c(), sleep_tx_frm_rd_cyc_fbk(), and wr_cyc_cmd_prc_async_dc_sync_rx_frm().

APIDescriptionParametersReturns
thrd_rt_pre_opt_c()Thread real-time optimization. Call once before the start of the real-time thread.N/AN/A
sleep_tx_frm_rd_cyc_fbk()Sleep the required amount of time and transmit EtherCAT frame.N/ABoolean
wr_cyc_cmd_prc_async_dc_sync_rx_frm()Process asynchronous EtherCAT commands, perform DC sync, and receive EtherCAT frame.N/AN/A
rd_slv_pos_act()Read slave actual position.slv_idx (uint)i32
wr_slv_trg_pos()Write slave target position.slv_idx (uint), pos (i32)N/A
rd_slv_sta_wrd()Read slave status word for warning and alarm monitoring.slv_idx (uint)u16
rd_slv_cnt_wrd()Read slave control word.slv_idx (uint)u16
wr_slv_cnt_wrd()Write slave control word (e.g., for clearing faults and alarms).slv_idx (uint), cnt_wrd (i16)N/A
rd_slv_servo_on_current()Read current slave servo status (ON or OFF).slv_idx (uint)Boolean
wr_slv_servo_on_desired()Write desired slave servo status. Causes changes to the control word.slv_idx (uint), servo_on_desired (Boolean)N/A

Main Thread APIs

All APIs below are blocking except for get_number_of_slaves().

APIDescriptionParametersReturns
get_ecat_master_ver()Get EtherCAT master library version.ver (u8 pointer), ver_len (uint)rslt (u16)
sys_rt_pre_opt_c()System real-time optimization. Call once at the beginning of main thread.N/AN/A
init_network()Scan the network for slaves and place them in pre-op EtherCAT state.N/Au16
get_number_of_slaves()Get number of slaves. Must be called after init_network.N/Au16
req_preop()Place all slaves in pre-operational EtherCAT state.N/Au16
req_op()Place all slaves in operational EtherCAT state.N/Au16

CoE Operations

APIDescriptionParametersReturns
coe_rd_u8()CoE u8 read operation.slv_idx, index (u16), sub_index (u8), data (u8 pointer)rslt (u16)
coe_wr_u8()CoE u8 write operation.slv_idx, index (u16), sub_index (u8), data (u8)rslt (u16)
coe_rd_u16()CoE u16 read operation.slv_idx, index (u16), sub_index (u8), data (u16 pointer)rslt (u16)
coe_wr_u16()CoE u16 write operation.slv_idx, index (u16), sub_index (u8), data (u16)rslt (u16)
coe_rd_u32()CoE u32 read operation.slv_idx, index (u16), sub_index (u8), data (u32 pointer)rslt (u16)
coe_wr_u32()CoE u32 write operation.slv_idx, index (u16), sub_index (u8), data (u32)rslt (u16)

CiA 402 Operations

APIDescriptionParametersReturns
set_cia402_op_mode()Set slave to cyclic position mode.slv_idx (uint), op_mode (u8)rslt (u16)

Appendix A: ECAT Result Error Codes

ValueDescription
0x0000Success
0x0001Version string length insufficient
0x0002Invalid slave index
0x0003Uninitialized slave index
0x0004Uninitialized station address
0x0005Working counter not incremented
0x0006Frame receive timeout
0x0007Frame command mismatch
0x0008ESI file not found
0x0009SM data cat section not found in SSI
0x000ARegister response mismatch
0x000BSSI maximum byte length reached
0x000CMax retries reached while polling CoE SM1 response mbox ready status
0x000DCoE mbox emergency
0x000ECoE mbox abort code
0x000FCiA operational mode maximum retries reached
0x0010EtherCAT command mismatch
0x0FFFUnspecified EtherCAT error

Appendix B: Ethernet Driver Optimization

Maximum possible update rate of 4000 Hz (250 μs cycle time) is only supported on PCs with real-time Ethernet driver support. Specifically, this requires a PC with Linux OS and an Intel Ethernet LAN adapter using the igc Intel driver.

Supported Intel Ethernet PCIe cards:

I225-LM, I225-V, I225-I, I220-V, I225-K, I225-K2, I226-K, I225-LMVP, I226-LMVP, I225-IT, I226-LM, I226-V, I226-IT, I221-V

Need the original PDF?

Available on the downloads page.

Go to Downloads