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