linux/simple_test/simple_test.c
Go to the documentation of this file.
1 
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/time.h>
15 #include <unistd.h>
16 #include <pthread.h>
17 
18 #include "ethercattype.h"
19 #include "nicdrv.h"
20 #include "ethercatbase.h"
21 #include "ethercatmain.h"
22 #include "ethercatdc.h"
23 #include "ethercatcoe.h"
24 #include "ethercatfoe.h"
25 #include "ethercatconfig.h"
26 #include "ethercatprint.h"
27 
28 #define EC_TIMEOUTMON 500
29 
30 char IOmap[4096];
31 pthread_t thread1;
33 boolean needlf;
34 volatile int wkc;
35 boolean inOP;
37 
38 void simpletest(char *ifname)
39 {
40  int i, j, oloop, iloop, wkc_count, chk;
41  needlf = FALSE;
42  inOP = FALSE;
43 
44  printf("Starting simple test\n");
45 
46  /* initialise SOEM, bind socket to ifname */
47  if (ec_init(ifname))
48  {
49  printf("ec_init on %s succeeded.\n",ifname);
50  /* find and auto-config slaves */
51 
52 
53  if ( ec_config_init(FALSE) > 0 )
54  {
55  printf("%d slaves found and configured.\n",ec_slavecount);
56 
58 
59  ec_configdc();
60 
61  printf("Slaves mapped, state to SAFE_OP.\n");
62  /* wait for all slaves to reach SAFE_OP state */
64 
65  oloop = ec_slave[0].Obytes;
66  if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
67  if (oloop > 8) oloop = 8;
68  iloop = ec_slave[0].Ibytes;
69  if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
70  if (iloop > 8) iloop = 8;
71 
72  printf("segments : %d : %d %d %d %d\n",ec_group[0].nsegments ,ec_group[0].IOsegment[0],ec_group[0].IOsegment[1],ec_group[0].IOsegment[2],ec_group[0].IOsegment[3]);
73 
74  printf("Request operational state for all slaves\n");
75  expectedWKC = (ec_group[0].outputsWKC * 2) + ec_group[0].inputsWKC;
76  printf("Calculated workcounter %d\n", expectedWKC);
78  /* send one valid process data to make outputs in slaves happy*/
81  /* request OP state for all slaves */
82  ec_writestate(0);
83  chk = 40;
84  /* wait for all slaves to reach OP state */
85  do
86  {
90  }
91  while (chk-- && (ec_slave[0].state != EC_STATE_OPERATIONAL));
92  if (ec_slave[0].state == EC_STATE_OPERATIONAL )
93  {
94  printf("Operational state reached for all slaves.\n");
95  wkc_count = 0;
96  inOP = TRUE;
97  /* cyclic loop */
98  for(i = 1; i <= 10000; i++)
99  {
102 
103  if(wkc >= expectedWKC)
104  {
105  printf("Processdata cycle %4d, WKC %d , O:", i, wkc);
106 
107  for(j = 0 ; j < oloop; j++)
108  {
109  printf(" %2.2x", *(ec_slave[0].outputs + j));
110  }
111 
112  printf(" I:");
113  for(j = 0 ; j < iloop; j++)
114  {
115  printf(" %2.2x", *(ec_slave[0].inputs + j));
116  }
117  printf(" T:%lld\r",ec_DCtime);
118  needlf = TRUE;
119  }
120  usleep(5000);
121 
122  }
123  inOP = FALSE;
124  }
125  else
126  {
127  printf("Not all slaves reached operational state.\n");
128  ec_readstate();
129  for(i = 1; i<=ec_slavecount ; i++)
130  {
131  if(ec_slave[i].state != EC_STATE_OPERATIONAL)
132  {
133  printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
134  i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
135  }
136  }
137  }
138  printf("\nRequest init state for all slaves\n");
140  /* request INIT state for all slaves */
141  ec_writestate(0);
142  }
143  else
144  {
145  printf("No slaves found!\n");
146  }
147  printf("End simple test, close socket\n");
148  /* stop SOEM, close socket */
149  ec_close();
150  }
151  else
152  {
153  printf("No socket connection on %s\nExcecute as root\n",ifname);
154  }
155 }
156 
157 void ecatcheck( void *ptr )
158 {
159  int slave;
160 
161  while(1)
162  {
163  if( inOP && ((wkc < expectedWKC) || ec_group[currentgroup].docheckstate))
164  {
165  if (needlf)
166  {
167  needlf = FALSE;
168  printf("\n");
169  }
170  /* one ore more slaves are not responding */
172  ec_readstate();
173  for (slave = 1; slave <= ec_slavecount; slave++)
174  {
175  if ((ec_slave[slave].group == currentgroup) && (ec_slave[slave].state != EC_STATE_OPERATIONAL))
176  {
178  if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
179  {
180  printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
182  ec_writestate(slave);
183  }
184  else if(ec_slave[slave].state == EC_STATE_SAFE_OP)
185  {
186  printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
188  ec_writestate(slave);
189  }
190  else if(ec_slave[slave].state > 0)
191  {
192  if (ec_reconfig_slave(slave, EC_TIMEOUTMON))
193  {
195  printf("MESSAGE : slave %d reconfigured\n",slave);
196  }
197  }
198  else if(!ec_slave[slave].islost)
199  {
200  /* re-check state */
202  if (!ec_slave[slave].state)
203  {
205  printf("ERROR : slave %d lost\n",slave);
206  }
207  }
208  }
209  if (ec_slave[slave].islost)
210  {
211  if(!ec_slave[slave].state)
212  {
213  if (ec_recover_slave(slave, EC_TIMEOUTMON))
214  {
216  printf("MESSAGE : slave %d recovered\n",slave);
217  }
218  }
219  else
220  {
222  printf("MESSAGE : slave %d found\n",slave);
223  }
224  }
225  }
226  if(!ec_group[currentgroup].docheckstate)
227  printf("OK : all slaves resumed OPERATIONAL.\n");
228  }
229  usleep(10000);
230  }
231 }
232 
233 int main(int argc, char *argv[])
234 {
235  int iret1;
236  printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n");
237 
238  if (argc > 1)
239  {
240  /* create thread to handle slave error handling in OP */
241  iret1 = pthread_create( &thread1, NULL, (void *) &ecatcheck, (void*) &ctime);
242  /* start cyclic part */
243  simpletest(argv[1]);
244  }
245  else
246  {
247  printf("Usage: simple_test ifname1\nifname = eth0 for example\n");
248  }
249 
250  printf("End program\n");
251  return (0);
252 }
ec_groupt ec_group[EC_MAXGROUP]
Definition: ethercatmain.c:108
int ec_reconfig_slave(uint16 slave, int timeout)
char * ec_ALstatuscode2string(uint16 ALstatuscode)
uint32 Obytes
Definition: ethercatmain.h:160
Headerfile for ethercatdc.c.
int ec_readstate(void)
int ec_send_processdata(void)
#define EC_TIMEOUTSTATE
Definition: ethercattype.h:102
PACKED_END ec_slavet ec_slave[EC_MAXSLAVE]
Definition: ethercatmain.c:104
uint8_t uint8
Definition: osal.h:33
#define EC_TIMEOUTMON
int ec_recover_slave(uint16 slave, int timeout)
int ec_receive_processdata(int timeout)
int64 ec_DCtime
Definition: ethercatmain.c:132
General typedefs and defines for EtherCAT.
void ecatcheck(void *ptr)
Headerfile for ethercatcoe.c.
#define TRUE
Definition: osal.h:28
int ec_config_map(void *pIOmap)
boolean islost
Definition: ethercatmain.h:260
void simpletest(char *ifname)
int ec_init(char *ifname)
Headerfile for ethercatfoe.c.
boolean docheckstate
Definition: ethercatmain.h:299
int slave
Definition: aliastool.c:51
#define FALSE
Definition: osal.h:29
int main(int argc, char *argv[])
char IOmap[4096]
#define EC_TIMEOUTRET
Definition: ethercattype.h:90
int ec_writestate(uint16 slave)
Headerfile for ethercatbase.c.
volatile int wkc
boolean ec_configdc(void)
Definition: ethercatdc.c:456
uint16 outputsWKC
Definition: ethercatmain.h:295
Headerfile for ethercatmain.c.
uint32 Ibytes
Definition: ethercatmain.h:168
uint16 state
Definition: ethercatmain.h:140
pthread_t thread1
void ec_close(void)
uint16 ec_statecheck(uint16 slave, uint16 reqstate, int timeout)
int ec_slavecount
Definition: ethercatmain.c:106
Headerfile for ethercatconfig.c.
int ec_config_init(uint8 usetable)
Headerfile for ethercatprint.c.


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 8 2019 18:02:17