master/slave.h
author Florian Pose <fp@igh-essen.com>
Tue, 13 Feb 2007 14:18:29 +0000
branchstable-1.2
changeset 1742 9195107c9441
parent 1739 5fcbd29151d2
child 1744 7bc131b92039
permissions -rw-r--r--
Removed unwanted label.
/******************************************************************************
 *
 *  $Id$
 *
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
 *
 *  This file is part of the IgH EtherCAT Master.
 *
 *  The IgH EtherCAT Master is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  The IgH EtherCAT Master is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  The right to use EtherCAT Technology is granted and comes free of
 *  charge under condition of compatibility of product made by
 *  Licensee. People intending to distribute/sell products based on the
 *  code, have to sign an agreement to guarantee that products using
 *  software based on IgH EtherCAT master stay compatible with the actual
 *  EtherCAT specification (which are released themselves as an open
 *  standard) as the (only) precondition to have the right to use EtherCAT
 *  Technology, IP and trade marks.
 *
 *****************************************************************************/

/**
   \file
   EtherCAT stave structure.
*/

/*****************************************************************************/

#ifndef _EC_SLAVE_H_
#define _EC_SLAVE_H_

#include <linux/list.h>
#include <linux/kobject.h>

#include "../include/ecrt.h"

#include "globals.h"
#include "datagram.h"

/*****************************************************************************/

/**
   State of an EtherCAT slave.
*/

typedef enum
{
    EC_SLAVE_STATE_UNKNOWN = 0x00,
    /**< unknown state */
    EC_SLAVE_STATE_INIT = 0x01,
    /**< INIT state (no mailbox communication, no IO) */
    EC_SLAVE_STATE_PREOP = 0x02,
    /**< PREOP state (mailbox communication, no IO) */
    EC_SLAVE_STATE_SAVEOP = 0x04,
    /**< SAVEOP (mailbox communication and input update) */
    EC_SLAVE_STATE_OP = 0x08,
    /**< OP (mailbox communication and input/output update) */
    EC_SLAVE_STATE_ACK_ERR = 0x10
    /**< Acknowledge/Error bit (no actual state) */
}
ec_slave_state_t;

/*****************************************************************************/

/**
   Supported mailbox protocols.
*/

enum
{
    EC_MBOX_AOE = 0x01, /**< ADS-over-EtherCAT */
    EC_MBOX_EOE = 0x02, /**< Ethernet-over-EtherCAT */
    EC_MBOX_COE = 0x04, /**< CANopen-over-EtherCAT */
    EC_MBOX_FOE = 0x08, /**< File-Access-over-EtherCAT */
    EC_MBOX_SOE = 0x10, /**< Servo-Profile-over-EtherCAT */
    EC_MBOX_VOE = 0x20  /**< Vendor specific */
};

/*****************************************************************************/

/**
   String object (EEPROM).
*/

typedef struct
{
    struct list_head list; /**< list item */
    size_t size; /**< size in bytes */
    char *data; /**< string data */
}
ec_sii_string_t;

/*****************************************************************************/

/**
   Sync manager configuration (EEPROM).
*/

typedef struct
{
    struct list_head list; /**< list item */
    unsigned int index; /**< sync manager index */
    uint16_t physical_start_address; /**< physical start address */
    uint16_t length; /**< data length in bytes */
    uint8_t control_register; /**< control register value */
    uint8_t enable; /**< enable bit */
    uint16_t est_length; /**< Estimated length. This is no field of the SII,
                            but it is used to calculate the length via
                            PDO ranges */
}
ec_sii_sync_t;

/*****************************************************************************/

/**
   PDO type.
*/

typedef enum
{
    EC_RX_PDO, /**< Reveive PDO */
    EC_TX_PDO /**< Transmit PDO */
}
ec_sii_pdo_type_t;

/*****************************************************************************/

/**
   PDO description (EEPROM).
*/

typedef struct
{
    struct list_head list; /**< list item */
    ec_sii_pdo_type_t type; /**< PDO type */
    uint16_t index; /**< PDO index */
    uint8_t sync_index; /**< assigned sync manager */
    char *name; /**< PDO name */
    struct list_head entries; /**< entry list */
}
ec_sii_pdo_t;

/*****************************************************************************/

/**
   PDO entry description (EEPROM).
*/

typedef struct
{
    struct list_head list; /**< list item */
    uint16_t index; /**< PDO index */
    uint8_t subindex; /**< entry subindex */
    char *name; /**< entry name */
    uint8_t bit_length; /**< entry length in bit */
}
ec_sii_pdo_entry_t;

/*****************************************************************************/

/**
   FMMU configuration.
*/

typedef struct
{
    unsigned int index; /**< FMMU index */
    const ec_domain_t *domain; /**< domain */
    const ec_sii_sync_t *sync; /**< sync manager */
    uint32_t logical_start_address; /**< logical start address */
}
ec_fmmu_t;

/*****************************************************************************/

/**
   EtherCAT slave.
*/

