lib/sdo_request.c
changeset 2589 2b9c78543663
parent 1959 656f114153c2
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
     1 /******************************************************************************
     1 /******************************************************************************
     2  *
     2  *
     3  *  $Id$
     3  *  $Id$
     4  *
     4  *
     5  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
     5  *  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
     6  *  
     6  *
     7  *  This file is part of the IgH EtherCAT master userspace library.
     7  *  This file is part of the IgH EtherCAT master userspace library.
     8  *  
     8  *
     9  *  The IgH EtherCAT master userspace library is free software; you can
     9  *  The IgH EtherCAT master userspace library is free software; you can
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    10  *  redistribute it and/or modify it under the terms of the GNU Lesser General
    11  *  Public License as published by the Free Software Foundation; version 2.1
    11  *  Public License as published by the Free Software Foundation; version 2.1
    12  *  of the License.
    12  *  of the License.
    13  *
    13  *
    17  *  GNU Lesser General Public License for more details.
    17  *  GNU Lesser General Public License for more details.
    18  *
    18  *
    19  *  You should have received a copy of the GNU Lesser General Public License
    19  *  You should have received a copy of the GNU Lesser General Public License
    20  *  along with the IgH EtherCAT master userspace library. If not, see
    20  *  along with the IgH EtherCAT master userspace library. If not, see
    21  *  <http://www.gnu.org/licenses/>.
    21  *  <http://www.gnu.org/licenses/>.
    22  *  
    22  *
    23  *  ---
    23  *  ---
    24  *  
    24  *
    25  *  The license mentioned above concerns the source code only. Using the
    25  *  The license mentioned above concerns the source code only. Using the
    26  *  EtherCAT technology and brand is only permitted in compliance with the
    26  *  EtherCAT technology and brand is only permitted in compliance with the
    27  *  industrial property and similar rights of Beckhoff Automation GmbH.
    27  *  industrial property and similar rights of Beckhoff Automation GmbH.
    28  *
    28  *
    29  *****************************************************************************/
    29  *****************************************************************************/
    33  */
    33  */
    34 
    34 
    35 /*****************************************************************************/
    35 /*****************************************************************************/
    36 
    36 
    37 #include <stdio.h>
    37 #include <stdio.h>
    38 #include <errno.h>
       
    39 #include <string.h>
    38 #include <string.h>
    40 
    39 
       
    40 #include "ioctl.h"
    41 #include "sdo_request.h"
    41 #include "sdo_request.h"
    42 #include "master/ioctl.h"
       
    43 #include "slave_config.h"
    42 #include "slave_config.h"
    44 #include "master.h"
    43 #include "master.h"
    45 
    44 
    46 /*****************************************************************************/
    45 /*****************************************************************************/
    47 
    46 
    54 
    53 
    55 /*****************************************************************************
    54 /*****************************************************************************
    56  * Application interface.
    55  * Application interface.
    57  ****************************************************************************/
    56  ****************************************************************************/
    58 
    57 
       
    58 void ecrt_sdo_request_index(ec_sdo_request_t *req, uint16_t index,
       
    59         uint8_t subindex)
       
    60 {
       
    61     ec_ioctl_sdo_request_t data;
       
    62     int ret;
       
    63 
       
    64     data.config_index = req->config->index;
       
    65     data.request_index = req->index;
       
    66     data.sdo_index = index;
       
    67     data.sdo_subindex = subindex;
       
    68 
       
    69     ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_INDEX, &data);
       
    70     if (EC_IOCTL_IS_ERROR(ret)) {
       
    71         fprintf(stderr, "Failed to set SDO request index/subindex: %s\n",
       
    72                 strerror(EC_IOCTL_ERRNO(ret)));
       
    73     }
       
    74 }
       
    75 
       
    76 /*****************************************************************************/
       
    77 
    59 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
    78 void ecrt_sdo_request_timeout(ec_sdo_request_t *req, uint32_t timeout)
    60 {
    79 {
    61     ec_ioctl_sdo_request_t data;
    80     ec_ioctl_sdo_request_t data;
       
    81     int ret;
    62 
    82 
    63     data.config_index = req->config->index;
    83     data.config_index = req->config->index;
    64     data.request_index = req->index;
    84     data.request_index = req->index;
    65     data.timeout = timeout;
    85     data.timeout = timeout;
    66 
    86 
    67     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_TIMEOUT,
    87     ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_TIMEOUT, &data);
    68                 &data) == -1)
    88     if (EC_IOCTL_IS_ERROR(ret)) {
    69         fprintf(stderr, "Failed to set SDO request timeout: %s\n",
    89         fprintf(stderr, "Failed to set SDO request timeout: %s\n",
    70                 strerror(errno));
    90                 strerror(EC_IOCTL_ERRNO(ret)));
       
    91     }
    71 }
    92 }
    72 
    93 
    73 /*****************************************************************************/
    94 /*****************************************************************************/
    74 
    95 
    75 uint8_t *ecrt_sdo_request_data(ec_sdo_request_t *req)
    96 uint8_t *ecrt_sdo_request_data(ec_sdo_request_t *req)
    87 /*****************************************************************************/
   108 /*****************************************************************************/
    88 
   109 
    89 ec_request_state_t ecrt_sdo_request_state(ec_sdo_request_t *req)
   110 ec_request_state_t ecrt_sdo_request_state(ec_sdo_request_t *req)
    90 {
   111 {
    91     ec_ioctl_sdo_request_t data;
   112     ec_ioctl_sdo_request_t data;
       
   113     int ret;
    92 
   114 
    93     data.config_index = req->config->index;
   115     data.config_index = req->config->index;
    94     data.request_index = req->index;
   116     data.request_index = req->index;
    95 
   117 
    96     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_STATE,
   118     ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_STATE, &data);
    97                 &data) == -1)
   119     if (EC_IOCTL_IS_ERROR(ret)) {
    98         fprintf(stderr, "Failed to get SDO request state: %s\n",
   120         fprintf(stderr, "Failed to get SDO request state: %s\n",
    99                 strerror(errno));
   121                 strerror(EC_IOCTL_ERRNO(ret)));
       
   122         return EC_REQUEST_ERROR;
       
   123     }
   100 
   124 
   101     if (data.size) { // new data waiting to be copied
   125     if (data.size) { // new data waiting to be copied
   102         if (req->mem_size < data.size) {
   126         if (req->mem_size < data.size) {
   103             if (req->data)
   127             fprintf(stderr, "Received %zu bytes do not fit info SDO data"
   104                 free(req->data);
   128                     " memory (%zu bytes)!\n", data.size, req->mem_size);
   105             req->data = malloc(data.size);
   129             return EC_REQUEST_ERROR;
   106             if (!req->data) {
       
   107                 req->mem_size = 0;
       
   108                 fprintf(stderr, "Failed to allocate %u bytes of SDO data"
       
   109                         " memory!\n", data.size);
       
   110                 return EC_REQUEST_ERROR;
       
   111             }
       
   112             req->mem_size = data.size;
       
   113         }
   130         }
   114 
   131 
   115         data.data = req->data;
   132         data.data = req->data;
   116 
   133 
   117         if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_DATA,
   134         ret = ioctl(req->config->master->fd,
   118                     &data) == -1) {
   135                 EC_IOCTL_SDO_REQUEST_DATA, &data);
   119             fprintf(stderr, "Failed to get SDO data: %s\n", strerror(errno));
   136         if (EC_IOCTL_IS_ERROR(ret)) {
       
   137             fprintf(stderr, "Failed to get SDO data: %s\n",
       
   138                     strerror(EC_IOCTL_ERRNO(ret)));
   120             return EC_REQUEST_ERROR;
   139             return EC_REQUEST_ERROR;
   121         }
   140         }
   122         req->data_size = data.size;
   141         req->data_size = data.size;
   123     }
   142     }
   124 
   143 
   128 /*****************************************************************************/
   147 /*****************************************************************************/
   129 
   148 
   130 void ecrt_sdo_request_read(ec_sdo_request_t *req)
   149 void ecrt_sdo_request_read(ec_sdo_request_t *req)
   131 {
   150 {
   132     ec_ioctl_sdo_request_t data;
   151     ec_ioctl_sdo_request_t data;
       
   152     int ret;
   133 
   153 
   134     data.config_index = req->config->index;
   154     data.config_index = req->config->index;
   135     data.request_index = req->index;
   155     data.request_index = req->index;
   136 
   156 
   137     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_READ,
   157     ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_READ, &data);
   138                 &data) == -1)
   158     if (EC_IOCTL_IS_ERROR(ret)) {
   139         fprintf(stderr, "Failed to command an SDO read operation : %s\n",
   159         fprintf(stderr, "Failed to command an SDO read operation : %s\n",
   140                 strerror(errno));
   160                 strerror(EC_IOCTL_ERRNO(ret)));
       
   161     }
   141 }
   162 }
   142 
   163 
   143 /*****************************************************************************/
   164 /*****************************************************************************/
   144 
   165 
   145 void ecrt_sdo_request_write(ec_sdo_request_t *req)
   166 void ecrt_sdo_request_write(ec_sdo_request_t *req)
   146 {
   167 {
   147     ec_ioctl_sdo_request_t data;
   168     ec_ioctl_sdo_request_t data;
       
   169     int ret;
   148 
   170 
   149     data.config_index = req->config->index;
   171     data.config_index = req->config->index;
   150     data.request_index = req->index;
   172     data.request_index = req->index;
   151     data.data = req->data;
   173     data.data = req->data;
   152     data.size = req->data_size;
   174     data.size = req->data_size;
   153 
   175 
   154     if (ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_WRITE,
   176     ret = ioctl(req->config->master->fd, EC_IOCTL_SDO_REQUEST_WRITE, &data);
   155                 &data) == -1)
   177     if (EC_IOCTL_IS_ERROR(ret)) {
   156         fprintf(stderr, "Failed to command an SDO write operation : %s\n",
   178         fprintf(stderr, "Failed to command an SDO write operation : %s\n",
   157                 strerror(errno));
   179                 strerror(EC_IOCTL_ERRNO(ret)));
       
   180     }
   158 }
   181 }
   159 
   182 
   160 /*****************************************************************************/
   183 /*****************************************************************************/