2.4. Example codes

A master application which includes DCM API needs to call following steps:

Main Thread:

Master initialization and controller configuration, main loop, shutdown.

#include <AtEthercat.h>

/* initialize the master */
dwRes = emInitMaster(&oInitParms);

/* configure the master */
dwRes = emConfigureMaster(eCnfType_Filename, (EC_T_PBYTE)szCfgFile, OsStrlen(szCfgFile));

/* register client */
dwRes = emRegisterClient(emNotifyCallback, pNotification, &oRegisterResults);

/* configure DCM bus shift */
OsMemset(&oDcmConfigure, 0, sizeof(EC_T_DCM_CONFIG_BUSSHIFT));
oDcmConfigure.nCtlSetVal  = DCM_CONTROLLER_SETVAL_NANOSEC;
oDcmConfigure.bLogEnabled = EC_FALSE;
dwRes = emDcmConfigure(&oDcmConfigure, 0);

/* set master and bus state to OP */
dwRes = emSetMasterState(dwStartTimeout+dwScanBustimeout, eemState_OP);

/* application loop */
while(bRun)
{
    dwRes = emDcmGetStatus(&dwStatus, &nDiffCur, &nDiffAvg, &nDiffMax);
}

/* stop master operation */
dwTmpRes = emStop(dwStartTimeout);

Cyclic (Job) Thread:

Master jobs and dcm logging.

EC_T_VOID tEcJobTask(EC_T_VOID* pvAppThreadParamDesc)
{
    while (!pDemoThreadParam->bJobThreadShutdown)
        {
                dwRes = emExecJob(eUsrJob_ProcessAllRxFrames, &bPrevCycProcessed);

                /* get logging information */
                dwRes = emDcmGetLog(&pszLog);

                /* send all cyclic frames */
                dwRes = emExecJob(eUsrJob_SendAllCycFrames, EC_NULL);

                /* run the master timer handler */
                dwRes = emExecJob(eUsrJob_MasterTimer, EC_NULL);

                /* send all queued acyclic EtherCAT frames */
                dwRes = emExecJob(eUsrJob_SendAcycFrames, EC_NULL);

                OsSleep(CYCLE_TIME);
        }
}

For closer details find a DCM example in project “EcMasterDemoDc” in the folder “Examples”.