lib/reg_request.c
branchstable-1.5
changeset 2443 2c3ccdde3919
equal deleted inserted replaced
2442:86ebf18a029f 2443:2c3ccdde3919
       
     1 /******************************************************************************
       
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2012  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT master userspace library.
       
     8  *
       
     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
       
    11  *  Public License as published by the Free Software Foundation; version 2.1
       
    12  *  of the License.
       
    13  *
       
    14  *  The IgH EtherCAT master userspace library is distributed in the hope that
       
    15  *  it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    16  *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    17  *  GNU Lesser General Public License for more details.
       
    18  *
       
    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
       
    21  *  <http://www.gnu.org/licenses/>.
       
    22  *
       
    23  *  ---
       
    24  *
       
    25  *  The license mentioned above concerns the source code only. Using the
       
    26  *  EtherCAT technology and brand is only permitted in compliance with the
       
    27  *  industrial property and similar rights of Beckhoff Automation GmbH.
       
    28  *
       
    29  *****************************************************************************/
       
    30 
       
    31 /** \file
       
    32  * EtherCAT register request functions.
       
    33  */
       
    34 
       
    35 /*****************************************************************************/
       
    36 
       
    37 #include <stdio.h>
       
    38 #include <string.h>
       
    39 
       
    40 #include "ioctl.h"
       
    41 #include "reg_request.h"
       
    42 #include "slave_config.h"
       
    43 #include "master.h"
       
    44 
       
    45 /*****************************************************************************/
       
    46 
       
    47 void ec_reg_request_clear(ec_reg_request_t *reg)
       
    48 {
       
    49     if (reg->data) {
       
    50         free(reg->data);
       
    51     }
       
    52 }
       
    53 
       
    54 /*****************************************************************************
       
    55  * Application interface.
       
    56  ****************************************************************************/
       
    57 
       
    58 uint8_t *ecrt_reg_request_data(ec_reg_request_t *reg)
       
    59 {
       
    60     return reg->data;
       
    61 }
       
    62 
       
    63 /*****************************************************************************/
       
    64 
       
    65 ec_request_state_t ecrt_reg_request_state(ec_reg_request_t *reg)
       
    66 {
       
    67     ec_ioctl_reg_request_t io;
       
    68     int ret;
       
    69 
       
    70     io.config_index = reg->config->index;
       
    71     io.request_index = reg->index;
       
    72 
       
    73     ret = ioctl(reg->config->master->fd, EC_IOCTL_REG_REQUEST_STATE, &io);
       
    74     if (EC_IOCTL_IS_ERROR(ret)) {
       
    75         fprintf(stderr, "Failed to get register request state: %s\n",
       
    76                 strerror(EC_IOCTL_ERRNO(ret)));
       
    77         return EC_REQUEST_ERROR;
       
    78     }
       
    79 
       
    80     if (io.new_data) { // new data waiting to be copied
       
    81 
       
    82         io.data = reg->data;
       
    83         io.mem_size = reg->mem_size;
       
    84 
       
    85         ret = ioctl(reg->config->master->fd,
       
    86                 EC_IOCTL_REG_REQUEST_DATA, &io);
       
    87         if (EC_IOCTL_IS_ERROR(ret)) {
       
    88             fprintf(stderr, "Failed to get register data: %s\n",
       
    89                     strerror(EC_IOCTL_ERRNO(ret)));
       
    90             return EC_REQUEST_ERROR;
       
    91         }
       
    92     }
       
    93 
       
    94     return io.state;
       
    95 }
       
    96 
       
    97 /*****************************************************************************/
       
    98 
       
    99 void ecrt_reg_request_write(ec_reg_request_t *reg, uint16_t address,
       
   100         size_t size)
       
   101 {
       
   102     ec_ioctl_reg_request_t io;
       
   103     int ret;
       
   104 
       
   105     io.config_index = reg->config->index;
       
   106     io.request_index = reg->index;
       
   107     io.data = reg->data;
       
   108     io.address = address;
       
   109     io.transfer_size = size;
       
   110 
       
   111     ret = ioctl(reg->config->master->fd, EC_IOCTL_REG_REQUEST_WRITE, &io);
       
   112     if (EC_IOCTL_IS_ERROR(ret)) {
       
   113         fprintf(stderr, "Failed to command an register write operation: %s\n",
       
   114                 strerror(EC_IOCTL_ERRNO(ret)));
       
   115     }
       
   116 }
       
   117 
       
   118 /*****************************************************************************/
       
   119 
       
   120 void ecrt_reg_request_read(ec_reg_request_t *reg, uint16_t address,
       
   121         size_t size)
       
   122 {
       
   123     ec_ioctl_reg_request_t io;
       
   124     int ret;
       
   125 
       
   126     io.config_index = reg->config->index;
       
   127     io.request_index = reg->index;
       
   128     io.address = address;
       
   129     io.transfer_size = size;
       
   130 
       
   131     ret = ioctl(reg->config->master->fd, EC_IOCTL_REG_REQUEST_READ, &io);
       
   132     if (EC_IOCTL_IS_ERROR(ret)) {
       
   133         fprintf(stderr, "Failed to command an register read operation: %s\n",
       
   134                 strerror(EC_IOCTL_ERRNO(ret)));
       
   135     }
       
   136 }
       
   137 
       
   138 /*****************************************************************************/