mirror of
https://github.com/OpenEtherCATsociety/SOEM.git
synced 2026-02-06 09:03:10 +08:00
Use the new osal functions to let tests build on all supported platforms. Change-Id: I6519ee55588e7bf0dff8d412679db268abbe86ce
401 lines
12 KiB
C
401 lines
12 KiB
C
/** \file
|
|
* \brief Example code for Simple Open EtherCAT master
|
|
*
|
|
* Usage : red_test [ifname1] [ifname2] [cycletime]
|
|
* ifname is NIC interface, f.e. eth0
|
|
* cycletime in us, f.e. 500
|
|
*
|
|
* This is a redundancy test.
|
|
*
|
|
* (c)Arthur Ketels 2008
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <inttypes.h>
|
|
|
|
#include "soem/soem.h"
|
|
|
|
#define NSEC_PER_SEC 1000000000
|
|
#define EC_TIMEOUTMON 500
|
|
|
|
char IOmap[4096];
|
|
OSAL_THREAD_HANDLE thread1;
|
|
OSAL_THREAD_HANDLE thread2;
|
|
int expectedWKC;
|
|
boolean needlf;
|
|
volatile int wkc;
|
|
boolean inOP;
|
|
uint8 currentgroup = 0;
|
|
boolean forceByteAlignment = FALSE;
|
|
uint8 *digout = NULL;
|
|
int dorun = 0;
|
|
int64 toff, gl_delta;
|
|
|
|
static ec_slavet ec_slave[EC_MAXSLAVE];
|
|
static int ec_slavecount;
|
|
static ec_groupt ec_group[EC_MAXGROUP];
|
|
static uint8 ec_esibuf[EC_MAXEEPBUF];
|
|
static uint32 ec_esimap[EC_MAXEEPBITMAP];
|
|
static ec_eringt ec_elist;
|
|
static ec_idxstackT ec_idxstack;
|
|
static ec_SMcommtypet ec_SMcommtype[EC_MAX_MAPT];
|
|
static ec_PDOassignt ec_PDOassign[EC_MAX_MAPT];
|
|
static ec_PDOdesct ec_PDOdesc[EC_MAX_MAPT];
|
|
static ec_eepromSMt ec_SM;
|
|
static ec_eepromFMMUt ec_FMMU;
|
|
static boolean EcatError = FALSE;
|
|
static int64 ec_DCtime;
|
|
static ecx_portt ecx_port;
|
|
static ec_mbxpoolt ec_mbxpool;
|
|
|
|
static ecx_contextt ctx = {
|
|
.port = &ecx_port,
|
|
.slavelist = &ec_slave[0],
|
|
.slavecount = &ec_slavecount,
|
|
.maxslave = EC_MAXSLAVE,
|
|
.grouplist = &ec_group[0],
|
|
.maxgroup = EC_MAXGROUP,
|
|
.esibuf = &ec_esibuf[0],
|
|
.esimap = &ec_esimap[0],
|
|
.esislave = 0,
|
|
.elist = &ec_elist,
|
|
.idxstack = &ec_idxstack,
|
|
.ecaterror = &EcatError,
|
|
.DCtime = &ec_DCtime,
|
|
.SMcommtype = &ec_SMcommtype[0],
|
|
.PDOassign = &ec_PDOassign[0],
|
|
.PDOdesc = &ec_PDOdesc[0],
|
|
.eepSM = &ec_SM,
|
|
.eepFMMU = &ec_FMMU,
|
|
.mbxpool = &ec_mbxpool,
|
|
.FOEhook = NULL,
|
|
.EOEhook = NULL,
|
|
.manualstatechange = 0,
|
|
.userdata = NULL,
|
|
};
|
|
|
|
void redtest(char *ifname, char *ifname2)
|
|
{
|
|
int cnt, i, j, oloop, iloop;
|
|
|
|
printf("Starting Redundant test\n");
|
|
|
|
/* initialise SOEM, bind socket to ifname */
|
|
(void)ifname2;
|
|
// if (ecx_init_redundant(&ctx, ifname, ifname2))
|
|
if (ecx_init(&ctx, ifname))
|
|
{
|
|
printf("ecx_init on %s succeeded.\n", ifname);
|
|
|
|
/* find and auto-config slaves */
|
|
if (ecx_config_init(&ctx, FALSE) > 0)
|
|
{
|
|
if (forceByteAlignment)
|
|
{
|
|
ecx_config_map_group_aligned(&ctx, &IOmap, 0);
|
|
}
|
|
else
|
|
{
|
|
ecx_config_map_group(&ctx, &IOmap, 0);
|
|
}
|
|
|
|
printf("%d slaves found and configured.\n", ec_slavecount);
|
|
|
|
/* wait for all slaves to reach SAFE_OP state */
|
|
ecx_statecheck(&ctx, 0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
|
|
|
|
ecx_configdc(&ctx);
|
|
|
|
/* read indevidual slave state and store in ec_slave[] */
|
|
ecx_readstate(&ctx);
|
|
for (cnt = 1; cnt <= ec_slavecount; cnt++)
|
|
{
|
|
printf("Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
|
|
cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
|
|
ec_slave[cnt].state, (int)ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
|
|
printf(" Out:%p,%4d In:%p,%4d\n",
|
|
ec_slave[cnt].outputs, ec_slave[cnt].Obytes, ec_slave[cnt].inputs, ec_slave[cnt].Ibytes);
|
|
/* check for EL2004 or EL2008 */
|
|
if (!digout && ((ec_slave[cnt].eep_id == 0x0af83052) || (ec_slave[cnt].eep_id == 0x07d83052)))
|
|
{
|
|
digout = ec_slave[cnt].outputs;
|
|
}
|
|
}
|
|
expectedWKC = (ec_group[0].outputsWKC * 2) + ec_group[0].inputsWKC;
|
|
printf("Calculated workcounter %d\n", expectedWKC);
|
|
|
|
printf("Request operational state for all slaves\n");
|
|
ec_slave[0].state = EC_STATE_OPERATIONAL;
|
|
/* request OP state for all slaves */
|
|
ecx_writestate(&ctx, 0);
|
|
/* activate cyclic process data */
|
|
dorun = 1;
|
|
/* wait for all slaves to reach OP state */
|
|
ecx_statecheck(&ctx, 0, EC_STATE_OPERATIONAL, 5 * EC_TIMEOUTSTATE);
|
|
oloop = ec_slave[0].Obytes;
|
|
if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
|
|
if (oloop > 8) oloop = 8;
|
|
iloop = ec_slave[0].Ibytes;
|
|
if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
|
|
if (iloop > 8) iloop = 8;
|
|
if (ec_slave[0].state == EC_STATE_OPERATIONAL)
|
|
{
|
|
printf("Operational state reached for all slaves.\n");
|
|
inOP = TRUE;
|
|
/* acyclic loop 5000 x 20ms = 10s */
|
|
for (i = 1; i <= 5000; i++)
|
|
{
|
|
printf("Processdata cycle %5d , Wck %3d, DCtime %12" PRId64 ", dt %12" PRId64 ", O:",
|
|
dorun, wkc, ec_DCtime, gl_delta);
|
|
for (j = 0; j < oloop; j++)
|
|
{
|
|
printf(" %2.2x", *(ec_slave[0].outputs + j));
|
|
}
|
|
printf(" I:");
|
|
for (j = 0; j < iloop; j++)
|
|
{
|
|
printf(" %2.2x", *(ec_slave[0].inputs + j));
|
|
}
|
|
printf("\r");
|
|
fflush(stdout);
|
|
osal_usleep(20000);
|
|
}
|
|
dorun = 0;
|
|
inOP = FALSE;
|
|
}
|
|
else
|
|
{
|
|
printf("Not all slaves reached operational state.\n");
|
|
ecx_readstate(&ctx);
|
|
for (i = 1; i <= ec_slavecount; i++)
|
|
{
|
|
if (ec_slave[i].state != EC_STATE_OPERATIONAL)
|
|
{
|
|
printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
|
|
i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
|
|
}
|
|
}
|
|
}
|
|
printf("Request safe operational state for all slaves\n");
|
|
ec_slave[0].state = EC_STATE_SAFE_OP;
|
|
/* request SAFE_OP state for all slaves */
|
|
ecx_writestate(&ctx, 0);
|
|
}
|
|
else
|
|
{
|
|
printf("No slaves found!\n");
|
|
}
|
|
printf("End redundant test, close socket\n");
|
|
/* stop SOEM, close socket */
|
|
ecx_close(&ctx);
|
|
}
|
|
else
|
|
{
|
|
printf("No socket connection on %s\nExcecute as root\n", ifname);
|
|
}
|
|
}
|
|
|
|
/* add ns to ec_timet */
|
|
void add_time_ns(ec_timet *ts, int64 addtime)
|
|
{
|
|
ec_timet addts;
|
|
|
|
addts.tv_nsec = addtime % NSEC_PER_SEC;
|
|
addts.tv_sec = (addtime - addts.tv_nsec) / NSEC_PER_SEC;
|
|
osal_timespecadd(ts, &addts, ts);
|
|
}
|
|
|
|
/* PI calculation to get linux time synced to DC time */
|
|
void ec_sync(int64 reftime, int64 cycletime, int64 *offsettime)
|
|
{
|
|
static int64 integral = 0;
|
|
int64 delta;
|
|
/* set linux sync point 50us later than DC sync, just as example */
|
|
delta = (reftime - 50000) % cycletime;
|
|
if (delta > (cycletime / 2))
|
|
{
|
|
delta = delta - cycletime;
|
|
}
|
|
if (delta > 0)
|
|
{
|
|
integral++;
|
|
}
|
|
if (delta < 0)
|
|
{
|
|
integral--;
|
|
}
|
|
*offsettime = -(delta / 100) - (integral / 20);
|
|
gl_delta = delta;
|
|
}
|
|
|
|
/* RT EtherCAT thread */
|
|
OSAL_THREAD_FUNC_RT ecatthread(void *ptr)
|
|
{
|
|
ec_timet ts;
|
|
int ht;
|
|
int64 cycletime;
|
|
|
|
osal_get_monotonic_time(&ts);
|
|
ht = (ts.tv_nsec / 1000000) + 1; /* round to nearest ms */
|
|
ts.tv_nsec = ht * 1000000;
|
|
if (ts.tv_nsec >= NSEC_PER_SEC)
|
|
{
|
|
ts.tv_sec++;
|
|
ts.tv_nsec -= NSEC_PER_SEC;
|
|
}
|
|
cycletime = *(int *)ptr * 1000; /* cycletime in ns */
|
|
toff = 0;
|
|
dorun = 0;
|
|
ecx_send_processdata(&ctx);
|
|
while (1)
|
|
{
|
|
/* calculate next cycle start */
|
|
add_time_ns(&ts, cycletime + toff);
|
|
/* wait to cycle start */
|
|
osal_monotonic_sleep(&ts);
|
|
if (dorun > 0)
|
|
{
|
|
wkc = ecx_receive_processdata(&ctx, EC_TIMEOUTRET);
|
|
|
|
dorun++;
|
|
/* if we have some digital output, cycle */
|
|
if (digout) *digout = (uint8)((dorun / 16) & 0xff);
|
|
|
|
if (ec_slave[0].hasdc)
|
|
{
|
|
/* calulate toff to get linux time and DC synced */
|
|
ec_sync(ec_DCtime, cycletime, &toff);
|
|
}
|
|
ecx_send_processdata(&ctx);
|
|
}
|
|
}
|
|
}
|
|
|
|
OSAL_THREAD_FUNC ecatcheck(void *ptr)
|
|
{
|
|
int slave;
|
|
(void)ptr; /* Not used */
|
|
|
|
while (1)
|
|
{
|
|
if (inOP && ((wkc < expectedWKC) || ec_group[currentgroup].docheckstate))
|
|
{
|
|
if (needlf)
|
|
{
|
|
needlf = FALSE;
|
|
printf("\n");
|
|
}
|
|
/* one ore more slaves are not responding */
|
|
ec_group[currentgroup].docheckstate = FALSE;
|
|
ecx_readstate(&ctx);
|
|
for (slave = 1; slave <= ec_slavecount; slave++)
|
|
{
|
|
if ((ec_slave[slave].group == currentgroup) && (ec_slave[slave].state != EC_STATE_OPERATIONAL))
|
|
{
|
|
ec_group[currentgroup].docheckstate = TRUE;
|
|
if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
|
|
{
|
|
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
|
|
ec_slave[slave].state = (EC_STATE_SAFE_OP + EC_STATE_ACK);
|
|
ecx_writestate(&ctx, slave);
|
|
}
|
|
else if (ec_slave[slave].state == EC_STATE_SAFE_OP)
|
|
{
|
|
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
|
|
ec_slave[slave].state = EC_STATE_OPERATIONAL;
|
|
if (ec_slave[slave].mbxhandlerstate == ECT_MBXH_LOST) ec_slave[slave].mbxhandlerstate = ECT_MBXH_CYCLIC;
|
|
ecx_writestate(&ctx, slave);
|
|
}
|
|
else if (ec_slave[slave].state > EC_STATE_NONE)
|
|
{
|
|
if (ecx_reconfig_slave(&ctx, slave, EC_TIMEOUTMON) >= EC_STATE_PRE_OP)
|
|
{
|
|
ec_slave[slave].islost = FALSE;
|
|
printf("MESSAGE : slave %d reconfigured\n", slave);
|
|
}
|
|
}
|
|
else if (!ec_slave[slave].islost)
|
|
{
|
|
/* re-check state */
|
|
ecx_statecheck(&ctx, slave, EC_STATE_OPERATIONAL, EC_TIMEOUTRET);
|
|
if (ec_slave[slave].state == EC_STATE_NONE)
|
|
{
|
|
ec_slave[slave].islost = TRUE;
|
|
ec_slave[slave].mbxhandlerstate = ECT_MBXH_LOST;
|
|
/* zero input data for this slave */
|
|
if (ec_slave[slave].Ibytes)
|
|
{
|
|
memset(ec_slave[slave].inputs, 0x00, ec_slave[slave].Ibytes);
|
|
printf("zero inputs %p %d\n\r", ec_slave[slave].inputs, ec_slave[slave].Ibytes);
|
|
}
|
|
printf("ERROR : slave %d lost\n", slave);
|
|
}
|
|
}
|
|
}
|
|
if (ec_slave[slave].islost)
|
|
{
|
|
if (ec_slave[slave].state <= EC_STATE_INIT)
|
|
{
|
|
if (ecx_recover_slave(&ctx, slave, EC_TIMEOUTMON))
|
|
{
|
|
ec_slave[slave].islost = FALSE;
|
|
printf("MESSAGE : slave %d recovered\n", slave);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ec_slave[slave].islost = FALSE;
|
|
printf("MESSAGE : slave %d found\n", slave);
|
|
}
|
|
}
|
|
}
|
|
if (!ec_group[currentgroup].docheckstate)
|
|
printf("OK : all slaves resumed OPERATIONAL.\n");
|
|
}
|
|
osal_usleep(10000);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int ctime;
|
|
|
|
printf("SOEM (Simple Open EtherCAT Master)\nRedundancy test\n");
|
|
|
|
if (argc > 3)
|
|
{
|
|
dorun = 0;
|
|
ctime = atoi(argv[3]);
|
|
|
|
/* create RT thread */
|
|
osal_thread_create_rt(&thread1, 128000, &ecatthread, (void *)&ctime);
|
|
|
|
/* create thread to handle slave error handling in OP */
|
|
osal_thread_create(&thread2, 128000, &ecatcheck, NULL);
|
|
|
|
/* start acyclic part */
|
|
redtest(argv[1], argv[2]);
|
|
}
|
|
else
|
|
{
|
|
ec_adaptert *adapter = NULL;
|
|
ec_adaptert *head = NULL;
|
|
printf("Usage: red_test ifname1 ifname2 cycletime\nifname = eth0 for example\ncycletime in us\n");
|
|
|
|
printf("\nAvailable adapters:\n");
|
|
head = adapter = ec_find_adapters();
|
|
while (adapter != NULL)
|
|
{
|
|
printf(" - %s (%s)\n", adapter->name, adapter->desc);
|
|
adapter = adapter->next;
|
|
}
|
|
ec_free_adapters(head);
|
|
}
|
|
|
|
printf("End program\n");
|
|
|
|
return (0);
|
|
}
|