IgH EtherCAT Master  1.5.2
voe_handler.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * $Id$
4  *
5  * Copyright (C) 2006-2008 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 and/or
10  * modify it under the terms of the GNU General Public License version 2, as
11  * published by the Free Software Foundation.
12  *
13  * The IgH EtherCAT Master is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with the IgH EtherCAT Master; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * ---
23  *
24  * The license mentioned above concerns the source code only. Using the
25  * EtherCAT technology and brand is only permitted in compliance with the
26  * industrial property and similar rights of Beckhoff Automation GmbH.
27  *
28  *****************************************************************************/
29 
34 /*****************************************************************************/
35 
36 #include <linux/module.h>
37 
38 #include "master.h"
39 #include "slave_config.h"
40 #include "mailbox.h"
41 #include "voe_handler.h"
42 
45 #define EC_MBOX_TYPE_VOE 0x0f
46 
49 #define EC_VOE_HEADER_SIZE 6
50 
53 #define EC_VOE_RESPONSE_TIMEOUT 500
54 
55 /*****************************************************************************/
56 
59 
63 
66 
69 
70 /*****************************************************************************/
71 
77  ec_voe_handler_t *voe,
78  ec_slave_config_t *sc,
79  size_t size
80  )
81 {
82  voe->config = sc;
83  voe->vendor_id = 0x00000000;
84  voe->vendor_type = 0x0000;
85  voe->data_size = 0;
86  voe->dir = EC_DIR_INVALID;
88  voe->request_state = EC_INT_REQUEST_INIT;
89 
91  return ec_datagram_prealloc(&voe->datagram,
93 }
94 
95 /*****************************************************************************/
96 
100  ec_voe_handler_t *voe
101  )
102 {
104 }
105 
106 /*****************************************************************************/
107 
113  const ec_voe_handler_t *voe
114  )
115 {
117  return voe->datagram.mem_size -
119  else
120  return 0;
121 }
122 
123 /*****************************************************************************
124  * Application interface.
125  ****************************************************************************/
126 
127 void ecrt_voe_handler_send_header(ec_voe_handler_t *voe, uint32_t vendor_id,
128  uint16_t vendor_type)
129 {
130  voe->vendor_id = vendor_id;
131  voe->vendor_type = vendor_type;
132 }
133 
134 /*****************************************************************************/
135 
137  uint32_t *vendor_id, uint16_t *vendor_type)
138 {
139  uint8_t *header = voe->datagram.data + EC_MBOX_HEADER_SIZE;
140 
141  if (vendor_id)
142  *vendor_id = EC_READ_U32(header);
143  if (vendor_type)
144  *vendor_type = EC_READ_U16(header + 4);
145 }
146 
147 /*****************************************************************************/
148 
150 {
152 }
153 
154 /*****************************************************************************/
155 
157 {
158  return voe->data_size;
159 }
160 
161 /*****************************************************************************/
162 
164 {
165  voe->dir = EC_DIR_INPUT;
167  voe->request_state = EC_INT_REQUEST_BUSY;
168 }
169 
170 /*****************************************************************************/
171 
173 {
174  voe->dir = EC_DIR_INPUT;
176  voe->request_state = EC_INT_REQUEST_BUSY;
177 }
178 
179 /*****************************************************************************/
180 
182 {
183  voe->dir = EC_DIR_OUTPUT;
184  voe->data_size = size;
186  voe->request_state = EC_INT_REQUEST_BUSY;
187 }
188 
189 /*****************************************************************************/
190 
192 {
193  if (voe->config->slave) { // FIXME locking?
194  voe->state(voe);
195  if (voe->request_state == EC_INT_REQUEST_BUSY) {
197  }
198  } else {
200  voe->request_state = EC_INT_REQUEST_FAILURE;
201  }
202 
204 }
205 
206 /******************************************************************************
207  * State functions.
208  *****************************************************************************/
209 
213 {
214  ec_slave_t *slave = voe->config->slave;
215  uint8_t *data;
216 
217  if (slave->master->debug_level) {
218  EC_SLAVE_DBG(slave, 0, "Writing %zu bytes of VoE data.\n",
219  voe->data_size);
221  }
222 
223  if (!(slave->sii.mailbox_protocols & EC_MBOX_VOE)) {
224  EC_SLAVE_ERR(slave, "Slave does not support VoE!\n");
226  voe->request_state = EC_INT_REQUEST_FAILURE;
227  return;
228  }
229 
230  data = ec_slave_mbox_prepare_send(slave, &voe->datagram,
232  if (IS_ERR(data)) {
234  voe->request_state = EC_INT_REQUEST_FAILURE;
235  return;
236  }
237 
238  EC_WRITE_U32(data, voe->vendor_id);
239  EC_WRITE_U16(data + 4, voe->vendor_type);
240  /* data already in datagram */
241 
242  voe->retries = EC_FSM_RETRIES;
243  voe->jiffies_start = jiffies;
245 }
246 
247 /*****************************************************************************/
248 
252 {
253  ec_datagram_t *datagram = &voe->datagram;
254  ec_slave_t *slave = voe->config->slave;
255 
256  if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--)
257  return;
258 
259  if (datagram->state != EC_DATAGRAM_RECEIVED) {
261  voe->request_state = EC_INT_REQUEST_FAILURE;
262  EC_SLAVE_ERR(slave, "Failed to receive VoE write request datagram: ");
263  ec_datagram_print_state(datagram);
264  return;
265  }
266 
267  if (datagram->working_counter != 1) {
268  if (!datagram->working_counter) {
269  unsigned long diff_ms =
270  (jiffies - voe->jiffies_start) * 1000 / HZ;
271  if (diff_ms < EC_VOE_RESPONSE_TIMEOUT) {
272  EC_SLAVE_DBG(slave, 1, "Slave did not respond to"
273  " VoE write request. Retrying after %lu ms...\n",
274  diff_ms);
275  // no response; send request datagram again
276  return;
277  }
278  }
280  voe->request_state = EC_INT_REQUEST_FAILURE;
281  EC_SLAVE_ERR(slave, "Reception of VoE write request failed: ");
282  ec_datagram_print_wc_error(datagram);
283  return;
284  }
285 
286  EC_CONFIG_DBG(voe->config, 1, "VoE write request successful.\n");
287 
288  voe->request_state = EC_INT_REQUEST_SUCCESS;
290 }
291 
292 /*****************************************************************************/
293 
297 {
298  ec_datagram_t *datagram = &voe->datagram;
299  ec_slave_t *slave = voe->config->slave;
300 
301  EC_SLAVE_DBG(slave, 1, "Reading VoE data.\n");
302 
303  if (!(slave->sii.mailbox_protocols & EC_MBOX_VOE)) {
304  EC_SLAVE_ERR(slave, "Slave does not support VoE!\n");
306  voe->request_state = EC_INT_REQUEST_FAILURE;
307  return;
308  }
309 
310  ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
311 
312  voe->jiffies_start = jiffies;
313  voe->retries = EC_FSM_RETRIES;
315 }
316 
317 /*****************************************************************************/
318 
322 {
323  ec_datagram_t *datagram = &voe->datagram;
324  ec_slave_t *slave = voe->config->slave;
325 
326  if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--)
327  return;
328 
329  if (datagram->state != EC_DATAGRAM_RECEIVED) {
331  voe->request_state = EC_INT_REQUEST_FAILURE;
332  EC_SLAVE_ERR(slave, "Failed to receive VoE mailbox check datagram: ");
333  ec_datagram_print_state(datagram);
334  return;
335  }
336 
337  if (datagram->working_counter != 1) {
339  voe->request_state = EC_INT_REQUEST_FAILURE;
340  EC_SLAVE_ERR(slave, "Reception of VoE mailbox check"
341  " datagram failed: ");
342  ec_datagram_print_wc_error(datagram);
343  return;
344  }
345 
346  if (!ec_slave_mbox_check(datagram)) {
347  unsigned long diff_ms =
348  (datagram->jiffies_received - voe->jiffies_start) * 1000 / HZ;
349  if (diff_ms >= EC_VOE_RESPONSE_TIMEOUT) {
351  voe->request_state = EC_INT_REQUEST_FAILURE;
352  EC_SLAVE_ERR(slave, "Timeout while waiting for VoE data.\n");
353  return;
354  }
355 
356  ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
357  voe->retries = EC_FSM_RETRIES;
358  return;
359  }
360 
361  // Fetch response
362  ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
363  voe->retries = EC_FSM_RETRIES;
365 }
366 
367 /*****************************************************************************/
368 
372 {
373  ec_datagram_t *datagram = &voe->datagram;
374  ec_slave_t *slave = voe->config->slave;
375  ec_master_t *master = voe->config->master;
376  uint8_t *data, mbox_prot;
377  size_t rec_size;
378 
379  if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--)
380  return;
381 
382  if (datagram->state != EC_DATAGRAM_RECEIVED) {
384  voe->request_state = EC_INT_REQUEST_FAILURE;
385  EC_SLAVE_ERR(slave, "Failed to receive VoE read datagram: ");
386  ec_datagram_print_state(datagram);
387  return;
388  }
389 
390  if (datagram->working_counter != 1) {
392  voe->request_state = EC_INT_REQUEST_FAILURE;
393  EC_SLAVE_ERR(slave, "Reception of VoE read response failed: ");
394  ec_datagram_print_wc_error(datagram);
395  return;
396  }
397 
398  data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
399  if (IS_ERR(data)) {
401  voe->request_state = EC_INT_REQUEST_FAILURE;
402  return;
403  }
404 
405  if (mbox_prot != EC_MBOX_TYPE_VOE) {
407  voe->request_state = EC_INT_REQUEST_FAILURE;
408  EC_SLAVE_WARN(slave, "Received mailbox protocol 0x%02X"
409  " as response.\n", mbox_prot);
410  ec_print_data(data, rec_size);
411  return;
412  }
413 
414  if (rec_size < EC_VOE_HEADER_SIZE) {
416  voe->request_state = EC_INT_REQUEST_FAILURE;
417  EC_SLAVE_ERR(slave, "Received VoE header is"
418  " incomplete (%zu bytes)!\n", rec_size);
419  return;
420  }
421 
422  if (master->debug_level) {
423  EC_CONFIG_DBG(voe->config, 0, "VoE data:\n");
424  ec_print_data(data, rec_size);
425  }
426 
427  voe->data_size = rec_size - EC_VOE_HEADER_SIZE;
428  voe->request_state = EC_INT_REQUEST_SUCCESS;
429  voe->state = ec_voe_handler_state_end; // success
430 }
431 
432 /*****************************************************************************/
433 
437 {
438  ec_datagram_t *datagram = &voe->datagram;
439  ec_slave_t *slave = voe->config->slave;
440 
441  EC_SLAVE_DBG(slave, 1, "Reading VoE data.\n");
442 
443  if (!(slave->sii.mailbox_protocols & EC_MBOX_VOE)) {
444  EC_SLAVE_ERR(slave, "Slave does not support VoE!\n");
446  voe->request_state = EC_INT_REQUEST_FAILURE;
447  return;
448  }
449 
450  ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
451 
452  voe->jiffies_start = jiffies;
453  voe->retries = EC_FSM_RETRIES;
455 }
456 
457 /*****************************************************************************/
458 
463 {
464  ec_datagram_t *datagram = &voe->datagram;
465  ec_slave_t *slave = voe->config->slave;
466  ec_master_t *master = voe->config->master;
467  uint8_t *data, mbox_prot;
468  size_t rec_size;
469 
470  if (datagram->state == EC_DATAGRAM_TIMED_OUT && voe->retries--)
471  return;
472 
473  if (datagram->state != EC_DATAGRAM_RECEIVED) {
475  voe->request_state = EC_INT_REQUEST_FAILURE;
476  EC_SLAVE_ERR(slave, "Failed to receive VoE read datagram: ");
477  ec_datagram_print_state(datagram);
478  return;
479  }
480 
481  if (datagram->working_counter == 0) {
483  voe->request_state = EC_INT_REQUEST_FAILURE;
484  EC_SLAVE_DBG(slave, 1, "Slave did not send VoE data.\n");
485  return;
486  }
487 
488  if (datagram->working_counter != 1) {
490  voe->request_state = EC_INT_REQUEST_FAILURE;
491  EC_SLAVE_WARN(slave, "Reception of VoE read response failed: ");
492  ec_datagram_print_wc_error(datagram);
493  return;
494  }
495 
496  if (!(data = ec_slave_mbox_fetch(slave, datagram,
497  &mbox_prot, &rec_size))) {
499  voe->request_state = EC_INT_REQUEST_FAILURE;
500  return;
501  }
502 
503  if (mbox_prot != EC_MBOX_TYPE_VOE) {
505  voe->request_state = EC_INT_REQUEST_FAILURE;
506  EC_SLAVE_WARN(slave, "Received mailbox protocol 0x%02X"
507  " as response.\n", mbox_prot);
508  ec_print_data(data, rec_size);
509  return;
510  }
511 
512  if (rec_size < EC_VOE_HEADER_SIZE) {
514  voe->request_state = EC_INT_REQUEST_FAILURE;
515  EC_SLAVE_ERR(slave, "Received VoE header is"
516  " incomplete (%zu bytes)!\n", rec_size);
517  return;
518  }
519 
520  if (master->debug_level) {
521  EC_CONFIG_DBG(voe->config, 1, "VoE data:\n");
522  ec_print_data(data, rec_size);
523  }
524 
525  voe->data_size = rec_size - EC_VOE_HEADER_SIZE;
526  voe->request_state = EC_INT_REQUEST_SUCCESS;
527  voe->state = ec_voe_handler_state_end; // success
528 }
529 
530 /*****************************************************************************/
531 
535 {
536 }
537 
538 /*****************************************************************************/
539 
543 {
544 }
545 
546 /*****************************************************************************/
547 
550 EXPORT_SYMBOL(ecrt_voe_handler_send_header);
551 EXPORT_SYMBOL(ecrt_voe_handler_received_header);
552 EXPORT_SYMBOL(ecrt_voe_handler_data);
553 EXPORT_SYMBOL(ecrt_voe_handler_data_size);
554 EXPORT_SYMBOL(ecrt_voe_handler_read);
555 EXPORT_SYMBOL(ecrt_voe_handler_write);
556 EXPORT_SYMBOL(ecrt_voe_handler_execute);
557 
560 /*****************************************************************************/
#define EC_FSM_RETRIES
Number of state machine retries on datagram timeout.
Definition: globals.h:59
uint8_t * ec_slave_mbox_prepare_send(const ec_slave_t *slave, ec_datagram_t *datagram, uint8_t type, size_t size)
Prepares a mailbox-send datagram.
Definition: mailbox.c:51
void ec_voe_handler_state_read_nosync_start(ec_voe_handler_t *)
Start reading VoE data without sending a sync message before.
Definition: voe_handler.c:436
ec_sii_t sii
Extracted SII data.
Definition: slave.h:223
void ecrt_voe_handler_received_header(const ec_voe_handler_t *voe, uint32_t *vendor_id, uint16_t *vendor_type)
Reads the header data of a received VoE message.
Definition: voe_handler.c:136
uint8_t * ec_slave_mbox_fetch(const ec_slave_t *slave, const ec_datagram_t *datagram, uint8_t *type, size_t *size)
Processes received mailbox data.
Definition: mailbox.c:165
size_t ecrt_voe_handler_data_size(const ec_voe_handler_t *voe)
Returns the current data size.
Definition: voe_handler.c:156
void ec_voe_handler_state_read_check(ec_voe_handler_t *)
Check for new data in the mailbox.
Definition: voe_handler.c:321
#define EC_SLAVE_DBG(slave, level, fmt, args...)
Convenience macro for printing slave-specific debug messages to syslog.
Definition: slave.h:106
ec_datagram_t datagram
State machine datagram.
Definition: voe_handler.h:52
size_t ec_voe_handler_mem_size(const ec_voe_handler_t *voe)
Get usable memory size.
Definition: voe_handler.c:112
#define EC_VOE_HEADER_SIZE
VoE header size.
Definition: voe_handler.c:49
int ec_slave_mbox_prepare_fetch(const ec_slave_t *slave, ec_datagram_t *datagram)
Prepares a datagram to fetch mailbox data.
Definition: mailbox.c:127
#define EC_SLAVE_WARN(slave, fmt, args...)
Convenience macro for printing slave-specific warnings to syslog.
Definition: slave.h:90
EtherCAT datagram.
Definition: datagram.h:87
ec_master_t * master
Master owning the slave configuration.
Definition: slave_config.h:120
uint16_t working_counter
Working counter.
Definition: datagram.h:99
#define EC_VOE_RESPONSE_TIMEOUT
VoE response timeout in [ms].
Definition: voe_handler.c:53
uint16_t vendor_type
Vendor type for the header.
Definition: voe_handler.h:54
void ecrt_voe_handler_read(ec_voe_handler_t *voe)
Start a VoE read operation.
Definition: voe_handler.c:163
void ecrt_voe_handler_read_nosync(ec_voe_handler_t *voe)
Start a VoE read operation without querying the sync manager status.
Definition: voe_handler.c:172
Vendor specific.
Definition: globals.h:152
EtherCAT master structure.
uint32_t vendor_id
Vendor ID for the header.
Definition: voe_handler.h:53
EtherCAT slave.
Definition: slave.h:176
void(* state)(ec_voe_handler_t *)
State function.
Definition: voe_handler.h:59
ec_datagram_state_t state
State.
Definition: datagram.h:100
#define EC_WRITE_U32(DATA, VAL)
Write a 32-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2221
ec_internal_request_state_t request_state
Handler state.
Definition: voe_handler.h:60
uint16_t mailbox_protocols
Supported mailbox protocols.
Definition: slave.h:147
size_t data_size
Size of VoE data.
Definition: voe_handler.h:55
unsigned int debug_level
Master debug level.
Definition: master.h:286
#define EC_SLAVE_ERR(slave, fmt, args...)
Convenience macro for printing slave-specific errors to syslog.
Definition: slave.h:76
void ec_datagram_print_wc_error(const ec_datagram_t *datagram)
Evaluates the working counter of a single-cast datagram.
Definition: datagram.c:602
#define EC_WRITE_U16(DATA, VAL)
Write a 16-bit unsigned value to EtherCAT data.
Definition: ecrt.h:2204
#define EC_CONFIG_DBG(sc, level, fmt, args...)
Convenience macro for printing configuration-specific debug messages to syslog.
Definition: slave_config.h:106
void ec_voe_handler_state_read_response(ec_voe_handler_t *)
Read the pending mailbox data.
Definition: voe_handler.c:371
#define EC_READ_U32(DATA)
Read a 32-bit unsigned value from EtherCAT data.
Definition: ecrt.h:2151
unsigned long jiffies_start
Timestamp for timeout calculation.
Definition: voe_handler.h:62
Vendor specific over EtherCAT handler.
Definition: voe_handler.h:49
ec_master_t * master
Master owning the slave.
Definition: slave.h:178
ec_request_state_t ecrt_voe_handler_execute(ec_voe_handler_t *voe)
Execute the handler.
Definition: voe_handler.c:191
void ec_master_queue_datagram(ec_master_t *master, ec_datagram_t *datagram)
Places a datagram in the datagram queue.
Definition: master.c:936
Values read by the master.
Definition: ecrt.h:420
int ec_voe_handler_init(ec_voe_handler_t *voe, ec_slave_config_t *sc, size_t size)
VoE handler constructor.
Definition: voe_handler.c:76
ec_slave_t * slave
Slave pointer.
Definition: slave_config.h:133
void ec_voe_handler_state_write_start(ec_voe_handler_t *)
Start writing VoE data.
Definition: voe_handler.c:212
#define EC_MBOX_HEADER_SIZE
Mailbox header size.
Definition: globals.h:95
void ec_voe_handler_state_write_response(ec_voe_handler_t *)
Wait for the mailbox response.
Definition: voe_handler.c:251
void ecrt_voe_handler_write(ec_voe_handler_t *voe, size_t size)
Start a VoE write operation.
Definition: voe_handler.c:181
void ec_print_data(const uint8_t *, size_t)
Outputs frame contents for debugging purposes.
Definition: module.c:341
int ec_datagram_prealloc(ec_datagram_t *datagram, size_t size)
Allocates internal payload memory.
Definition: datagram.c:150
void ec_voe_handler_state_read_start(ec_voe_handler_t *)
Start reading VoE data.
Definition: voe_handler.c:296
unsigned int retries
retries upon datagram timeout
Definition: voe_handler.h:61
int ec_slave_mbox_prepare_check(const ec_slave_t *slave, ec_datagram_t *datagram)
Prepares a datagram for checking the mailbox state.
Definition: mailbox.c:96
#define EC_READ_U16(DATA)
Read a 16-bit unsigned value from EtherCAT data.
Definition: ecrt.h:2135
ec_direction_t dir
Direction.
Definition: voe_handler.h:56
void ec_datagram_print_state(const ec_datagram_t *datagram)
Prints the state of a datagram.
Definition: datagram.c:565
Mailbox functionality.
Invalid direction.
Definition: ecrt.h:418
Vendor specific over EtherCAT protocol handler.
void ec_datagram_init(ec_datagram_t *datagram)
Constructor.
Definition: datagram.c:88
Timed out (dequeued).
Definition: datagram.h:79
void ec_voe_handler_state_read_nosync_response(ec_voe_handler_t *)
Read the pending mailbox data without sending a sync message before.
Definition: voe_handler.c:462
uint8_t * data
Datagram payload.
Definition: datagram.h:94
void ec_voe_handler_state_error(ec_voe_handler_t *)
Failure termination state function.
Definition: voe_handler.c:542
EtherCAT slave configuration.
Definition: slave_config.h:118
void ecrt_voe_handler_send_header(ec_voe_handler_t *voe, uint32_t vendor_id, uint16_t vendor_type)
Sets the VoE header for future send operations.
Definition: voe_handler.c:127
EtherCAT slave configuration structure.
size_t mem_size
Datagram data memory size.
Definition: datagram.h:96
ec_request_state_t
Request state.
Definition: ecrt.h:517
ec_slave_config_t * config
Parent slave configuration.
Definition: voe_handler.h:51
Values written by the master.
Definition: ecrt.h:419
Received (dequeued).
Definition: datagram.h:78
const ec_request_state_t ec_request_state_translation_table[]
Global request state type translation table.
Definition: module.c:651
EtherCAT master.
Definition: master.h:194
uint8_t * ecrt_voe_handler_data(ec_voe_handler_t *voe)
Access to the VoE handler's data.
Definition: voe_handler.c:149
#define EC_MBOX_TYPE_VOE
VoE mailbox type.
Definition: voe_handler.c:45
unsigned long jiffies_received
Jiffies, when the datagram was received.
Definition: datagram.h:108
int ec_slave_mbox_check(const ec_datagram_t *datagram)
Processes a mailbox state checking datagram.
Definition: mailbox.c:115
void ec_voe_handler_clear(ec_voe_handler_t *voe)
VoE handler destructor.
Definition: voe_handler.c:99
void ec_datagram_clear(ec_datagram_t *datagram)
Destructor.
Definition: datagram.c:118
void ec_voe_handler_state_end(ec_voe_handler_t *)
Successful termination state function.
Definition: voe_handler.c:534