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