EtherCAT SubDevice
 
Loading...
Searching...
No Matches
Register Custom Read/Write Callbacks for Objects

This page provides the details of registering the custom read and write callbacks to objects of type Variable, Array and Record.

Create the custom object read callback function to be registered with object in the format given below.

uint8_t(*EC_API_SLV_CBObjRead_t)(void* pContext, uint16_t index, uint8_t subindex, uint32_t length, uint16_t* pData, uint8_t completeAccess);
uint8_t(* EC_API_SLV_CBObjRead_t)(void *pContext, uint16_t index, uint8_t subindex, uint32_t length, uint16_t *pData, uint8_t completeAccess)
Callback trigered by a SDO Upload operation.
Definition ecSlvApi_types.h:483

This callback function is called when the EtherCAT MainDevice sends the object read request (SDO upload request). It is the responsibility of the registered callback function to update the value pointed to by pData. pData is returned within the SDO Response datagram.

The code snippet is the sample custom read callback implementation.

uint8_t EC_SLV_APP_readObjectCb(void* pContext,uint16_t index_p,uint8_t subindex_p,uint32_t size_p,uint16_t MBXMEM * pData_p,uint8_t completeAccess_p)
{
OSAL_printf("SDO Upload: %s ==> Idx: 0x%04x:%d | Size: %d | access: %d\r\n", __func__,index_p, subindex_p, size_p, completeAccess_p);
/* Check if SDO upload is with complete access or not. */
if (completeAccess_p)
{
/* if SDO upload is with complete access and if subindex_p = 0 then pData_p should contain the value of SI0 also.*/
if (subindex_p == 0)
{
/* copy object entry data including SI0 value into pData_p */
}
else if (subindex_p == 1)
{
/* copy object entry data excluding SI0 value into pData_p */
}
else
{
OSAL_printf("Invalid SI for Complete Access -> 0x%04x:%d\r\n",index_p,subindex_p);
}
}
/* single object entry SDO upload */
else
{
/* copy corresponding subindex_p data from object entry into pData_p */
}
return (uint8_t)error;
}
enum EC_API_EError EC_API_EError_t
#define ABORT_NOERROR
No SDO error.
Definition ecSlvApiDef_CoE.h:158

Create the custom object write callback function to be registered with object in the format given below.

uint8_t(*EC_API_SLV_CBObjWrite_t)(void* pContext, uint16_t index, uint8_t subindex, uint32_t length, uint16_t* pData, uint8_t completeAccess);
uint8_t(* EC_API_SLV_CBObjWrite_t)(void *pContext, uint16_t index, uint8_t subindex, uint32_t length, uint16_t *pData, uint8_t completeAccess)
Callback trigered by a SDO Download operation.
Definition ecSlvApi_types.h:511

This callback function is called when the EtherCAT MainDevice sends the object write request (SDO download request). It is the responsibility of the callback function to copy the value pointed to by pData to the object entry.

The code snippet is the sample write callback implementation.

uint8_t EC_SLV_APP_writeObjectCb(void* pContext,uint16_t index_p,uint8_t subindex_p,uint32_t size_p,uint16_t MBXMEM * pData_p,uint8_t completeAccess_p)
{
OSAL_printf("SDO Download: %s ==> Idx: 0x%04x:%d | Size: %d | access: %d\r\n", __func__,index_p, subindex_p, size_p, completeAccess_p);
/* Check if SDO download is with complete access or not. */
if (completeAccess_p)
{
/* if SDO download is with complete access and if subindex_p = 0 then pData_p contains the value of SI0 also.
Note that SI0 is read only. */
if(subindex_p == 0)
{
/* copy data from pData_p into the object entry data memory block*/
}
/* if SDO download is with complete access and if subindex_p = 1 then pData_p doesnot contain the value of SI0. */
else if(subindex_p == 1)
{
/* copy data from pData_p into the object entry data memory block*/
}
}
/* single object entry SDO download */
else
{
/* SI0 is read only. */
if(subindex_p!=0)
{
/* copy subindex_p data from pData_p into the corresponding object entry */
}
}
return (uint8_t)error;
}

Register these custom object read and write callbacks during the object creation. The code snippet below registers the custom read and write callbacks for a variable type object.

ptSubDevice,
0xYYYY,
"Name of Variable",
32,
EC_SLV_APP_readObjectCb, // Register read callback
pContext, // read callback context
EC_SLV_APP_writeObjectCb, // Register write callback
pContext); // write callback context
uint32_t EC_API_SLV_CoE_odAddVariable(EC_API_SLV_SHandle_t *pHandle, uint16_t index, char *pName, uint16_t type, uint16_t bitLen, uint16_t flags, EC_API_SLV_CBObjRead_t cbRead, void *pReadCtxt, EC_API_SLV_CBObjWrite_t cbWrite, void *pWriteCtxt)
This function creates a Base Data Type Object for the Object Dictionary.
Definition ecSlvApi_CoE.c:1606
#define ACCESS_READWRITE
Read/write in all states.
Definition ecSlvApiDef_CoE.h:134
#define DEFTYPE_UNSIGNED32
UNSIGNED32.
Definition ecSlvApiDef_CoE.h:46