SOEM
test
linux
simple_test
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];
21
OSAL_THREAD_HANDLE
thread1
;
22
int
expectedWKC
;
23
boolean
needlf
;
24
volatile
int
wkc
;
25
boolean
inOP
;
26
uint8
currentgroup
= 0;
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
47
ec_config_map
(&
IOmap
);
48
49
ec_configdc
();
50
51
printf(
"Slaves mapped, state to SAFE_OP.\n"
);
52
/* wait for all slaves to reach SAFE_OP state */
53
ec_statecheck
(0,
EC_STATE_SAFE_OP
,
EC_TIMEOUTSTATE
* 4);
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
);
67
ec_slave
[0].
state
=
EC_STATE_OPERATIONAL
;
68
/* send one valid process data to make outputs in slaves happy*/
69
ec_send_processdata
();
70
ec_receive_processdata
(
EC_TIMEOUTRET
);
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
{
77
ec_send_processdata
();
78
ec_receive_processdata
(
EC_TIMEOUTRET
);
79
ec_statecheck
(0,
EC_STATE_OPERATIONAL
, 50000);
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
{
89
ec_send_processdata
();
90
wkc
=
ec_receive_processdata
(
EC_TIMEOUTRET
);
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"
);
128
ec_slave
[0].
state
=
EC_STATE_INIT
;
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
146
OSAL_THREAD_FUNC
ecatcheck
(
void
*ptr )
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 */
161
ec_group
[
currentgroup
].
docheckstate
=
FALSE
;
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
{
167
ec_group
[
currentgroup
].
docheckstate
=
TRUE
;
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
);
171
ec_slave
[
slave
].
state
= (
EC_STATE_SAFE_OP
+
EC_STATE_ACK
);
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
);
177
ec_slave
[
slave
].
state
=
EC_STATE_OPERATIONAL
;
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
{
184
ec_slave
[
slave
].
islost
=
FALSE
;
185
printf(
"MESSAGE : slave %d reconfigured\n"
,
slave
);
186
}
187
}
188
else
if
(!
ec_slave
[
slave
].islost)
189
{
190
/* re-check state */
191
ec_statecheck
(
slave
,
EC_STATE_OPERATIONAL
,
EC_TIMEOUTRET
);
192
if
(
ec_slave
[
slave
].state ==
EC_STATE_NONE
)
193
{
194
ec_slave
[
slave
].
islost
=
TRUE
;
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
{
205
ec_slave
[
slave
].
islost
=
FALSE
;
206
printf(
"MESSAGE : slave %d recovered\n"
,
slave
);
207
}
208
}
209
else
210
{
211
ec_slave
[
slave
].
islost
=
FALSE
;
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
}
ec_configdc
boolean ec_configdc(void)
Definition:
ethercatdc.c:444
slave
int slave
Definition:
aliastool.c:44
simpletest
void simpletest(char *ifname)
Definition:
linux/simple_test/simple_test.c:28
ec_slave::islost
boolean islost
Definition:
ethercatmain.h:229
currentgroup
uint8 currentgroup
Definition:
linux/simple_test/simple_test.c:26
ec_readstate
int ec_readstate(void)
Definition:
ethercatmain.c:2138
ec_close
void ec_close(void)
Definition:
ethercatmain.c:2048
ecatcheck
OSAL_THREAD_FUNC ecatcheck(void *ptr)
Definition:
linux/simple_test/simple_test.c:146
ec_send_processdata
int ec_send_processdata(void)
Definition:
ethercatmain.c:2383
ethercat.h
Headerfile for all ethercat headers.
ec_init
int ec_init(const char *ifname)
Definition:
ethercatmain.c:2029
ec_group
Definition:
ethercatmain.h:239
wkc
volatile int wkc
Definition:
linux/simple_test/simple_test.c:24
thread1
OSAL_THREAD_HANDLE thread1
Definition:
linux/simple_test/simple_test.c:21
ec_slave::Ibytes
uint32 Ibytes
Definition:
ethercatmain.h:137
ec_group::outputsWKC
uint16 outputsWKC
Definition:
ethercatmain.h:266
ec_DCtime
int64 ec_DCtime
Definition:
ethercatmain.c:95
uint8
uint8_t uint8
Definition:
osal.h:28
EC_STATE_SAFE_OP
@ EC_STATE_SAFE_OP
Definition:
ethercattype.h:166
PRId64
#define PRId64
Definition:
inttypes.h:78
j
int j
Definition:
linux/firm_update/firm_update.c:31
ec_slave::state
uint16 state
Definition:
ethercatmain.h:109
EC_TIMEOUTMON
#define EC_TIMEOUTMON
Definition:
linux/simple_test/simple_test.c:18
ec_config_init
int ec_config_init(uint8 usetable)
Definition:
ethercatconfig.c:1606
EC_TIMEOUTRET
#define EC_TIMEOUTRET
Definition:
ethercattype.h:64
ec_recover_slave
int ec_recover_slave(uint16 slave, int timeout)
Definition:
ethercatconfig.c:1700
ec_group::docheckstate
boolean docheckstate
Definition:
ethercatmain.h:270
main
int main(int argc, char *argv[])
Definition:
linux/simple_test/simple_test.c:223
ec_reconfig_slave
int ec_reconfig_slave(uint16 slave, int timeout)
Definition:
ethercatconfig.c:1712
inOP
boolean inOP
Definition:
linux/simple_test/simple_test.c:25
EC_STATE_INIT
@ EC_STATE_INIT
Definition:
ethercattype.h:160
OSAL_THREAD_FUNC
#define OSAL_THREAD_FUNC
Definition:
intime/osal_defs.h:36
osal_usleep
int osal_usleep(uint32 usec)
Definition:
erika/osal.c:22
TRUE
#define TRUE
Definition:
osal.h:19
ec_writestate
int ec_writestate(uint16 slave)
Definition:
ethercatmain.c:2149
IOmap
char IOmap[4096]
Definition:
linux/simple_test/simple_test.c:20
ec_slave
Definition:
ethercatmain.h:106
FALSE
#define FALSE
Definition:
osal.h:22
EC_STATE_ERROR
@ EC_STATE_ERROR
Definition:
ethercattype.h:171
EC_TIMEOUTSTATE
#define EC_TIMEOUTSTATE
Definition:
ethercattype.h:76
ec_slavecount
int ec_slavecount
Definition:
ethercatmain.c:69
OSAL_THREAD_HANDLE
#define OSAL_THREAD_HANDLE
Definition:
intime/osal_defs.h:35
ec_statecheck
uint16 ec_statecheck(uint16 slave, uint16 reqstate, int timeout)
Definition:
ethercatmain.c:2162
ec_receive_processdata
int ec_receive_processdata(int timeout)
Definition:
ethercatmain.c:2393
ec_ALstatuscode2string
char * ec_ALstatuscode2string(uint16 ALstatuscode)
Definition:
ethercatprint.c:245
ec_config_map
int ec_config_map(void *pIOmap)
Definition:
ethercatconfig.c:1643
inttypes.h
expectedWKC
int expectedWKC
Definition:
linux/simple_test/simple_test.c:22
ec_slave::Obytes
uint32 Obytes
Definition:
ethercatmain.h:129
EC_STATE_NONE
@ EC_STATE_NONE
Definition:
ethercattype.h:158
osal_thread_create
int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
Definition:
linux/osal.c:102
EC_STATE_OPERATIONAL
@ EC_STATE_OPERATIONAL
Definition:
ethercattype.h:168
needlf
boolean needlf
Definition:
linux/simple_test/simple_test.c:23
EC_STATE_ACK
@ EC_STATE_ACK
Definition:
ethercattype.h:170
soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Wed Mar 2 2022 01:01:49