CiA402 Application implementation.
#include <ecSlvApi.h>
#include <ecApiDef.h>
#include "ecSlvCiA402.h"
#include "ESL_cia402Obd.h"
#include "ESL_cia402Demo.h"
#include "project.h"
#include <ESL_os.h>
#include <ESL_BOARD_config.h>
#include <ESL_vendor.h>
#include <ESL_phyLibTlk110.h>
#include <ESL_gpioHelper.h>
#include <ESL_foeDemo.h>
#include <ESL_soeDemo.h>
#include <ESL_eeprom.h>
#if (defined HAVEDISPLAY) && (HAVEDISPLAY==1)
#include <oled_drv.h>
#endif
#include <ecSlvApi.h>
#if !(defined MBXMEM)
#define MBXMEM
#endif
#define I2C_IOEXP_ADDR 0x60 // The I2C address for GPIO expander
#define RXPDOMAP_INDEX 0x1600
#define TXPDOMAP_INDEX 0x1A00
#define AXIS_ONE 0
#define AXIS_TWO 1
#define AXIS_THREE 2
static void EC_SLV_APP_applicationRun(void* pAppCtxt_p);
#if (defined HAVEDISPLAY) && (HAVEDISPLAY==1)
void oled_init(void)
{
const char oled_line1[] = "Kunbus GmbH";
const char oled_line2[] = "EtherCAT DTK Demo";
Board_oledInit();
clear();
setline(0);
setOrientation(1);
printstr((int8_t *)oled_line1);
setline(1);
setOrientation(1);
printstr((int8_t *)oled_line2);
scrollDisplayRight();
}
void oled_run(uint8_t i8uState)
{
const char oled_line1[] = "EtherCAT DTK Demo";
clear();
setline(0);
setOrientation(1);
printstr((int8_t *)oled_line1);
setline(1);
setOrientation(1);
char line2[] = "State: ";
char buffer[7];
sprintf(buffer, "%d ", i8uState);
strcat(line2, buffer);
printstr((int8_t *)line2);
scrollDisplayRight();
}
#endif
{
if (!pApplicationInstance_p)
{
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
ET1100_BOOT_MBXOUT_START, ET1100_BOOT_MBXOUT_DEF_LENGTH,
ET1100_BOOT_MBXIN_START, ET1100_BOOT_MBXIN_DEF_LENGTH);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
ET1100_MBXOUT_START, ET1100_MBXOUT_DEF_LENGTH,
ET1100_MBXIN_START, ET1100_MBXIN_DEF_LENGTH);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
0, ET1100_MBXOUT_START, ET1100_MBXOUT_DEF_LENGTH,
ET1100_MBXOUT_CONTROLREG, ET1100_MBXOUT_ENABLE);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
1, ET1100_MBXIN_START, ET1100_MBXIN_DEF_LENGTH,
ET1100_MBXIN_CONTROLREG, ET1100_MBXIN_ENABLE);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
2, ET1100_OUTPUT_START, ET1100_OUTPUT_DEF_LENGTH,
ET1100_OUTPUT_CONTROLREG, ET1100_OUTPUT_ENABLE);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
3, ET1100_INPUT_START, ET1100_INPUT_DEF_LENGTH,
ET1100_INPUT_CONTROLREG, ET1100_INPUT_ENABLE);
{
OSAL_printf("%s:%d (%s) Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
Exit:
return error;
}
{
if (!pApplicationInstance_p)
{
goto Exit;
}
{
OSAL_printf("%s:%d (%s) CiA402 Object Dictionary error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
Exit:
return error;
}
{
uint16_t rxIndex = RXPDOMAP_INDEX + axis_p;
if (!pApplicationInstance_p)
{
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Create PDO 0x%04x error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, rxIndex, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
Exit:
return error;
}
{
uint16_t txIndex = TXPDOMAP_INDEX + axis_p;
if (!pApplicationInstance_p)
{
goto Exit;
}
{
OSAL_printf("Create PDO for axis %d error code: 0x%08x\r\n", axis_p);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("PDO 0x1A00 Entry 2 Error code: 0x%08x\r\n", error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("PDO 0x1A00 Entry 2 Error code: 0x%08x\r\n", error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("PDO 0x1A00 Entry 2 Error code: 0x%08x\r\n", error);
goto Exit;
}
{
OSAL_printf("%s:%d (%s) Variable Error code: 0x%08x\r\n", __FILE__, __LINE__, __FUNCTION__, error);
goto Exit;
}
{
OSAL_printf("PDO 0x1A00 Entry 2 Error code: 0x%08x\r\n", error);
goto Exit;
}
Exit:
return error;
}
#if !(defined DPRAM_REMOTE) && !(defined FBTL_REMOTE)
static OSAL_FUNC_UNUSED void EC_SLV_APP_boardPhyReset(void* pCtxt_p, uint8_t phyIdx_p, bool reset_p)
{
uint8_t module = (uint8_t)~0;
uint8_t pin = (uint8_t)~0;
switch(phyIdx_p)
{
case 0:
#if (defined APP_PHY00_RESET_MODULE) && (defined APP_PHY00_RESET_PIN)
{
module = APP_PHY00_RESET_MODULE;
pin = APP_PHY00_RESET_PIN;
}
#endif
#if (defined APP_PHY10_RESET_MODULE) && (defined APP_PHY10_RESET_PIN)
{
module = APP_PHY10_RESET_MODULE;
pin = APP_PHY10_RESET_PIN;
}
#endif
break;
case 1:
#if (defined APP_PHY01_RESET_MODULE) && (defined APP_PHY01_RESET_PIN)
{
module = APP_PHY01_RESET_MODULE;
pin = APP_PHY01_RESET_PIN;
}
#endif
#if (defined APP_PHY11_RESET_MODULE) && (defined APP_PHY11_RESET_PIN)
{
module = APP_PHY11_RESET_MODULE;
pin = APP_PHY11_RESET_PIN;
}
#endif
break;
default:
return;
}
if ((uint8_t)-1 == pin || (uint8_t)-1 == module)
{
OSAL_printf("Invalid PHY config %u\r\n", phyIdx_p);
OSAL_error(__FILE__, __LINE__, OSAL_STACK_PHYDETECT_ERROR, true, 0);
}
if (reset_p)
{
OSAL_printf("Phy Reset: %u.%u\r\n", module, pin);
ESL_GPIO_write(pApplicationInstance->
gpioHandle, (ESL_GPIO_EModule_t)module, (ESL_GPIO_EPin_t)pin, ESL_GPIO_enPINSTATE_LOW);
}
else
{
OSAL_printf("Phy UnReset: %u.%u\r\n", module, pin);
ESL_GPIO_write(pApplicationInstance->
gpioHandle, (ESL_GPIO_EModule_t)module, (ESL_GPIO_EPin_t)pin, ESL_GPIO_enPINSTATE_HIGH);
}
}
#endif
static void EC_SLV_APP_appBoardStatusLed(void* pCallContext_p, void* pLedContext_p, bool runLed_p, bool errLed_p)
{
uint8_t runMod = ~0;
uint8_t runPin = ~0;
uint8_t errMod = ~0;
uint8_t errPin = ~0;
OSALUNREF_PARM(pLedContext_p);
if (NULL == pApplicationInstance)
{
goto Exit;
}
#if (defined GPIO_LEDRG0_BANK) && (defined GPIO_LEDRG0_PIN) && (defined GPIO_LEDLR0_BANK) && (defined GPIO_LEDLR0_PIN)
{
runMod = GPIO_LEDRG0_BANK; runPin = GPIO_LEDRG0_PIN;
errMod = GPIO_LEDLR0_BANK; errPin = GPIO_LEDLR0_PIN;
}
#endif
#if (defined GPIO_LEDRG1_BANK) && (defined GPIO_LEDRG1_PIN) && (defined GPIO_LEDLR1_BANK) && (defined GPIO_LEDLR1_PIN)
{
runMod = GPIO_LEDRG1_BANK; runPin = GPIO_LEDRG1_PIN;
errMod = GPIO_LEDLR1_BANK; errPin = GPIO_LEDLR1_PIN;
}
#endif
if ((uint8_t)-1 == runMod || (uint8_t)-1 == runPin)
{
OSAL_printf("Run LED config invalid \r\n");
OSAL_error(__FILE__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
}
if ((uint8_t)-1 == errMod || (uint8_t)-1 == errPin)
{
OSAL_printf("Error LED config invalid \r\n");
OSAL_error(__FILE__, __LINE__, OSAL_STACK_INIT_ERROR, true, 0);
}
ESL_GPIO_write(pApplicationInstance->
gpioHandle, (ESL_GPIO_EModule_t)runMod, (ESL_GPIO_EPin_t)runPin, runLed_p?ESL_GPIO_enPINSTATE_HIGH:ESL_GPIO_enPINSTATE_LOW);
ESL_GPIO_write(pApplicationInstance->
gpioHandle, (ESL_GPIO_EModule_t)errMod, (ESL_GPIO_EPin_t)errPin, errLed_p?ESL_GPIO_enPINSTATE_HIGH:ESL_GPIO_enPINSTATE_LOW);
Exit:
return;
}
{
if (!pApplicationInstance)
{
goto Exit;
}
,EC_SLV_APP_appBoardStatusLed
Exit:
return error;
}
{
if (!pAppInstance_p)
{
goto Exit;
}
#if !(defined DPRAM_REMOTE) && !(defined FBTL_REMOTE)
#if (defined APP_PHY00_RESET_PIN) && (defined APP_PHY00_RESET_MODULE) && (defined APP_PHY01_RESET_PIN) && (defined APP_PHY01_RESET_MODULE)
{
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, APP_PHY00_RESET_MODULE, APP_PHY00_RESET_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, APP_PHY01_RESET_MODULE, APP_PHY01_RESET_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
}
#endif
#if (defined APP_PHY10_RESET_PIN) && (defined APP_PHY10_RESET_MODULE) && (defined APP_PHY11_RESET_PIN) && (defined APP_PHY11_RESET_MODULE)
{
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, APP_PHY10_RESET_MODULE, APP_PHY10_RESET_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, APP_PHY11_RESET_MODULE, APP_PHY11_RESET_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
}
#endif
#else
OSALUNREF_PARM(pAppInstance_p);
#endif
#if (defined GPIO_LEDRG0_BANK) && (defined GPIO_LEDRG0_PIN) && (defined GPIO_LEDLR0_BANK) && (defined GPIO_LEDLR0_PIN)
{
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, GPIO_LEDRG0_BANK, GPIO_LEDRG0_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, GPIO_LEDLR0_BANK, GPIO_LEDLR0_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
}
#endif
#if (defined GPIO_LEDRG1_BANK) && (defined GPIO_LEDRG1_PIN) && (defined GPIO_LEDLR1_BANK) && (defined GPIO_LEDLR1_PIN)
{
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, GPIO_LEDRG1_BANK, GPIO_LEDRG1_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
ESL_GPIO_setConfigMode(pAppInstance_p->
gpioHandle, GPIO_LEDLR1_BANK, GPIO_LEDLR1_PIN, ESL_GPIO_enDIRECTION_MODE_OUTPUT, ESL_GPIO_enIRQ_MODE_NONE);
}
#endif
#if !(defined DPRAM_REMOTE) && !(defined FBTL_REMOTE)
#if (defined APP_PHY00_RESET_PIN) && (defined APP_PHY00_RESET_MODULE) && (defined APP_PHY01_RESET_PIN) && (defined APP_PHY01_RESET_MODULE)
{
EC_SLV_APP_boardPhyReset(pAppInstance_p, 0, false);
EC_SLV_APP_boardPhyReset(pAppInstance_p, 1, false);
}
#endif
#if (defined APP_PHY10_RESET_PIN) && (defined APP_PHY10_RESET_MODULE) && (defined APP_PHY11_RESET_PIN) && (defined APP_PHY11_RESET_MODULE)
{
EC_SLV_APP_boardPhyReset(pAppInstance_p, 0, false);
EC_SLV_APP_boardPhyReset(pAppInstance_p, 1, false);
}
#endif
#endif
Exit:
return;
}
{
if (!pAppInstance_p)
{
goto Exit;
}
#if !(defined DPRAM_REMOTE) && !(defined FBTL_REMOTE)
#if !(defined TIESC_PHYADDR_0) && !(defined TIESC_PHYADDR_1) && !(defined TIESC_PHYADDR_2) && !(defined TIESC_PHYADDR_3)
#error "EtherCAT without PHY is useless"
#endif
#if (defined TIESC_PHYADDR_0) && (defined TIESC_PHYADDR_1)
{
}
#endif
#if (defined TIESC_PHYADDR_2) && (defined TIESC_PHYADDR_3)
{
}
#endif
#if (defined TIESC_PHYADDR_4) && (defined TIESC_PHYADDR_5)
{
}
#endif
#endif
Exit:
return;
}
void EC_SLV_APP_dumpVersions(void)
{
uint32_t dwVersion = 0;
uint32_t dwStrVersRd = 0;
char strVersion[200] = {0};
char strGit[200] = {0};
dwVersion = EC_API_SLV_getVersion();
OSAL_printf("\r\n*********************************************************************\r\n");
OSAL_printf("Numeric Version: 0x%08X\r\n", dwVersion);
{
OSAL_printf("Friendly Version: <%s>\r\n", strVersion);
}
{
OSAL_printf("Source Id: <%s>\r\n", strGit);
}
OSAL_printf("*********************************************************************\r\n");
}
{
if (!pAppInstance_p)
{
goto Exit;
}
{
OSAL_error(__FILE__, __LINE__, OSAL_CONTAINER_NOMEMORY, true, 0);
goto Exit;
}
error = EC_SLV_APP_populateBoardFunctions(pAppInstance_p);
{
OSAL_printf("Populate board functions Error code: 0x%08x\r\n", error);
goto Exit;
}
error = EC_SLV_APP_populateSlaveInfo(pAppInstance_p);
{
OSAL_printf("Create Slave Info Error code: 0x%08x\r\n", error);
goto Exit;
}
error = EC_SLV_APP_populateCiA402Functions(pAppInstance_p);
{
OSAL_printf("Create slave information error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateRxPDO(pAppInstance_p, pAppInstance_p->
ptRxPdo1600, AXIS_ONE);
{
OSAL_printf("Create RxPDO error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateRxPDO(pAppInstance_p, pAppInstance_p->
ptRxPdo1601, AXIS_TWO);
{
OSAL_printf("Create RxPDO error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateRxPDO(pAppInstance_p, pAppInstance_p->
ptRxPdo1602, AXIS_THREE);
{
OSAL_printf("Create RxPDO error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateTxPDO(pAppInstance_p, pAppInstance_p->
ptTxPdo1A00, AXIS_ONE);
{
OSAL_printf("Create TxPDO error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateTxPDO(pAppInstance_p, pAppInstance_p->
ptTxPdo1A01, AXIS_TWO);
{
OSAL_printf("Create TxPDO error code: 0x%08x\r\n", error);
return;
}
error = EC_SLV_APP_populateTxPDO(pAppInstance_p, pAppInstance_p->
ptTxPdo1A02, AXIS_THREE);
{
OSAL_printf("Create TxPDO error code: 0x%08x\r\n", error);
return;
}
#if !(defined DPRAM_REMOTE) && !(defined FBTL_REMOTE)
#else
OSAL_SCHED_startTask(EC_SLV_APP_usrAppRunWrapper, pAppInstance_p, OSAL_TASK_ePRIO_Idle
,(uint8_t*)EC_SLV_APP_appRunWrapTaskStack_g, APPRWRAP_TASK_SIZE_BYTE, 0, "AppRun");
#endif
{
OSAL_printf("Slave Init Error Code: 0x%08x\r\n", error);
return;
}
Exit:
return;
}
{
if (pAppInstance_p)
{
}
}
void EC_SLV_APP_applicationRun(void* pAppRunCtxt_p)
{
OSALUNREF_PARM(pAppRunCtxt_p);
}