EtherCAT Slave
 
Loading...
Searching...
No Matches
EtherCAT_Slave_ESI_Parser.c

EtherCAT® Slave Example Application.

EtherCAT® Slave Example Application

Author
KUNBUS GmbH
Date
2020-06-19
#define TIESC_HW 0
#define PRU_200MHZ 1
/* this one shall the only one to be using it ! */
#include "project.h"
#include <ecSlvESI.h>
#if (defined FBTL_REMOTE) && (FBTL_REMOTE==1)
#include <sysLib_sharedMemory.h>
#endif
/* OS */
#include <osal.h>
/* ESL */
#include <ESL_os.h>
#include <ESL_BOARD_config.h>
#include <ESL_version.h>
/* stack */
#include <ecSlvApi.h>
static OSAL_PJumpBuf_t farJumpBuf_g;
static uint32_t EC_SLV_APP_remoteInit(void** pRemHandle_p);
static void EC_SLV_APP_loopTask(void* pArg_p)
{
EC_SLV_APP_Application_t* pApplicationInstance = (EC_SLV_APP_Application_t*)pArg_p;
uint32_t error = EC_API_eERR_NONE;
uint16_t alErrorCode = 0;
if (!pApplicationInstance)
{
/* @cppcheck_justify{misra-c2012-15.1} goto is used to assure single point of exit */
/* cppcheck-suppress misra-c2012-15.1 */
goto Exit;
}
EC_SLV_APP_ESIP_initBoardFunctions(pApplicationInstance);
error = EC_API_SLV_stackInit(); // EtherCAT stack init
if (error != EC_API_eERR_NONE)
{
OSAL_printf("%s:%d Error code: 0x%08x\n", __func__, __LINE__, error);
OSAL_error(__func__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
}
EC_SLV_APP_ESIP_applicationInit(pApplicationInstance);
/* spit out versions */
ESL_dumpVersions(pApplicationInstance->ptEcSlvApi);
for(;;)
{
EC_API_SLV_mainLoopCyclic();
EC_API_SLV_getState(pApplicationInstance->ptEcSlvApi, &state, &alErrorCode);
if (EC_API_SLV_eESM_init < state)
{
OSAL_SCHED_yield();
}
else
{
/* for carve up give some air */
OSAL_SCHED_sleep(10);
}
}
// not reachable
//EC_SLV_APP_applicationDeInit();
Exit:
ESL_OS_taskLeave();
return;
}
static void EC_SLV_APP_ESIP_mainTask(void* pArg_p)
{
EC_SLV_APP_Application_t* applicationInstance = (EC_SLV_APP_Application_t*)pArg_p;
uint32_t retVal = OSAL_ERR_NoError;
if(!applicationInstance)
{
OSAL_error(__func__, __LINE__, OSAL_ERR_InvalidParm, true, 1, "Application instance missing!!!\r\n");
}
retVal = ESL_OS_boardInit(applicationInstance->selectedPruInstance);
if(OSAL_ERR_NoError!= retVal)
{
OSAL_error(__func__, __LINE__, retVal, true, 1, "OS Board init error\r\n");
}
retVal = EC_SLV_APP_remoteInit(&applicationInstance->remoteHandle);
if (!((OSAL_CONTAINER_LOCALIMPLEMENTATION == retVal) || (OSAL_ERR_NoError == retVal)))
{
OSAL_error(__func__, __LINE__, OSAL_ERR_InvalidParm, true, 1, "Fatal error remote API init!!!\r\n");
return;
}
retVal = EC_API_SLV_load(&farJumpBuf_g, NULL /* &applErrHandler*/, applicationInstance->selectedPruInstance);
if (0 == retVal)
{
EC_API_SLV_prepareTasks(KBECSLV_PRIO_PDI, KBECSLV_PRIO_LED, KBECSLV_PRIO_SYNC0, KBECSLV_PRIO_SYNC1);
applicationInstance->loopThreadHandle = OSAL_SCHED_startTask(EC_SLV_APP_loopTask
,applicationInstance
#if !(defined FBTL_REMOTE) || (0==FBTL_REMOTE)
,OSAL_TASK_Prio_Normal
#else
,OSAL_TASK_Prio_Idle
#endif
,(uint8_t*)EC_SLV_APP_applLoopTaskStack_g
,APPLLOOP_TASK_SIZE_BYTE
,0
,"Appl_LoopTask");
if ( NULL == applicationInstance->loopThreadHandle)
{
OSAL_printf("Error return start Loop Task\r\n");
OSAL_error(__func__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
}
OSAL_SCHED_joinTask(applicationInstance->loopThreadHandle);
/* No default tasks (run forever) here, so introduce a wait cycle */
for (;;)
{
OSAL_SCHED_sleep(1000);
}
}
else
{
for (;;)
{
for (;;)
{
OSAL_printf("long jmp restart\n");
OSAL_SCHED_sleep(1000);
}
}
}
// not reachable
//ESL_OS_boardDeinit();
//EC_API_SLV_unLoad();
//return 0;
}
/* @cppcheck_justify{misra-c2012-8.6} entry point always called main */
/* cppcheck-suppress misra-c2012-8.6 */
int main(int argc, char *argv[])
{
static
uint32_t selectedPruInstance = ~0;
static
EC_SLV_APP_Application_t applicationInstance = {0};
uint32_t error = OSAL_ERR_NoError;
ESL_OS_init();
selectedPruInstance = ESL_DEFAULT_PRUICSS;
if (1 < argc)
{
char* inst = argv[1];
selectedPruInstance = strtoul(inst, NULL, 0);
}
#if (defined OSAL_TIRTOS)
/* workaround to assure PRUICSS linkage */
if (0x1000 == argc)
{
/* make this link */
PRUICSS_pruDisable(NULL, 0);
PRUICSS_pruDisable(NULL, 1);
PRUICSS_registerIrqHandler(NULL, 0, 0, 0, 0, NULL);
}
#endif
/* Init osal */
OSAL_init();
applicationInstance.selectedPruInstance = selectedPruInstance;
#if (defined OSAL_FREERTOS) || (defined OSAL_FREERTOS_JACINTO)
TaskP_Params_init(&applicationInstance.mainThreadParam);
applicationInstance.mainThreadParam.name = "mainThread";
applicationInstance.mainThreadParam.stackSize = MAIN_TASK_SIZE_BYTE; /* FreeRTOS wants Stacksize in Units, when called direct, using TaskP it's bytes */
applicationInstance.mainThreadParam.stack = (uint8_t*)EC_SLV_APP_mainTaskStack_g;
applicationInstance.mainThreadParam.priority = (TaskP_PRIORITY_HIGHEST-1);
applicationInstance.mainThreadParam.taskMain = (TaskP_FxnMain)EC_SLV_APP_ESIP_mainTask;
applicationInstance.mainThreadParam.args = (void*)&applicationInstance;
error = TaskP_construct(&applicationInstance.mainThreadHandle
,&applicationInstance.mainThreadParam);
if (SystemP_SUCCESS != error)
{
OSAL_printf("Error setting create thread of %s (%ld)\r\n", applicationInstance.mainThreadParam.name, error);
OSAL_error(__func__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
}
#else
applicationInstance.mainThreadHandle = OSAL_SCHED_startTask(EC_SLV_APP_ESIP_mainTask, &applicationInstance,
OSAL_TASK_Prio_Idle,
(uint8_t*)EC_SLV_APP_mainTaskStack_g,
MAIN_TASK_SIZE_BYTE, 0, "MainTask");
if ( NULL == applicationInstance.mainThreadHandle)
{
OSAL_printf("Error return start Main Task\r\n");
OSAL_error(__func__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
error = OSAL_STACK_INIT_ERROR;
}
#endif
OSAL_startOs();
OSAL_error(__func__, __LINE__, OSAL_ERR_InvalidParm, true, 1, "Not reachable by design!!!\r\n");
// not reachable
//EC_API_SLV_unLoad();
return error;
}
static uint32_t EC_SLV_APP_remoteInit(void** pRemHandle_p)
{
uint32_t retVal = OSAL_CONTAINER_LOCALIMPLEMENTATION;
if (!pRemHandle_p)
{
/* @cppcheck_justify{misra-c2012-15.1} goto is used to assure single point of exit */
/* cppcheck-suppress misra-c2012-15.1 */
goto Exit;
}
#if (defined FBTL_REMOTE) && (FBTL_REMOTE==1)
retVal = SYSLIB_createLibInstance(FBTLSHARED_MEM_NAME, FBTLSHARED_MEM_SIZE, false,
FBTL_MAX_ASYNC_LEN, FBTL_MAX_ASYNC_LEN, FBTL_MAX_PD_LEN, FBTL_MAX_PD_LEN
#if (defined FBTLTRACECALLS) && (FBTLTRACECALLS==1)
,true
#else
,false
#endif
,pRemHandle_p
);
#else
OSALUNREF_PARM(pRemHandle_p);
retVal = OSAL_CONTAINER_LOCALIMPLEMENTATION;
#endif
if (OSAL_CONTAINER_LOCALIMPLEMENTATION == retVal)
{
OSAL_printf("\r\nLocal Implementation\r\n");
}
#if (defined FBTL_REMOTE) && (FBTL_REMOTE==1)
else if (0 == retVal)
{
retVal = EC_API_SLV_FBTL_configuration(*pRemHandle_p);
if (0 != retVal)
{
OSAL_printf("%s:%d Error code: 0x%08x\n", __func__, __LINE__, retVal);
OSAL_error(__func__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
return retVal;
}
}
#endif
Exit:
return retVal;
}
@ EC_API_eERR_NONE
Definition ecSlvApiDef_error.h:45
enum EC_API_SLV_EEsmState EC_API_SLV_EEsmState_t
EC_STATE_T EtherCAT State Machine states.
@ EC_API_SLV_eESM_uninit
Uninitialized State.
Definition ecSlvApi_types.h:70
@ EC_API_SLV_eESM_init
Init State.
Definition ecSlvApi_types.h:71
void EC_SLV_APP_ESIP_initBoardFunctions(EC_SLV_APP_Application_t *pAppInstance_p)
Initialize board related functions.
Definition ecSlvESI.c:262
void EC_SLV_APP_ESIP_registerStacklessBoardFunctions(EC_SLV_APP_Application_t *pAppInstance_p)
Register board related functions, which do not use stack handle.
Definition ecSlvESI.c:328
void * loopThreadHandle
Definition ecSlvESI.h:65
void * mainThreadHandle
Definition ecSlvESI.h:63
uint32_t selectedPruInstance
Definition ecSlvESI.h:56
EC_API_SLV_SHandle_t * ptEcSlvApi
Definition ecSlvESI.h:93
void * remoteHandle
Definition ecSlvESI.h:69
Definition ecSlvESI.h:55