include/ecrt.h
branchstable-1.0
changeset 1618 5cff10efb927
parent 195 674071846ee3
child 1619 0d4119024f55
equal deleted inserted replaced
1617:9f83a343ae75 1618:5cff10efb927
     1 /******************************************************************************
     1 /******************************************************************************
     2  *
       
     3  *  EtherCAT realtime interface.
       
     4  *
     2  *
     5  *  $Id$
     3  *  $Id$
     6  *
     4  *
     7  *****************************************************************************/
     5  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it
       
    10  *  and/or modify it under the terms of the GNU General Public License
       
    11  *  as published by the Free Software Foundation; version 2 of the License.
       
    12  *
       
    13  *  The IgH EtherCAT Master is distributed in the hope that it will be
       
    14  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16  *  GNU General Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License
       
    19  *  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  *
       
    22  *****************************************************************************/
       
    23 
       
    24 /**
       
    25    \file
       
    26    EtherCAT realtime interface.
       
    27 */
       
    28 
       
    29 /**
       
    30    \defgroup RealtimeInterface EtherCAT realtime interface
       
    31    EtherCAT interface for realtime modules.
       
    32    This interface is designed for realtime modules that want to use EtherCAT.
       
    33    There are functions to request a master, to map process data, to communicate
       
    34    with slaves via CoE and to configure and activate the bus.
       
    35 */
       
    36 
       
    37 /*****************************************************************************/
     8 
    38 
     9 #ifndef __ECRT_H__
    39 #ifndef __ECRT_H__
    10 #define __ECRT_H__
    40 #define __ECRT_H__
    11 
    41 
    12 #include <asm/byteorder.h>
    42 #include <asm/byteorder.h>
    26 typedef struct ec_domain ec_domain_t;
    56 typedef struct ec_domain ec_domain_t;
    27 
    57 
    28 struct ec_slave;
    58 struct ec_slave;
    29 typedef struct ec_slave ec_slave_t;
    59 typedef struct ec_slave ec_slave_t;
    30 
    60 
       
    61 /**
       
    62    Initialization type for field registrations.
       
    63    This type is used as a parameter for the ec_domain_register_field_list()
       
    64    function.
       
    65 */
       
    66 
    31 typedef struct
    67 typedef struct
    32 {
    68 {
    33     void **data_ptr;
    69     void **data_ptr; /**< address of the process data pointer */
    34     const char *slave_address;
    70     const char *slave_address; /**< slave address string (see
    35     const char *vendor_name;
    71                                   ecrt_master_get_slave()) */
    36     const char *product_name;
    72     const char *vendor_name; /**< vendor name */
    37     const char *field_name;
    73     const char *product_name; /**< product name */
    38     unsigned int field_index;
    74     const char *field_name; /**< data field name */
    39     unsigned int field_count;
    75     unsigned int field_index; /**< index in data fields with same name */
       
    76     unsigned int field_count; /**< number of data fields with same name */
    40 }
    77 }
    41 ec_field_init_t;
    78 ec_field_init_t;
    42 
    79 
    43 /*****************************************************************************/
    80 /******************************************************************************
    44 // Master request functions
    81  *  Master request functions
       
    82  *****************************************************************************/
    45 
    83 
    46 ec_master_t *ecrt_request_master(unsigned int master_index);
    84 ec_master_t *ecrt_request_master(unsigned int master_index);
    47 void ecrt_release_master(ec_master_t *master);
    85 void ecrt_release_master(ec_master_t *master);
    48 
    86 
    49 /*****************************************************************************/
    87 /******************************************************************************
    50 // Master methods
    88  *  Master methods
       
    89  *****************************************************************************/
    51 
    90 
    52 ec_domain_t *ecrt_master_create_domain(ec_master_t *master);
    91 ec_domain_t *ecrt_master_create_domain(ec_master_t *master);
    53 
    92 
    54 int ecrt_master_activate(ec_master_t *master);
    93 int ecrt_master_activate(ec_master_t *master);
    55 void ecrt_master_deactivate(ec_master_t *master);
    94 void ecrt_master_deactivate(ec_master_t *master);
    66 void ecrt_master_debug(ec_master_t *master, int level);
   105 void ecrt_master_debug(ec_master_t *master, int level);
    67 void ecrt_master_print(const ec_master_t *master, unsigned int verbosity);
   106 void ecrt_master_print(const ec_master_t *master, unsigned int verbosity);
    68 
   107 
    69 ec_slave_t *ecrt_master_get_slave(const ec_master_t *, const char *);
   108 ec_slave_t *ecrt_master_get_slave(const ec_master_t *, const char *);
    70 
   109 
    71 /*****************************************************************************/
   110 /******************************************************************************
    72 // Domain Methods
   111  *  Domain Methods
       
   112  *****************************************************************************/
    73 
   113 
    74 ec_slave_t *ecrt_domain_register_field(ec_domain_t *domain,
   114 ec_slave_t *ecrt_domain_register_field(ec_domain_t *domain,
    75                                        const char *address,
   115                                        const char *address,
    76                                        const char *vendor_name,
   116                                        const char *vendor_name,
    77                                        const char *product_name,
   117                                        const char *product_name,
    84 void ecrt_domain_queue(ec_domain_t *domain);
   124 void ecrt_domain_queue(ec_domain_t *domain);
    85 void ecrt_domain_process(ec_domain_t *domain);
   125 void ecrt_domain_process(ec_domain_t *domain);
    86 
   126 
    87 int ecrt_domain_state(ec_domain_t *domain);
   127 int ecrt_domain_state(ec_domain_t *domain);
    88 
   128 
    89 /*****************************************************************************/
   129 /******************************************************************************
    90 // Slave Methods
   130  *  Slave Methods
       
   131  *****************************************************************************/
    91 
   132 
    92 int ecrt_slave_sdo_read_exp8(ec_slave_t *slave, uint16_t sdo_index,
   133 int ecrt_slave_sdo_read_exp8(ec_slave_t *slave, uint16_t sdo_index,
    93                               uint8_t sdo_subindex, uint8_t *value);
   134                               uint8_t sdo_subindex, uint8_t *value);
    94 int ecrt_slave_sdo_read_exp16(ec_slave_t *slave, uint16_t sdo_index,
   135 int ecrt_slave_sdo_read_exp16(ec_slave_t *slave, uint16_t sdo_index,
    95                               uint8_t sdo_subindex, uint16_t *value);
   136                               uint8_t sdo_subindex, uint16_t *value);
   104 int ecrt_slave_sdo_read(ec_slave_t *slave, uint16_t sdo_index,
   145 int ecrt_slave_sdo_read(ec_slave_t *slave, uint16_t sdo_index,
   105                         uint8_t sdo_subindex, uint8_t *data, size_t *size);
   146                         uint8_t sdo_subindex, uint8_t *data, size_t *size);
   106 
   147 
   107 int ecrt_slave_write_alias(ec_slave_t *slave, uint16_t alias);
   148 int ecrt_slave_write_alias(ec_slave_t *slave, uint16_t alias);
   108 
   149 
       
   150 /******************************************************************************
       
   151  *  Bitwise read/write macros
       
   152  *****************************************************************************/
       
   153 
       
   154 /**
       
   155    Read a certain bit of an EtherCAT data byte.
       
   156    \param DATA EtherCAT data pointer
       
   157    \param POS bit position
       
   158 */
       
   159 
       
   160 #define EC_READ_BIT(DATA, POS) ((*((uint8_t *) (DATA)) >> (POS)) & 0x01)
       
   161 
       
   162 /**
       
   163    Write a certain bit of an EtherCAT data byte.
       
   164    \param DATA EtherCAT data pointer
       
   165    \param POS bit position
       
   166    \param VAL new bit value
       
   167 */
       
   168 
       
   169 #define EC_WRITE_BIT(DATA, POS, VAL) \
       
   170     do { \
       
   171         if (VAL) *((uint8_t *) (DATA)) |=  (1 << (POS)); \
       
   172         else     *((uint8_t *) (DATA)) &= ~(1 << (POS)); \
       
   173     } while (0)
       
   174 
       
   175 /******************************************************************************
       
   176  *  Read macros
       
   177  *****************************************************************************/
       
   178 
       
   179 /**
       
   180    Read an 8-bit unsigned value from EtherCAT data.
       
   181    \return EtherCAT data value
       
   182 */
       
   183 
       
   184 #define EC_READ_U8(DATA) \
       
   185     ((uint8_t) *((uint8_t *) (DATA)))
       
   186 
       
   187 /**
       
   188    Read an 8-bit signed value from EtherCAT data.
       
   189    \param DATA EtherCAT data pointer
       
   190    \return EtherCAT data value
       
   191 */
       
   192 
       
   193 #define EC_READ_S8(DATA) \
       
   194      ((int8_t)  *((uint8_t *) (DATA)))
       
   195 
       
   196 /**
       
   197    Read a 16-bit unsigned value from EtherCAT data.
       
   198    \param DATA EtherCAT data pointer
       
   199    \return EtherCAT data value
       
   200 */
       
   201 
       
   202 #define EC_READ_U16(DATA) \
       
   203      ((uint16_t) le16_to_cpup((void *) (DATA)))
       
   204 
       
   205 /**
       
   206    Read a 16-bit signed value from EtherCAT data.
       
   207    \param DATA EtherCAT data pointer
       
   208    \return EtherCAT data value
       
   209 */
       
   210 
       
   211 #define EC_READ_S16(DATA) \
       
   212      ((int16_t)  le16_to_cpup((void *) (DATA)))
       
   213 
       
   214 /**
       
   215    Read a 32-bit unsigned value from EtherCAT data.
       
   216    \param DATA EtherCAT data pointer
       
   217    \return EtherCAT data value
       
   218 */
       
   219 
       
   220 #define EC_READ_U32(DATA) \
       
   221      ((uint32_t) le32_to_cpup((void *) (DATA)))
       
   222 
       
   223 /**
       
   224    Read a 32-bit signed value from EtherCAT data.
       
   225    \param DATA EtherCAT data pointer
       
   226    \return EtherCAT data value
       
   227 */
       
   228 
       
   229 #define EC_READ_S32(DATA) \
       
   230      ((int32_t)  le32_to_cpup((void *) (DATA)))
       
   231 
       
   232 
       
   233 /******************************************************************************
       
   234  *  Write macros
       
   235  *****************************************************************************/
       
   236 
       
   237 /**
       
   238    Write an 8-bit unsigned value to EtherCAT data.
       
   239    \param DATA EtherCAT data pointer
       
   240    \param VAL new value
       
   241 */
       
   242 
       
   243 #define EC_WRITE_U8(DATA, VAL) \
       
   244     do { \
       
   245         *((uint8_t *)(DATA)) = ((uint8_t) (VAL)); \
       
   246     } while (0)
       
   247 
       
   248 /**
       
   249    Write an 8-bit signed value to EtherCAT data.
       
   250    \param DATA EtherCAT data pointer
       
   251    \param VAL new value
       
   252 */
       
   253 
       
   254 #define EC_WRITE_S8(DATA, VAL) EC_WRITE_U8(DATA, VAL)
       
   255 
       
   256 /**
       
   257    Write a 16-bit unsigned value to EtherCAT data.
       
   258    \param DATA EtherCAT data pointer
       
   259    \param VAL new value
       
   260 */
       
   261 
       
   262 #define EC_WRITE_U16(DATA, VAL) \
       
   263     do { \
       
   264         *((uint16_t *) (DATA)) = (uint16_t) (VAL); \
       
   265         cpu_to_le16s(DATA); \
       
   266     } while (0)
       
   267 
       
   268 /**
       
   269    Write a 16-bit signed value to EtherCAT data.
       
   270    \param DATA EtherCAT data pointer
       
   271    \param VAL new value
       
   272 */
       
   273 
       
   274 #define EC_WRITE_S16(DATA, VAL) EC_WRITE_U16(DATA, VAL)
       
   275 
       
   276 /**
       
   277    Write a 32-bit unsigned value to EtherCAT data.
       
   278    \param DATA EtherCAT data pointer
       
   279    \param VAL new value
       
   280 */
       
   281 
       
   282 #define EC_WRITE_U32(DATA, VAL) \
       
   283     do { \
       
   284         *((uint32_t *) (DATA)) = (uint32_t) (VAL); \
       
   285         cpu_to_le16s(DATA); \
       
   286     } while (0)
       
   287 
       
   288 /**
       
   289    Write a 32-bit signed value to EtherCAT data.
       
   290    \param DATA EtherCAT data pointer
       
   291    \param VAL new value
       
   292 */
       
   293 
       
   294 #define EC_WRITE_S32(DATA, VAL) EC_WRITE_U32(DATA, VAL)
       
   295 
   109 /*****************************************************************************/
   296 /*****************************************************************************/
   110 // Bitwise read/write macros
       
   111 
       
   112 #define EC_READ_BIT(PD, CH) ((*((uint8_t *) (PD)) >> (CH)) & 0x01)
       
   113 
       
   114 #define EC_WRITE_BIT(PD, CH, VAL) \
       
   115     do { \
       
   116         if (VAL) *((uint8_t *) (PD)) |=  (1 << (CH)); \
       
   117         else     *((uint8_t *) (PD)) &= ~(1 << (CH)); \
       
   118     } while (0)
       
   119 
       
   120 /*****************************************************************************/
       
   121 // Read macros
       
   122 
       
   123 #define EC_READ_U8(PD) ((uint8_t) *((uint8_t *) (PD)))
       
   124 #define EC_READ_S8(PD) ((int8_t)  *((uint8_t *) (PD)))
       
   125 
       
   126 #define EC_READ_U16(PD) ((uint16_t) le16_to_cpup((void *) (PD)))
       
   127 #define EC_READ_S16(PD) ((int16_t)  le16_to_cpup((void *) (PD)))
       
   128 
       
   129 #define EC_READ_U32(PD) ((uint32_t) le32_to_cpup((void *) (PD)))
       
   130 #define EC_READ_S32(PD) ((int32_t)  le32_to_cpup((void *) (PD)))
       
   131 
       
   132 /*****************************************************************************/
       
   133 // Write macros
       
   134 
       
   135 #define EC_WRITE_U8(PD, VAL) \
       
   136     do { \
       
   137         *((uint8_t *)(PD)) = ((uint8_t) (VAL)); \
       
   138     } while (0)
       
   139 
       
   140 #define EC_WRITE_S8(PD, VAL) EC_WRITE_U8(PD, VAL)
       
   141 
       
   142 #define EC_WRITE_U16(PD, VAL) \
       
   143     do { \
       
   144         *((uint16_t *) (PD)) = (uint16_t) (VAL); \
       
   145         cpu_to_le16s(PD); \
       
   146     } while (0)
       
   147 
       
   148 #define EC_WRITE_S16(PD, VAL) EC_WRITE_U16(PD, VAL)
       
   149 
       
   150 #define EC_WRITE_U32(PD, VAL) \
       
   151     do { \
       
   152         *((uint32_t *) (PD)) = (uint32_t) (VAL); \
       
   153         cpu_to_le16s(PD); \
       
   154     } while (0)
       
   155 
       
   156 #define EC_WRITE_S32(PD, VAL) EC_WRITE_U32(PD, VAL)
       
   157 
       
   158 /*****************************************************************************/
       
   159 
   297 
   160 #endif
   298 #endif