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