EtherCAT Slave  1.06.01
Simple EtherCAT Slave

Scope

This example demonstrates the steps to configure the hardware, create the basic EtherCAT Slave information as well as the Object Dictionary and the Process Data configuration.

Related Files

EtherCAT_Slave_Simple.c

Basic Setup

The example starts by initializing operating system (OSAL) and hardware abstraction layers (HWAL). The EtherCAT functionality itself is implemented within the Appl_LoopTask that is started from the main application entry point, both are implemented in EtherCAT_Slave_Simple.c.

Initialization of Stack, PRU, and Example Application

Before entering the main stack loop, the example application itself is initialized in EC_SLV_APP_applicationInit. This function creates a new EtherCAT Slave device instance, registers a board status LED callback, sets vendor ID, product code and name, revision number, etc.

Initialization of the EtherCAT Slave

Manufacturer Specific Objects

Next various objects are created in the Object Dictionary of the EtherCAT Slave device. These objects are used to display information related to the device or used to map them as Process Data. These objects are declared in the Manufacturer Specific Area, whereas the CoE Communication Area objects are created automatically by the SDK.

RXPDO configuration

static EC_API_EError_t EC_SLV_APP_populateOutObjects(EC_SLV_APP_Sapplication_t* pApplicationInstance)
{
...
// Create an Object of type RECORD
error = (EC_API_EError_t)EC_API_SLV_CoE_odAddRecord(ptSlave, 0x2000, "Out Object Record"
,NULL, NULL, NULL, NULL, &pApplicationInstance->ptRecObjOut);
if (error != EC_API_eERR_NONE)
{
OSAL_printf("Object 0x2000 Record Error code: 0x%08x\n", error);
goto Exit;
}
// Create a subindex 1 of the RECORD
error = (EC_API_EError_t)EC_API_SLV_CoE_configRecordSubIndex(ptSlave, pApplicationInstance->ptRecObjOut
,1, "SubIndex 1", DEFTYPE_UNSIGNED32, 32
if (error != EC_API_eERR_NONE)
{
OSAL_printf("Object 0x2000 SubIndex 1 Error code: 0x%08x\n", error);
goto Exit;
}
...
}

TXPDO configuration

static EC_API_EError_t EC_SLV_APP_populateInOutObjects(EC_SLV_APP_Sapplication_t* pApplicationInstance)
{
...
// Create an Object of type RECORD
error = (EC_API_EError_t)EC_API_SLV_CoE_odAddRecord(ptSlave, 0x2002, "Test Record"
,NULL, NULL, NULL, NULL
,&pApplicationInstance->pt2002RecObj);
if (error != EC_API_eERR_NONE)
{
OSAL_printf("Object Record Error code: 0x%08x\n", error);
goto Exit;
}
// Create a subindex 1 of the RECORD
error = (EC_API_EError_t)EC_API_SLV_CoE_configRecordSubIndex(ptSlave, pApplicationInstance->pt2002RecObj
,1, "SubIndex 1", DEFTYPE_UNSIGNED32, 32
if (error != EC_API_eERR_NONE)
{
OSAL_printf("Object 0x2002 SubIndex 1 Error code: 0x%08x\n", error);
goto Exit;
}
...
}

Objects 0x200C and 0x200E are used to demonstrate CoE callback functions. These callbacks, sendEoEFrame and sendEmergencyMsg are deactivated by default in order to pass the EtherCAT Conformance Tests, however, these may be used as write parameter of the stated objects. Using these callback functions, the example application sends an EoE frame or a CoE emergency message with the data written to them. See the code snippet below:

// Create an Object and provide a function to send an emergency message when the object is written
i32uErr = (EC_API_ERR_T)EC_API_SLV_CoE_odAddVariable(ptSlv, 0x200C, "Emergency message", DEFTYPE_UNSIGNED32, 32, ACCESS_READWRITE, NULL, &sendEmergencyMsg);
if (i32uErr != eECERR_NONE)
{
osal_Printf("Object 0x200C Error code: 0x%08x\n", i32uErr);
goto Exit;
}
// Send an EoE message when the Object is written
i32uErr = (EC_API_ERR_T)EC_API_SLV_CoE_odAddVariable(ptSlv, 0x200E, "EoE Send Frame", DEFTYPE_UNSIGNED32, 32, ACCESS_READWRITE, NULL, &sendEoEFrame);
if (i32uErr != eECERR_NONE)
{
osal_Printf("Object 0x200E Error code: 0x%08x\n", i32uErr);
goto Exit;
}

Process Data Object Configuration

Once the Object Dictionary is configured, Process Data are defined. The function EC_SLV_APP_populateRxPDO() is used to generate the output PDOs and EC_SLV_APP_populateTxPDO() to generate the input PDOs. By default, the SDKsets the PDO assignment fixed, meaning that a PDO generated by the application is attached to a SyncManager and this configuration cannot be changed. However, it allows by default to modify the PDO entry content. To change these behaviours refer to EC_API_SLV_PDO_setFixedMapping() and EC_API_SLV_PDO_setAssignment() functions.

Initialization and starting the Stacks

The EC_API_SLV_init() function initializes the SDKStack and validates whether the user defined configuration is valid or not. If the EtherCAT configuration is valid, then object content can be written and finally, the EC_API_SLV_run() function starts the Beckhoff Stack.

EC_API_SLV_CoE_odAddVariable
uint32_t EC_API_SLV_CoE_odAddVariable(EC_API_SLV_SHandle_t *pEcSlaveApi_p, uint16_t index_p, char *pName_p, uint16_t type_p, uint16_t bitLen_p, uint16_t flags_p, EC_API_SLV_CBObjRead_t cbRead_p, void *pReadCtxt_p, EC_API_SLV_CBObjWrite_t cbWrite_p, void *pWriteCtxt_p)
This function creates a Base Data Type Object for the Object Dictionary.
Definition: ecSlvApi_CoEStub.c:624
EC_SLV_APP_Sapplication::ptRecObjOut
EC_API_SLV_SCoE_Object_t * ptRecObjOut
Definition: ecSlvSimple.h:87
OBJACCESS_TXPDOMAPPING
#define OBJACCESS_TXPDOMAPPING
Mappable in TxPDOs.
Definition: ecApiDef.h:143
EC_API_EError_t
enum EC_API_EError EC_API_EError_t
EC_SLV_APP_Sapplication::pt2002RecObj
EC_API_SLV_SCoE_Object_t * pt2002RecObj
Definition: ecSlvSimple.h:88
ACCESS_READWRITE
#define ACCESS_READWRITE
Read/write in all states.
Definition: ecApiDef.h:132
EC_SLV_APP_Sapplication
Definition: ecSlvSimple.h:55
EC_API_SLV_SHandle_t
struct EC_API_SLV_SHandle EC_API_SLV_SHandle_t
EC_API_SLV_SHandle_t describes the EtherCAT Slave API.
Definition: ecSlvApi.h:135
EC_API_eERR_INVALID
@ EC_API_eERR_INVALID
Definition: ecApiError.h:73
ACCESS_READ
#define ACCESS_READ
Read only in all states.
Definition: ecApiDef.h:133
EC_API_SLV_CoE_configRecordSubIndex
uint32_t EC_API_SLV_CoE_configRecordSubIndex(EC_API_SLV_SHandle_t *pEcSlaveApi_p, EC_API_SLV_SCoE_Object_t *pObject_p, uint8_t subIndex_p, char *pName_p, uint16_t type_p, uint16_t bitLen_p, uint16_t flags_p)
This function creates a subIndex for the Record Object.
Definition: ecSlvApi_CoEStub.c:780
EC_API_SLV_CoE_odAddRecord
uint32_t EC_API_SLV_CoE_odAddRecord(EC_API_SLV_SHandle_t *pEcSlaveApi_p, uint16_t index_p, char *pName_p, EC_API_SLV_CBObjRead_t cbRead_p, void *pReadCtxt_p, EC_API_SLV_CBObjWrite_t cbWrite_p, void *pWriteCtxt_p, EC_API_SLV_SCoE_Object_t **pOutSdo_p)
This function creates a Record Object.
Definition: ecSlvApi_CoEStub.c:731
OBJACCESS_RXPDOMAPPING
#define OBJACCESS_RXPDOMAPPING
Mappable in RxPDOs.
Definition: ecApiDef.h:142
DEFTYPE_UNSIGNED32
#define DEFTYPE_UNSIGNED32
UNSIGNED32.
Definition: ecApiDef.h:71
EC_API_eERR_NONE
@ EC_API_eERR_NONE
Definition: ecApiError.h:66