fp@628: /****************************************************************************** fp@628: * fp@628: * $Id$ fp@628: * fp@1326: * Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH fp@628: * fp@628: * This file is part of the IgH EtherCAT Master. fp@628: * fp@1326: * The IgH EtherCAT Master is free software; you can redistribute it and/or fp@1326: * modify it under the terms of the GNU General Public License version 2, as fp@1326: * published by the Free Software Foundation. fp@628: * fp@1326: * The IgH EtherCAT Master is distributed in the hope that it will be useful, fp@1326: * but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1326: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General fp@1326: * Public License for more details. fp@628: * fp@1326: * You should have received a copy of the GNU General Public License along fp@1326: * 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@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@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@1509: const ec_sync_config_t *sync_config, /**< Configuration. */ fp@2123: uint8_t pdo_xfer, /**< Non-zero, if PDOs will be transferred via this fp@2123: sync manager. */ fp@792: uint8_t *data /**> Configuration memory. */ fp@635: ) fp@635: { fp@2123: // enable only if (SII enable is set or PDO xfer) fp@2123: // and size is > 0 and SM is not virtual fp@2123: uint16_t enable = ((sync->enable & 0x01) || pdo_xfer) ch1010252@1801: && data_size ch1010252@1801: && ((sync->enable & 0x04) == 0); fp@1055: uint8_t control = sync->control_register; fp@943: fp@1509: if (sync_config) { fp@1509: fp@1509: switch (sync_config->dir) { fp@1509: case EC_DIR_OUTPUT: fp@1509: case EC_DIR_INPUT: fp@1509: EC_WRITE_BIT(&control, 2, fp@1509: sync_config->dir == EC_DIR_OUTPUT ? 1 : 0); fp@1509: EC_WRITE_BIT(&control, 3, 0); fp@1509: break; fp@1509: default: fp@1509: break; fp@1509: } fp@1509: fp@1509: switch (sync_config->watchdog_mode) { fp@1509: case EC_WD_ENABLE: fp@1509: case EC_WD_DISABLE: fp@1509: EC_WRITE_BIT(&control, 6, fp@1509: sync_config->watchdog_mode == EC_WD_ENABLE); fp@1509: break; fp@1509: default: fp@1509: break; fp@1509: } fp@1055: } fp@1055: fp@1921: EC_SLAVE_DBG(sync->slave, 1, "SM%u: Addr 0x%04X, Size %3u," fp@1921: " Ctrl 0x%02X, En %u\n", fp@1921: sync_index, sync->physical_start_address, fp@1921: 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@1327: /** 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@1327: 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@2522: * fp@2522: * \return Direction. 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: /*****************************************************************************/