lib/master.c
author Florian Pose <fp@igh-essen.com>
Thu, 09 Oct 2008 14:35:08 +0000
changeset 1256 45fe0af4340b
parent 1255 38b7e05b20c1
child 1258 900f1124e8f8
permissions -rw-r--r--
Warning.
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  and/or modify it under the terms of the GNU General Public License
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  The IgH EtherCAT Master is distributed in the hope that it will be
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <stdlib.h>
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#include <sys/ioctl.h>
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <stdio.h>
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <errno.h>
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#include <string.h>
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
#include "master.h"
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "domain.h"
1246
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    42
#include "slave_config.h"
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include "master/ioctl.h"
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
ec_domain_t *ecrt_master_create_domain(ec_master_t *master)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
{
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
    ec_domain_t *domain;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    int index;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
    domain = malloc(sizeof(ec_domain_t));
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
    if (!domain) {
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
        fprintf(stderr, "Failed to allocate memory.\n");
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        return 0;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    }
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
    index = ioctl(master->fd, EC_IOCTL_CREATE_DOMAIN, NULL);
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    if (index == -1) {
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        fprintf(stderr, "Failed to create domain: %s\n", strerror(errno));
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        free(domain);
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        return 0; 
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
    }
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    domain->index = (unsigned int) index;
1255
38b7e05b20c1 Added some slave configuration methods.
Florian Pose <fp@igh-essen.com>
parents: 1247
diff changeset
    66
    domain->master = master;
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    return domain;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
ec_slave_config_t *ecrt_master_slave_config(ec_master_t *master,
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        uint16_t alias, uint16_t position, uint32_t vendor_id,
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
        uint32_t product_code)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
{
1246
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    76
    ec_ioctl_config_t data;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    77
    ec_slave_config_t *sc;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    78
    int index;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    79
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    80
    sc = malloc(sizeof(ec_slave_config_t));
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    81
    if (!sc) {
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    82
        fprintf(stderr, "Failed to allocate memory.\n");
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    83
        return 0;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    84
    }
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    85
    
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    86
    data.alias = alias;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    87
    data.position = position;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    88
    data.vendor_id = vendor_id;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    89
    data.product_code = product_code;
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    90
    
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    91
    if (ioctl(master->fd, EC_IOCTL_CREATE_SLAVE_CONFIG, &data) == -1) {
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    92
        fprintf(stderr, "Failed to create slave config: %s\n",
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    93
                strerror(errno));
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    94
        free(sc);
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    95
        return 0; 
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    96
    }
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    97
1255
38b7e05b20c1 Added some slave configuration methods.
Florian Pose <fp@igh-essen.com>
parents: 1247
diff changeset
    98
    sc->master = master;
1246
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
    99
    sc->index = data.config_index;
1255
38b7e05b20c1 Added some slave configuration methods.
Florian Pose <fp@igh-essen.com>
parents: 1247
diff changeset
   100
    sc->alias = alias;
38b7e05b20c1 Added some slave configuration methods.
Florian Pose <fp@igh-essen.com>
parents: 1247
diff changeset
   101
    sc->position = position;
1246
4042bda8c980 Creating slave configurations.
Florian Pose <fp@igh-essen.com>
parents: 1244
diff changeset
   102
    return sc;
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
int ecrt_master_activate(ec_master_t *master)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
{
1247
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   109
    if (ioctl(master->fd, EC_IOCTL_ACTIVATE, NULL) == -1) {
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   110
        fprintf(stderr, "Failed to activate master: %s\n",
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   111
                strerror(errno));
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   112
        return -1; 
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   113
    }
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   114
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
    return 0;
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
void ecrt_master_send(ec_master_t *master)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
{
1247
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   122
    if (ioctl(master->fd, EC_IOCTL_SEND, NULL) == -1) {
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   123
        fprintf(stderr, "Failed to send: %s\n", strerror(errno));
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   124
    }
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
void ecrt_master_receive(ec_master_t *master)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
{
1247
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   131
    if (ioctl(master->fd, EC_IOCTL_RECEIVE, NULL) == -1) {
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   132
        fprintf(stderr, "Failed to receive: %s\n", strerror(errno));
5f1f1a3e6636 Activate, Send and received; cyclic timer with setitimer().
Florian Pose <fp@igh-essen.com>
parents: 1246
diff changeset
   133
    }
1244
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
/*****************************************************************************/
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state)
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
{
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
}
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
0b70040d3daa No kernel handles in userspace; Domain creation.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
/*****************************************************************************/