struct ec_slave
{
    struct list_head list; /**< list item */
    struct kobject kobj; /**< kobject */
    ec_master_t *master; /**< master owning the slave */

    ec_slave_state_t requested_state; /**< requested slave state */
    ec_slave_state_t current_state; /**< current slave state */
    unsigned int self_configured; /**< slave was configured by this master */
    unsigned int error_flag; /**< stop processing after an error */
    unsigned int online; /**< non-zero, if the slave responds. */

    // addresses
    uint16_t ring_position; /**< ring position */
    uint16_t station_address; /**< configured station address */
    uint16_t coupler_index; /**< index of the last bus coupler */
    uint16_t coupler_subindex; /**< index of this slave after last coupler */

    // base data
    uint8_t base_type; /**< slave type */
    uint8_t base_revision; /**< revision */
    uint16_t base_build; /**< build number */
    uint16_t base_fmmu_count; /**< number of supported FMMUs */
    uint16_t base_sync_count; /**< number of supported sync managers */

    // data link status
    uint8_t dl_link[4]; /**< link detected */
    uint8_t dl_loop[4]; /**< loop closed */
    uint8_t dl_signal[4]; /**< detected signal on RX port */

    // EEPROM
    uint8_t *eeprom_data; /**< Complete EEPROM image */
    uint16_t eeprom_size; /**< size of the EEPROM contents in byte */
    uint16_t *new_eeprom_data; /**< new EEPROM data to write */
    uint16_t new_eeprom_size; /**< size of new EEPROM data in words */

    // slave information interface
    uint16_t sii_alias; /**< configured station alias */
    uint32_t sii_vendor_id; /**< vendor id */
    uint32_t sii_product_code; /**< vendor's product code */
    uint32_t sii_revision_number; /**< revision number */
    uint32_t sii_serial_number; /**< serial number */
    uint16_t sii_rx_mailbox_offset; /**< mailbox address (master to slave) */
    uint16_t sii_rx_mailbox_size; /**< mailbox size (master to slave) */
    uint16_t sii_tx_mailbox_offset; /**< mailbox address (slave to master) */
    uint16_t sii_tx_mailbox_size; /**< mailbox size (slave to master) */
    uint16_t sii_mailbox_protocols; /**< supported mailbox protocols */
    uint8_t sii_physical_layer[4]; /**< port media */
    struct list_head sii_strings; /**< EEPROM STRING categories */
    struct list_head sii_syncs; /**< EEPROM SYNC MANAGER categories */
    struct list_head sii_pdos; /**< EEPROM [RT]XPDO categories */
    char *sii_group; /**< slave group acc. to EEPROM */
    char *sii_image; /**< slave image name acc. to EEPROM */
    char *sii_order; /**< slave order number acc. to EEPROM */
    char *sii_name; /**< slave name acc. to EEPROM */
    int16_t sii_current_on_ebus; /**< power consumption */

    ec_fmmu_t fmmus[EC_MAX_FMMUS]; /**< FMMU configurations */
    uint8_t fmmu_count; /**< number of FMMUs used */

    struct kobject sdo_kobj; /**< kobject for SDOs */
    struct list_head sdo_dictionary; /**< SDO dictionary list */
    struct list_head sdo_confs; /**< list of SDO configurations */
    uint8_t sdo_dictionary_fetched; /**< dictionary has been fetched */
    unsigned long jiffies_preop; /**< time, the slave went to PREOP */
};

/*****************************************************************************/

// slave construction/destruction
int ec_slave_init(ec_slave_t *, ec_master_t *, uint16_t, uint16_t);
void ec_slave_destroy(ec_slave_t *);

void ec_slave_reset(ec_slave_t *);

int ec_slave_prepare_fmmu(ec_slave_t *, const ec_domain_t *,
                          const ec_sii_sync_t *);

void ec_slave_request_state(ec_slave_t *, ec_slave_state_t);

// SII categories
int ec_slave_fetch_strings(ec_slave_t *, const uint8_t *);
void ec_slave_fetch_general(ec_slave_t *, const uint8_t *);
int ec_slave_fetch_sync(ec_slave_t *, const uint8_t *, size_t);
int ec_slave_fetch_pdo(ec_slave_t *, const uint8_t *, size_t,
                       ec_sii_pdo_type_t);
int ec_slave_locate_string(ec_slave_t *, unsigned int, char **);

// misc.
void ec_slave_sync_config(const ec_slave_t *, const ec_sii_sync_t *,
        uint8_t *);
void ec_slave_fmmu_config(const ec_slave_t *, const ec_fmmu_t *, uint8_t *);
uint16_t ec_slave_calc_sync_size(const ec_slave_t *, const ec_sii_sync_t *);

int ec_slave_is_coupler(const ec_slave_t *);
int ec_slave_has_subbus(const ec_slave_t *);

int ec_slave_validate(const ec_slave_t *, uint32_t, uint32_t);

void ec_slave_sdo_dict_info(const ec_slave_t *,
                            unsigned int *, unsigned int *);

/*****************************************************************************/

#endif