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@792: /** \file fp@792: * EtherCAT sync manager methods. fp@792: */ 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@792: /** Constructor. fp@628: */ fp@628: void ec_sync_init( fp@792: ec_sync_t *sync, /**< EtherCAT sync manager. */ fp@1055: ec_slave_t *slave /**< EtherCAT slave. */ fp@628: ) fp@628: { fp@628: sync->slave = slave; fp@1055: sync->physical_start_address = 0x0000; fp@1055: sync->default_length = 0x0000; fp@1055: sync->control_register = 0x00; fp@1055: sync->enable = 0x00; fp@879: ec_pdo_list_init(&sync->pdos); fp@628: } fp@628: fp@628: /*****************************************************************************/ fp@628: fp@873: /** Copy constructor. fp@873: */ fp@873: void ec_sync_init_copy( fp@873: ec_sync_t *sync, /**< EtherCAT sync manager. */ fp@873: const ec_sync_t *other /**< Sync manager to copy from. */ fp@873: ) fp@873: { fp@873: sync->slave = other->slave; fp@873: sync->physical_start_address = other->physical_start_address; fp@1055: sync->default_length = other->default_length; fp@873: sync->control_register = other->control_register; fp@873: sync->enable = other->enable; fp@879: ec_pdo_list_init(&sync->pdos); fp@879: ec_pdo_list_copy(&sync->pdos, &other->pdos); fp@873: } fp@873: fp@873: /*****************************************************************************/ fp@873: fp@792: /** Destructor. fp@628: */ fp@628: void ec_sync_clear( fp@792: ec_sync_t *sync /**< EtherCAT sync manager. */ fp@628: ) fp@628: { fp@879: ec_pdo_list_clear(&sync->pdos); fp@635: } fp@635: fp@635: /*****************************************************************************/ fp@635: fp@1055: /** Initializes a sync manager configuration page. fp@792: * fp@792: * The referenced memory (\a data) must be at least \a EC_SYNC_SIZE bytes. fp@635: */ fp@1055: void ec_sync_page( fp@792: const ec_sync_t *sync, /**< Sync manager. */ fp@1055: uint8_t sync_index, /**< Index of the sync manager. */ fp@792: uint16_t data_size, /**< Data size. */ fp@1055: ec_direction_t dir, /**< Direction (overrides the control byte, fp@1055: if set to EC_DIR_INPUT or EC_DIR_OUTPUT). */ fp@792: uint8_t *data /**> Configuration memory. */ fp@635: ) fp@635: { fp@943: // enable only if SII enable is set and size is > 0. fp@943: uint16_t enable = sync->enable && data_size; fp@1055: uint8_t control = sync->control_register; fp@943: fp@1055: if (dir == EC_DIR_OUTPUT || dir == EC_DIR_INPUT) { fp@1055: // override sync manager direction bits with dir parameter fp@1055: EC_WRITE_BIT(&control, 2, dir == EC_DIR_OUTPUT ? 1 : 0); fp@1055: EC_WRITE_BIT(&control, 3, 0); fp@1055: } fp@1055: fp@1055: if (sync->slave->master->debug_level) fp@943: EC_DBG("SM%u: Addr 0x%04X, Size %3u, Ctrl 0x%02X, En %u\n", fp@1055: sync_index, sync->physical_start_address, fp@1055: data_size, control, enable); fp@628: fp@628: EC_WRITE_U16(data, sync->physical_start_address); fp@792: EC_WRITE_U16(data + 2, data_size); fp@1055: EC_WRITE_U8 (data + 4, control); fp@628: EC_WRITE_U8 (data + 5, 0x00); // status byte (read only) fp@943: EC_WRITE_U16(data + 6, enable); fp@628: } fp@628: fp@628: /*****************************************************************************/ fp@635: fp@814: /** Adds a Pdo to the list of known mapped Pdos. fp@792: * fp@758: * \return 0 on success, else < 0 fp@635: */ fp@635: int ec_sync_add_pdo( fp@792: ec_sync_t *sync, /**< EtherCAT sync manager. */ fp@814: const ec_pdo_t *pdo /**< Pdo to map. */ fp@635: ) fp@635: { fp@879: return ec_pdo_list_add_pdo_copy(&sync->pdos, pdo); fp@635: } fp@635: fp@635: /*****************************************************************************/ fp@635: fp@1055: /** Determines the default direction from the control register. fp@635: */ fp@1055: ec_direction_t ec_sync_default_direction( fp@792: const ec_sync_t *sync /**< EtherCAT sync manager. */ fp@748: ) fp@748: { fp@1055: switch ((sync->control_register & 0x0C) >> 2) { fp@1055: case 0x0: return EC_DIR_INPUT; fp@1060: case 0x1: return EC_DIR_OUTPUT; fp@1055: default: return EC_DIR_INVALID; fp@748: } fp@748: } fp@748: fp@748: /*****************************************************************************/