fp@1244: /******************************************************************************
fp@1266: *
fp@1326: * $Id$
fp@1326: *
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@1244: *
fp@1244: *****************************************************************************/
fp@1244:
fp@1244: #include
fp@1244: #include
fp@1244: #include
fp@1244: #include
fp@1244: #include
fp@1258: #include
fp@1244:
fp@1244: #include "master.h"
fp@1244: #include "domain.h"
fp@1246: #include "slave_config.h"
fp@1244: #include "master/ioctl.h"
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1497: int ecrt_master_reserve(ec_master_t *master)
fp@1497: {
fp@1497: if (ioctl(master->fd, EC_IOCTL_REQUEST, NULL) == -1) {
fp@1497: fprintf(stderr, "Failed to reserve master: %s\n",
fp@1497: strerror(errno));
fp@1497: return -1;
fp@1497: }
fp@1975: return 0;
fp@1497: }
fp@1497:
fp@1497: /*****************************************************************************/
fp@1497:
fp@1960: void ec_master_clear_config(ec_master_t *master)
fp@1959: {
fp@1959: ec_domain_t *d, *next_d;
fp@1959: ec_slave_config_t *c, *next_c;
fp@1959:
fp@1959: d = master->first_domain;
fp@1959: while (d) {
fp@1959: next_d = d->next;
fp@1959: ec_domain_clear(d);
fp@1959: d = next_d;
fp@1959: }
fp@1960: master->first_domain = NULL;
fp@1959:
fp@1959: c = master->first_config;
fp@1959: while (c) {
fp@1959: next_c = c->next;
fp@1959: ec_slave_config_clear(c);
fp@1959: c = next_c;
fp@1959: }
fp@1960: master->first_config = NULL;
fp@1960: }
fp@1960:
fp@1960: /*****************************************************************************/
fp@1960:
fp@1960: void ec_master_clear(ec_master_t *master)
fp@1960: {
fp@1960: if (master->process_data) {
fp@1960: munmap(master->process_data, master->process_data_size);
fp@1960: }
fp@1960:
fp@1960: ec_master_clear_config(master);
fp@1959:
fp@1959: if (master->fd != -1) {
fp@1959: close(master->fd);
fp@1959: }
fp@1959: }
fp@1959:
fp@1959: /*****************************************************************************/
fp@1959:
fp@1959: void ec_master_add_domain(ec_master_t *master, ec_domain_t *domain)
fp@1959: {
fp@1959: if (master->first_domain) {
fp@1959: ec_domain_t *d = master->first_domain;
fp@1959: while (d->next) {
fp@1959: d = d->next;
fp@1959: }
fp@1959: d->next = domain;
fp@1959: } else {
fp@1959: master->first_domain = domain;
fp@1959: }
fp@1959: }
fp@1959:
fp@1959: /*****************************************************************************/
fp@1959:
fp@1244: ec_domain_t *ecrt_master_create_domain(ec_master_t *master)
fp@1244: {
fp@1244: ec_domain_t *domain;
fp@1244: int index;
fp@1244:
fp@1244: domain = malloc(sizeof(ec_domain_t));
fp@1244: if (!domain) {
fp@1244: fprintf(stderr, "Failed to allocate memory.\n");
fp@1244: return 0;
fp@1244: }
fp@1244:
fp@1244: index = ioctl(master->fd, EC_IOCTL_CREATE_DOMAIN, NULL);
fp@1244: if (index == -1) {
fp@1244: fprintf(stderr, "Failed to create domain: %s\n", strerror(errno));
fp@1244: free(domain);
fp@1244: return 0;
fp@1244: }
fp@1244:
fp@1959: domain->next = NULL;
fp@1244: domain->index = (unsigned int) index;
fp@1255: domain->master = master;
fp@1258: domain->process_data = NULL;
fp@1959:
fp@1959: ec_master_add_domain(master, domain);
fp@1959:
fp@1244: return domain;
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1959: void ec_master_add_slave_config(ec_master_t *master, ec_slave_config_t *sc)
fp@1959: {
fp@1959: if (master->first_config) {
fp@1959: ec_slave_config_t *c = master->first_config;
fp@1959: while (c->next) {
fp@1959: c = c->next;
fp@1959: }
fp@1959: c->next = sc;
fp@1959: } else {
fp@1959: master->first_config = sc;
fp@1959: }
fp@1959: }
fp@1959:
fp@1959: /*****************************************************************************/
fp@1959:
fp@1244: ec_slave_config_t *ecrt_master_slave_config(ec_master_t *master,
fp@1244: uint16_t alias, uint16_t position, uint32_t vendor_id,
fp@1244: uint32_t product_code)
fp@1244: {
fp@1246: ec_ioctl_config_t data;
fp@1246: ec_slave_config_t *sc;
fp@1246: int index;
fp@1246:
fp@1246: sc = malloc(sizeof(ec_slave_config_t));
fp@1246: if (!sc) {
fp@1246: fprintf(stderr, "Failed to allocate memory.\n");
fp@1246: return 0;
fp@1246: }
fp@1246:
fp@1246: data.alias = alias;
fp@1246: data.position = position;
fp@1246: data.vendor_id = vendor_id;
fp@1246: data.product_code = product_code;
fp@1246:
fp@1246: if (ioctl(master->fd, EC_IOCTL_CREATE_SLAVE_CONFIG, &data) == -1) {
fp@1246: fprintf(stderr, "Failed to create slave config: %s\n",
fp@1246: strerror(errno));
fp@1246: free(sc);
fp@1246: return 0;
fp@1246: }
fp@1246:
fp@1959: sc->next = NULL;
fp@1255: sc->master = master;
fp@1246: sc->index = data.config_index;
fp@1255: sc->alias = alias;
fp@1255: sc->position = position;
fp@1959: sc->first_sdo_request = NULL;
fp@1959: sc->first_voe_handler = NULL;
fp@1959:
fp@1959: ec_master_add_slave_config(master, sc);
fp@1959:
fp@1246: return sc;
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1497: int ecrt_master(ec_master_t* master, ec_master_info_t *master_info)
fp@1497: {
fp@1508: ec_ioctl_master_t data;
fp@1508:
fp@1508: if (ioctl(master->fd, EC_IOCTL_MASTER, &data) < 0) {
fp@1497: fprintf(stderr, "Failed to get master info: %s\n", strerror(errno));
fp@1508: return -1;
fp@1508: }
fp@1508:
fp@1508: master_info->slave_count = data.slave_count;
fp@1508: master_info->link_up = data.devices[0].link_state;
martin@1579: master_info->scan_busy = data.scan_busy;
fp@1508: master_info->app_time = data.app_time;
fp@1508: return 0;
fp@1497: }
fp@1497:
fp@1497: /*****************************************************************************/
fp@1497:
fp@1510: int ecrt_master_get_slave(ec_master_t *master, uint16_t slave_position,
fp@1341: ec_slave_info_t *slave_info)
fp@1341: {
fp@1341: ec_ioctl_slave_t data;
fp@1341: int index;
fp@1341:
fp@1510: data.position = slave_position;
fp@1341:
fp@1341: if (ioctl(master->fd, EC_IOCTL_SLAVE, &data) == -1) {
fp@1497: fprintf(stderr, "Failed to get slave info: %s\n", strerror(errno));
fp@1341: return -1;
fp@1341: }
fp@1341:
fp@1341: slave_info->position = data.position;
fp@1341: slave_info->vendor_id = data.vendor_id;
fp@1341: slave_info->product_code = data.product_code;
fp@1341: slave_info->revision_number = data.revision_number;
fp@1341: slave_info->serial_number = data.serial_number;
fp@1341: slave_info->alias = data.alias;
fp@1341: slave_info->current_on_ebus = data.current_on_ebus;
fp@1341: slave_info->al_state = data.al_state;
fp@1341: slave_info->error_flag = data.error_flag;
fp@1341: slave_info->sync_count = data.sync_count;
fp@1341: slave_info->sdo_count = data.sdo_count;
fp@1593: strncpy(slave_info->name, data.name, EC_MAX_STRING_LENGTH);
fp@1341: return 0;
fp@1341: }
fp@1341:
fp@1341: /*****************************************************************************/
fp@1341:
fp@1510: int ecrt_master_get_sync_manager(ec_master_t *master, uint16_t slave_position,
fp@1510: uint8_t sync_index, ec_sync_info_t *sync)
fp@1510: {
fp@1804: ec_ioctl_slave_sync_t data;
fp@1510:
fp@1510: if (sync_index >= EC_MAX_SYNC_MANAGERS)
fp@1510: return -ENOENT;
fp@1510:
fp@1510: memset(&data, 0x00, sizeof(ec_ioctl_slave_sync_t));
fp@1510: data.slave_position = slave_position;
fp@1510: data.sync_index = sync_index;
fp@1510:
fp@1510: if (ioctl(master->fd, EC_IOCTL_SLAVE_SYNC, &data) == -1) {
fp@1510: fprintf(stderr, "Failed to get sync manager information: %s\n",
fp@1510: strerror(errno));
fp@1510: return -1; // FIXME
fp@1510: }
fp@1510:
fp@1510: sync->index = sync_index;
fp@1804: sync->dir = EC_READ_BIT(&data.control_register, 2) ?
fp@1510: EC_DIR_OUTPUT : EC_DIR_INPUT;
fp@1510: sync->n_pdos = data.pdo_count;
fp@1510: sync->pdos = NULL;
fp@1510: sync->watchdog_mode = EC_READ_BIT(&data.control_register, 6) ?
fp@1510: EC_WD_ENABLE : EC_WD_DISABLE;
fp@1510:
fp@1510: return 0;
fp@1510: }
fp@1510:
fp@1510: /*****************************************************************************/
fp@1510:
fp@1510: int ecrt_master_get_pdo(ec_master_t *master, uint16_t slave_position,
fp@1510: uint8_t sync_index, uint16_t pos, ec_pdo_info_t *pdo)
fp@1510: {
fp@1804: ec_ioctl_slave_sync_pdo_t data;
fp@1510:
fp@1510: if (sync_index >= EC_MAX_SYNC_MANAGERS)
fp@1510: return -ENOENT;
fp@1510:
fp@1510: memset(&data, 0x00, sizeof(ec_ioctl_slave_sync_pdo_t));
fp@1510: data.slave_position = slave_position;
fp@1510: data.sync_index = sync_index;
fp@1510: data.pdo_pos = pos;
fp@1510:
fp@1510: if (ioctl(master->fd, EC_IOCTL_SLAVE_SYNC_PDO, &data) == -1) {
fp@1510: fprintf(stderr, "Failed to get pdo information: %s\n",
fp@1510: strerror(errno));
fp@1510: return -1; // FIXME
fp@1510: }
fp@1510:
fp@1510: pdo->index = data.index;
fp@1510: pdo->n_entries = data.entry_count;
fp@1510: pdo->entries = NULL;
fp@1510:
fp@1510: return 0;
fp@1510: }
fp@1510:
fp@1510: /*****************************************************************************/
fp@1510:
fp@1510: int ecrt_master_get_pdo_entry(ec_master_t *master, uint16_t slave_position,
fp@1510: uint8_t sync_index, uint16_t pdo_pos, uint16_t entry_pos,
fp@1510: ec_pdo_entry_info_t *entry)
fp@1510: {
fp@1804: ec_ioctl_slave_sync_pdo_entry_t data;
fp@1510:
fp@1510: if (sync_index >= EC_MAX_SYNC_MANAGERS)
fp@1510: return -ENOENT;
fp@1510:
fp@1510: memset(&data, 0x00, sizeof(ec_ioctl_slave_sync_pdo_entry_t));
fp@1510: data.slave_position = slave_position;
fp@1510: data.sync_index = sync_index;
fp@1510: data.pdo_pos = pdo_pos;
fp@1510: data.entry_pos = entry_pos;
fp@1510:
fp@1510: if (ioctl(master->fd, EC_IOCTL_SLAVE_SYNC_PDO_ENTRY, &data) == -1) {
fp@1510: fprintf(stderr, "Failed to get pdo entry information: %s\n",
fp@1510: strerror(errno));
fp@1510: return -1; // FIXME
fp@1510: }
fp@1510:
fp@1510: entry->index = data.index;
fp@1510: entry->subindex = data.subindex;
fp@1510: entry->bit_length = data.bit_length;
fp@1510:
fp@1510: return 0;
fp@1510: }
fp@1510:
fp@1510: /*****************************************************************************/
fp@1510:
fp@1510: int ecrt_master_sdo_download(ec_master_t *master, uint16_t slave_position,
fp@1510: uint16_t index, uint8_t subindex, uint8_t *data,
fp@1510: size_t data_size, uint32_t *abort_code)
fp@1510: {
fp@1510: ec_ioctl_slave_sdo_download_t download;
fp@1510:
fp@1510: download.slave_position = slave_position;
fp@1510: download.sdo_index = index;
fp@1510: download.sdo_entry_subindex = subindex;
fp@1510: download.data_size = data_size;
fp@1510: download.data = data;
fp@1510:
fp@1510: if (ioctl(master->fd, EC_IOCTL_SLAVE_SDO_DOWNLOAD, &download) == -1) {
fp@1911: if (errno == EIO && abort_code) {
fp@1911: *abort_code = download.abort_code;
fp@1510: }
fp@1510: fprintf(stderr, "Failed to execute SDO download: %s\n",
fp@1510: strerror(errno));
fp@1510: return -1;
fp@1510: }
fp@1510:
fp@1510: return 0;
fp@1510: }
fp@1510:
fp@1510: /*****************************************************************************/
fp@1510:
fp@1510: int ecrt_master_sdo_upload(ec_master_t *master, uint16_t slave_position,
fp@1510: uint16_t index, uint8_t subindex, uint8_t *target,
fp@1510: size_t target_size, size_t *result_size, uint32_t *abort_code)
fp@1510: {
fp@1510: ec_ioctl_slave_sdo_upload_t upload;
fp@1510:
fp@1510: upload.slave_position = slave_position;
fp@1510: upload.sdo_index = index;
fp@1510: upload.sdo_entry_subindex = subindex;
fp@1510: upload.target_size = target_size;
fp@1510: upload.target = target;
fp@1510:
fp@1510: if (ioctl(master->fd, EC_IOCTL_SLAVE_SDO_UPLOAD, &upload) == -1) {
fp@1911: if (errno == EIO && abort_code) {
fp@1911: *abort_code = upload.abort_code;
fp@1510: }
fp@1510: fprintf(stderr, "Failed to execute SDO upload: %s\n",
fp@1510: strerror(errno));
fp@1510: return -1;
fp@1510: }
fp@1510:
fp@1510: *result_size = upload.data_size;
fp@1510: return 0;
fp@1510: }
fp@1510:
fp@1510: /*****************************************************************************/
fp@1510:
fp@1913: int ecrt_master_write_idn(ec_master_t *master, uint16_t slave_position,
fp@1952: uint8_t drive_no, uint16_t idn, uint8_t *data, size_t data_size,
fp@1952: uint16_t *error_code)
fp@1913: {
fp@1913: ec_ioctl_slave_soe_write_t io;
fp@1913:
fp@1913: io.slave_position = slave_position;
fp@1952: io.drive_no = drive_no;
fp@1913: io.idn = idn;
fp@1913: io.data_size = data_size;
fp@1913: io.data = data;
fp@1913:
fp@1913: if (ioctl(master->fd, EC_IOCTL_SLAVE_SOE_WRITE, &io) == -1) {
fp@1913: if (errno == EIO && error_code) {
fp@1913: *error_code = io.error_code;
fp@1913: }
fp@1913: fprintf(stderr, "Failed to write IDN: %s\n", strerror(errno));
fp@1913: return -1;
fp@1913: }
fp@1913:
fp@1913: return 0;
fp@1913: }
fp@1913:
fp@1913: /*****************************************************************************/
fp@1913:
fp@1913: int ecrt_master_read_idn(ec_master_t *master, uint16_t slave_position,
fp@1952: uint8_t drive_no, uint16_t idn, uint8_t *target, size_t target_size,
fp@1947: size_t *result_size, uint16_t *error_code)
fp@1913: {
fp@1913: ec_ioctl_slave_soe_read_t io;
fp@1913:
fp@1913: io.slave_position = slave_position;
fp@1952: io.drive_no = drive_no;
fp@1913: io.idn = idn;
fp@1913: io.mem_size = target_size;
fp@1913: io.data = target;
fp@1913:
fp@1913: if (ioctl(master->fd, EC_IOCTL_SLAVE_SOE_READ, &io) == -1) {
fp@1913: if (errno == EIO && error_code) {
fp@1913: *error_code = io.error_code;
fp@1913: }
fp@1913: fprintf(stderr, "Failed to read IDN: %s\n", strerror(errno));
fp@1913: return -1;
fp@1913: }
fp@1913:
fp@1913: *result_size = io.data_size;
fp@1913: return 0;
fp@1913: }
fp@1913:
fp@1913: /*****************************************************************************/
fp@1913:
fp@1244: int ecrt_master_activate(ec_master_t *master)
fp@1244: {
fp@1258: if (ioctl(master->fd, EC_IOCTL_ACTIVATE,
fp@1258: &master->process_data_size) == -1) {
fp@1247: fprintf(stderr, "Failed to activate master: %s\n",
fp@1247: strerror(errno));
fp@1313: return -1; // FIXME
fp@1247: }
fp@1247:
fp@1258: if (master->process_data_size) {
fp@1258: master->process_data = mmap(0, master->process_data_size,
fp@1259: PROT_READ | PROT_WRITE, MAP_SHARED, master->fd, 0);
fp@1258: if (master->process_data == MAP_FAILED) {
fp@1258: fprintf(stderr, "Failed to map process data: %s", strerror(errno));
fp@1258: master->process_data = NULL;
fp@1258: master->process_data_size = 0;
fp@1313: return -1; // FIXME
fp@1258: }
fp@1258:
fp@1258: // Access the mapped region to cause the initial page fault
fp@2062: master->process_data[0] = 0x00;
fp@1258: }
fp@1258:
fp@1244: return 0;
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1531: void ecrt_master_deactivate(ec_master_t *master)
fp@1531: {
fp@1531: if (ioctl(master->fd, EC_IOCTL_DEACTIVATE, NULL) == -1) {
fp@1531: fprintf(stderr, "Failed to deactivate master: %s\n", strerror(errno));
fp@1531: return;
fp@1531: }
fp@1960:
fp@1960: ec_master_clear_config(master);
fp@1960: }
martin@1585:
martin@1585: /*****************************************************************************/
martin@1585:
martin@1600: int ecrt_master_set_send_interval(ec_master_t *master,size_t send_interval_us)
martin@1600: {
fp@1804: if (ioctl(master->fd, EC_IOCTL_SET_SEND_INTERVAL,
fp@1804: &send_interval_us) == -1) {
fp@1804: fprintf(stderr, "Failed to set send interval: %s\n",
martin@1585: strerror(errno));
martin@1585: return -1; // FIXME
martin@1585: }
martin@1585: return 0;
martin@1585: }
martin@1585:
fp@1531: /*****************************************************************************/
fp@1531:
fp@1244: void ecrt_master_send(ec_master_t *master)
fp@1244: {
fp@1247: if (ioctl(master->fd, EC_IOCTL_SEND, NULL) == -1) {
fp@1247: fprintf(stderr, "Failed to send: %s\n", strerror(errno));
fp@1247: }
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1244: void ecrt_master_receive(ec_master_t *master)
fp@1244: {
fp@1247: if (ioctl(master->fd, EC_IOCTL_RECEIVE, NULL) == -1) {
fp@1247: fprintf(stderr, "Failed to receive: %s\n", strerror(errno));
fp@1247: }
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1244:
fp@1244: void ecrt_master_state(const ec_master_t *master, ec_master_state_t *state)
fp@1244: {
fp@1259: if (ioctl(master->fd, EC_IOCTL_MASTER_STATE, state) == -1) {
fp@1259: fprintf(stderr, "Failed to get master state: %s\n", strerror(errno));
fp@1259: }
fp@1244: }
fp@1244:
fp@1244: /*****************************************************************************/
fp@1413:
fp@1434: void ecrt_master_application_time(ec_master_t *master, uint64_t app_time)
fp@1434: {
fp@1434: ec_ioctl_app_time_t data;
fp@1413:
fp@1417: data.app_time = app_time;
fp@1413:
fp@1434: if (ioctl(master->fd, EC_IOCTL_APP_TIME, &data) == -1) {
fp@1434: fprintf(stderr, "Failed to set application time: %s\n",
fp@1434: strerror(errno));
fp@1434: }
fp@1434: }
fp@1434:
fp@1434: /*****************************************************************************/
fp@1434:
fp@1434: void ecrt_master_sync_reference_clock(ec_master_t *master)
fp@1434: {
fp@1434: if (ioctl(master->fd, EC_IOCTL_SYNC_REF, NULL) == -1) {
fp@1413: fprintf(stderr, "Failed to sync reference clock: %s\n",
fp@1413: strerror(errno));
fp@1413: }
fp@1413: }
fp@1413:
fp@1413: /*****************************************************************************/
fp@1413:
fp@1413: void ecrt_master_sync_slave_clocks(ec_master_t *master)
fp@1413: {
fp@1413: if (ioctl(master->fd, EC_IOCTL_SYNC_SLAVES, NULL) == -1) {
fp@1413: fprintf(stderr, "Failed to sync slave clocks: %s\n", strerror(errno));
fp@1413: }
fp@1413: }
fp@1413:
fp@1413: /*****************************************************************************/
fp@1535:
fp@1535: void ecrt_master_sync_monitor_queue(ec_master_t *master)
fp@1535: {
fp@1535: if (ioctl(master->fd, EC_IOCTL_SYNC_MON_QUEUE, NULL) == -1) {
fp@1535: fprintf(stderr, "Failed to queue sync monitor datagram: %s\n",
fp@1535: strerror(errno));
fp@1535: }
fp@1535: }
fp@1535:
fp@1535: /*****************************************************************************/
fp@1535:
fp@1535: uint32_t ecrt_master_sync_monitor_process(ec_master_t *master)
fp@1535: {
fp@1535: uint32_t time_diff;
fp@1535:
fp@1535: if (ioctl(master->fd, EC_IOCTL_SYNC_MON_PROCESS, &time_diff) == -1) {
fp@1535: time_diff = 0xffffffff;
fp@1535: fprintf(stderr, "Failed to process sync monitor datagram: %s\n",
fp@1535: strerror(errno));
fp@1535: }
fp@1535:
fp@1535: return time_diff;
fp@1535: }
fp@1535:
fp@1535: /*****************************************************************************/
fp@2009:
fp@2009: void ecrt_master_reset(ec_master_t *master)
fp@2009: {
fp@2009: if (ioctl(master->fd, EC_IOCTL_RESET, NULL) == -1) {
fp@2009: fprintf(stderr, "Failed to reset master: %s\n", strerror(errno));
fp@2009: }
fp@2009: }
fp@2009:
fp@2009: /*****************************************************************************/