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@792:         ec_slave_t *slave, /**< EtherCAT slave. */
fp@792:         unsigned int index /**< Sync manager index. */
fp@628:         )
fp@628: {
fp@628:     sync->slave = slave;
fp@628:     sync->index = index;    
fp@628: 
fp@879:     ec_pdo_list_init(&sync->pdos);
fp@879:     sync->assign_source = EC_ASSIGN_NONE;
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->index = other->index;
fp@873:    sync->physical_start_address = other->physical_start_address;
fp@873:    sync->length = other->length;
fp@873:    sync->control_register = other->control_register;
fp@873:    sync->enable = other->enable;
fp@873:    
fp@879:    ec_pdo_list_init(&sync->pdos);
fp@879:    ec_pdo_list_copy(&sync->pdos, &other->pdos);
fp@873: 
fp@879:    sync->assign_source = other->assign_source;
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@872: /** Initializes a sync manager configuration page with SII data.
fp@792:  *
fp@792:  * The referenced memory (\a data) must be at least \a EC_SYNC_SIZE bytes.
fp@635:  */
fp@792: void ec_sync_config(
fp@792:         const ec_sync_t *sync, /**< Sync manager. */
fp@792:         uint16_t data_size, /**< Data size. */
fp@792:         uint8_t *data /**> Configuration memory. */
fp@635:         )
fp@635: {
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@792:                data_size, sync->control_register, sync->enable);
fp@628:     }
fp@628: 
fp@628:     EC_WRITE_U16(data,     sync->physical_start_address);
fp@792:     EC_WRITE_U16(data + 2, data_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@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@792: /** Get direction covered by sync manager.
fp@792:  *
fp@792:  * \return Direction covered by the given sync manager.
fp@635:  */
fp@792: ec_direction_t ec_sync_direction(
fp@792:         const ec_sync_t *sync /**< EtherCAT sync manager. */
fp@748:         )
fp@748: {
fp@748:     int index = sync->index;
fp@748: 
fp@834:     if (sync->slave && sync->slave->sii.mailbox_protocols) {
fp@748:         index -= 2;
fp@748:     }
fp@748: 
fp@748:     if (index < 0 || index > 1) {
fp@792:         EC_WARN("ec_sync_get_direction(): invalid sync manager index.\n");
fp@792:         return EC_DIR_OUTPUT;
fp@748:     }
fp@748: 
fp@792:     return (ec_direction_t) index;
fp@748: }
fp@748: 
fp@748: /*****************************************************************************/