fp@104: /****************************************************************************** fp@104: * fp@125: * $Id$ fp@104: * fp@197: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@197: * fp@197: * This file is part of the IgH EtherCAT Master. fp@197: * fp@197: * The IgH EtherCAT Master is free software; you can redistribute it fp@197: * and/or modify it under the terms of the GNU General Public License fp@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. fp@197: * fp@197: * The IgH EtherCAT Master is distributed in the hope that it will be fp@197: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@197: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@197: * GNU General Public License for more details. fp@197: * fp@197: * You should have received a copy of the GNU General Public License fp@197: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@197: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@197: * fp@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@104: *****************************************************************************/ fp@104: fp@199: /** fp@199: \file fp@199: EtherCAT realtime interface. fp@199: */ fp@199: fp@199: /** fp@199: \defgroup RealtimeInterface EtherCAT realtime interface fp@199: EtherCAT interface for realtime modules. fp@199: This interface is designed for realtime modules that want to use EtherCAT. fp@199: There are functions to request a master, to map process data, to communicate fp@199: with slaves via CoE and to configure and activate the bus. fp@199: */ fp@199: fp@199: /*****************************************************************************/ fp@199: fp@125: #ifndef __ECRT_H__ fp@125: #define __ECRT_H__ fp@104: fp@104: #include fp@104: fp@110: #ifdef __KERNEL__ fp@110: #include fp@110: #else fp@110: #include fp@110: #endif fp@110: fp@104: /*****************************************************************************/ fp@104: fp@541: #define ECRT_VER_MAJOR 1U fp@541: #define ECRT_VER_MINOR 2U fp@541: fp@541: #define ECRT_VERSION(a,b) (((a) << 8) + (b)) fp@541: #define ECRT_VERSION_MAGIC ECRT_VERSION(ECRT_VER_MAJOR, ECRT_VER_MINOR) fp@541: fp@541: /*****************************************************************************/ fp@541: fp@104: struct ec_master; fp@286: typedef struct ec_master ec_master_t; /**< \see ec_master */ fp@104: fp@104: struct ec_domain; fp@286: typedef struct ec_domain ec_domain_t; /**< \see ec_domain */ fp@104: fp@104: struct ec_slave; fp@286: typedef struct ec_slave ec_slave_t; /**< \see ec_slave */ fp@104: fp@200: /** fp@612: * Bus status. fp@612: */ fp@612: fp@612: typedef enum { fp@612: EC_BUS_FAILURE, // some slaves offline fp@612: EC_BUS_OK, // all slaves online fp@612: EC_BUS_REDUNDANCY // bus interrupted, but redundancy active fp@612: } fp@612: ec_bus_status_t; fp@612: fp@612: /** fp@612: * Master status. fp@612: * This is used for the output parameter of ecrt_master_get_status(). fp@612: */ fp@612: fp@612: typedef struct { fp@612: ec_bus_status_t bus_status; fp@612: unsigned int bus_tainted; fp@612: unsigned int slaves_responding; fp@612: } fp@612: ec_master_status_t; fp@612: fp@612: /** fp@325: Initialization type for PDO registrations. fp@612: This type is used as a parameter for the ecrt_domain_register_pdo_list() fp@200: function. fp@200: */ fp@200: fp@104: typedef struct fp@104: { fp@200: const char *slave_address; /**< slave address string (see fp@200: ecrt_master_get_slave()) */ fp@325: uint32_t vendor_id; /**< vendor ID */ fp@325: uint32_t product_code; /**< product code */ fp@325: uint16_t pdo_index; /**< PDO index */ fp@325: uint8_t pdo_subindex; /**< PDO subindex */ fp@325: void **data_ptr; /**< address of the process data pointer */ fp@104: } fp@325: ec_pdo_reg_t; fp@104: fp@416: /** fp@612: Direction type for ecrt_domain_register_pdo_range() fp@416: */ fp@416: fp@416: typedef enum {EC_DIR_INPUT, EC_DIR_OUTPUT} ec_direction_t; fp@416: fp@199: /****************************************************************************** fp@199: * Master request functions fp@199: *****************************************************************************/ fp@104: fp@104: ec_master_t *ecrt_request_master(unsigned int master_index); fp@104: void ecrt_release_master(ec_master_t *master); fp@104: fp@541: unsigned int ecrt_version_magic(void); fp@541: fp@199: /****************************************************************************** fp@199: * Master methods fp@199: *****************************************************************************/ fp@104: fp@204: void ecrt_master_callbacks(ec_master_t *master, int (*request_cb)(void *), fp@204: void (*release_cb)(void *), void *cb_data); fp@204: fp@104: ec_domain_t *ecrt_master_create_domain(ec_master_t *master); fp@138: fp@104: int ecrt_master_activate(ec_master_t *master); fp@138: fp@325: void ecrt_master_send(ec_master_t *master); fp@325: void ecrt_master_receive(ec_master_t *master); fp@138: fp@144: void ecrt_master_run(ec_master_t *master); fp@144: fp@138: ec_slave_t *ecrt_master_get_slave(const ec_master_t *, const char *); fp@104: fp@612: void ecrt_master_get_status(const ec_master_t *master, ec_master_status_t *); fp@325: fp@199: /****************************************************************************** fp@199: * Domain Methods fp@199: *****************************************************************************/ fp@104: fp@325: ec_slave_t *ecrt_domain_register_pdo(ec_domain_t *domain, fp@325: const char *address, fp@325: uint32_t vendor_id, fp@325: uint32_t product_code, fp@325: uint16_t pdo_index, fp@325: uint8_t pdo_subindex, fp@325: void **data_ptr); fp@416: fp@325: int ecrt_domain_register_pdo_list(ec_domain_t *domain, fp@325: const ec_pdo_reg_t *pdos); fp@325: fp@416: ec_slave_t *ecrt_domain_register_pdo_range(ec_domain_t *domain, fp@416: const char *address, fp@416: uint32_t vendor_id, fp@416: uint32_t product_code, fp@416: ec_direction_t direction, fp@416: uint16_t offset, fp@416: uint16_t length, fp@416: void **data_ptr); fp@416: fp@104: void ecrt_domain_process(ec_domain_t *domain); fp@494: void ecrt_domain_queue(ec_domain_t *domain); fp@325: int ecrt_domain_state(const ec_domain_t *domain); fp@104: fp@199: /****************************************************************************** fp@199: * Slave Methods fp@199: *****************************************************************************/ fp@104: fp@325: int ecrt_slave_conf_sdo8(ec_slave_t *slave, uint16_t sdo_index, fp@325: uint8_t sdo_subindex, uint8_t value); fp@325: int ecrt_slave_conf_sdo16(ec_slave_t *slave, uint16_t sdo_index, fp@325: uint8_t sdo_subindex, uint16_t value); fp@325: int ecrt_slave_conf_sdo32(ec_slave_t *slave, uint16_t sdo_index, fp@325: uint8_t sdo_subindex, uint32_t value); fp@325: fp@199: /****************************************************************************** fp@199: * Bitwise read/write macros fp@199: *****************************************************************************/ fp@199: fp@199: /** fp@199: Read a certain bit of an EtherCAT data byte. fp@199: \param DATA EtherCAT data pointer fp@199: \param POS bit position fp@199: */ fp@199: fp@199: #define EC_READ_BIT(DATA, POS) ((*((uint8_t *) (DATA)) >> (POS)) & 0x01) fp@199: fp@199: /** fp@199: Write a certain bit of an EtherCAT data byte. fp@199: \param DATA EtherCAT data pointer fp@199: \param POS bit position fp@199: \param VAL new bit value fp@199: */ fp@199: fp@199: #define EC_WRITE_BIT(DATA, POS, VAL) \ fp@199: do { \ fp@199: if (VAL) *((uint8_t *) (DATA)) |= (1 << (POS)); \ fp@199: else *((uint8_t *) (DATA)) &= ~(1 << (POS)); \ fp@199: } while (0) fp@199: fp@199: /****************************************************************************** fp@199: * Read macros fp@199: *****************************************************************************/ fp@199: fp@199: /** fp@199: Read an 8-bit unsigned value from EtherCAT data. fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_U8(DATA) \ fp@199: ((uint8_t) *((uint8_t *) (DATA))) fp@199: fp@199: /** fp@199: Read an 8-bit signed value from EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_S8(DATA) \ fp@288: ((int8_t) *((uint8_t *) (DATA))) fp@199: fp@199: /** fp@199: Read a 16-bit unsigned value from EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_U16(DATA) \ fp@199: ((uint16_t) le16_to_cpup((void *) (DATA))) fp@199: fp@199: /** fp@199: Read a 16-bit signed value from EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_S16(DATA) \ fp@288: ((int16_t) le16_to_cpup((void *) (DATA))) fp@199: fp@199: /** fp@199: Read a 32-bit unsigned value from EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_U32(DATA) \ fp@199: ((uint32_t) le32_to_cpup((void *) (DATA))) fp@199: fp@199: /** fp@199: Read a 32-bit signed value from EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \return EtherCAT data value fp@199: */ fp@199: fp@199: #define EC_READ_S32(DATA) \ fp@288: ((int32_t) le32_to_cpup((void *) (DATA))) fp@199: fp@199: fp@199: /****************************************************************************** fp@199: * Write macros fp@199: *****************************************************************************/ fp@199: fp@199: /** fp@199: Write an 8-bit unsigned value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_U8(DATA, VAL) \ fp@199: do { \ fp@199: *((uint8_t *)(DATA)) = ((uint8_t) (VAL)); \ fp@199: } while (0) fp@199: fp@199: /** fp@199: Write an 8-bit signed value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_S8(DATA, VAL) EC_WRITE_U8(DATA, VAL) fp@199: fp@199: /** fp@199: Write a 16-bit unsigned value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_U16(DATA, VAL) \ fp@199: do { \ fp@199: *((uint16_t *) (DATA)) = (uint16_t) (VAL); \ fp@199: cpu_to_le16s(DATA); \ fp@199: } while (0) fp@199: fp@199: /** fp@199: Write a 16-bit signed value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_S16(DATA, VAL) EC_WRITE_U16(DATA, VAL) fp@199: fp@199: /** fp@199: Write a 32-bit unsigned value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_U32(DATA, VAL) \ fp@199: do { \ fp@199: *((uint32_t *) (DATA)) = (uint32_t) (VAL); \ fp@199: cpu_to_le16s(DATA); \ fp@199: } while (0) fp@199: fp@199: /** fp@199: Write a 32-bit signed value to EtherCAT data. fp@199: \param DATA EtherCAT data pointer fp@199: \param VAL new value fp@199: */ fp@199: fp@199: #define EC_WRITE_S32(DATA, VAL) EC_WRITE_U32(DATA, VAL) fp@199: fp@104: /*****************************************************************************/ fp@104: fp@104: #endif