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


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 27 2020 03:48:21