fp@1242: /******************************************************************************
fp@1266: *
fp@1326: * $Id$
fp@1266: *
fp@1326: * Copyright (C) 2006-2009 Florian Pose, Ingenieurgemeinschaft IgH
fp@1326: *
fp@1326: * This file is part of the IgH EtherCAT master userspace library.
fp@1326: *
fp@1326: * The IgH EtherCAT master userspace library is free software; you can
fp@1326: * redistribute it and/or modify it under the terms of the GNU Lesser General
fp@1326: * Public License as published by the Free Software Foundation; version 2.1
fp@1326: * of the License.
fp@1287: *
fp@1326: * The IgH EtherCAT master userspace library is distributed in the hope that
fp@1326: * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
fp@1326: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fp@1326: * GNU Lesser General Public License for more details.
fp@1287: *
fp@1326: * You should have received a copy of the GNU Lesser General Public License
fp@1326: * along with the IgH EtherCAT master userspace library. If not, see
fp@1326: * .
fp@1326: *
fp@1363: * ---
fp@1363: *
fp@1363: * The license mentioned above concerns the source code only. Using the
fp@1363: * EtherCAT technology and brand is only permitted in compliance with the
fp@1363: * industrial property and similar rights of Beckhoff Automation GmbH.
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@1258: #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@1497: ec_master_t *ecrt_request_master(unsigned int master_index)
fp@1497: {
fp@1497: ec_master_t *master = ecrt_open_master(master_index);
fp@1497: if (master) {
fp@1497: if (ecrt_master_reserve(master) < 0) {
fp@1497: close(master->fd);
fp@1497: free(master);
fp@1497: master = 0;
fp@1497: }
fp@1497: }
fp@1497:
fp@1497: return master;
fp@1497: }
fp@1497:
fp@1497: /*****************************************************************************/
fp@1497:
fp@1495: #define MAX_PATH_LEN 64
fp@1495:
fp@1497: ec_master_t *ecrt_open_master(unsigned int master_index)
fp@1242: {
fp@1495: char path[MAX_PATH_LEN];
fp@1826: ec_master_t *master = NULL;
fp@1826: ec_ioctl_module_t module_data;
fp@1495:
fp@1495: master = malloc(sizeof(ec_master_t));
fp@1495: if (!master) {
fp@1495: fprintf(stderr, "Failed to allocate memory.\n");
fp@1495: return 0;
fp@1495: }
fp@1495:
fp@1495: master->process_data = NULL;
fp@1495: master->process_data_size = 0;
fp@1495:
fp@1495: snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
fp@1495:
fp@1495: master->fd = open(path, O_RDWR);
fp@1495: if (master->fd == -1) {
fp@1495: fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
fp@1826: goto out_free;
fp@1826: }
fp@1826:
fp@1826: if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
fp@1826: fprintf(stderr, "Failed to get module information from %s: %s\n",
fp@1826: path, strerror(errno));
fp@1826: goto out_close;
fp@1826: }
fp@1826:
fp@1826: if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
fp@1826: fprintf(stderr, "ioctl() version magic is differing:"
fp@1826: " %s: %u, libethercat: %u.\n",
fp@1826: path, module_data.ioctl_version_magic,
fp@1826: EC_IOCTL_VERSION_MAGIC);
fp@1826: goto out_close;
fp@1495: }
fp@1495:
fp@1242: return master;
fp@1826:
fp@1826: out_close:
fp@1826: close(master->fd);
fp@1826: out_free:
fp@1826: free(master);
fp@1826: return 0;
fp@1242: }
fp@1242:
fp@1242: /*****************************************************************************/
fp@1242:
fp@1242: void ecrt_release_master(ec_master_t *master)
fp@1242: {
fp@1258: if (master->process_data) {
fp@1258: munmap(master->process_data, master->process_data_size);
fp@1258: }
fp@1258:
fp@1242: close(master->fd);
fp@1242: free(master);
fp@1242: }
fp@1242:
fp@1242: /*****************************************************************************/