author | Florian Pose <fp@igh-essen.com> |
Tue, 02 Jan 2007 13:27:03 +0000 | |
changeset 510 | 0d0c6fe0e3b2 |
parent 505 | bc443ca0077f |
child 594 | 07dd2a7df66e |
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 |
||
263 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
264 |
fsm->retries = EC_FSM_RETRIES; |
436 | 265 |
fsm->state = ec_fsm_coe_dict_request; |
266 |
} |
|
267 |
||
268 |
/*****************************************************************************/ |
|
269 |
||
270 |
/** |
|
271 |
CoE state: DICT REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
272 |
\todo Timeout behavior |
436 | 273 |
*/ |
274 |
||
275 |
void ec_fsm_coe_dict_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
276 |
{ |
|
277 |
ec_datagram_t *datagram = fsm->datagram; |
|
278 |
ec_slave_t *slave = fsm->slave; |
|
279 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
280 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
281 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
282 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
283 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
284 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
285 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
286 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
287 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
288 |
EC_ERR("Failed to receive CoE dictionary request datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
289 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
290 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
291 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
292 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
293 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
294 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
295 |
EC_ERR("Reception of CoE dictionary request failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
296 |
" not respond.\n", slave->ring_position); |
436 | 297 |
return; |
298 |
} |
|
299 |
||
300 |
fsm->cycles_start = datagram->cycles_sent; |
|
301 |
||
302 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
303 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
304 |
fsm->retries = EC_FSM_RETRIES; |
436 | 305 |
fsm->state = ec_fsm_coe_dict_check; |
306 |
} |
|
307 |
||
308 |
/*****************************************************************************/ |
|
309 |
||
310 |
/** |
|
311 |
CoE state: DICT CHECK. |
|
312 |
*/ |
|
313 |
||
314 |
void ec_fsm_coe_dict_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
315 |
{ |
|
316 |
ec_datagram_t *datagram = fsm->datagram; |
|
317 |
ec_slave_t *slave = fsm->slave; |
|
318 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
319 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
320 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
321 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
322 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
323 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
324 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
325 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
326 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i.\n", |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
327 |
slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
328 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
329 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
330 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
331 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
332 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
333 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
334 |
" not respond.\n", |
436 | 335 |
slave->ring_position); |
336 |
return; |
|
337 |
} |
|
338 |
||
339 |
if (!ec_slave_mbox_check(datagram)) { |
|
340 |
if (datagram->cycles_received |
|
341 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
342 |
fsm->state = ec_fsm_coe_error; |
|
343 |
EC_ERR("Timeout while checking SDO dictionary on slave %i.\n", |
|
344 |
slave->ring_position); |
|
345 |
return; |
|
346 |
} |
|
347 |
||
348 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
349 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
350 |
fsm->retries = EC_FSM_RETRIES; |
436 | 351 |
return; |
352 |
} |
|
353 |
||
354 |
// Fetch response |
|
355 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
356 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
357 |
fsm->retries = EC_FSM_RETRIES; |
436 | 358 |
fsm->state = ec_fsm_coe_dict_response; |
359 |
} |
|
360 |
||
361 |
/*****************************************************************************/ |
|
362 |
||
363 |
/** |
|
364 |
CoE state: DICT RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
365 |
\todo Timeout behavior |
436 | 366 |
*/ |
367 |
||
368 |
void ec_fsm_coe_dict_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
369 |
{ |
|
370 |
ec_datagram_t *datagram = fsm->datagram; |
|
371 |
ec_slave_t *slave = fsm->slave; |
|
372 |
uint8_t *data, mbox_prot; |
|
373 |
size_t rec_size; |
|
374 |
unsigned int sdo_count, i; |
|
375 |
uint16_t sdo_index; |
|
376 |
ec_sdo_t *sdo; |
|
377 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
378 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
379 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
380 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
381 |
return; |
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 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
384 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
385 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
386 |
EC_ERR("Failed to receive CoE dictionary response datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
387 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
388 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
389 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
390 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
391 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
392 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
393 |
EC_ERR("Reception of CoE dictionary response failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
394 |
" not respond.\n", slave->ring_position); |
436 | 395 |
return; |
396 |
} |
|
397 |
||
398 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
399 |
&mbox_prot, &rec_size))) { |
|
400 |
fsm->state = ec_fsm_coe_error; |
|
401 |
return; |
|
402 |
} |
|
403 |
||
404 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 405 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 406 |
fsm->state = ec_fsm_coe_error; |
407 |
return; |
|
408 |
} |
|
409 |
||
410 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
411 |
(EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response |
|
412 |
EC_ERR("SDO information error response at slave %i!\n", |
|
413 |
slave->ring_position); |
|
414 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
415 |
fsm->state = ec_fsm_coe_error; |
|
416 |
return; |
|
417 |
} |
|
418 |
||
419 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
420 |
(EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response |
|
421 |
EC_ERR("Invalid SDO list response at slave %i!\n", |
|
422 |
slave->ring_position); |
|
423 |
ec_print_data(data, rec_size); |
|
424 |
fsm->state = ec_fsm_coe_error; |
|
425 |
return; |
|
426 |
} |
|
427 |
||
428 |
if (rec_size < 8) { |
|
429 |
EC_ERR("Invalid data size!\n"); |
|
430 |
ec_print_data(data, rec_size); |
|
431 |
fsm->state = ec_fsm_coe_error; |
|
432 |
return; |
|
433 |
} |
|
434 |
||
435 |
sdo_count = (rec_size - 8) / 2; |
|
436 |
||
437 |
for (i = 0; i < sdo_count; i++) { |
|
438 |
sdo_index = EC_READ_U16(data + 8 + i * 2); |
|
439 |
if (!sdo_index) { |
|
461
b9eda9235173
Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
459
diff
changeset
|
440 |
if (slave->master->debug_level) |
b9eda9235173
Print certain warnings only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
459
diff
changeset
|
441 |
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
|
442 |
slave->ring_position); |
436 | 443 |
continue; |
444 |
} |
|
445 |
||
446 |
if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_ATOMIC))) { |
|
447 |
EC_ERR("Failed to allocate memory for SDO!\n"); |
|
448 |
fsm->state = ec_fsm_coe_error; |
|
449 |
return; |
|
450 |
} |
|
451 |
||
452 |
if (ec_sdo_init(sdo, sdo_index, slave)) { |
|
453 |
EC_ERR("Failed to init SDO!\n"); |
|
454 |
fsm->state = ec_fsm_coe_error; |
|
455 |
return; |
|
456 |
} |
|
457 |
||
458 |
list_add_tail(&sdo->list, &slave->sdo_dictionary); |
|
459 |
} |
|
460 |
||
461 |
if (EC_READ_U8(data + 2) & 0x80) { // more messages waiting. check again. |
|
462 |
fsm->cycles_start = datagram->cycles_sent; |
|
463 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
464 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
465 |
fsm->retries = EC_FSM_RETRIES; |
436 | 466 |
fsm->state = ec_fsm_coe_dict_check; |
467 |
return; |
|
468 |
} |
|
469 |
||
470 |
if (list_empty(&slave->sdo_dictionary)) { |
|
471 |
// no SDOs in dictionary. finished. |
|
472 |
fsm->state = ec_fsm_coe_end; // success |
|
473 |
return; |
|
474 |
} |
|
475 |
||
476 |
// fetch SDO descriptions |
|
477 |
fsm->sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list); |
|
478 |
||
479 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { |
|
480 |
fsm->state = ec_fsm_coe_error; |
|
481 |
return; |
|
482 |
} |
|
483 |
||
484 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
485 |
EC_WRITE_U8 (data + 2, 0x03); // Get object description request |
|
486 |
EC_WRITE_U8 (data + 3, 0x00); |
|
487 |
EC_WRITE_U16(data + 4, 0x0000); |
|
488 |
EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index |
|
489 |
||
490 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
491 |
fsm->retries = EC_FSM_RETRIES; |
436 | 492 |
fsm->state = ec_fsm_coe_dict_desc_request; |
493 |
} |
|
494 |
||
495 |
/*****************************************************************************/ |
|
496 |
||
497 |
/** |
|
498 |
CoE state: DICT DESC REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
499 |
\todo Timeout behavior |
436 | 500 |
*/ |
501 |
||
502 |
void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
503 |
{ |
|
504 |
ec_datagram_t *datagram = fsm->datagram; |
|
505 |
ec_slave_t *slave = fsm->slave; |
|
506 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
507 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
508 |
// FIXME: check for response first? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
509 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
510 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
511 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
512 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
513 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
514 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
515 |
EC_ERR("Failed to receive CoE SDO description request datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
516 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
517 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
518 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
519 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
520 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
521 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
522 |
EC_ERR("Reception of CoE SDO description request failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
523 |
" not respond.\n", slave->ring_position); |
436 | 524 |
return; |
525 |
} |
|
526 |
||
527 |
fsm->cycles_start = datagram->cycles_sent; |
|
528 |
||
529 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
530 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
531 |
fsm->retries = EC_FSM_RETRIES; |
436 | 532 |
fsm->state = ec_fsm_coe_dict_desc_check; |
533 |
} |
|
534 |
||
535 |
/*****************************************************************************/ |
|
536 |
||
537 |
/** |
|
538 |
CoE state: DICT DESC CHECK. |
|
539 |
*/ |
|
540 |
||
541 |
void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
542 |
{ |
|
543 |
ec_datagram_t *datagram = fsm->datagram; |
|
544 |
ec_slave_t *slave = fsm->slave; |
|
545 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
546 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
547 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
548 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
549 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
550 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
551 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
552 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
553 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i.\n", |
436 | 554 |
slave->ring_position); |
555 |
return; |
|
556 |
} |
|
557 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
558 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
559 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
560 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
561 |
" not respond.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
562 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
563 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
564 |
|
436 | 565 |
if (!ec_slave_mbox_check(datagram)) { |
566 |
if (datagram->cycles_received |
|
567 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
568 |
fsm->state = ec_fsm_coe_error; |
|
569 |
EC_ERR("Timeout while checking SDO description on slave %i.\n", |
|
570 |
slave->ring_position); |
|
571 |
return; |
|
572 |
} |
|
573 |
||
574 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
575 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
576 |
fsm->retries = EC_FSM_RETRIES; |
436 | 577 |
return; |
578 |
} |
|
579 |
||
580 |
// Fetch response |
|
581 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
582 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
583 |
fsm->retries = EC_FSM_RETRIES; |
436 | 584 |
fsm->state = ec_fsm_coe_dict_desc_response; |
585 |
} |
|
586 |
||
587 |
/*****************************************************************************/ |
|
588 |
||
589 |
/** |
|
590 |
CoE state: DICT DESC RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
591 |
\todo Timeout behavior |
436 | 592 |
*/ |
593 |
||
594 |
void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *fsm |
|
595 |
/**< finite state machine */) |
|
596 |
{ |
|
597 |
ec_datagram_t *datagram = fsm->datagram; |
|
598 |
ec_slave_t *slave = fsm->slave; |
|
599 |
ec_sdo_t *sdo = fsm->sdo; |
|
600 |
uint8_t *data, mbox_prot; |
|
601 |
size_t rec_size, name_size; |
|
602 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
603 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
604 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
605 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
606 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
607 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
608 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
609 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
610 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
611 |
EC_ERR("Failed to receive CoE SDO description response datagram from" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
612 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
613 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
614 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
615 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
616 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
617 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
618 |
EC_ERR("Reception of CoE SDO description response failed - slave %i" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
619 |
" did not respond.\n", slave->ring_position); |
436 | 620 |
return; |
621 |
} |
|
622 |
||
623 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
624 |
&mbox_prot, &rec_size))) { |
|
625 |
fsm->state = ec_fsm_coe_error; |
|
626 |
return; |
|
627 |
} |
|
628 |
||
629 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 630 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 631 |
fsm->state = ec_fsm_coe_error; |
632 |
return; |
|
633 |
} |
|
634 |
||
635 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
636 |
(EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response |
|
637 |
EC_ERR("SDO information error response at slave %i while" |
|
638 |
" fetching SDO 0x%04X!\n", slave->ring_position, |
|
639 |
sdo->index); |
|
640 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
641 |
fsm->state = ec_fsm_coe_error; |
|
642 |
return; |
|
643 |
} |
|
644 |
||
645 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
646 |
(EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response |
|
647 |
EC_READ_U16(data + 6) != sdo->index) { // SDO index |
|
648 |
EC_ERR("Invalid object description response at slave %i while" |
|
649 |
" fetching SDO 0x%04X!\n", slave->ring_position, |
|
650 |
sdo->index); |
|
651 |
ec_print_data(data, rec_size); |
|
652 |
fsm->state = ec_fsm_coe_error; |
|
653 |
return; |
|
654 |
} |
|
655 |
||
656 |
if (rec_size < 12) { |
|
657 |
EC_ERR("Invalid data size!\n"); |
|
658 |
ec_print_data(data, rec_size); |
|
659 |
fsm->state = ec_fsm_coe_error; |
|
660 |
return; |
|
661 |
} |
|
662 |
||
663 |
sdo->subindices = EC_READ_U8(data + 10); |
|
664 |
sdo->object_code = EC_READ_U8(data + 11); |
|
665 |
||
666 |
name_size = rec_size - 12; |
|
667 |
if (name_size) { |
|
668 |
if (!(sdo->name = kmalloc(name_size + 1, GFP_ATOMIC))) { |
|
669 |
EC_ERR("Failed to allocate SDO name!\n"); |
|
670 |
fsm->state = ec_fsm_coe_error; |
|
671 |
return; |
|
672 |
} |
|
673 |
||
674 |
memcpy(sdo->name, data + 12, name_size); |
|
675 |
sdo->name[name_size] = 0; |
|
676 |
} |
|
677 |
||
678 |
if (EC_READ_U8(data + 2) & 0x80) { |
|
679 |
EC_ERR("Fragment follows (not implemented)!\n"); |
|
680 |
fsm->state = ec_fsm_coe_error; |
|
681 |
return; |
|
682 |
} |
|
683 |
||
684 |
// start fetching entries |
|
685 |
||
686 |
fsm->subindex = 0; |
|
687 |
||
688 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
|
689 |
fsm->state = ec_fsm_coe_error; |
|
690 |
return; |
|
691 |
} |
|
692 |
||
693 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
694 |
EC_WRITE_U8 (data + 2, 0x05); // Get entry description request |
|
695 |
EC_WRITE_U8 (data + 3, 0x00); |
|
696 |
EC_WRITE_U16(data + 4, 0x0000); |
|
697 |
EC_WRITE_U16(data + 6, sdo->index); // SDO index |
|
698 |
EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex |
|
699 |
EC_WRITE_U8 (data + 9, 0x00); // value info (no values) |
|
700 |
||
701 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
702 |
fsm->retries = EC_FSM_RETRIES; |
436 | 703 |
fsm->state = ec_fsm_coe_dict_entry_request; |
704 |
} |
|
705 |
||
706 |
/*****************************************************************************/ |
|
707 |
||
708 |
/** |
|
709 |
CoE state: DICT ENTRY REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
710 |
\todo Timeout behavior |
436 | 711 |
*/ |
712 |
||
713 |
void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *fsm |
|
714 |
/**< finite state machine */) |
|
715 |
{ |
|
716 |
ec_datagram_t *datagram = fsm->datagram; |
|
717 |
ec_slave_t *slave = fsm->slave; |
|
718 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
719 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
720 |
// FIXME: check for response first? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
721 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
722 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
723 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
724 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
725 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
726 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
727 |
EC_ERR("Failed to receive CoE SDO entry request datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
728 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
729 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
730 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
731 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
732 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
733 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
734 |
EC_ERR("Reception of CoE SDO entry request failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
735 |
" not respond.\n", slave->ring_position); |
436 | 736 |
return; |
737 |
} |
|
738 |
||
739 |
fsm->cycles_start = datagram->cycles_sent; |
|
740 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
741 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail |
436 | 742 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
743 |
fsm->retries = EC_FSM_RETRIES; |
436 | 744 |
fsm->state = ec_fsm_coe_dict_entry_check; |
745 |
} |
|
746 |
||
747 |
/*****************************************************************************/ |
|
748 |
||
749 |
/** |
|
750 |
CoE state: DICT ENTRY CHECK. |
|
751 |
*/ |
|
752 |
||
753 |
void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *fsm |
|
754 |
/**< finite state machine */) |
|
755 |
{ |
|
756 |
ec_datagram_t *datagram = fsm->datagram; |
|
757 |
ec_slave_t *slave = fsm->slave; |
|
758 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
759 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
760 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
761 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
762 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
763 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
764 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
765 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
766 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i.\n", |
436 | 767 |
slave->ring_position); |
768 |
return; |
|
769 |
} |
|
770 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
771 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
772 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
773 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
774 |
" not respond.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
775 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
776 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
777 |
|
436 | 778 |
if (!ec_slave_mbox_check(datagram)) { |
779 |
if (datagram->cycles_received |
|
780 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
781 |
fsm->state = ec_fsm_coe_error; |
|
782 |
EC_ERR("Timeout while checking SDO entry on slave %i.\n", |
|
783 |
slave->ring_position); |
|
784 |
return; |
|
785 |
} |
|
786 |
||
787 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
788 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
789 |
fsm->retries = EC_FSM_RETRIES; |
436 | 790 |
return; |
791 |
} |
|
792 |
||
793 |
// Fetch response |
|
794 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
795 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
796 |
fsm->retries = EC_FSM_RETRIES; |
436 | 797 |
fsm->state = ec_fsm_coe_dict_entry_response; |
798 |
} |
|
799 |
||
800 |
/*****************************************************************************/ |
|
801 |
||
802 |
/** |
|
803 |
CoE state: DICT ENTRY RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
804 |
\todo Timeout behavior |
436 | 805 |
*/ |
806 |
||
807 |
void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *fsm |
|
808 |
/**< finite state machine */) |
|
809 |
{ |
|
810 |
ec_datagram_t *datagram = fsm->datagram; |
|
811 |
ec_slave_t *slave = fsm->slave; |
|
812 |
ec_sdo_t *sdo = fsm->sdo; |
|
813 |
uint8_t *data, mbox_prot; |
|
814 |
size_t rec_size, data_size; |
|
815 |
ec_sdo_entry_t *entry; |
|
816 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
817 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
818 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
819 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
820 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
821 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
822 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
823 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
824 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
825 |
EC_ERR("Failed to receive CoE SDO description response datagram from" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
826 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
827 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
828 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
829 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
830 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
831 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
832 |
EC_ERR("Reception of CoE SDO description response failed - slave %i" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
833 |
" did not respond.\n", slave->ring_position); |
436 | 834 |
return; |
835 |
} |
|
836 |
||
837 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
838 |
&mbox_prot, &rec_size))) { |
|
839 |
fsm->state = ec_fsm_coe_error; |
|
840 |
return; |
|
841 |
} |
|
842 |
||
843 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 844 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 845 |
fsm->state = ec_fsm_coe_error; |
846 |
return; |
|
847 |
} |
|
848 |
||
849 |
if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information |
|
850 |
(EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response |
|
851 |
EC_ERR("SDO information error response at slave %i while" |
|
852 |
" fetching SDO entry 0x%04X:%i!\n", slave->ring_position, |
|
853 |
sdo->index, fsm->subindex); |
|
854 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
855 |
fsm->state = ec_fsm_coe_error; |
|
856 |
return; |
|
857 |
} |
|
858 |
||
859 |
if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information |
|
860 |
(EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response |
|
861 |
EC_READ_U16(data + 6) != sdo->index || // SDO index |
|
862 |
EC_READ_U8(data + 8) != fsm->subindex) { // SDO subindex |
|
863 |
EC_ERR("Invalid entry description response at slave %i while" |
|
864 |
" fetching SDO entry 0x%04X:%i!\n", slave->ring_position, |
|
865 |
sdo->index, fsm->subindex); |
|
866 |
ec_print_data(data, rec_size); |
|
867 |
fsm->state = ec_fsm_coe_error; |
|
868 |
return; |
|
869 |
} |
|
870 |
||
871 |
if (rec_size < 16) { |
|
872 |
EC_ERR("Invalid data size!\n"); |
|
873 |
ec_print_data(data, rec_size); |
|
874 |
fsm->state = ec_fsm_coe_error; |
|
875 |
return; |
|
876 |
} |
|
877 |
||
878 |
data_size = rec_size - 16; |
|
879 |
||
880 |
if (!(entry = (ec_sdo_entry_t *) |
|
881 |
kmalloc(sizeof(ec_sdo_entry_t), GFP_ATOMIC))) { |
|
882 |
EC_ERR("Failed to allocate entry!\n"); |
|
883 |
fsm->state = ec_fsm_coe_error; |
|
884 |
return; |
|
885 |
} |
|
886 |
||
887 |
if (ec_sdo_entry_init(entry, fsm->subindex, sdo)) { |
|
888 |
EC_ERR("Failed to init entry!\n"); |
|
889 |
fsm->state = ec_fsm_coe_error; |
|
890 |
return; |
|
891 |
} |
|
892 |
||
893 |
entry->data_type = EC_READ_U16(data + 10); |
|
894 |
entry->bit_length = EC_READ_U16(data + 12); |
|
895 |
||
896 |
if (data_size) { |
|
484
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
897 |
uint8_t *desc; |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
898 |
if (!(desc = kmalloc(data_size + 1, GFP_ATOMIC))) { |
436 | 899 |
EC_ERR("Failed to allocate SDO entry name!\n"); |
900 |
fsm->state = ec_fsm_coe_error; |
|
901 |
return; |
|
902 |
} |
|
484
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
903 |
memcpy(desc, data + 16, data_size); |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
904 |
desc[data_size] = 0; |
9fde4a17b820
kobject_add() in constructors.
Florian Pose <fp@igh-essen.com>
parents:
476
diff
changeset
|
905 |
entry->description = desc; |
436 | 906 |
} |
907 |
||
908 |
list_add_tail(&entry->list, &sdo->entries); |
|
909 |
||
910 |
if (fsm->subindex < sdo->subindices) { |
|
911 |
fsm->subindex++; |
|
912 |
||
913 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
|
914 |
fsm->state = ec_fsm_coe_error; |
|
915 |
return; |
|
916 |
} |
|
917 |
||
918 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
919 |
EC_WRITE_U8 (data + 2, 0x05); // Get entry description request |
|
920 |
EC_WRITE_U8 (data + 3, 0x00); |
|
921 |
EC_WRITE_U16(data + 4, 0x0000); |
|
922 |
EC_WRITE_U16(data + 6, sdo->index); // SDO index |
|
923 |
EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex |
|
924 |
EC_WRITE_U8 (data + 9, 0x00); // value info (no values) |
|
925 |
||
926 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
927 |
fsm->retries = EC_FSM_RETRIES; |
436 | 928 |
fsm->state = ec_fsm_coe_dict_entry_request; |
929 |
return; |
|
930 |
} |
|
931 |
||
932 |
// another SDO description to fetch? |
|
933 |
if (fsm->sdo->list.next != &slave->sdo_dictionary) { |
|
934 |
fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list); |
|
935 |
||
936 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) { |
|
937 |
fsm->state = ec_fsm_coe_error; |
|
938 |
return; |
|
939 |
} |
|
940 |
||
941 |
EC_WRITE_U16(data, 0x8 << 12); // SDO information |
|
942 |
EC_WRITE_U8 (data + 2, 0x03); // Get object description request |
|
943 |
EC_WRITE_U8 (data + 3, 0x00); |
|
944 |
EC_WRITE_U16(data + 4, 0x0000); |
|
945 |
EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index |
|
946 |
||
947 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
948 |
fsm->retries = EC_FSM_RETRIES; |
436 | 949 |
fsm->state = ec_fsm_coe_dict_desc_request; |
950 |
return; |
|
951 |
} |
|
952 |
||
953 |
fsm->state = ec_fsm_coe_end; |
|
954 |
} |
|
955 |
||
956 |
/****************************************************************************** |
|
957 |
* CoE state machine |
|
958 |
*****************************************************************************/ |
|
959 |
||
960 |
/** |
|
961 |
CoE state: DOWN START. |
|
962 |
*/ |
|
963 |
||
964 |
void ec_fsm_coe_down_start(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
965 |
{ |
|
966 |
ec_datagram_t *datagram = fsm->datagram; |
|
967 |
ec_slave_t *slave = fsm->slave; |
|
968 |
ec_sdo_data_t *sdodata = fsm->sdodata; |
|
969 |
uint8_t *data; |
|
970 |
||
459
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
971 |
if (fsm->slave->master->debug_level) |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
972 |
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
|
973 |
sdodata->index, sdodata->subindex, slave->ring_position); |
436 | 974 |
|
975 |
if (slave->sii_rx_mailbox_size < 6 + 10 + sdodata->size) { |
|
976 |
EC_ERR("SDO fragmenting not supported yet!\n"); |
|
977 |
fsm->state = ec_fsm_coe_error; |
|
978 |
return; |
|
979 |
} |
|
980 |
||
981 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, |
|
982 |
sdodata->size + 10))) { |
|
983 |
fsm->state = ec_fsm_coe_error; |
|
984 |
return; |
|
985 |
} |
|
986 |
||
987 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
988 |
EC_WRITE_U8 (data + 2, (0x1 // size specified |
|
989 |
| 0x1 << 5)); // Download request |
|
990 |
EC_WRITE_U16(data + 3, sdodata->index); |
|
991 |
EC_WRITE_U8 (data + 5, sdodata->subindex); |
|
992 |
EC_WRITE_U32(data + 6, sdodata->size); |
|
993 |
memcpy(data + 10, sdodata->data, sdodata->size); |
|
994 |
||
995 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
996 |
fsm->retries = EC_FSM_RETRIES; |
436 | 997 |
fsm->state = ec_fsm_coe_down_request; |
998 |
} |
|
999 |
||
1000 |
/*****************************************************************************/ |
|
1001 |
||
1002 |
/** |
|
1003 |
CoE state: DOWN REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1004 |
\todo Timeout behavior |
436 | 1005 |
*/ |
1006 |
||
1007 |
void ec_fsm_coe_down_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1008 |
{ |
|
1009 |
ec_datagram_t *datagram = fsm->datagram; |
|
1010 |
ec_slave_t *slave = fsm->slave; |
|
1011 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1012 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1013 |
// FIXME: check for response first? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1014 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1015 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1016 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1017 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1018 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1019 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1020 |
EC_ERR("Failed to receive CoE download request datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1021 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1022 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1023 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1024 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1025 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1026 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1027 |
EC_ERR("Reception of CoE download request failed - slave %i did not" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1028 |
" respond.\n", slave->ring_position); |
436 | 1029 |
return; |
1030 |
} |
|
1031 |
||
1032 |
fsm->cycles_start = datagram->cycles_sent; |
|
1033 |
||
1034 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1035 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1036 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1037 |
fsm->state = ec_fsm_coe_down_check; |
1038 |
} |
|
1039 |
||
1040 |
/*****************************************************************************/ |
|
1041 |
||
1042 |
/** |
|
1043 |
CoE state: DOWN CHECK. |
|
1044 |
*/ |
|
1045 |
||
1046 |
void ec_fsm_coe_down_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1047 |
{ |
|
1048 |
ec_datagram_t *datagram = fsm->datagram; |
|
1049 |
ec_slave_t *slave = fsm->slave; |
|
1050 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1051 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1052 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1053 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1054 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1055 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1056 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1057 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1058 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i.\n", |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1059 |
slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1060 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1061 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1062 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1063 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1064 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1065 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1066 |
" not respond.\n", slave->ring_position); |
436 | 1067 |
return; |
1068 |
} |
|
1069 |
||
1070 |
if (!ec_slave_mbox_check(datagram)) { |
|
1071 |
if (datagram->cycles_received |
|
1072 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1073 |
fsm->state = ec_fsm_coe_error; |
|
1074 |
EC_ERR("Timeout while checking SDO configuration on slave %i.\n", |
|
1075 |
slave->ring_position); |
|
1076 |
return; |
|
1077 |
} |
|
1078 |
||
1079 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1080 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1081 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1082 |
return; |
1083 |
} |
|
1084 |
||
1085 |
// Fetch response |
|
1086 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
1087 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1088 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1089 |
fsm->state = ec_fsm_coe_down_response; |
1090 |
} |
|
1091 |
||
1092 |
/*****************************************************************************/ |
|
1093 |
||
1094 |
/** |
|
1095 |
CoE state: DOWN RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1096 |
\todo Timeout behavior |
436 | 1097 |
*/ |
1098 |
||
1099 |
void ec_fsm_coe_down_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1100 |
{ |
|
1101 |
ec_datagram_t *datagram = fsm->datagram; |
|
1102 |
ec_slave_t *slave = fsm->slave; |
|
1103 |
uint8_t *data, mbox_prot; |
|
1104 |
size_t rec_size; |
|
1105 |
ec_sdo_data_t *sdodata = fsm->sdodata; |
|
1106 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1107 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1108 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1109 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1110 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1111 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1112 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1113 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1114 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1115 |
EC_ERR("Failed to receive CoE download response datagram from" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1116 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1117 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1118 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1119 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1120 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1121 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1122 |
EC_ERR("Reception of CoE download response failed - slave %i did not" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1123 |
" respond.\n", slave->ring_position); |
436 | 1124 |
return; |
1125 |
} |
|
1126 |
||
1127 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1128 |
&mbox_prot, &rec_size))) { |
|
1129 |
fsm->state = ec_fsm_coe_error; |
|
1130 |
return; |
|
1131 |
} |
|
1132 |
||
1133 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 1134 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 1135 |
fsm->state = ec_fsm_coe_error; |
1136 |
return; |
|
1137 |
} |
|
1138 |
||
1139 |
if (rec_size < 6) { |
|
1140 |
fsm->state = ec_fsm_coe_error; |
|
1141 |
EC_ERR("Received data is too small (%i bytes):\n", rec_size); |
|
1142 |
ec_print_data(data, rec_size); |
|
1143 |
return; |
|
1144 |
} |
|
1145 |
||
1146 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1147 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1148 |
fsm->state = ec_fsm_coe_error; |
|
1149 |
EC_ERR("SDO download 0x%04X:%X (%i bytes) aborted on slave %i.\n", |
|
1150 |
sdodata->index, sdodata->subindex, sdodata->size, |
|
1151 |
slave->ring_position); |
|
1152 |
if (rec_size < 10) { |
|
1153 |
EC_ERR("Incomplete Abort command:\n"); |
|
1154 |
ec_print_data(data, rec_size); |
|
1155 |
} |
|
1156 |
else |
|
1157 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
1158 |
return; |
|
1159 |
} |
|
1160 |
||
1161 |
if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response |
|
1162 |
EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response |
|
1163 |
EC_READ_U16(data + 3) != sdodata->index || // index |
|
1164 |
EC_READ_U8 (data + 5) != sdodata->subindex) { // subindex |
|
1165 |
fsm->state = ec_fsm_coe_error; |
|
1166 |
EC_ERR("SDO download 0x%04X:%X (%i bytes) failed:\n", |
|
1167 |
sdodata->index, sdodata->subindex, sdodata->size); |
|
1168 |
EC_ERR("Invalid SDO download response at slave %i!\n", |
|
1169 |
slave->ring_position); |
|
1170 |
ec_print_data(data, rec_size); |
|
1171 |
return; |
|
1172 |
} |
|
1173 |
||
1174 |
fsm->state = ec_fsm_coe_end; // success |
|
1175 |
} |
|
1176 |
||
1177 |
/*****************************************************************************/ |
|
1178 |
||
1179 |
/** |
|
1180 |
CoE state: UP START. |
|
1181 |
*/ |
|
1182 |
||
1183 |
void ec_fsm_coe_up_start(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1184 |
{ |
|
1185 |
ec_datagram_t *datagram = fsm->datagram; |
|
1186 |
ec_slave_t *slave = fsm->slave; |
|
1187 |
ec_master_t *master = slave->master; |
|
1188 |
ec_sdo_request_t *request = fsm->request; |
|
1189 |
ec_sdo_t *sdo = request->sdo; |
|
1190 |
ec_sdo_entry_t *entry = request->entry; |
|
1191 |
uint8_t *data; |
|
1192 |
||
459
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
1193 |
if (master->debug_level) |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
1194 |
EC_DBG("Uploading SDO 0x%04X:%i from slave %i.\n", |
f0da52cf6b5b
Print certain logs only at debug_level.
Florian Pose <fp@igh-essen.com>
parents:
446
diff
changeset
|
1195 |
sdo->index, entry->subindex, slave->ring_position); |
436 | 1196 |
|
476
fd7c0ee645f8
Fixed CoE Upload Request, now 10 bytes of Mailbox Service Data.
Florian Pose <fp@igh-essen.com>
parents:
461
diff
changeset
|
1197 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) { |
436 | 1198 |
fsm->state = ec_fsm_coe_error; |
1199 |
return; |
|
1200 |
} |
|
1201 |
||
1202 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1203 |
EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request |
|
1204 |
EC_WRITE_U16(data + 3, sdo->index); |
|
1205 |
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
|
1206 |
memset(data + 6, 0x00, 4); |
436 | 1207 |
|
1208 |
if (master->debug_level) { |
|
1209 |
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
|
1210 |
ec_print_data(data, 10); |
436 | 1211 |
} |
1212 |
||
1213 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1214 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1215 |
fsm->state = ec_fsm_coe_up_request; |
1216 |
} |
|
1217 |
||
1218 |
/*****************************************************************************/ |
|
1219 |
||
1220 |
/** |
|
1221 |
CoE state: UP REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1222 |
\todo Timeout behavior |
436 | 1223 |
*/ |
1224 |
||
1225 |
void ec_fsm_coe_up_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1226 |
{ |
|
1227 |
ec_datagram_t *datagram = fsm->datagram; |
|
1228 |
ec_slave_t *slave = fsm->slave; |
|
1229 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1230 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1231 |
// FIXME: check for response first? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1232 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1233 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1234 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1235 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1236 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1237 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1238 |
EC_ERR("Failed to receive CoE upload request for slave %i.\n", |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1239 |
slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1240 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1241 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1242 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1243 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1244 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1245 |
EC_ERR("Reception of CoE upload request failed - slave %i did not" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1246 |
" respond.\n", slave->ring_position); |
436 | 1247 |
return; |
1248 |
} |
|
1249 |
||
1250 |
fsm->cycles_start = datagram->cycles_sent; |
|
1251 |
||
1252 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1253 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1254 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1255 |
fsm->state = ec_fsm_coe_up_check; |
1256 |
} |
|
1257 |
||
1258 |
/*****************************************************************************/ |
|
1259 |
||
1260 |
/** |
|
1261 |
CoE state: UP CHECK. |
|
1262 |
*/ |
|
1263 |
||
1264 |
void ec_fsm_coe_up_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1265 |
{ |
|
1266 |
ec_datagram_t *datagram = fsm->datagram; |
|
1267 |
ec_slave_t *slave = fsm->slave; |
|
1268 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1269 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1270 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1271 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1272 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1273 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1274 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1275 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1276 |
EC_ERR("Failed to receive CoE mailbox check datagram from slave %i.\n", |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1277 |
slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1278 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1279 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1280 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1281 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1282 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1283 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1284 |
" not respond.\n", slave->ring_position); |
436 | 1285 |
return; |
1286 |
} |
|
1287 |
||
1288 |
if (!ec_slave_mbox_check(datagram)) { |
|
1289 |
if (datagram->cycles_received |
|
1290 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1291 |
fsm->state = ec_fsm_coe_error; |
|
1292 |
EC_ERR("Timeout while checking SDO upload on slave %i.\n", |
|
1293 |
slave->ring_position); |
|
1294 |
return; |
|
1295 |
} |
|
1296 |
||
1297 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1298 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1299 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1300 |
return; |
1301 |
} |
|
1302 |
||
1303 |
// Fetch response |
|
1304 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
1305 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1306 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1307 |
fsm->state = ec_fsm_coe_up_response; |
1308 |
} |
|
1309 |
||
1310 |
/*****************************************************************************/ |
|
1311 |
||
1312 |
/** |
|
1313 |
CoE state: UP RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1314 |
\todo Timeout behavior |
436 | 1315 |
*/ |
1316 |
||
1317 |
void ec_fsm_coe_up_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1318 |
{ |
|
1319 |
ec_datagram_t *datagram = fsm->datagram; |
|
1320 |
ec_slave_t *slave = fsm->slave; |
|
1321 |
ec_master_t *master = slave->master; |
|
1322 |
uint8_t *data, mbox_prot; |
|
1323 |
size_t rec_size, data_size; |
|
1324 |
ec_sdo_request_t *request = fsm->request; |
|
1325 |
ec_sdo_t *sdo = request->sdo; |
|
1326 |
ec_sdo_entry_t *entry = request->entry; |
|
1327 |
uint32_t complete_size; |
|
1328 |
unsigned int expedited, size_specified; |
|
1329 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1330 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1331 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1332 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1333 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1334 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1335 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1336 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1337 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1338 |
EC_ERR("Failed to receive CoE upload response datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1339 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1340 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1341 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1342 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1343 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1344 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1345 |
EC_ERR("Reception of CoE upload response failed - slave %i did not" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1346 |
" respond.\n", slave->ring_position); |
436 | 1347 |
return; |
1348 |
} |
|
1349 |
||
1350 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1351 |
&mbox_prot, &rec_size))) { |
|
1352 |
fsm->state = ec_fsm_coe_error; |
|
1353 |
return; |
|
1354 |
} |
|
1355 |
||
1356 |
if (master->debug_level) { |
|
1357 |
EC_DBG("Upload response:\n"); |
|
1358 |
ec_print_data(data, rec_size); |
|
1359 |
} |
|
1360 |
||
1361 |
if (mbox_prot != 0x03) { // CoE |
|
1362 |
EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
|
1363 |
fsm->state = ec_fsm_coe_error; |
|
1364 |
return; |
|
1365 |
} |
|
1366 |
||
1367 |
if (rec_size < 10) { |
|
1368 |
EC_ERR("Received currupted SDO upload response!\n"); |
|
1369 |
ec_print_data(data, rec_size); |
|
1370 |
fsm->state = ec_fsm_coe_error; |
|
1371 |
return; |
|
1372 |
} |
|
1373 |
||
1374 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1375 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1376 |
EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n", |
|
1377 |
sdo->index, entry->subindex, slave->ring_position); |
|
1378 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
1379 |
fsm->state = ec_fsm_coe_error; |
|
1380 |
return; |
|
1381 |
} |
|
1382 |
||
1383 |
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
|
1384 |
EC_READ_U8 (data + 2) >> 5 != 0x2 || // upload response |
436 | 1385 |
EC_READ_U16(data + 3) != sdo->index || // index |
1386 |
EC_READ_U8 (data + 5) != entry->subindex) { // subindex |
|
1387 |
EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo->index, entry->subindex); |
|
1388 |
EC_ERR("Invalid SDO upload response at slave %i!\n", |
|
1389 |
slave->ring_position); |
|
1390 |
ec_print_data(data, rec_size); |
|
1391 |
fsm->state = ec_fsm_coe_error; |
|
1392 |
return; |
|
1393 |
} |
|
1394 |
||
1395 |
data_size = rec_size - 10; |
|
1396 |
expedited = EC_READ_U8(data + 2) & 0x02; |
|
1397 |
||
1398 |
if (expedited) { |
|
1399 |
size_specified = EC_READ_U8(data + 2) & 0x01; |
|
1400 |
if (size_specified) { |
|
1401 |
complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2); |
|
1402 |
} |
|
1403 |
else { |
|
1404 |
complete_size = 4; |
|
1405 |
} |
|
1406 |
} |
|
1407 |
else { |
|
1408 |
complete_size = EC_READ_U32(data + 6); |
|
1409 |
} |
|
1410 |
||
1411 |
if (request->data) { |
|
1412 |
kfree(request->data); |
|
1413 |
request->data = NULL; |
|
1414 |
} |
|
1415 |
request->size = 0; |
|
1416 |
||
1417 |
if (complete_size) { |
|
446 | 1418 |
if (!(request->data = (uint8_t *) |
1419 |
kmalloc(complete_size + 1, GFP_ATOMIC))) { |
|
1420 |
EC_ERR("Failed to allocate %i bytes of SDO data!\n", |
|
1421 |
complete_size); |
|
436 | 1422 |
fsm->state = ec_fsm_coe_error; |
1423 |
return; |
|
1424 |
} |
|
1425 |
request->data[complete_size] = 0x00; // just to be sure... |
|
1426 |
} |
|
1427 |
||
1428 |
if (expedited) { |
|
1429 |
memcpy(request->data, data + 6, complete_size); |
|
1430 |
request->size = complete_size; |
|
1431 |
} |
|
1432 |
else { |
|
1433 |
memcpy(request->data, data + 10, data_size); |
|
1434 |
request->size = data_size; |
|
1435 |
fsm->toggle = 0; |
|
1436 |
||
1437 |
if (data_size < complete_size) { |
|
446 | 1438 |
EC_WARN("SDO data incomplete (%i / %i).\n", |
1439 |
data_size, complete_size); |
|
1440 |
||
1441 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, |
|
1442 |
0x03, 3))) { |
|
436 | 1443 |
fsm->state = ec_fsm_coe_error; |
1444 |
return; |
|
1445 |
} |
|
1446 |
||
1447 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1448 |
EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle |
|
1449 |
| 0x3 << 5)); // upload segment request |
|
1450 |
||
1451 |
if (master->debug_level) { |
|
1452 |
EC_DBG("Upload segment request:\n"); |
|
1453 |
ec_print_data(data, 3); |
|
1454 |
} |
|
1455 |
||
1456 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1457 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1458 |
fsm->state = ec_fsm_coe_up_seg_request; |
1459 |
return; |
|
1460 |
} |
|
1461 |
} |
|
1462 |
||
1463 |
fsm->state = ec_fsm_coe_end; // success |
|
1464 |
} |
|
1465 |
||
1466 |
/*****************************************************************************/ |
|
1467 |
||
1468 |
/** |
|
1469 |
CoE state: UP REQUEST. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1470 |
\todo Timeout behavior |
436 | 1471 |
*/ |
1472 |
||
1473 |
void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1474 |
{ |
|
1475 |
ec_datagram_t *datagram = fsm->datagram; |
|
1476 |
ec_slave_t *slave = fsm->slave; |
|
1477 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1478 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1479 |
// FIXME: check for response first? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1480 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1481 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1482 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1483 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1484 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1485 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1486 |
EC_ERR("Failed to receive CoE upload segment request datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1487 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1488 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1489 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1490 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1491 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1492 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1493 |
EC_ERR("Reception of CoE upload segment request failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1494 |
" not respond.\n", slave->ring_position); |
436 | 1495 |
return; |
1496 |
} |
|
1497 |
||
1498 |
fsm->cycles_start = datagram->cycles_sent; |
|
1499 |
||
1500 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1501 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1502 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1503 |
fsm->state = ec_fsm_coe_up_seg_check; |
1504 |
} |
|
1505 |
||
1506 |
/*****************************************************************************/ |
|
1507 |
||
1508 |
/** |
|
1509 |
CoE state: UP CHECK. |
|
1510 |
*/ |
|
1511 |
||
1512 |
void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1513 |
{ |
|
1514 |
ec_datagram_t *datagram = fsm->datagram; |
|
1515 |
ec_slave_t *slave = fsm->slave; |
|
1516 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1517 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1518 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1519 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1520 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1521 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1522 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1523 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1524 |
EC_ERR("Failed to receive CoE mailbox check datagram for slave %i.\n", |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1525 |
slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1526 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1527 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1528 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1529 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1530 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1531 |
EC_ERR("Reception of CoE mailbox check datagram failed - slave %i did" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1532 |
" not respond.\n", slave->ring_position); |
436 | 1533 |
return; |
1534 |
} |
|
1535 |
||
1536 |
if (!ec_slave_mbox_check(datagram)) { |
|
1537 |
if (datagram->cycles_received |
|
1538 |
- fsm->cycles_start >= (cycles_t) 100 * cpu_khz) { |
|
1539 |
fsm->state = ec_fsm_coe_error; |
|
1540 |
EC_ERR("Timeout while checking SDO upload segment on slave %i.\n", |
|
1541 |
slave->ring_position); |
|
1542 |
return; |
|
1543 |
} |
|
1544 |
||
1545 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
1546 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1547 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1548 |
return; |
1549 |
} |
|
1550 |
||
1551 |
// Fetch response |
|
1552 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
1553 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1554 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1555 |
fsm->state = ec_fsm_coe_up_seg_response; |
1556 |
} |
|
1557 |
||
1558 |
/*****************************************************************************/ |
|
1559 |
||
1560 |
/** |
|
1561 |
CoE state: UP RESPONSE. |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1562 |
\todo Timeout behavior |
436 | 1563 |
*/ |
1564 |
||
1565 |
void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1566 |
{ |
|
1567 |
ec_datagram_t *datagram = fsm->datagram; |
|
1568 |
ec_slave_t *slave = fsm->slave; |
|
1569 |
ec_master_t *master = slave->master; |
|
1570 |
uint8_t *data, mbox_prot; |
|
1571 |
size_t rec_size, data_size; |
|
1572 |
ec_sdo_request_t *request = fsm->request; |
|
1573 |
ec_sdo_t *sdo = request->sdo; |
|
1574 |
ec_sdo_entry_t *entry = request->entry; |
|
1575 |
uint32_t seg_size; |
|
1576 |
unsigned int last_segment; |
|
1577 |
||
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1578 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1579 |
// FIXME: request again? |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1580 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1581 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1582 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1583 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1584 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1585 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1586 |
EC_ERR("Failed to receive CoE upload segment response datagram for" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1587 |
" slave %i.\n", slave->ring_position); |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1588 |
return; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1589 |
} |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1590 |
|
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1591 |
if (datagram->working_counter != 1) { |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1592 |
fsm->state = ec_fsm_coe_error; |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1593 |
EC_ERR("Reception of CoE upload segment response failed - slave %i" |
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1594 |
" did not respond.\n", slave->ring_position); |
436 | 1595 |
return; |
1596 |
} |
|
1597 |
||
1598 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
1599 |
&mbox_prot, &rec_size))) { |
|
1600 |
fsm->state = ec_fsm_coe_error; |
|
1601 |
return; |
|
1602 |
} |
|
1603 |
||
1604 |
if (master->debug_level) { |
|
1605 |
EC_DBG("Upload segment response:\n"); |
|
1606 |
ec_print_data(data, rec_size); |
|
1607 |
} |
|
1608 |
||
1609 |
if (mbox_prot != 0x03) { // CoE |
|
446 | 1610 |
EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
436 | 1611 |
fsm->state = ec_fsm_coe_error; |
1612 |
return; |
|
1613 |
} |
|
1614 |
||
1615 |
if (rec_size < 10) { |
|
1616 |
EC_ERR("Received currupted SDO upload segment response!\n"); |
|
1617 |
ec_print_data(data, rec_size); |
|
1618 |
fsm->state = ec_fsm_coe_error; |
|
1619 |
return; |
|
1620 |
} |
|
1621 |
||
1622 |
if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request |
|
1623 |
EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request |
|
1624 |
EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n", |
|
1625 |
sdo->index, entry->subindex, slave->ring_position); |
|
1626 |
ec_canopen_abort_msg(EC_READ_U32(data + 6)); |
|
1627 |
fsm->state = ec_fsm_coe_error; |
|
1628 |
return; |
|
1629 |
} |
|
1630 |
||
1631 |
if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response |
|
1632 |
EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response |
|
1633 |
EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo->index, entry->subindex); |
|
1634 |
EC_ERR("Invalid SDO upload segment response at slave %i!\n", |
|
1635 |
slave->ring_position); |
|
1636 |
ec_print_data(data, rec_size); |
|
1637 |
fsm->state = ec_fsm_coe_error; |
|
1638 |
return; |
|
1639 |
} |
|
1640 |
||
1641 |
last_segment = EC_READ_U8(data + 2) & 0x01; |
|
1642 |
seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1; |
|
1643 |
data_size = rec_size - 10; |
|
1644 |
||
1645 |
if (data_size != seg_size) { |
|
1646 |
EC_WARN("SDO segment data invalid (%i / %i)" |
|
1647 |
" - Fragmenting not implemented.\n", |
|
1648 |
data_size, seg_size); |
|
1649 |
} |
|
1650 |
||
1651 |
memcpy(request->data + request->size, data + 10, data_size); |
|
1652 |
request->size += data_size; |
|
1653 |
||
1654 |
if (!last_segment) { |
|
1655 |
fsm->toggle = !fsm->toggle; |
|
1656 |
||
1657 |
if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 3))) { |
|
1658 |
fsm->state = ec_fsm_coe_error; |
|
1659 |
return; |
|
1660 |
} |
|
1661 |
||
1662 |
EC_WRITE_U16(data, 0x2 << 12); // SDO request |
|
1663 |
EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle |
|
1664 |
| 0x3 << 5)); // upload segment request |
|
1665 |
||
1666 |
if (master->debug_level) { |
|
1667 |
EC_DBG("Upload segment request:\n"); |
|
1668 |
ec_print_data(data, 3); |
|
1669 |
} |
|
1670 |
||
1671 |
ec_master_queue_datagram(fsm->slave->master, datagram); |
|
505
bc443ca0077f
State machines re-send datagrams on timeout.
Florian Pose <fp@igh-essen.com>
parents:
501
diff
changeset
|
1672 |
fsm->retries = EC_FSM_RETRIES; |
436 | 1673 |
fsm->state = ec_fsm_coe_up_seg_request; |
1674 |
return; |
|
1675 |
} |
|
1676 |
||
1677 |
fsm->state = ec_fsm_coe_end; // success |
|
1678 |
} |
|
1679 |
||
1680 |
/*****************************************************************************/ |
|
1681 |
||
1682 |
/** |
|
1683 |
State: ERROR. |
|
1684 |
*/ |
|
1685 |
||
1686 |
void ec_fsm_coe_error(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1687 |
{ |
|
1688 |
} |
|
1689 |
||
1690 |
/*****************************************************************************/ |
|
1691 |
||
1692 |
/** |
|
1693 |
State: END. |
|
1694 |
*/ |
|
1695 |
||
1696 |
void ec_fsm_coe_end(ec_fsm_coe_t *fsm /**< finite state machine */) |
|
1697 |
{ |
|
1698 |
} |
|
1699 |
||
1700 |
/*****************************************************************************/ |