fp@1744: /****************************************************************************** fp@1744: * fp@1744: * $Id$ fp@1744: * fp@1744: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1744: * fp@1744: * This file is part of the IgH EtherCAT Master. fp@1744: * fp@1744: * The IgH EtherCAT Master is free software; you can redistribute it fp@1744: * and/or modify it under the terms of the GNU General Public License fp@1744: * as published by the Free Software Foundation; either version 2 of the fp@1744: * License, or (at your option) any later version. fp@1744: * fp@1744: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1744: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1744: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1744: * GNU General Public License for more details. fp@1744: * fp@1744: * You should have received a copy of the GNU General Public License fp@1744: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1744: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1744: * fp@1744: * The right to use EtherCAT Technology is granted and comes free of fp@1744: * charge under condition of compatibility of product made by fp@1744: * Licensee. People intending to distribute/sell products based on the fp@1744: * code, have to sign an agreement to guarantee that products using fp@1744: * software based on IgH EtherCAT master stay compatible with the actual fp@1744: * EtherCAT specification (which are released themselves as an open fp@1744: * standard) as the (only) precondition to have the right to use EtherCAT fp@1744: * Technology, IP and trade marks. fp@1744: * fp@1744: *****************************************************************************/ fp@1744: fp@1744: /** fp@1744: \file fp@1744: EtherCAT sync manager methods. fp@1744: */ fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: #include "globals.h" fp@1744: #include "slave.h" fp@1744: #include "master.h" fp@1744: #include "pdo.h" fp@1744: #include "sync.h" fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Constructor. fp@1744: */ fp@1744: fp@1744: void ec_sync_init( fp@1744: ec_sync_t *sync, /**< EtherCAT sync manager */ fp@1744: ec_slave_t *slave, /**< EtherCAT slave */ fp@1744: unsigned int index /**< sync manager index */ fp@1744: ) fp@1744: { fp@1744: sync->slave = slave; fp@1744: sync->index = index; fp@1744: fp@1744: sync->est_length = 0; fp@1744: INIT_LIST_HEAD(&sync->pdos); fp@1744: sync->alt_mapping = 0; fp@1749: sync->mapping_source = EC_SYNC_MAPPING_NONE; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Destructor. fp@1744: */ fp@1744: fp@1744: void ec_sync_clear( fp@1744: ec_sync_t *sync /**< EtherCAT sync manager */ fp@1744: ) fp@1744: { fp@1744: ec_sync_clear_pdos(sync); fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: * Calculates the size of a sync manager by evaluating PDO sizes. fp@1744: * \return sync manager size fp@1744: */ fp@1744: fp@1744: uint16_t ec_sync_size( fp@1744: const ec_sync_t *sync /**< sync manager */ fp@1744: ) fp@1744: { fp@1744: const ec_pdo_t *pdo; fp@1744: const ec_pdo_entry_t *pdo_entry; fp@1744: unsigned int bit_size, byte_size; fp@1744: fp@1744: if (sync->length) return sync->length; fp@1744: if (sync->est_length) return sync->est_length; fp@1744: fp@1744: bit_size = 0; fp@1744: list_for_each_entry(pdo, &sync->pdos, list) { fp@1744: list_for_each_entry(pdo_entry, &pdo->entries, list) { fp@1744: bit_size += pdo_entry->bit_length; fp@1744: } fp@1744: } fp@1744: fp@1744: if (bit_size % 8) // round up to full bytes fp@1744: byte_size = bit_size / 8 + 1; fp@1744: else fp@1744: byte_size = bit_size / 8; fp@1744: fp@1744: return byte_size; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1744: Initializes a sync manager configuration page with EEPROM data. fp@1744: The referenced memory (\a data) must be at least EC_SYNC_SIZE bytes. fp@1744: */ fp@1744: fp@1744: void ec_sync_config( fp@1744: const ec_sync_t *sync, /**< sync manager */ fp@1744: uint8_t *data /**> configuration memory */ fp@1744: ) fp@1744: { fp@1744: size_t sync_size = ec_sync_size(sync); fp@1744: fp@1744: if (sync->slave->master->debug_level) { fp@1744: EC_DBG("SM%i: Addr 0x%04X, Size %3i, Ctrl 0x%02X, En %i\n", fp@1744: sync->index, sync->physical_start_address, fp@1744: sync_size, sync->control_register, sync->enable); fp@1744: } fp@1744: fp@1744: EC_WRITE_U16(data, sync->physical_start_address); fp@1744: EC_WRITE_U16(data + 2, sync_size); fp@1744: EC_WRITE_U8 (data + 4, sync->control_register); fp@1744: EC_WRITE_U8 (data + 5, 0x00); // status byte (read only) fp@1744: EC_WRITE_U16(data + 6, sync->enable ? 0x0001 : 0x0000); // enable fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Adds a PDO to the list of known mapped PDOs. fp@1746: * \return 0 on success, else < 0 fp@1744: */ fp@1744: fp@1744: int ec_sync_add_pdo( fp@1744: ec_sync_t *sync, /**< EtherCAT sync manager */ fp@1744: const ec_pdo_t *pdo /**< PDO to map */ fp@1744: ) fp@1744: { fp@1744: ec_pdo_t *mapped_pdo; fp@1744: fp@1744: // PDO already mapped? fp@1744: list_for_each_entry(mapped_pdo, &sync->pdos, list) { fp@1744: if (mapped_pdo->index != pdo->index) continue; fp@1744: EC_ERR("PDO 0x%04X is already mapped!\n", pdo->index); fp@1744: return -1; fp@1744: } fp@1744: fp@1744: if (!(mapped_pdo = kmalloc(sizeof(ec_pdo_t), GFP_KERNEL))) { fp@1744: EC_ERR("Failed to allocate memory for PDO mapping.\n"); fp@1744: return -1; fp@1744: } fp@1744: fp@1744: ec_pdo_init(mapped_pdo); fp@1744: if (ec_pdo_copy(mapped_pdo, pdo)) { fp@1744: ec_pdo_clear(mapped_pdo); fp@1744: kfree(mapped_pdo); fp@1744: return -1; fp@1744: } fp@1744: fp@1744: // set appropriate sync manager index fp@1744: mapped_pdo->sync_index = sync->index; fp@1744: fp@1744: list_add_tail(&mapped_pdo->list, &sync->pdos); fp@1744: return 0; fp@1744: } fp@1744: fp@1744: /*****************************************************************************/ fp@1744: fp@1744: /** fp@1746: * Clears the list of known mapped PDOs. fp@1744: */ fp@1744: fp@1744: void ec_sync_clear_pdos( fp@1744: ec_sync_t *sync /**< EtherCAT sync manager */ fp@1744: ) fp@1744: { fp@1744: ec_pdo_t *pdo, *next; fp@1744: fp@1744: // free all mapped PDOs fp@1744: list_for_each_entry_safe(pdo, next, &sync->pdos, list) { fp@1744: list_del(&pdo->list); fp@1744: ec_pdo_clear(pdo); fp@1744: kfree(pdo); fp@1744: } fp@1746: } fp@1746: fp@1746: /*****************************************************************************/ fp@1746: fp@1746: /** fp@1746: * \return Type of PDOs covered by the given sync manager. fp@1746: */ fp@1746: fp@1746: ec_pdo_type_t ec_sync_get_pdo_type( fp@1746: const ec_sync_t *sync /**< EtherCAT sync manager */ fp@1746: ) fp@1746: { fp@1746: int index = sync->index; fp@1746: fp@1746: if (sync->slave && sync->slave->sii_mailbox_protocols) { fp@1746: index -= 2; fp@1746: } fp@1746: fp@1746: if (index < 0 || index > 1) { fp@1746: EC_WARN("ec_sync_get_pdo_type(): invalid sync manager index.\n"); fp@1746: return EC_RX_PDO; fp@1746: } fp@1746: fp@1746: return (ec_pdo_type_t) index; fp@1746: } fp@1746: fp@1746: /*****************************************************************************/