author | Florian Pose <fp@igh-essen.com> |
Wed, 24 Sep 2008 08:37:35 +0000 | |
changeset 1220 | 15e8768b304f |
parent 1218 | 6f3a973fc29e |
child 1224 | 784bb76105cd |
permissions | -rw-r--r-- |
1209 | 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 |
/** \file |
|
35 |
* Vendor-specific-over-EtherCAT protocol handler functions. |
|
36 |
*/ |
|
37 |
||
38 |
/*****************************************************************************/ |
|
39 |
||
40 |
#include <linux/module.h> |
|
41 |
||
42 |
#include "master.h" |
|
43 |
#include "slave_config.h" |
|
44 |
#include "mailbox.h" |
|
45 |
#include "voe_handler.h" |
|
46 |
||
1216
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
47 |
/** VoE mailbox type. |
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
48 |
*/ |
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
49 |
#define EC_MBOX_TYPE_VOE 0xff |
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
50 |
|
1218 | 51 |
/** VoE header size. |
52 |
*/ |
|
53 |
#define EC_VOE_HEADER_SIZE 6 |
|
54 |
||
1209 | 55 |
/** VoE response timeout in [ms]. |
56 |
*/ |
|
57 |
#define EC_VOE_RESPONSE_TIMEOUT 500 |
|
58 |
||
59 |
/*****************************************************************************/ |
|
60 |
||
61 |
void ec_voe_handler_clear_data(ec_voe_handler_t *); |
|
62 |
||
63 |
void ec_voe_handler_state_write_start(ec_voe_handler_t *); |
|
64 |
void ec_voe_handler_state_write_response(ec_voe_handler_t *); |
|
65 |
||
66 |
void ec_voe_handler_state_read_start(ec_voe_handler_t *); |
|
67 |
void ec_voe_handler_state_read_check(ec_voe_handler_t *); |
|
68 |
void ec_voe_handler_state_read_response(ec_voe_handler_t *); |
|
69 |
||
70 |
void ec_voe_handler_state_end(ec_voe_handler_t *); |
|
71 |
void ec_voe_handler_state_error(ec_voe_handler_t *); |
|
72 |
||
73 |
/*****************************************************************************/ |
|
74 |
||
75 |
/** VoE handler constructor. |
|
76 |
*/ |
|
77 |
int ec_voe_handler_init( |
|
78 |
ec_voe_handler_t *voe, /**< VoE handler. */ |
|
79 |
ec_slave_config_t *sc, /**< Parent slave configuration. */ |
|
80 |
size_t size /**< Size of memory to reserve. */ |
|
81 |
) |
|
82 |
{ |
|
83 |
voe->config = sc; |
|
1218 | 84 |
voe->vendor_id = 0x00000000; |
85 |
voe->vendor_type = 0x0000; |
|
1209 | 86 |
voe->data_size = 0; |
87 |
voe->dir = EC_DIR_INVALID; |
|
88 |
voe->state = ec_voe_handler_state_error; |
|
89 |
voe->request_state = EC_INT_REQUEST_INIT; |
|
90 |
||
91 |
ec_datagram_init(&voe->datagram); |
|
1218 | 92 |
if (ec_datagram_prealloc(&voe->datagram, |
93 |
size + EC_MBOX_HEADER_SIZE + EC_VOE_HEADER_SIZE)) |
|
1209 | 94 |
return -1; |
95 |
||
96 |
return 0; |
|
97 |
} |
|
98 |
||
99 |
/*****************************************************************************/ |
|
100 |
||
101 |
/** VoE handler destructor. |
|
102 |
*/ |
|
103 |
void ec_voe_handler_clear( |
|
104 |
ec_voe_handler_t *voe /**< VoE handler. */ |
|
105 |
) |
|
106 |
{ |
|
107 |
ec_datagram_clear(&voe->datagram); |
|
108 |
} |
|
109 |
||
110 |
/***************************************************************************** |
|
111 |
* Application interface. |
|
112 |
****************************************************************************/ |
|
113 |
||
1218 | 114 |
void ecrt_voe_handler_header(ec_voe_handler_t *voe, uint32_t vendor_id, |
115 |
uint16_t vendor_type) |
|
116 |
{ |
|
117 |
voe->vendor_id = vendor_id; |
|
118 |
voe->vendor_type = vendor_type; |
|
119 |
} |
|
120 |
||
121 |
/*****************************************************************************/ |
|
122 |
||
1209 | 123 |
uint8_t *ecrt_voe_handler_data(ec_voe_handler_t *voe) |
124 |
{ |
|
1218 | 125 |
return voe->datagram.data + EC_MBOX_HEADER_SIZE + EC_VOE_HEADER_SIZE; |
1209 | 126 |
} |
127 |
||
128 |
/*****************************************************************************/ |
|
129 |
||
130 |
size_t ecrt_voe_handler_data_size(const ec_voe_handler_t *voe) |
|
131 |
{ |
|
132 |
return voe->data_size; |
|
133 |
} |
|
134 |
||
135 |
/*****************************************************************************/ |
|
136 |
||
137 |
void ecrt_voe_handler_read(ec_voe_handler_t *voe) |
|
138 |
{ |
|
139 |
voe->dir = EC_DIR_INPUT; |
|
140 |
voe->state = ec_voe_handler_state_read_start; |
|
141 |
voe->request_state = EC_INT_REQUEST_QUEUED; |
|
142 |
} |
|
143 |
||
144 |
/*****************************************************************************/ |
|
145 |
||
146 |
void ecrt_voe_handler_write(ec_voe_handler_t *voe, size_t size) |
|
147 |
{ |
|
148 |
voe->dir = EC_DIR_OUTPUT; |
|
1218 | 149 |
voe->data_size = size; |
1209 | 150 |
voe->state = ec_voe_handler_state_write_start; |
151 |
voe->request_state = EC_INT_REQUEST_QUEUED; |
|
152 |
} |
|
153 |
||
154 |
/*****************************************************************************/ |
|
155 |
||
156 |
ec_request_state_t ecrt_voe_handler_execute(ec_voe_handler_t *voe) |
|
157 |
{ |
|
158 |
if (voe->config->slave) { |
|
159 |
voe->state(voe); |
|
160 |
if (voe->request_state == EC_REQUEST_BUSY) |
|
161 |
ec_master_queue_datagram(voe->config->master, &voe->datagram); |
|
162 |
} else { |
|
163 |
voe->state = ec_voe_handler_state_error; |
|
164 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
165 |
} |
|
166 |
||
167 |
return ec_request_state_translation_table[voe->request_state]; |
|
168 |
} |
|
169 |
||
170 |
/****************************************************************************** |
|
171 |
* State functions. |
|
172 |
*****************************************************************************/ |
|
173 |
||
174 |
void ec_voe_handler_state_write_start(ec_voe_handler_t *voe) |
|
175 |
{ |
|
176 |
ec_slave_t *slave = voe->config->slave; |
|
177 |
uint8_t *data; |
|
178 |
||
179 |
if (slave->master->debug_level) { |
|
180 |
EC_DBG("Writing %u bytes of VoE data to slave %u.\n", |
|
181 |
voe->data_size, slave->ring_position); |
|
182 |
ec_print_data(ecrt_voe_handler_data(voe), voe->data_size); |
|
183 |
} |
|
184 |
||
185 |
if (!(slave->sii.mailbox_protocols & EC_MBOX_VOE)) { |
|
186 |
EC_ERR("Slave %u does not support VoE!\n", slave->ring_position); |
|
187 |
voe->state = ec_voe_handler_state_error; |
|
188 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
189 |
return; |
|
190 |
} |
|
191 |
||
1216
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
192 |
if (!(data = ec_slave_mbox_prepare_send(slave, &voe->datagram, |
1218 | 193 |
EC_MBOX_TYPE_VOE, EC_VOE_HEADER_SIZE + voe->data_size))) { |
194 |
voe->state = ec_voe_handler_state_error; |
|
195 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
196 |
return; |
|
197 |
} |
|
198 |
||
199 |
EC_WRITE_U32(data, voe->vendor_id); |
|
200 |
EC_WRITE_U16(data + 4, voe->vendor_type); |
|
1209 | 201 |
|
202 |
voe->retries = EC_FSM_RETRIES; |
|
203 |
voe->jiffies_start = jiffies; |
|
204 |
voe->state = ec_voe_handler_state_write_response; |
|
205 |
} |
|
206 |
||
207 |
/*****************************************************************************/ |
|
208 |
||
209 |
void ec_voe_handler_state_write_response(ec_voe_handler_t *voe) |
|
210 |
{ |
|
211 |
ec_datagram_t *datagram = &voe->datagram; |
|
212 |
ec_slave_t *slave = voe->config->slave; |
|
213 |
||
214 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--) |
|
215 |
return; |
|
216 |
||
217 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
|
218 |
voe->state = ec_voe_handler_state_error; |
|
219 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
220 |
EC_ERR("Failed to receive VoE write request datagram for" |
|
221 |
" slave %u (datagram state %u).\n", |
|
222 |
slave->ring_position, datagram->state); |
|
223 |
return; |
|
224 |
} |
|
225 |
||
226 |
if (datagram->working_counter != 1) { |
|
227 |
if (!datagram->working_counter) { |
|
228 |
unsigned long diff_ms = |
|
229 |
(jiffies - voe->jiffies_start) * 1000 / HZ; |
|
230 |
if (diff_ms < EC_VOE_RESPONSE_TIMEOUT) { |
|
231 |
if (slave->master->debug_level) { |
|
232 |
EC_DBG("Slave %u did not respond to VoE write request. " |
|
233 |
"Retrying after %u ms...\n", |
|
234 |
slave->ring_position, (u32) diff_ms); |
|
235 |
} |
|
236 |
// no response; send request datagram again |
|
237 |
return; |
|
238 |
} |
|
239 |
} |
|
240 |
voe->state = ec_voe_handler_state_error; |
|
241 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
242 |
EC_ERR("Reception of VoE write request failed on slave %u: ", |
|
243 |
slave->ring_position); |
|
244 |
ec_datagram_print_wc_error(datagram); |
|
245 |
return; |
|
246 |
} |
|
247 |
||
248 |
if (voe->config->master->debug_level) |
|
249 |
EC_DBG("VoE write request successful.\n"); |
|
250 |
||
251 |
voe->request_state = EC_INT_REQUEST_SUCCESS; |
|
252 |
voe->state = ec_voe_handler_state_end; |
|
253 |
} |
|
254 |
||
255 |
/*****************************************************************************/ |
|
256 |
||
257 |
void ec_voe_handler_state_read_start(ec_voe_handler_t *voe) |
|
258 |
{ |
|
259 |
ec_datagram_t *datagram = &voe->datagram; |
|
260 |
ec_slave_t *slave = voe->config->slave; |
|
261 |
||
262 |
if (slave->master->debug_level) |
|
263 |
EC_DBG("Reading VoE data to slave %u.\n", slave->ring_position); |
|
264 |
||
265 |
if (!(slave->sii.mailbox_protocols & EC_MBOX_VOE)) { |
|
266 |
EC_ERR("Slave %u does not support VoE!\n", slave->ring_position); |
|
267 |
voe->state = ec_voe_handler_state_error; |
|
268 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
269 |
return; |
|
270 |
} |
|
271 |
||
272 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
273 |
||
274 |
voe->jiffies_start = jiffies; |
|
275 |
voe->retries = EC_FSM_RETRIES; |
|
276 |
voe->state = ec_voe_handler_state_read_check; |
|
277 |
} |
|
278 |
||
279 |
/*****************************************************************************/ |
|
280 |
||
281 |
void ec_voe_handler_state_read_check(ec_voe_handler_t *voe) |
|
282 |
{ |
|
283 |
ec_datagram_t *datagram = &voe->datagram; |
|
284 |
ec_slave_t *slave = voe->config->slave; |
|
285 |
||
286 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--) |
|
287 |
return; |
|
288 |
||
289 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
|
290 |
voe->state = ec_voe_handler_state_error; |
|
291 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
292 |
EC_ERR("Failed to receive VoE mailbox check datagram from slave %u" |
|
293 |
" (datagram state %u).\n", |
|
294 |
slave->ring_position, datagram->state); |
|
295 |
return; |
|
296 |
} |
|
297 |
||
298 |
if (datagram->working_counter != 1) { |
|
299 |
voe->state = ec_voe_handler_state_error; |
|
300 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
301 |
EC_ERR("Reception of VoE mailbox check" |
|
302 |
" datagram failed on slave %u: ", slave->ring_position); |
|
303 |
ec_datagram_print_wc_error(datagram); |
|
304 |
return; |
|
305 |
} |
|
306 |
||
307 |
if (!ec_slave_mbox_check(datagram)) { |
|
308 |
unsigned long diff_ms = |
|
309 |
(datagram->jiffies_received - voe->jiffies_start) * 1000 / HZ; |
|
310 |
if (diff_ms >= EC_VOE_RESPONSE_TIMEOUT) { |
|
311 |
voe->state = ec_voe_handler_state_error; |
|
312 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
313 |
EC_ERR("Timeout while waiting for VoE data on " |
|
314 |
"slave %u.\n", slave->ring_position); |
|
315 |
return; |
|
316 |
} |
|
317 |
||
318 |
ec_slave_mbox_prepare_check(slave, datagram); // can not fail. |
|
319 |
voe->retries = EC_FSM_RETRIES; |
|
320 |
return; |
|
321 |
} |
|
322 |
||
323 |
// Fetch response |
|
324 |
ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. |
|
325 |
voe->retries = EC_FSM_RETRIES; |
|
326 |
voe->state = ec_voe_handler_state_read_response; |
|
327 |
} |
|
328 |
||
329 |
/*****************************************************************************/ |
|
330 |
||
331 |
void ec_voe_handler_state_read_response(ec_voe_handler_t *voe) |
|
332 |
{ |
|
333 |
ec_datagram_t *datagram = &voe->datagram; |
|
334 |
ec_slave_t *slave = voe->config->slave; |
|
335 |
ec_master_t *master = voe->config->master; |
|
336 |
uint8_t *data, mbox_prot; |
|
337 |
size_t rec_size; |
|
338 |
||
339 |
if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--) |
|
340 |
return; |
|
341 |
||
342 |
if (datagram->state != EC_DATAGRAM_RECEIVED) { |
|
343 |
voe->state = ec_voe_handler_state_error; |
|
344 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
345 |
EC_ERR("Failed to receive VoE read datagram for" |
|
346 |
" slave %u (datagram state %u).\n", |
|
347 |
slave->ring_position, datagram->state); |
|
348 |
return; |
|
349 |
} |
|
350 |
||
351 |
if (datagram->working_counter != 1) { |
|
352 |
voe->state = ec_voe_handler_state_error; |
|
353 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
354 |
EC_ERR("Reception of VoE read response failed on slave %u: ", |
|
355 |
slave->ring_position); |
|
356 |
ec_datagram_print_wc_error(datagram); |
|
357 |
return; |
|
358 |
} |
|
359 |
||
360 |
if (!(data = ec_slave_mbox_fetch(slave, datagram, |
|
361 |
&mbox_prot, &rec_size))) { |
|
362 |
voe->state = ec_voe_handler_state_error; |
|
363 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
364 |
return; |
|
365 |
} |
|
366 |
||
1216
e70c4d71cebd
Changed VoE mailbox type to 0xff.
Florian Pose <fp@igh-essen.com>
parents:
1209
diff
changeset
|
367 |
if (mbox_prot != EC_MBOX_TYPE_VOE) { |
1209 | 368 |
voe->state = ec_voe_handler_state_error; |
369 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
370 |
EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot); |
|
371 |
ec_print_data(data, rec_size); |
|
372 |
return; |
|
373 |
} |
|
374 |
||
1218 | 375 |
if (rec_size < EC_VOE_HEADER_SIZE) { |
376 |
voe->state = ec_voe_handler_state_error; |
|
377 |
voe->request_state = EC_INT_REQUEST_FAILURE; |
|
378 |
EC_ERR("Received VoE header is incomplete (%u bytes)!\n", rec_size); |
|
379 |
return; |
|
380 |
} |
|
381 |
||
1209 | 382 |
if (master->debug_level) { |
383 |
EC_DBG("VoE data:\n"); |
|
384 |
ec_print_data(data, rec_size); |
|
385 |
} |
|
386 |
||
1218 | 387 |
voe->data_size = rec_size - EC_VOE_HEADER_SIZE; |
1209 | 388 |
voe->request_state = EC_INT_REQUEST_SUCCESS; |
389 |
voe->state = ec_voe_handler_state_end; // success |
|
390 |
} |
|
391 |
||
392 |
/*****************************************************************************/ |
|
393 |
||
394 |
void ec_voe_handler_state_end(ec_voe_handler_t *voe) |
|
395 |
{ |
|
396 |
} |
|
397 |
||
398 |
/*****************************************************************************/ |
|
399 |
||
400 |
void ec_voe_handler_state_error(ec_voe_handler_t *voe) |
|
401 |
{ |
|
402 |
} |
|
403 |
||
404 |
/*****************************************************************************/ |
|
405 |
||
406 |
/** \cond */ |
|
407 |
||
1218 | 408 |
EXPORT_SYMBOL(ecrt_voe_handler_header); |
1209 | 409 |
EXPORT_SYMBOL(ecrt_voe_handler_data); |
410 |
EXPORT_SYMBOL(ecrt_voe_handler_data_size); |
|
411 |
EXPORT_SYMBOL(ecrt_voe_handler_read); |
|
412 |
EXPORT_SYMBOL(ecrt_voe_handler_write); |
|
413 |
EXPORT_SYMBOL(ecrt_voe_handler_execute); |
|
414 |
||
415 |
/** \endcond */ |
|
416 |
||
417 |
/*****************************************************************************/ |