fp@1242: /****************************************************************************** fp@1242: * fp@1242: * $Id$ fp@1242: * fp@1242: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1242: * fp@1242: * This file is part of the IgH EtherCAT Master. fp@1242: * fp@1242: * The IgH EtherCAT Master is free software; you can redistribute it fp@1242: * and/or modify it under the terms of the GNU General Public License fp@1242: * as published by the Free Software Foundation; either version 2 of the fp@1242: * License, or (at your option) any later version. fp@1242: * fp@1242: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1242: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1242: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1242: * GNU General Public License for more details. fp@1242: * fp@1242: * You should have received a copy of the GNU General Public License fp@1242: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1242: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1242: * fp@1242: * The right to use EtherCAT Technology is granted and comes free of fp@1242: * charge under condition of compatibility of product made by fp@1242: * Licensee. People intending to distribute/sell products based on the fp@1242: * code, have to sign an agreement to guarantee that products using fp@1242: * software based on IgH EtherCAT master stay compatible with the actual fp@1242: * EtherCAT specification (which are released themselves as an open fp@1242: * standard) as the (only) precondition to have the right to use EtherCAT fp@1242: * Technology, IP and trade marks. fp@1242: * fp@1242: *****************************************************************************/ fp@1242: fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1242: #include fp@1244: #include fp@1242: fp@1244: #include "master.h" fp@1242: #include "master/ioctl.h" fp@1242: fp@1244: /*****************************************************************************/ fp@1244: fp@1244: unsigned int ecrt_version_magic(void) fp@1244: { fp@1244: return ECRT_VERSION_MAGIC; fp@1244: } fp@1242: fp@1242: /*****************************************************************************/ fp@1242: fp@1242: #define MAX_PATH_LEN 64 fp@1242: fp@1242: ec_master_t *ecrt_request_master(unsigned int master_index) fp@1242: { fp@1242: char path[MAX_PATH_LEN]; fp@1242: ec_master_t *master; fp@1242: fp@1242: master = malloc(sizeof(ec_master_t)); fp@1242: if (!master) { fp@1242: fprintf(stderr, "Failed to allocate memory.\n"); fp@1242: return 0; fp@1242: } fp@1242: fp@1242: snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index); fp@1242: fp@1242: master->fd = open(path, O_RDWR); fp@1242: if (master->fd == -1) { fp@1242: fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); fp@1242: free(master); fp@1242: return 0; fp@1242: } fp@1242: fp@1244: if (ioctl(master->fd, EC_IOCTL_REQUEST, NULL) == -1) { fp@1242: fprintf(stderr, "Failed to request master %u: %s\n", fp@1242: master_index, strerror(errno)); fp@1242: close(master->fd); fp@1242: free(master); fp@1242: return 0; fp@1242: } fp@1242: fp@1242: return master; fp@1242: } fp@1242: fp@1242: /*****************************************************************************/ fp@1242: fp@1242: void ecrt_release_master(ec_master_t *master) fp@1242: { fp@1242: close(master->fd); fp@1242: free(master); fp@1242: } fp@1242: fp@1242: /*****************************************************************************/