lib/sdo_request.c
changeset 1352 275d2fdeab48
child 1363 11c0b2caa253
equal deleted inserted replaced
1351:cf8f08631c8e 1352:275d2fdeab48
       
     1 /******************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2006-2009  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  *  Using the EtherCAT technology and brand is permitted in compliance with
       
    23  *  the industrial property and similar rights of Beckhoff Automation GmbH.
       
    24  *
       
    25  *****************************************************************************/
       
    26 
       
    27 /** \file
       
    28  * Canopen over EtherCAT SDO request functions.
       
    29  */
       
    30 
       
    31 /*****************************************************************************/
       
    32 
       
    33 #include <stdio.h>
       
    34 #include <errno.h>
       
    35 #include <string.h>
       
    36 
       
    37 #include "sdo_request.h"
       
    38 #include "master/ioctl.h"
       
    39 #include "slave_config.h"
       
    40 #include "master.h"
       
    41 
       
    42 /*****************************************************************************
       
    43  * Realtime interface.
       
    44  ****************************************************************************/
       
    45 
       
    46 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
       
    47 {
       
    48     ec_ioctl_sdo_request_t data;
       
    49 
       
    50     data.config_index = req->config->index;
       
    51     data.request_index = req->index;
       
    52     data.timeout = timeout;
       
    53 
       
    54     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_TIMEOUT,
       
    55                 &data) == -1)
       
    56         fprintf(stderr, "Failed to set SDO request timeout: %s\n",
       
    57                 strerror(errno));
       
    58 }
       
    59 
       
    60 /*****************************************************************************/
       
    61 
       
    62 uint8_t *ecrt_sdo_request_data(ec_sdo_request_t *req)
       
    63 {
       
    64     return req->data;
       
    65 }
       
    66 
       
    67 /*****************************************************************************/
       
    68 
       
    69 size_t ecrt_sdo_request_data_size(const ec_sdo_request_t *req)
       
    70 {
       
    71     return req->data_size;
       
    72 }
       
    73 
       
    74 /*****************************************************************************/
       
    75 
       
    76 ec_request_state_t ecrt_sdo_request_state(ec_sdo_request_t *req)
       
    77 {
       
    78     ec_ioctl_sdo_request_t data;
       
    79 
       
    80     data.config_index = req->config->index;
       
    81     data.request_index = req->index;
       
    82 
       
    83     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_STATE,
       
    84                 &data) == -1)
       
    85         fprintf(stderr, "Failed to get SDO request state: %s\n",
       
    86                 strerror(errno));
       
    87 
       
    88     if (data.size) { // new data waiting to be copied
       
    89         if (req->mem_size < data.size) {
       
    90             if (req->data)
       
    91                 free(req->data);
       
    92             req->data = malloc(data.size);
       
    93             if (!req->data) {
       
    94                 req->mem_size = 0;
       
    95                 fprintf(stderr, "Failed to allocate %u bytes of SDO data"
       
    96                         " memory!\n", data.size);
       
    97                 return EC_REQUEST_ERROR;
       
    98             }
       
    99             req->mem_size = data.size;
       
   100         }
       
   101 
       
   102         data.data = req->data;
       
   103 
       
   104         if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_DATA,
       
   105                     &data) == -1) {
       
   106             fprintf(stderr, "Failed to get SDO data: %s\n", strerror(errno));
       
   107             return EC_REQUEST_ERROR;
       
   108         }
       
   109         req->data_size = data.size;
       
   110     }
       
   111 
       
   112     return data.state;
       
   113 }
       
   114 
       
   115 /*****************************************************************************/
       
   116 
       
   117 void ecrt_sdo_request_read(ec_sdo_request_t *req)
       
   118 {
       
   119     ec_ioctl_sdo_request_t data;
       
   120 
       
   121     data.config_index = req->config->index;
       
   122     data.request_index = req->index;
       
   123 
       
   124     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_READ,
       
   125                 &data) == -1)
       
   126         fprintf(stderr, "Failed to command an SDO read operation : %s\n",
       
   127                 strerror(errno));
       
   128 }
       
   129 
       
   130 /*****************************************************************************/
       
   131 
       
   132 void ecrt_sdo_request_write(ec_sdo_request_t *req)
       
   133 {
       
   134     ec_ioctl_sdo_request_t data;
       
   135 
       
   136     data.config_index = req->config->index;
       
   137     data.request_index = req->index;
       
   138     data.data = req->data;
       
   139     data.size = req->data_size;
       
   140 
       
   141     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_WRITE,
       
   142                 &data) == -1)
       
   143         fprintf(stderr, "Failed to command an SDO write operation : %s\n",
       
   144                 strerror(errno));
       
   145 }
       
   146 
       
   147 /*****************************************************************************/