author | Florian Pose <fp@igh-essen.com> |
Mon, 24 Sep 2007 07:54:51 +0000 | |
changeset 754 | 0b47b49c5976 |
parent 739 | 4a02162a6301 |
child 769 | 2195c2ea37b4 |
permissions | -rw-r--r-- |
436 | 1 |
/****************************************************************************** |
2 |
* |
|
3 |
* $Id$ |
|
4 |
* |
|
5 |
* Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH |
|
6 |
* |
|
7 |
* This file is part of the IgH EtherCAT Master. |
|
8 |
* |
|
9 |
* The IgH EtherCAT Master is free software; you can redistribute it |
|
10 |
* and/or modify it under the terms of the GNU General Public License |
|
11 |
* as published by the Free Software Foundation; either version 2 of the |
|
12 |
* License, or (at your option) any later version. |
|
13 |
* |
|
14 |
* The IgH EtherCAT Master is distributed in the hope that it will be |
|
15 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with the IgH EtherCAT Master; if not, write to the Free Software |
|
21 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
22 |
* |
|
23 |
* The right to use EtherCAT Technology is granted and comes free of |
|
24 |
* charge under condition of compatibility of product made by |
|
25 |
* Licensee. People intending to distribute/sell products based on the |
|
26 |
* code, have to sign an agreement to guarantee that products using |
|
27 |
* software based on IgH EtherCAT master stay compatible with the actual |
|
28 |
* EtherCAT specification (which are released themselves as an open |
|
29 |
* standard) as the (only) precondition to have the right to use EtherCAT |
|
30 |
* Technology, IP and trade marks. |
|
31 |
* |
|
32 |
*****************************************************************************/ |
|
33 |
||
34 |
/** |
|
35 |
\file |
|
36 |
EtherCAT CoE state machines. |
|
37 |
*/ |
|
38 |
||
39 |
/*****************************************************************************/ |
|
40 |
||
41 |
#include "globals.h" |
|
42 |
#include "master.h" |
|
43 |
#include "mailbox.h" |
|
44 |
#include "fsm_coe.h" |
|
45 |
||
46 |
/*****************************************************************************/ |
|
47 |
||
48 |
void ec_fsm_coe_dict_start(ec_fsm_coe_t *); |
|
49 |
void ec_fsm_coe_dict_request(ec_fsm_coe_t *); |
|
50 |
void ec_fsm_coe_dict_check(ec_fsm_coe_t *); |
|
51 |
void ec_fsm_coe_dict_response(ec_fsm_coe_t *); |
|
52 |
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *); |
|
53 |
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *); |
|
54 |
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *); |
|
55 |
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *); |
|
56 |
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *); |
|
57 |
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *); |
|
58 |
||
59 |
void ec_fsm_coe_down_start(ec_fsm_coe_t *); |
|
60 |
void ec_fsm_coe_down_request(ec_fsm_coe_t *); |
|
61 |
void ec_fsm_coe_down_check(ec_fsm_coe_t *); |
|
62 |
void ec_fsm_coe_down_response(ec_fsm_coe_t *); |
|
63 |
||
64 |
void ec_fsm_coe_up_start(ec_fsm_coe_t *); |
|
65 |
void ec_fsm_coe_up_request(ec_fsm_coe_t *); |
|
66 |
void ec_fsm_coe_up_check(ec_fsm_coe_t *); |
|
67 |
void ec_fsm_coe_up_response(ec_fsm_coe_t *); |
|
68 |
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *); |
|
69 |
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *); |
|
70 |
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *); |
|
71 |
||
72 |
void ec_fsm_coe_end(ec_fsm_coe_t *); |
|
73 |
void ec_fsm_coe_error(ec_fsm_coe_t *); |
|
74 |
||
75 |
/*****************************************************************************/ |
|
76 |
||
77 |
/** |
|
78 |
SDO abort messages. |
|
79 |
The "abort SDO transfer request" supplies an abort code, |
|
80 |
which can be translated to clear text. This table does |
|
81 |
the mapping of the codes and messages. |
|
82 |
*/ |
|
83 |
||
84 |
const ec_code_msg_t sdo_abort_messages[] = { |
|
85 |
{0x05030000, "Toggle bit not changed"}, |
|
86 |
{0x05040000, "SDO protocol timeout"}, |
|
87 |
{0x05040001, "Client/Server command specifier not valid or unknown"}, |
|
88 |
{0x05040005, "Out of memory"}, |
|
89 |
{0x06010000, "Unsupported access to an object"}, |
|
90 |
{0x06010001, "Attempt to read a write-only object"}, |
|
91 |
{0x06010002, "Attempt to write a read-only object"}, |
|
92 |
{0x06020000, "This object does not exist in the object directory"}, |
|
93 |
{0x06040041, "The object cannot be mapped into the PDO"}, |
|
94 |
{0x06040042, "The number and length of the objects to be mapped would" |
|
95 |
" exceed the PDO length"}, |
|
96 |
{0x06040043, "General parameter incompatibility reason"}, |
|
97 |
{0x06040047, "Gerneral internal incompatibility in device"}, |
|
98 |
{0x06060000, "Access failure due to a hardware error"}, |
|
99 |
{0x06070010, "Data type does not match, length of service parameter does" |
|
100 |
" not match"}, |
|
101 |
{0x06070012, "Data type does not match, length of service parameter too" |
|
102 |
" high"}, |
|
103 |
{0x06070013, "Data type does not match, length of service parameter too" |
|
104 |
" low"}, |
|
105 |
{0x06090011, "Subindex does not exist"}, |
|
106 |
{0x06090030, "Value range of parameter exceeded"}, |
|
107 |
{0x06090031, "Value of parameter written too high"}, |
|
108 |
{0x06090032, "Value of parameter written too low"}, |
|
109 |
{0x06090036, "Maximum value is less than minimum value"}, |
|
110 |
{0x08000000, "General error"}, |
|
111 |
{0x08000020, "Data cannot be transferred or stored to the application"}, |
|
112 |
{0x08000021, "Data cannot be transferred or stored to the application" |
|
113 |
" because of local control"}, |
|
114 |
{0x08000022, "Data cannot be transferred or stored to the application" |
|
115 |
" because of the present device state"}, |
|
116 |
{0x08000023, "Object dictionary dynamic generation fails or no object" |
|
117 |
" dictionary is present"}, |
|
118 |
{} |
|
119 |
}; |
|
120 |
||
121 |
/*****************************************************************************/ |
|
122 |
||
123 |
/** |
|
124 |
Outputs an SDO abort message. |
|
125 |
*/ |
|
126 |
||
127 |
void ec_canopen_abort_msg(uint32_t abort_code) |
|
128 |
{ |
|
129 |
const ec_code_msg_t *abort_msg; |
|
130 |
||
131 |
for (abort_msg = sdo_abort_messages; abort_msg->code; abort_msg++) { |
|
132 |
if (abort_msg->code == abort_code) { |
|
133 |
EC_ERR("SDO abort message 0x%08X: \"%s\".\n", |
|
134 |
abort_msg->code, abort_msg->message); |
|
135 |
return; |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
EC_ERR("Unknown SDO abort code 0x%08X.\n", abort_code); |
|
140 |
} |
|
141 |
||
142 |
/*****************************************************************************/ |
|
143 |
||
144 |
/** |
|
145 |
Constructor. |
|
146 |
*/ |
|
147 |
||
148 |
void ec_fsm_coe_init(ec_fsm_coe_t *fsm, /**< finite state machine */ |
|
149 |
ec_datagram_t *datagram /**< datagram */ |
|
150 |
) |
|
151 |
{ |
|
152 |
fsm->state = NULL; |
|
153 |
fsm->datagram = datagram; |
|
154 |
} |
|
155 |
||
156 |
/*****************************************************************************/ |
|
157 |
||
158 |
/** |
|
159 |
Destructor. |
|
160 |
*/ |
|
161 |
||
162 |
void ec_fsm_coe_clear(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
163 |
{ |
|
164 |
} |
|
165 |
||
166 |
/*****************************************************************************/ |
|
167 |
||
168 |
/** |
|
169 |
Starts reading a slaves' SDO dictionary. |
|
170 |
*/ |
|
171 |
||
172 |
void ec_fsm_coe_dictionary(ec_fsm_coe_t *fsm, /**< finite state machine */ |
|
173 |
ec_slave_t *slave /**< EtherCAT slave */ |
|
174 |
) |
|
175 |
{ |
|
176 |
fsm->slave = slave; |
|
177 |
fsm->state = ec_fsm_coe_dict_start; |
|
178 |
} |
|
179 |
||
180 |
/*****************************************************************************/ |
|
181 |
||
182 |
/** |
|
183 |
Starts to download an SDO to a slave. |
|
184 |
*/ |
|
185 |
||
186 |
void ec_fsm_coe_download(ec_fsm_coe_t *fsm, /**< finite state machine */ |
|
187 |
ec_slave_t *slave, /**< EtherCAT slave */ |
|
188 |
ec_sdo_data_t *sdodata /**< SDO data object */ |
|
189 |
) |
|
190 |
{ |
|
191 |
fsm->slave = slave; |
|
192 |
fsm->sdodata = sdodata; |
|
193 |
fsm->state = ec_fsm_coe_down_start; |
|
194 |
} |
|
195 |
||
196 |
/*****************************************************************************/ |
|
197 |
||
198 |
/** |
|
199 |
Starts to upload an SDO from a slave. |
|
200 |
*/ |
|
201 |
||
202 |
void ec_fsm_coe_upload(ec_fsm_coe_t *fsm, /**< finite state machine */ |
|
203 |
ec_slave_t *slave, /**< EtherCAT slave */ |
|
204 |
ec_sdo_request_t *request /**< SDO request */ |
|
205 |
) |
|
206 |
{ |
|
207 |
fsm->slave = slave; |
|
208 |
fsm->request = request; |
|
209 |
fsm->state = ec_fsm_coe_up_start; |
|
210 |
} |
|
211 |
||
212 |
/*****************************************************************************/ |
|
213 |
||
214 |
/** |
|
215 |
Executes the current state of the state machine. |
|
216 |
\return false, if state machine has terminated |
|
217 |
*/ |
|
218 |
||
219 |
int ec_fsm_coe_exec(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
220 |
{ |
|
221 |
fsm->state(fsm); |
|
222 |
||
223 |
return fsm->state != ec_fsm_coe_end && fsm->state != ec_fsm_coe_error; |
|
224 |
} |
|
225 |
||
226 |
/*****************************************************************************/ |
|
227 |
||
228 |
/** |
|
229 |
Returns, if the state machine terminated with success. |
|
230 |
\return non-zero if successful. |
|
231 |
*/ |
|
232 |
||
233 |
int ec_fsm_coe_success(ec_fsm_coe_t *fsm /**< Finite state machine */) |
|
234 |
{ |
|
235 |
return fsm->state == ec_fsm_coe_end; |
|
236 |
} |
|
237 |
||
238 |
/****************************************************************************** |
|
239 |
* CoE dictionary state machine |
|
240 |
*****************************************************************************/ |
|
241 |
||
242 |
/** |
|
243 |
CoE state: DICT START. |
|
244 |
*/ |
|
245 |
||
246 |
void ec_fsm_coe_dict_start(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
247 |
{ |
|
248 |
ec_datagram_t *datagram = fsm->datagram; |
|
249 |
ec_slave_t *slave = fsm->slave; |
|
250 |
uint8_t *data; |
|
251 |
||
252 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { |
|
253 |
fsm->state = ec_fsm_coe_error; |
|
254 |
return; |
|
255 |
} |
|
256 |
||
257 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
258 |
EC_WRITE_U8 (data + 2, 0x01); // Get OD List Request |
|
259 |
EC_WRITE_U8 (data + 3, 0x00); |
|
260 |
EC_WRITE_U16(data + 4, 0x0000); |
|
261 |
EC_WRITE_U16(data + 6, 0x0001); // deliver all SDOs! |
|
262 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
263 |
fsm->retries = EC_FSM_RETRIES; |
436 | 264 |
fsm->state = ec_fsm_coe_dict_request; |
265 |
} |
|
266 |
||
267 |
/*****************************************************************************/ |
|
268 |
||
269 |
/** |
|
270 |
CoE state: DICT REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
271 |
\todo Timeout behavior |
436 | 272 |
*/ |
273 |
||
274 |
void ec_fsm_coe_dict_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
275 |
{ |
|
276 |
ec_datagram_t *datagram = fsm->datagram; |
|
277 |
ec_slave_t *slave = fsm->slave; |
|
278 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
279 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
280 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
281 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
282 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
283 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
284 |
EC_ERR("Failed to receive CoE dictionary request datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
285 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
286 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
287 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
288 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
289 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
290 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
291 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
292 |
EC_ERR("Reception of CoE dictionary request failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
293 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
294 |
ec_datagram_print_wc_error(datagram); |
436 | 295 |
return; |
296 |
} |
|
297 |
||
298 |
fsm->cycles_start = datagram->cycles_sent; |
|
299 |
||
300 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
301 |
fsm->retries = EC_FSM_RETRIES; |
436 | 302 |
fsm->state = ec_fsm_coe_dict_check; |
303 |
} |
|
304 |
||
305 |
/*****************************************************************************/ |
|
306 |
||
307 |
/** |
|
308 |
CoE state: DICT CHECK. |
|
309 |
*/ |
|
310 |
||
311 |
void ec_fsm_coe_dict_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
312 |
{ |
|
313 |
ec_datagram_t *datagram = fsm->datagram; |
|
314 |
ec_slave_t *slave = fsm->slave; |
|
315 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
316 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
317 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
318 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
319 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
320 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
321 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
322 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
323 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
324 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
325 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
326 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
327 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
328 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
329 |
EC_ERR("Reception of CoE mailbox check datagram failed on slave %i: ", |
436 | 330 |
slave->ring_position); |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
331 |
ec_datagram_print_wc_error(datagram); |
436 | 332 |
return; |
333 |
} |
|
334 |
||
335 |
if (!ec_slave_mbox_check(datagram)) { |
|
336 |
if (datagram->cycles_received |
|
337 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
338 |
fsm->state = ec_fsm_coe_error; |
|
339 |
EC_ERR("Timeout while checking SDO dictionary on slave %i.\n", |
|
340 |
slave->ring_position); |
|
341 |
return; |
|
342 |
} |
|
343 |
||
344 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
345 |
fsm->retries = EC_FSM_RETRIES; |
436 | 346 |
return; |
347 |
} |
|
348 |
||
349 |
// Fetch response |
|
350 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
351 |
fsm->retries = EC_FSM_RETRIES; |
436 | 352 |
fsm->state = ec_fsm_coe_dict_response; |
353 |
} |
|
354 |
||
355 |
/*****************************************************************************/ |
|
356 |
||
357 |
/** |
|
358 |
CoE state: DICT RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
359 |
\todo Timeout behavior |
436 | 360 |
*/ |
361 |
||
362 |
void ec_fsm_coe_dict_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
363 |
{ |
|
364 |
ec_datagram_t *datagram = fsm->datagram; |
|
365 |
ec_slave_t *slave = fsm->slave; |
|
366 |
uint8_t *data, mbox_prot; |
|
367 |
size_t rec_size; |
|
368 |
unsigned int sdo_count, i; |
|
369 |
uint16_t sdo_index; |
|
370 |
ec_sdo_t *sdo; |
|
371 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
372 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
373 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
374 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
375 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
376 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
377 |
EC_ERR("Failed to receive CoE dictionary response datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
378 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
379 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
380 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
381 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
382 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
383 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
384 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
385 |
EC_ERR("Reception of CoE dictionary response failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
386 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
387 |
ec_datagram_print_wc_error(datagram); |
436 | 388 |
return; |
389 |
} |
|
390 |
||
391 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
392 |
&mbox_prot, &rec_size))) { |
|
393 |
fsm->state = ec_fsm_coe_error; |
|
394 |
return; |
|
395 |
} |
|
396 |
||
397 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 398 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 399 |
fsm->state = ec_fsm_coe_error; |
400 |
return; |
|
401 |
} |
|
402 |
||
403 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
404 |
(EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response |
|
405 |
EC_ERR("SDO information error response at slave %i!\n", |
|
406 |
slave->ring_position); |
|
407 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
408 |
fsm->state = ec_fsm_coe_error; |
|
409 |
return; |
|
410 |
} |
|
411 |
||
412 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
413 |
(EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response |
|
414 |
EC_ERR("Invalid SDO list response at slave %i!\n", |
|
415 |
slave->ring_position); |
|
416 |
ec_print_data(data, rec_size); |
|
417 |
fsm->state = ec_fsm_coe_error; |
|
418 |
return; |
|
419 |
} |
|
420 |
||
421 |
if (rec_size < 8) { |
|
422 |
EC_ERR("Invalid data size!\n"); |
|
423 |
ec_print_data(data, rec_size); |
|
424 |
fsm->state = ec_fsm_coe_error; |
|
425 |
return; |
|
426 |
} |
|
427 |
||
428 |
sdo_count = (rec_size - 8) / 2; |
|
429 |
||
430 |
for (i = 0; i < sdo_count; i++) { |
|
431 |
sdo_index = EC_READ_U16(data + 8 + i * 2); |
|
432 |
if (!sdo_index) { |
|
461
b9eda9235173
Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
459
diff
changeset
|
433 |
if (slave->master->debug_level) |
b9eda9235173
Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
459
diff
changeset
|
434 |
EC_WARN("SDO dictionary of slave %i contains index 0x0000.\n", |
b9eda9235173
Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
459
diff
changeset
|
435 |
slave->ring_position); |
436 | 436 |
continue; |
437 |
} |
|
438 |
||
439 |
if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_ATOMIC))) { |
|
440 |
EC_ERR("Failed to allocate memory for SDO!\n"); |
|
441 |
fsm->state = ec_fsm_coe_error; |
|
442 |
return; |
|
443 |
} |
|
444 |
||
445 |
if (ec_sdo_init(sdo, sdo_index, slave)) { |
|
446 |
EC_ERR("Failed to init SDO!\n"); |
|
447 |
fsm->state = ec_fsm_coe_error; |
|
448 |
return; |
|
449 |
} |
|
450 |
||
451 |
list_add_tail(&sdo->list, &slave->sdo_dictionary); |
|
452 |
} |
|
453 |
||
454 |
if (EC_READ_U8(data + 2) & 0x80) { // more messages waiting. check again. |
|
455 |
fsm->cycles_start = datagram->cycles_sent; |
|
456 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
457 |
fsm->retries = EC_FSM_RETRIES; |
436 | 458 |
fsm->state = ec_fsm_coe_dict_check; |
459 |
return; |
|
460 |
} |
|
461 |
||
462 |
if (list_empty(&slave->sdo_dictionary)) { |
|
463 |
// no SDOs in dictionary. finished. |
|
464 |
fsm->state = ec_fsm_coe_end; // success |
|
465 |
return; |
|
466 |
} |
|
467 |
||
468 |
// fetch SDO descriptions |
|
469 |
fsm->sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list); |
|
470 |
||
471 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { |
|
472 |
fsm->state = ec_fsm_coe_error; |
|
473 |
return; |
|
474 |
} |
|
475 |
||
476 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
477 |
EC_WRITE_U8 (data + 2, 0x03); // Get object description request |
|
478 |
EC_WRITE_U8 (data + 3, 0x00); |
|
479 |
EC_WRITE_U16(data + 4, 0x0000); |
|
480 |
EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index |
|
481 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
482 |
fsm->retries = EC_FSM_RETRIES; |
436 | 483 |
fsm->state = ec_fsm_coe_dict_desc_request; |
484 |
} |
|
485 |
||
486 |
/*****************************************************************************/ |
|
487 |
||
488 |
/** |
|
489 |
CoE state: DICT DESC REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
490 |
\todo Timeout behavior |
436 | 491 |
*/ |
492 |
||
493 |
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
494 |
{ |
|
495 |
ec_datagram_t *datagram = fsm->datagram; |
|
496 |
ec_slave_t *slave = fsm->slave; |
|
497 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
498 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
499 |
return; // FIXME: check for response first? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
500 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
501 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
502 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
503 |
EC_ERR("Failed to receive CoE SDO description request datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
504 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
505 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
506 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
507 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
508 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
509 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
510 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
511 |
EC_ERR("Reception of CoE SDO description" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
512 |
" request failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
513 |
ec_datagram_print_wc_error(datagram); |
436 | 514 |
return; |
515 |
} |
|
516 |
||
517 |
fsm->cycles_start = datagram->cycles_sent; |
|
518 |
||
519 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
520 |
fsm->retries = EC_FSM_RETRIES; |
436 | 521 |
fsm->state = ec_fsm_coe_dict_desc_check; |
522 |
} |
|
523 |
||
524 |
/*****************************************************************************/ |
|
525 |
||
526 |
/** |
|
527 |
CoE state: DICT DESC CHECK. |
|
528 |
*/ |
|
529 |
||
530 |
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
531 |
{ |
|
532 |
ec_datagram_t *datagram = fsm->datagram; |
|
533 |
ec_slave_t *slave = fsm->slave; |
|
534 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
535 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
536 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
537 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
538 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
539 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
540 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
541 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
542 |
slave->ring_position, datagram->state); |
436 | 543 |
return; |
544 |
} |
|
545 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
546 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
547 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
548 |
EC_ERR("Reception of CoE mailbox check" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
549 |
" datagram failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
550 |
ec_datagram_print_wc_error(datagram); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
551 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
552 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
553 |
|
436 | 554 |
if (!ec_slave_mbox_check(datagram)) { |
555 |
if (datagram->cycles_received |
|
556 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
557 |
fsm->state = ec_fsm_coe_error; |
|
558 |
EC_ERR("Timeout while checking SDO description on slave %i.\n", |
|
559 |
slave->ring_position); |
|
560 |
return; |
|
561 |
} |
|
562 |
||
563 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
564 |
fsm->retries = EC_FSM_RETRIES; |
436 | 565 |
return; |
566 |
} |
|
567 |
||
568 |
// Fetch response |
|
569 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
570 |
fsm->retries = EC_FSM_RETRIES; |
436 | 571 |
fsm->state = ec_fsm_coe_dict_desc_response; |
572 |
} |
|
573 |
||
574 |
/*****************************************************************************/ |
|
575 |
||
576 |
/** |
|
577 |
CoE state: DICT DESC RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
578 |
\todo Timeout behavior |
436 | 579 |
*/ |
580 |
||
581 |
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *fsm |
|
582 |
/**< finite state machine */) |
|
583 |
{ |
|
584 |
ec_datagram_t *datagram = fsm->datagram; |
|
585 |
ec_slave_t *slave = fsm->slave; |
|
586 |
ec_sdo_t *sdo = fsm->sdo; |
|
587 |
uint8_t *data, mbox_prot; |
|
588 |
size_t rec_size, name_size; |
|
589 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
590 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
591 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
592 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
593 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
594 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
595 |
EC_ERR("Failed to receive CoE SDO description response datagram from" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
596 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
597 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
598 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
599 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
600 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
601 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
602 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
603 |
EC_ERR("Reception of CoE SDO description" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
604 |
" response failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
605 |
ec_datagram_print_wc_error(datagram); |
436 | 606 |
return; |
607 |
} |
|
608 |
||
609 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
610 |
&mbox_prot, &rec_size))) { |
|
611 |
fsm->state = ec_fsm_coe_error; |
|
612 |
return; |
|
613 |
} |
|
614 |
||
615 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 616 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 617 |
fsm->state = ec_fsm_coe_error; |
618 |
return; |
|
619 |
} |
|
620 |
||
621 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
622 |
(EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response |
|
623 |
EC_ERR("SDO information error response at slave %i while" |
|
624 |
" fetching SDO 0x%04X!\n", slave->ring_position, |
|
625 |
sdo->index); |
|
626 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
627 |
fsm->state = ec_fsm_coe_error; |
|
628 |
return; |
|
629 |
} |
|
630 |
||
631 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
632 |
(EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response |
|
633 |
EC_READ_U16(data + 6) != sdo->index) { // SDO index |
|
634 |
EC_ERR("Invalid object description response at slave %i while" |
|
635 |
" fetching SDO 0x%04X!\n", slave->ring_position, |
|
636 |
sdo->index); |
|
637 |
ec_print_data(data, rec_size); |
|
638 |
fsm->state = ec_fsm_coe_error; |
|
639 |
return; |
|
640 |
} |
|
641 |
||
642 |
if (rec_size < 12) { |
|
643 |
EC_ERR("Invalid data size!\n"); |
|
644 |
ec_print_data(data, rec_size); |
|
645 |
fsm->state = ec_fsm_coe_error; |
|
646 |
return; |
|
647 |
} |
|
648 |
||
649 |
sdo->subindices = EC_READ_U8(data + 10); |
|
650 |
sdo->object_code = EC_READ_U8(data + 11); |
|
651 |
||
652 |
name_size = rec_size - 12; |
|
653 |
if (name_size) { |
|
654 |
if (!(sdo->name = kmalloc(name_size + 1, GFP_ATOMIC))) { |
|
655 |
EC_ERR("Failed to allocate SDO name!\n"); |
|
656 |
fsm->state = ec_fsm_coe_error; |
|
657 |
return; |
|
658 |
} |
|
659 |
||
660 |
memcpy(sdo->name, data + 12, name_size); |
|
661 |
sdo->name[name_size] = 0; |
|
662 |
} |
|
663 |
||
664 |
if (EC_READ_U8(data + 2) & 0x80) { |
|
665 |
EC_ERR("Fragment follows (not implemented)!\n"); |
|
666 |
fsm->state = ec_fsm_coe_error; |
|
667 |
return; |
|
668 |
} |
|
669 |
||
670 |
// start fetching entries |
|
671 |
||
672 |
fsm->subindex = 0; |
|
673 |
||
674 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
|
675 |
fsm->state = ec_fsm_coe_error; |
|
676 |
return; |
|
677 |
} |
|
678 |
||
679 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
680 |
EC_WRITE_U8 (data + 2, 0x05); // Get entry description request |
|
681 |
EC_WRITE_U8 (data + 3, 0x00); |
|
682 |
EC_WRITE_U16(data + 4, 0x0000); |
|
683 |
EC_WRITE_U16(data + 6, sdo->index); // SDO index |
|
684 |
EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex |
|
685 |
EC_WRITE_U8 (data + 9, 0x00); // value info (no values) |
|
686 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
687 |
fsm->retries = EC_FSM_RETRIES; |
436 | 688 |
fsm->state = ec_fsm_coe_dict_entry_request; |
689 |
} |
|
690 |
||
691 |
/*****************************************************************************/ |
|
692 |
||
693 |
/** |
|
694 |
CoE state: DICT ENTRY REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
695 |
\todo Timeout behavior |
436 | 696 |
*/ |
697 |
||
698 |
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *fsm |
|
699 |
/**< finite state machine */) |
|
700 |
{ |
|
701 |
ec_datagram_t *datagram = fsm->datagram; |
|
702 |
ec_slave_t *slave = fsm->slave; |
|
703 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
704 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
705 |
return; // FIXME: check for response first? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
706 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
707 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
708 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
709 |
EC_ERR("Failed to receive CoE SDO entry request datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
710 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
711 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
712 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
713 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
714 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
715 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
716 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
717 |
EC_ERR("Reception of CoE SDO entry request failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
718 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
719 |
ec_datagram_print_wc_error(datagram); |
436 | 720 |
return; |
721 |
} |
|
722 |
||
723 |
fsm->cycles_start = datagram->cycles_sent; |
|
724 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
725 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
726 |
fsm->retries = EC_FSM_RETRIES; |
436 | 727 |
fsm->state = ec_fsm_coe_dict_entry_check; |
728 |
} |
|
729 |
||
730 |
/*****************************************************************************/ |
|
731 |
||
732 |
/** |
|
733 |
CoE state: DICT ENTRY CHECK. |
|
734 |
*/ |
|
735 |
||
736 |
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *fsm |
|
737 |
/**< finite state machine */) |
|
738 |
{ |
|
739 |
ec_datagram_t *datagram = fsm->datagram; |
|
740 |
ec_slave_t *slave = fsm->slave; |
|
741 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
742 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
743 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
744 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
745 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
746 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
747 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
748 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
749 |
slave->ring_position, datagram->state); |
436 | 750 |
return; |
751 |
} |
|
752 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
753 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
754 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
755 |
EC_ERR("Reception of CoE mailbox check" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
756 |
" datagram failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
757 |
ec_datagram_print_wc_error(datagram); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
758 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
759 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
760 |
|
436 | 761 |
if (!ec_slave_mbox_check(datagram)) { |
762 |
if (datagram->cycles_received |
|
763 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
764 |
fsm->state = ec_fsm_coe_error; |
|
765 |
EC_ERR("Timeout while checking SDO entry on slave %i.\n", |
|
766 |
slave->ring_position); |
|
767 |
return; |
|
768 |
} |
|
769 |
||
770 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
771 |
fsm->retries = EC_FSM_RETRIES; |
436 | 772 |
return; |
773 |
} |
|
774 |
||
775 |
// Fetch response |
|
776 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
777 |
fsm->retries = EC_FSM_RETRIES; |
436 | 778 |
fsm->state = ec_fsm_coe_dict_entry_response; |
779 |
} |
|
780 |
||
781 |
/*****************************************************************************/ |
|
782 |
||
783 |
/** |
|
784 |
CoE state: DICT ENTRY RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
785 |
\todo Timeout behavior |
436 | 786 |
*/ |
787 |
||
788 |
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *fsm |
|
789 |
/**< finite state machine */) |
|
790 |
{ |
|
791 |
ec_datagram_t *datagram = fsm->datagram; |
|
792 |
ec_slave_t *slave = fsm->slave; |
|
793 |
ec_sdo_t *sdo = fsm->sdo; |
|
794 |
uint8_t *data, mbox_prot; |
|
795 |
size_t rec_size, data_size; |
|
796 |
ec_sdo_entry_t *entry; |
|
797 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
798 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
799 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
800 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
801 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
802 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
803 |
EC_ERR("Failed to receive CoE SDO description response datagram from" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
804 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
805 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
806 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
807 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
808 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
809 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
810 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
811 |
EC_ERR("Reception of CoE SDO description" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
812 |
" response failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
813 |
ec_datagram_print_wc_error(datagram); |
436 | 814 |
return; |
815 |
} |
|
816 |
||
817 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
818 |
&mbox_prot, &rec_size))) { |
|
819 |
fsm->state = ec_fsm_coe_error; |
|
820 |
return; |
|
821 |
} |
|
822 |
||
823 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 824 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 825 |
fsm->state = ec_fsm_coe_error; |
826 |
return; |
|
827 |
} |
|
828 |
||
829 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
830 |
(EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response |
|
831 |
EC_ERR("SDO information error response at slave %i while" |
|
832 |
" fetching SDO entry 0x%04X:%i!\n", slave->ring_position, |
|
833 |
sdo->index, fsm->subindex); |
|
834 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
835 |
fsm->state = ec_fsm_coe_error; |
|
836 |
return; |
|
837 |
} |
|
838 |
||
839 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
840 |
(EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response |
|
841 |
EC_READ_U16(data + 6) != sdo->index || // SDO index |
|
842 |
EC_READ_U8(data + 8) != fsm->subindex) { // SDO subindex |
|
843 |
EC_ERR("Invalid entry description response at slave %i while" |
|
844 |
" fetching SDO entry 0x%04X:%i!\n", slave->ring_position, |
|
845 |
sdo->index, fsm->subindex); |
|
846 |
ec_print_data(data, rec_size); |
|
847 |
fsm->state = ec_fsm_coe_error; |
|
848 |
return; |
|
849 |
} |
|
850 |
||
851 |
if (rec_size < 16) { |
|
852 |
EC_ERR("Invalid data size!\n"); |
|
853 |
ec_print_data(data, rec_size); |
|
854 |
fsm->state = ec_fsm_coe_error; |
|
855 |
return; |
|
856 |
} |
|
857 |
||
858 |
data_size = rec_size - 16; |
|
859 |
||
860 |
if (!(entry = (ec_sdo_entry_t *) |
|
861 |
kmalloc(sizeof(ec_sdo_entry_t), GFP_ATOMIC))) { |
|
862 |
EC_ERR("Failed to allocate entry!\n"); |
|
863 |
fsm->state = ec_fsm_coe_error; |
|
864 |
return; |
|
865 |
} |
|
866 |
||
867 |
if (ec_sdo_entry_init(entry, fsm->subindex, sdo)) { |
|
868 |
EC_ERR("Failed to init entry!\n"); |
|
869 |
fsm->state = ec_fsm_coe_error; |
|
870 |
return; |
|
871 |
} |
|
872 |
||
873 |
entry->data_type = EC_READ_U16(data + 10); |
|
874 |
entry->bit_length = EC_READ_U16(data + 12); |
|
875 |
||
876 |
if (data_size) { |
|
484
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
877 |
uint8_t *desc; |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
878 |
if (!(desc = kmalloc(data_size + 1, GFP_ATOMIC))) { |
436 | 879 |
EC_ERR("Failed to allocate SDO entry name!\n"); |
880 |
fsm->state = ec_fsm_coe_error; |
|
881 |
return; |
|
882 |
} |
|
484
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
883 |
memcpy(desc, data + 16, data_size); |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
884 |
desc[data_size] = 0; |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
885 |
entry->description = desc; |
436 | 886 |
} |
887 |
||
888 |
list_add_tail(&entry->list, &sdo->entries); |
|
889 |
||
890 |
if (fsm->subindex < sdo->subindices) { |
|
891 |
fsm->subindex++; |
|
892 |
||
893 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
|
894 |
fsm->state = ec_fsm_coe_error; |
|
895 |
return; |
|
896 |
} |
|
897 |
||
898 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
899 |
EC_WRITE_U8 (data + 2, 0x05); // Get entry description request |
|
900 |
EC_WRITE_U8 (data + 3, 0x00); |
|
901 |
EC_WRITE_U16(data + 4, 0x0000); |
|
902 |
EC_WRITE_U16(data + 6, sdo->index); // SDO index |
|
903 |
EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex |
|
904 |
EC_WRITE_U8 (data + 9, 0x00); // value info (no values) |
|
905 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
906 |
fsm->retries = EC_FSM_RETRIES; |
436 | 907 |
fsm->state = ec_fsm_coe_dict_entry_request; |
908 |
return; |
|
909 |
} |
|
910 |
||
911 |
// another SDO description to fetch? |
|
912 |
if (fsm->sdo->list.next != &slave->sdo_dictionary) { |
|
913 |
fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list); |
|
914 |
||
915 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { |
|
916 |
fsm->state = ec_fsm_coe_error; |
|
917 |
return; |
|
918 |
} |
|
919 |
||
920 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
921 |
EC_WRITE_U8 (data + 2, 0x03); // Get object description request |
|
922 |
EC_WRITE_U8 (data + 3, 0x00); |
|
923 |
EC_WRITE_U16(data + 4, 0x0000); |
|
924 |
EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index |
|
925 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
926 |
fsm->retries = EC_FSM_RETRIES; |
436 | 927 |
fsm->state = ec_fsm_coe_dict_desc_request; |
928 |
return; |
|
929 |
} |
|
930 |
||
931 |
fsm->state = ec_fsm_coe_end; |
|
932 |
} |
|
933 |
||
934 |
/****************************************************************************** |
|
935 |
* CoE state machine |
|
936 |
*****************************************************************************/ |
|
937 |
||
938 |
/** |
|
939 |
CoE state: DOWN START. |
|
940 |
*/ |
|
941 |
||
942 |
void ec_fsm_coe_down_start(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
943 |
{ |
|
944 |
ec_datagram_t *datagram = fsm->datagram; |
|
945 |
ec_slave_t *slave = fsm->slave; |
|
946 |
ec_sdo_data_t *sdodata = fsm->sdodata; |
|
947 |
uint8_t *data; |
|
948 |
||
459
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
949 |
if (fsm->slave->master->debug_level) |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
950 |
EC_DBG("Downloading SDO 0x%04X:%i to slave %i.\n", |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
951 |
sdodata->index, sdodata->subindex, slave->ring_position); |
436 | 952 |
|
953 |
if (slave->sii_rx_mailbox_size < 6 + 10 + sdodata->size) { |
|
954 |
EC_ERR("SDO fragmenting not supported yet!\n"); |
|
955 |
fsm->state = ec_fsm_coe_error; |
|
956 |
return; |
|
957 |
} |
|
958 |
||
959 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, |
|
960 |
sdodata->size + 10))) { |
|
961 |
fsm->state = ec_fsm_coe_error; |
|
962 |
return; |
|
963 |
} |
|
964 |
||
965 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
966 |
EC_WRITE_U8 (data + 2, (0x1 // size specified |
|
967 |
| 0x1 << 5)); // Download request |
|
968 |
EC_WRITE_U16(data + 3, sdodata->index); |
|
969 |
EC_WRITE_U8 (data + 5, sdodata->subindex); |
|
970 |
EC_WRITE_U32(data + 6, sdodata->size); |
|
971 |
memcpy(data + 10, sdodata->data, sdodata->size); |
|
972 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
973 |
fsm->retries = EC_FSM_RETRIES; |
436 | 974 |
fsm->state = ec_fsm_coe_down_request; |
975 |
} |
|
976 |
||
977 |
/*****************************************************************************/ |
|
978 |
||
979 |
/** |
|
980 |
CoE state: DOWN REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
981 |
\todo Timeout behavior |
436 | 982 |
*/ |
983 |
||
984 |
void ec_fsm_coe_down_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
985 |
{ |
|
986 |
ec_datagram_t *datagram = fsm->datagram; |
|
987 |
ec_slave_t *slave = fsm->slave; |
|
988 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
989 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
990 |
return; // FIXME: check for response first? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
991 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
992 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
993 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
994 |
EC_ERR("Failed to receive CoE download request datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
995 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
996 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
997 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
998 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
999 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1000 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1001 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1002 |
EC_ERR("Reception of CoE download request failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1003 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1004 |
ec_datagram_print_wc_error(datagram); |
436 | 1005 |
return; |
1006 |
} |
|
1007 |
||
1008 |
fsm->cycles_start = datagram->cycles_sent; |
|
1009 |
||
1010 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1011 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1012 |
fsm->state = ec_fsm_coe_down_check; |
1013 |
} |
|
1014 |
||
1015 |
/*****************************************************************************/ |
|
1016 |
||
1017 |
/** |
|
1018 |
CoE state: DOWN CHECK. |
|
1019 |
*/ |
|
1020 |
||
1021 |
void ec_fsm_coe_down_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1022 |
{ |
|
1023 |
ec_datagram_t *datagram = fsm->datagram; |
|
1024 |
ec_slave_t *slave = fsm->slave; |
|
1025 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1026 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1027 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1028 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1029 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1030 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1031 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1032 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1033 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1034 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1035 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1036 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1037 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1038 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1039 |
EC_ERR("Reception of CoE mailbox check" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1040 |
" datagram failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1041 |
ec_datagram_print_wc_error(datagram); |
436 | 1042 |
return; |
1043 |
} |
|
1044 |
||
1045 |
if (!ec_slave_mbox_check(datagram)) { |
|
1046 |
if (datagram->cycles_received |
|
1047 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1048 |
fsm->state = ec_fsm_coe_error; |
|
1049 |
EC_ERR("Timeout while checking SDO configuration on slave %i.\n", |
|
1050 |
slave->ring_position); |
|
1051 |
return; |
|
1052 |
} |
|
1053 |
||
1054 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1055 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1056 |
return; |
1057 |
} |
|
1058 |
||
1059 |
// Fetch response |
|
1060 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1061 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1062 |
fsm->state = ec_fsm_coe_down_response; |
1063 |
} |
|
1064 |
||
1065 |
/*****************************************************************************/ |
|
1066 |
||
1067 |
/** |
|
1068 |
CoE state: DOWN RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1069 |
\todo Timeout behavior |
436 | 1070 |
*/ |
1071 |
||
1072 |
void ec_fsm_coe_down_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1073 |
{ |
|
1074 |
ec_datagram_t *datagram = fsm->datagram; |
|
1075 |
ec_slave_t *slave = fsm->slave; |
|
1076 |
uint8_t *data, mbox_prot; |
|
1077 |
size_t rec_size; |
|
1078 |
ec_sdo_data_t *sdodata = fsm->sdodata; |
|
1079 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1080 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1081 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1082 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1083 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1084 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1085 |
EC_ERR("Failed to receive CoE download response datagram from" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1086 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1087 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1088 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1089 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1090 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1091 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1092 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1093 |
EC_ERR("Reception of CoE download response failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1094 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1095 |
ec_datagram_print_wc_error(datagram); |
436 | 1096 |
return; |
1097 |
} |
|
1098 |
||
1099 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1100 |
&mbox_prot, &rec_size))) { |
|
1101 |
fsm->state = ec_fsm_coe_error; |
|
1102 |
return; |
|
1103 |
} |
|
1104 |
||
1105 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 1106 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 1107 |
fsm->state = ec_fsm_coe_error; |
1108 |
return; |
|
1109 |
} |
|
1110 |
||
1111 |
if (rec_size < 6) { |
|
1112 |
fsm->state = ec_fsm_coe_error; |
|
1113 |
EC_ERR("Received data is too small (%i bytes):\n", rec_size); |
|
1114 |
ec_print_data(data, rec_size); |
|
1115 |
return; |
|
1116 |
} |
|
1117 |
||
1118 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1119 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1120 |
fsm->state = ec_fsm_coe_error; |
|
1121 |
EC_ERR("SDO download 0x%04X:%X (%i bytes) aborted on slave %i.\n", |
|
1122 |
sdodata->index, sdodata->subindex, sdodata->size, |
|
1123 |
slave->ring_position); |
|
1124 |
if (rec_size < 10) { |
|
1125 |
EC_ERR("Incomplete Abort command:\n"); |
|
1126 |
ec_print_data(data, rec_size); |
|
1127 |
} |
|
1128 |
else |
|
1129 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
1130 |
return; |
|
1131 |
} |
|
1132 |
||
1133 |
if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response |
|
1134 |
EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response |
|
1135 |
EC_READ_U16(data + 3) != sdodata->index || // index |
|
1136 |
EC_READ_U8 (data + 5) != sdodata->subindex) { // subindex |
|
1137 |
fsm->state = ec_fsm_coe_error; |
|
1138 |
EC_ERR("SDO download 0x%04X:%X (%i bytes) failed:\n", |
|
1139 |
sdodata->index, sdodata->subindex, sdodata->size); |
|
1140 |
EC_ERR("Invalid SDO download response at slave %i!\n", |
|
1141 |
slave->ring_position); |
|
1142 |
ec_print_data(data, rec_size); |
|
1143 |
return; |
|
1144 |
} |
|
1145 |
||
1146 |
fsm->state = ec_fsm_coe_end; // success |
|
1147 |
} |
|
1148 |
||
1149 |
/*****************************************************************************/ |
|
1150 |
||
1151 |
/** |
|
1152 |
CoE state: UP START. |
|
1153 |
*/ |
|
1154 |
||
1155 |
void ec_fsm_coe_up_start(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1156 |
{ |
|
1157 |
ec_datagram_t *datagram = fsm->datagram; |
|
1158 |
ec_slave_t *slave = fsm->slave; |
|
1159 |
ec_master_t *master = slave->master; |
|
1160 |
ec_sdo_request_t *request = fsm->request; |
|
1161 |
ec_sdo_entry_t *entry = request->entry; |
|
1162 |
uint8_t *data; |
|
1163 |
||
459
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
1164 |
if (master->debug_level) |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
1165 |
EC_DBG("Uploading SDO 0x%04X:%i from slave %i.\n", |
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1166 |
entry->sdo->index, entry->subindex, slave->ring_position); |
436 | 1167 |
|
476
fd7c0ee645f8
Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents:
461
diff
changeset
|
1168 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
436 | 1169 |
fsm->state = ec_fsm_coe_error; |
1170 |
return; |
|
1171 |
} |
|
1172 |
||
1173 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1174 |
EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request |
|
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1175 |
EC_WRITE_U16(data + 3, entry->sdo->index); |
436 | 1176 |
EC_WRITE_U8 (data + 5, entry->subindex); |
476
fd7c0ee645f8
Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents:
461
diff
changeset
|
1177 |
memset(data + 6, 0x00, 4); |
436 | 1178 |
|
1179 |
if (master->debug_level) { |
|
1180 |
EC_DBG("Upload request:\n"); |
|
476
fd7c0ee645f8
Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents:
461
diff
changeset
|
1181 |
ec_print_data(data, 10); |
436 | 1182 |
} |
1183 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1184 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1185 |
fsm->state = ec_fsm_coe_up_request; |
1186 |
} |
|
1187 |
||
1188 |
/*****************************************************************************/ |
|
1189 |
||
1190 |
/** |
|
1191 |
CoE state: UP REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1192 |
\todo Timeout behavior |
436 | 1193 |
*/ |
1194 |
||
1195 |
void ec_fsm_coe_up_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1196 |
{ |
|
1197 |
ec_datagram_t *datagram = fsm->datagram; |
|
1198 |
ec_slave_t *slave = fsm->slave; |
|
1199 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1200 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1201 |
return; // FIXME: check for response first? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1202 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1203 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1204 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1205 |
EC_ERR("Failed to receive CoE upload request for slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1206 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1207 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1208 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1209 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1210 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1211 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1212 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1213 |
EC_ERR("Reception of CoE upload request failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1214 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1215 |
ec_datagram_print_wc_error(datagram); |
436 | 1216 |
return; |
1217 |
} |
|
1218 |
||
1219 |
fsm->cycles_start = datagram->cycles_sent; |
|
1220 |
||
1221 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1222 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1223 |
fsm->state = ec_fsm_coe_up_check; |
1224 |
} |
|
1225 |
||
1226 |
/*****************************************************************************/ |
|
1227 |
||
1228 |
/** |
|
1229 |
CoE state: UP CHECK. |
|
1230 |
*/ |
|
1231 |
||
1232 |
void ec_fsm_coe_up_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1233 |
{ |
|
1234 |
ec_datagram_t *datagram = fsm->datagram; |
|
1235 |
ec_slave_t *slave = fsm->slave; |
|
1236 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1237 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1238 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1239 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1240 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1241 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1242 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1243 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1244 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1245 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1246 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1247 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1248 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1249 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1250 |
EC_ERR("Reception of CoE mailbox check" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1251 |
" datagram failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1252 |
ec_datagram_print_wc_error(datagram); |
436 | 1253 |
return; |
1254 |
} |
|
1255 |
||
1256 |
if (!ec_slave_mbox_check(datagram)) { |
|
1257 |
if (datagram->cycles_received |
|
1258 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1259 |
fsm->state = ec_fsm_coe_error; |
|
1260 |
EC_ERR("Timeout while checking SDO upload on slave %i.\n", |
|
1261 |
slave->ring_position); |
|
1262 |
return; |
|
1263 |
} |
|
1264 |
||
1265 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1266 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1267 |
return; |
1268 |
} |
|
1269 |
||
1270 |
// Fetch response |
|
1271 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1272 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1273 |
fsm->state = ec_fsm_coe_up_response; |
1274 |
} |
|
1275 |
||
1276 |
/*****************************************************************************/ |
|
1277 |
||
1278 |
/** |
|
1279 |
CoE state: UP RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1280 |
\todo Timeout behavior |
436 | 1281 |
*/ |
1282 |
||
1283 |
void ec_fsm_coe_up_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1284 |
{ |
|
1285 |
ec_datagram_t *datagram = fsm->datagram; |
|
1286 |
ec_slave_t *slave = fsm->slave; |
|
1287 |
ec_master_t *master = slave->master; |
|
1288 |
uint8_t *data, mbox_prot; |
|
1289 |
size_t rec_size, data_size; |
|
1290 |
ec_sdo_request_t *request = fsm->request; |
|
1291 |
ec_sdo_entry_t *entry = request->entry; |
|
1292 |
uint32_t complete_size; |
|
1293 |
unsigned int expedited, size_specified; |
|
1294 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1295 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1296 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1297 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1298 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1299 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1300 |
EC_ERR("Failed to receive CoE upload response datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1301 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1302 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1303 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1304 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1305 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1306 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1307 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1308 |
EC_ERR("Reception of CoE upload response failed on slave %i: ", |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1309 |
slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1310 |
ec_datagram_print_wc_error(datagram); |
436 | 1311 |
return; |
1312 |
} |
|
1313 |
||
1314 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1315 |
&mbox_prot, &rec_size))) { |
|
1316 |
fsm->state = ec_fsm_coe_error; |
|
1317 |
return; |
|
1318 |
} |
|
1319 |
||
1320 |
if (master->debug_level) { |
|
1321 |
EC_DBG("Upload response:\n"); |
|
1322 |
ec_print_data(data, rec_size); |
|
1323 |
} |
|
1324 |
||
1325 |
if (mbox_prot != 0x03) { // CoE |
|
1326 |
EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
|
1327 |
fsm->state = ec_fsm_coe_error; |
|
1328 |
return; |
|
1329 |
} |
|
1330 |
||
1331 |
if (rec_size < 10) { |
|
1332 |
EC_ERR("Received currupted SDO upload response!\n"); |
|
1333 |
ec_print_data(data, rec_size); |
|
1334 |
fsm->state = ec_fsm_coe_error; |
|
1335 |
return; |
|
1336 |
} |
|
1337 |
||
1338 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1339 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1340 |
EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n", |
|
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1341 |
entry->sdo->index, entry->subindex, slave->ring_position); |
436 | 1342 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
1343 |
fsm->state = ec_fsm_coe_error; |
|
1344 |
return; |
|
1345 |
} |
|
1346 |
||
1347 |
if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response |
|
476
fd7c0ee645f8
Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents:
461
diff
changeset
|
1348 |
EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response |
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1349 |
EC_READ_U16(data + 3) != entry->sdo->index || // index |
436 | 1350 |
EC_READ_U8 (data + 5) != entry->subindex) { // subindex |
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1351 |
EC_ERR("SDO upload 0x%04X:%X failed:\n", entry->sdo->index, entry->subindex); |
436 | 1352 |
EC_ERR("Invalid SDO upload response at slave %i!\n", |
1353 |
slave->ring_position); |
|
1354 |
ec_print_data(data, rec_size); |
|
1355 |
fsm->state = ec_fsm_coe_error; |
|
1356 |
return; |
|
1357 |
} |
|
1358 |
||
1359 |
data_size = rec_size - 10; |
|
1360 |
expedited = EC_READ_U8(data + 2) & 0x02; |
|
1361 |
||
1362 |
if (expedited) { |
|
1363 |
size_specified = EC_READ_U8(data + 2) & 0x01; |
|
1364 |
if (size_specified) { |
|
1365 |
complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2); |
|
1366 |
} |
|
1367 |
else { |
|
1368 |
complete_size = 4; |
|
1369 |
} |
|
1370 |
} |
|
1371 |
else { |
|
1372 |
complete_size = EC_READ_U32(data + 6); |
|
1373 |
} |
|
1374 |
||
1375 |
if (request->data) { |
|
1376 |
kfree(request->data); |
|
1377 |
request->data = NULL; |
|
1378 |
} |
|
1379 |
request->size = 0; |
|
1380 |
||
1381 |
if (complete_size) { |
|
446 | 1382 |
if (!(request->data = (uint8_t *) |
1383 |
kmalloc(complete_size + 1, GFP_ATOMIC))) { |
|
1384 |
EC_ERR("Failed to allocate %i bytes of SDO data!\n", |
|
1385 |
complete_size); |
|
436 | 1386 |
fsm->state = ec_fsm_coe_error; |
1387 |
return; |
|
1388 |
} |
|
1389 |
request->data[complete_size] = 0x00; // just to be sure... |
|
1390 |
} |
|
1391 |
||
1392 |
if (expedited) { |
|
1393 |
memcpy(request->data, data + 6, complete_size); |
|
1394 |
request->size = complete_size; |
|
1395 |
} |
|
1396 |
else { |
|
1397 |
memcpy(request->data, data + 10, data_size); |
|
1398 |
request->size = data_size; |
|
1399 |
fsm->toggle = 0; |
|
1400 |
||
1401 |
if (data_size < complete_size) { |
|
446 | 1402 |
EC_WARN("SDO data incomplete (%i / %i).\n", |
1403 |
data_size, complete_size); |
|
1404 |
||
1405 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, |
|
1406 |
0x03, 3))) { |
|
436 | 1407 |
fsm->state = ec_fsm_coe_error; |
1408 |
return; |
|
1409 |
} |
|
1410 |
||
1411 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1412 |
EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle |
|
1413 |
| 0x3 << 5)); // upload segment request |
|
1414 |
||
1415 |
if (master->debug_level) { |
|
1416 |
EC_DBG("Upload segment request:\n"); |
|
1417 |
ec_print_data(data, 3); |
|
1418 |
} |
|
1419 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1420 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1421 |
fsm->state = ec_fsm_coe_up_seg_request; |
1422 |
return; |
|
1423 |
} |
|
1424 |
} |
|
1425 |
||
1426 |
fsm->state = ec_fsm_coe_end; // success |
|
1427 |
} |
|
1428 |
||
1429 |
/*****************************************************************************/ |
|
1430 |
||
1431 |
/** |
|
1432 |
CoE state: UP REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1433 |
\todo Timeout behavior |
436 | 1434 |
*/ |
1435 |
||
1436 |
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1437 |
{ |
|
1438 |
ec_datagram_t *datagram = fsm->datagram; |
|
1439 |
ec_slave_t *slave = fsm->slave; |
|
1440 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1441 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1442 |
return; // FIXME: check for response first? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1443 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1444 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1445 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1446 |
EC_ERR("Failed to receive CoE upload segment request datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1447 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1448 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1449 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1450 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1451 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1452 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1453 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1454 |
EC_ERR("Reception of CoE upload segment" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1455 |
" request failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1456 |
ec_datagram_print_wc_error(datagram); |
436 | 1457 |
return; |
1458 |
} |
|
1459 |
||
1460 |
fsm->cycles_start = datagram->cycles_sent; |
|
1461 |
||
1462 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1463 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1464 |
fsm->state = ec_fsm_coe_up_seg_check; |
1465 |
} |
|
1466 |
||
1467 |
/*****************************************************************************/ |
|
1468 |
||
1469 |
/** |
|
1470 |
CoE state: UP CHECK. |
|
1471 |
*/ |
|
1472 |
||
1473 |
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1474 |
{ |
|
1475 |
ec_datagram_t *datagram = fsm->datagram; |
|
1476 |
ec_slave_t *slave = fsm->slave; |
|
1477 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1478 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1479 |
return; |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1480 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1481 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1482 |
fsm->state = ec_fsm_coe_error; |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1483 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i" |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1484 |
" (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1485 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1486 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1487 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1488 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1489 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1490 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1491 |
EC_ERR("Reception of CoE mailbox check" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1492 |
" datagram failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1493 |
ec_datagram_print_wc_error(datagram); |
436 | 1494 |
return; |
1495 |
} |
|
1496 |
||
1497 |
if (!ec_slave_mbox_check(datagram)) { |
|
1498 |
if (datagram->cycles_received |
|
1499 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1500 |
fsm->state = ec_fsm_coe_error; |
|
1501 |
EC_ERR("Timeout while checking SDO upload segment on slave %i.\n", |
|
1502 |
slave->ring_position); |
|
1503 |
return; |
|
1504 |
} |
|
1505 |
||
1506 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1507 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1508 |
return; |
1509 |
} |
|
1510 |
||
1511 |
// Fetch response |
|
1512 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1513 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1514 |
fsm->state = ec_fsm_coe_up_seg_response; |
1515 |
} |
|
1516 |
||
1517 |
/*****************************************************************************/ |
|
1518 |
||
1519 |
/** |
|
1520 |
CoE state: UP RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1521 |
\todo Timeout behavior |
436 | 1522 |
*/ |
1523 |
||
1524 |
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1525 |
{ |
|
1526 |
ec_datagram_t *datagram = fsm->datagram; |
|
1527 |
ec_slave_t *slave = fsm->slave; |
|
1528 |
ec_master_t *master = slave->master; |
|
1529 |
uint8_t *data, mbox_prot; |
|
1530 |
size_t rec_size, data_size; |
|
1531 |
ec_sdo_request_t *request = fsm->request; |
|
1532 |
ec_sdo_entry_t *entry = request->entry; |
|
1533 |
uint32_t seg_size; |
|
1534 |
unsigned int last_segment; |
|
1535 |
||
637
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1536 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) |
d5d04c868e0e
Removed ecrt_master_run(), removed datagram queueing from state
Florian Pose <fp@igh-essen.com>
parents:
594
diff
changeset
|
1537 |
return; // FIXME: request again? |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1538 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1539 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1540 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1541 |
EC_ERR("Failed to receive CoE upload segment response datagram for" |
594
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1542 |
" slave %i (datagram state %i).\n", |
07dd2a7df66e
Added datagram state to "Failed to receive..." messages.
Florian Pose <fp@igh-essen.com>
parents:
505
diff
changeset
|
1543 |
slave->ring_position, datagram->state); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1544 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1545 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1546 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1547 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1548 |
fsm->state = ec_fsm_coe_error; |
713
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1549 |
EC_ERR("Reception of CoE upload segment" |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1550 |
" response failed on slave %i: ", slave->ring_position); |
ae41cadd25b6
Better handling of unexpected working counters; introduced
Florian Pose <fp@igh-essen.com>
parents:
637
diff
changeset
|
1551 |
ec_datagram_print_wc_error(datagram); |
436 | 1552 |
return; |
1553 |
} |
|
1554 |
||
1555 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1556 |
&mbox_prot, &rec_size))) { |
|
1557 |
fsm->state = ec_fsm_coe_error; |
|
1558 |
return; |
|
1559 |
} |
|
1560 |
||
1561 |
if (master->debug_level) { |
|
1562 |
EC_DBG("Upload segment response:\n"); |
|
1563 |
ec_print_data(data, rec_size); |
|
1564 |
} |
|
1565 |
||
1566 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 1567 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 1568 |
fsm->state = ec_fsm_coe_error; |
1569 |
return; |
|
1570 |
} |
|
1571 |
||
1572 |
if (rec_size < 10) { |
|
1573 |
EC_ERR("Received currupted SDO upload segment response!\n"); |
|
1574 |
ec_print_data(data, rec_size); |
|
1575 |
fsm->state = ec_fsm_coe_error; |
|
1576 |
return; |
|
1577 |
} |
|
1578 |
||
1579 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1580 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1581 |
EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n", |
|
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1582 |
entry->sdo->index, entry->subindex, slave->ring_position); |
436 | 1583 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
1584 |
fsm->state = ec_fsm_coe_error; |
|
1585 |
return; |
|
1586 |
} |
|
1587 |
||
1588 |
if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response |
|
1589 |
EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response |
|
739
4a02162a6301
Removed 'sdo' field from ec_sdo_request_t; Exported
Florian Pose <fp@igh-essen.com>
parents:
713
diff
changeset
|
1590 |
EC_ERR("SDO upload 0x%04X:%X failed:\n", entry->sdo->index, entry->subindex); |
436 | 1591 |
EC_ERR("Invalid SDO upload segment response at slave %i!\n", |
1592 |
slave->ring_position); |
|
1593 |
ec_print_data(data, rec_size); |
|
1594 |
fsm->state = ec_fsm_coe_error; |
|
1595 |
return; |
|
1596 |
} |
|
1597 |
||
1598 |
last_segment = EC_READ_U8(data + 2) & 0x01; |
|
1599 |
seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1; |
|
1600 |
data_size = rec_size - 10; |
|
1601 |
||
1602 |
if (data_size != seg_size) { |
|
1603 |
EC_WARN("SDO segment data invalid (%i / %i)" |
|
1604 |
" - Fragmenting not implemented.\n", |
|
1605 |
data_size, seg_size); |
|
1606 |
} |
|
1607 |
||
1608 |
memcpy(request->data + request->size, data + 10, data_size); |
|
1609 |
request->size += data_size; |
|
1610 |
||
1611 |
if (!last_segment) { |
|
1612 |
fsm->toggle = !fsm->toggle; |
|
1613 |
||
1614 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 3))) { |
|
1615 |
fsm->state = ec_fsm_coe_error; |
|
1616 |
return; |
|
1617 |
} |
|
1618 |
||
1619 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1620 |
EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle |
|
1621 |
| 0x3 << 5)); // upload segment request |
|
1622 |
||
1623 |
if (master->debug_level) { |
|
1624 |
EC_DBG("Upload segment request:\n"); |
|
1625 |
ec_print_data(data, 3); |
|
1626 |
} |
|
1627 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1628 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1629 |
fsm->state = ec_fsm_coe_up_seg_request; |
1630 |
return; |
|
1631 |
} |
|
1632 |
||
1633 |
fsm->state = ec_fsm_coe_end; // success |
|
1634 |
} |
|
1635 |
||
1636 |
/*****************************************************************************/ |
|
1637 |
||
1638 |
/** |
|
1639 |
State: ERROR. |
|
1640 |
*/ |
|
1641 |
||
1642 |
void ec_fsm_coe_error(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1643 |
{ |
|
1644 |
} |
|
1645 |
||
1646 |
/*****************************************************************************/ |
|
1647 |
||
1648 |
/** |
|
1649 |
State: END. |
|
1650 |
*/ |
|
1651 |
||
1652 |
void ec_fsm_coe_end(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1653 |
{ |
|
1654 |
} |
|
1655 |
||
1656 |
/*****************************************************************************/ |