include/ecrt.h
changeset 779 b39a6a4d3671
parent 758 8fa6f825eb7d
child 780 53b9a0a92154
equal deleted inserted replaced
778:7fc85507f49a 779:b39a6a4d3671
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
    29  *  standard) as the (only) precondition to have the right to use EtherCAT
    30  *  Technology, IP and trade marks.
    30  *  Technology, IP and trade marks.
    31  *
    31  *
    32  *****************************************************************************/
    32  *****************************************************************************/
    33 
    33 
    34 /**
    34 /** \file EtherCAT realtime interface.
    35    \file
    35 
    36    EtherCAT realtime interface.
    36   \defgroup RealtimeInterface EtherCAT realtime interface
    37 */
    37 
    38 
    38   EtherCAT interface for realtime modules. This interface is designed for
    39 /**
    39   realtime modules that want to use EtherCAT. There are functions to request a
    40    \defgroup RealtimeInterface EtherCAT realtime interface
    40   master, to map process data, to communicate with slaves via CoE and to
    41    EtherCAT interface for realtime modules.
    41   configure and activate the bus.
    42    This interface is designed for realtime modules that want to use EtherCAT.
       
    43    There are functions to request a master, to map process data, to communicate
       
    44    with slaves via CoE and to configure and activate the bus.
       
    45 */
    42 */
    46 
    43 
    47 /*****************************************************************************/
    44 /*****************************************************************************/
    48 
    45 
    49 #ifndef __ECRT_H__
    46 #ifndef __ECRT_H__
    55 #include <linux/types.h>
    52 #include <linux/types.h>
    56 #else
    53 #else
    57 #include <stdint.h>
    54 #include <stdint.h>
    58 #endif
    55 #endif
    59 
    56 
    60 /*****************************************************************************/
    57 /******************************************************************************
    61 
    58  * Global definitions
    62 /**
    59  *****************************************************************************/
    63  * EtherCAT real-time interface major version number.
    60 
       
    61 /** EtherCAT real-time interface major version number.
    64  */
    62  */
    65 #define ECRT_VER_MAJOR 1
    63 #define ECRT_VER_MAJOR 1
    66 
    64 
    67 /**
    65 /** EtherCAT real-time interface minor version number.
    68  * EtherCAT real-time interface minor version number.
    66  */
    69  */
    67 #define ECRT_VER_MINOR 4
    70 #define ECRT_VER_MINOR 3
    68 
    71 
    69 /** EtherCAT real-time interface version word generator.
    72 /**
    70  */
    73  * EtherCAT real-time interface version word generator.
    71 #define ECRT_VERSION(a, b) (((a) << 8) + (b))
    74  */
    72 
    75 #define ECRT_VERSION(a,b) (((a) << 8) + (b))
    73 /** EtherCAT real-time interface version word.
    76 
       
    77 /**
       
    78  * EtherCAT real-time interface version word.
       
    79  */
    74  */
    80 #define ECRT_VERSION_MAGIC ECRT_VERSION(ECRT_VER_MAJOR, ECRT_VER_MINOR)
    75 #define ECRT_VERSION_MAGIC ECRT_VERSION(ECRT_VER_MAJOR, ECRT_VER_MINOR)
    81 
    76 
    82 /*****************************************************************************/
    77 /******************************************************************************
       
    78  * Data types 
       
    79  *****************************************************************************/
    83 
    80 
    84 struct ec_master;
    81 struct ec_master;
    85 typedef struct ec_master ec_master_t; /**< \see ec_master */
    82 typedef struct ec_master ec_master_t; /**< \see ec_master */
    86 
    83 
    87 struct ec_domain;
    84 struct ec_domain;
    88 typedef struct ec_domain ec_domain_t; /**< \see ec_domain */
    85 typedef struct ec_domain ec_domain_t; /**< \see ec_domain */
    89 
    86 
    90 struct ec_slave;
    87 struct ec_slave;
    91 typedef struct ec_slave ec_slave_t; /**< \see ec_slave */
    88 typedef struct ec_slave ec_slave_t; /**< \see ec_slave */
    92 
    89 
    93 /**
    90 /*****************************************************************************/
    94  * Bus status.
    91 
    95  */
    92 /** Bus status.
    96 
    93  */
    97 typedef enum {
    94 typedef enum {
    98     EC_BUS_FAILURE = -1, /**< At least one slave with process data exchange
    95     EC_BUS_FAILURE = -1, /**< At least one slave with process data exchange
    99                            is offline. */
    96                            is offline. */
   100     EC_BUS_OK            /**< All slaves with process data exchange are
    97     EC_BUS_OK            /**< All slaves with process data exchange are
   101                            online. */
    98                            online. */
   102 }
    99 }
   103 ec_bus_status_t;
   100 ec_bus_status_t;
   104 
   101 
   105 /**
   102 /*****************************************************************************/
   106  * Master status.
   103 
       
   104 /** Master status.
   107  * This is used for the output parameter of ecrt_master_get_status().
   105  * This is used for the output parameter of ecrt_master_get_status().
   108  */
   106  */
   109 
       
   110 typedef struct {
   107 typedef struct {
   111     ec_bus_status_t bus_status; /**< \see ec_bus_status_t */
   108     ec_bus_status_t bus_status; /**< \see ec_bus_status_t */
   112     unsigned int bus_tainted; /**< non-zero, if the bus topology is invalid */
   109     unsigned int bus_tainted; /**< non-zero, if the bus topology is invalid */
   113     unsigned int slaves_responding; /**< number of responging slaves */
   110     unsigned int slaves_responding; /**< number of responging slaves */
   114 }
   111 }
   115 ec_master_status_t;
   112 ec_master_status_t;
   116 
   113 
   117 /**
   114 /*****************************************************************************/
   118  * List entry for domain PDO registrations.
   115 
       
   116 /** List entry for domain PDO registrations.
   119  * This type is used as a parameter for the ecrt_domain_register_pdo_list()
   117  * This type is used as a parameter for the ecrt_domain_register_pdo_list()
   120  * convenience function.
   118  * convenience function.
   121  */
   119  */
   122 
       
   123 typedef struct {
   120 typedef struct {
   124     const char *slave_address; /**< slave address string
   121     const char *slave_address; /**< slave address string
   125                                  \see ec_master_parse_slave_address() */
   122                                  \see ec_master_parse_slave_address() */
   126     uint32_t vendor_id; /**< vendor ID */
   123     uint32_t vendor_id; /**< vendor ID */
   127     uint32_t product_code; /**< product code */
   124     uint32_t product_code; /**< product code */
   129     uint8_t pdo_entry_subindex; /**< PDO entry subindex */
   126     uint8_t pdo_entry_subindex; /**< PDO entry subindex */
   130     void **data_ptr; /**< address of the process data pointer */
   127     void **data_ptr; /**< address of the process data pointer */
   131 }
   128 }
   132 ec_pdo_reg_t;
   129 ec_pdo_reg_t;
   133 
   130 
   134 /**
   131 /*****************************************************************************/
   135  * Direction type for PDO mapping and range registration functions.
   132 
   136  */
   133 /** Direction type for PDO mapping and range registration functions.
   137 
   134  */
   138 typedef enum {
   135 typedef enum {
   139     EC_DIR_OUTPUT, /**< values written by master */
   136     EC_DIR_OUTPUT, /**< values written by master */
   140     EC_DIR_INPUT   /**< values read by master */
   137     EC_DIR_INPUT   /**< values read by master */
   141 }
   138 }
   142 ec_direction_t;
   139 ec_direction_t;
   143 
   140 
   144 /******************************************************************************
   141 /******************************************************************************
   145  *  Global functions
   142  * Global functions
   146  *****************************************************************************/
   143  *****************************************************************************/
   147 
   144 
   148 ec_master_t *ecrt_request_master(unsigned int master_index);
   145 ec_master_t *ecrt_request_master(unsigned int master_index);
   149 void ecrt_release_master(ec_master_t *master);
   146 void ecrt_release_master(ec_master_t *master);
   150 
   147 
   151 unsigned int ecrt_version_magic(void);
   148 unsigned int ecrt_version_magic(void);
   152 
   149 
   153 /******************************************************************************
   150 /******************************************************************************
   154  *  Master methods
   151  * Master methods
   155  *****************************************************************************/
   152  *****************************************************************************/
   156 
   153 
   157 void ecrt_master_callbacks(ec_master_t *master, int (*request_cb)(void *),
   154 void ecrt_master_callbacks(ec_master_t *master, int (*request_cb)(void *),
   158                            void (*release_cb)(void *), void *cb_data);
   155                            void (*release_cb)(void *), void *cb_data);
   159 
   156 
   170 void ecrt_master_receive(ec_master_t *master);
   167 void ecrt_master_receive(ec_master_t *master);
   171 
   168 
   172 void ecrt_master_get_status(const ec_master_t *master, ec_master_status_t *);
   169 void ecrt_master_get_status(const ec_master_t *master, ec_master_status_t *);
   173 
   170 
   174 /******************************************************************************
   171 /******************************************************************************
   175  *  Domain Methods
   172  * Domain methods
   176  *****************************************************************************/
   173  *****************************************************************************/
   177 
   174 
   178 int ecrt_domain_register_pdo(ec_domain_t *domain, ec_slave_t *slave,
   175 int ecrt_domain_register_pdo(ec_domain_t *domain, ec_slave_t *slave,
   179         uint16_t pdo_index, uint8_t pdo_subindex, void **data_ptr);
   176         uint16_t pdo_index, uint8_t pdo_subindex, void **data_ptr);
   180 
   177 
   189 void ecrt_domain_queue(ec_domain_t *domain);
   186 void ecrt_domain_queue(ec_domain_t *domain);
   190 
   187 
   191 int ecrt_domain_state(const ec_domain_t *domain);
   188 int ecrt_domain_state(const ec_domain_t *domain);
   192 
   189 
   193 /******************************************************************************
   190 /******************************************************************************
   194  *  Slave Methods
   191  * Slave methods
   195  *****************************************************************************/
   192  *****************************************************************************/
   196 
   193 
   197 int ecrt_slave_conf_sdo8(ec_slave_t *slave, uint16_t sdo_index,
   194 int ecrt_slave_conf_sdo8(ec_slave_t *slave, uint16_t sdo_index,
   198                          uint8_t sdo_subindex, uint8_t value);
   195                          uint8_t sdo_subindex, uint8_t value);
   199 int ecrt_slave_conf_sdo16(ec_slave_t *slave, uint16_t sdo_index,
   196 int ecrt_slave_conf_sdo16(ec_slave_t *slave, uint16_t sdo_index,
   204 void ecrt_slave_pdo_mapping_clear(ec_slave_t *, ec_direction_t);
   201 void ecrt_slave_pdo_mapping_clear(ec_slave_t *, ec_direction_t);
   205 int ecrt_slave_pdo_mapping_add(ec_slave_t *, ec_direction_t, uint16_t);
   202 int ecrt_slave_pdo_mapping_add(ec_slave_t *, ec_direction_t, uint16_t);
   206 int ecrt_slave_pdo_mapping(ec_slave_t *, ec_direction_t, unsigned int, ...);
   203 int ecrt_slave_pdo_mapping(ec_slave_t *, ec_direction_t, unsigned int, ...);
   207 
   204 
   208 /******************************************************************************
   205 /******************************************************************************
   209  *  Bitwise read/write macros
   206  * Bitwise read/write macros
   210  *****************************************************************************/
   207  *****************************************************************************/
   211 
   208 
   212 /**
   209 /** Read a certain bit of an EtherCAT data byte.
   213  * Read a certain bit of an EtherCAT data byte.
       
   214  * \param DATA EtherCAT data pointer
   210  * \param DATA EtherCAT data pointer
   215  * \param POS bit position
   211  * \param POS bit position
   216  */
   212  */
   217 
       
   218 #define EC_READ_BIT(DATA, POS) ((*((uint8_t *) (DATA)) >> (POS)) & 0x01)
   213 #define EC_READ_BIT(DATA, POS) ((*((uint8_t *) (DATA)) >> (POS)) & 0x01)
   219 
   214 
   220 /**
   215 /** Write a certain bit of an EtherCAT data byte.
   221  * Write a certain bit of an EtherCAT data byte.
       
   222  * \param DATA EtherCAT data pointer
   216  * \param DATA EtherCAT data pointer
   223  * \param POS bit position
   217  * \param POS bit position
   224  * \param VAL new bit value
   218  * \param VAL new bit value
   225  */
   219  */
   226 
       
   227 #define EC_WRITE_BIT(DATA, POS, VAL) \
   220 #define EC_WRITE_BIT(DATA, POS, VAL) \
   228     do { \
   221     do { \
   229         if (VAL) *((uint8_t *) (DATA)) |=  (1 << (POS)); \
   222         if (VAL) *((uint8_t *) (DATA)) |=  (1 << (POS)); \
   230         else     *((uint8_t *) (DATA)) &= ~(1 << (POS)); \
   223         else     *((uint8_t *) (DATA)) &= ~(1 << (POS)); \
   231     } while (0)
   224     } while (0)
   232 
   225 
   233 /******************************************************************************
   226 /******************************************************************************
   234  *  Read macros
   227  * Read macros
   235  *****************************************************************************/
   228  *****************************************************************************/
   236 
   229 
   237 /**
   230 /** Read an 8-bit unsigned value from EtherCAT data.
   238  * Read an 8-bit unsigned value from EtherCAT data.
   231  * \return EtherCAT data value
   239  * \return EtherCAT data value
   232  */
   240  */
       
   241 
       
   242 #define EC_READ_U8(DATA) \
   233 #define EC_READ_U8(DATA) \
   243     ((uint8_t) *((uint8_t *) (DATA)))
   234     ((uint8_t) *((uint8_t *) (DATA)))
   244 
   235 
   245 /**
   236 /** Read an 8-bit signed value from EtherCAT data.
   246  * Read an 8-bit signed value from EtherCAT data.
   237  * \param DATA EtherCAT data pointer
   247  * \param DATA EtherCAT data pointer
   238  * \return EtherCAT data value
   248  * \return EtherCAT data value
   239  */
   249  */
       
   250 
       
   251 #define EC_READ_S8(DATA) \
   240 #define EC_READ_S8(DATA) \
   252      ((int8_t) *((uint8_t *) (DATA)))
   241      ((int8_t) *((uint8_t *) (DATA)))
   253 
   242 
   254 /**
   243 /** Read a 16-bit unsigned value from EtherCAT data.
   255  * Read a 16-bit unsigned value from EtherCAT data.
   244  * \param DATA EtherCAT data pointer
   256  * \param DATA EtherCAT data pointer
   245  * \return EtherCAT data value
   257  * \return EtherCAT data value
   246  */
   258  */
       
   259 
       
   260 #define EC_READ_U16(DATA) \
   247 #define EC_READ_U16(DATA) \
   261      ((uint16_t) le16_to_cpup((void *) (DATA)))
   248      ((uint16_t) le16_to_cpup((void *) (DATA)))
   262 
   249 
   263 /**
   250 /** Read a 16-bit signed value from EtherCAT data.
   264  * Read a 16-bit signed value from EtherCAT data.
   251  * \param DATA EtherCAT data pointer
   265  * \param DATA EtherCAT data pointer
   252  * \return EtherCAT data value
   266  * \return EtherCAT data value
   253  */
   267  */
       
   268 
       
   269 #define EC_READ_S16(DATA) \
   254 #define EC_READ_S16(DATA) \
   270      ((int16_t) le16_to_cpup((void *) (DATA)))
   255      ((int16_t) le16_to_cpup((void *) (DATA)))
   271 
   256 
   272 /**
   257 /** Read a 32-bit unsigned value from EtherCAT data.
   273  * Read a 32-bit unsigned value from EtherCAT data.
   258  * \param DATA EtherCAT data pointer
   274  * \param DATA EtherCAT data pointer
   259  * \return EtherCAT data value
   275  * \return EtherCAT data value
   260  */
   276  */
       
   277 
       
   278 #define EC_READ_U32(DATA) \
   261 #define EC_READ_U32(DATA) \
   279      ((uint32_t) le32_to_cpup((void *) (DATA)))
   262      ((uint32_t) le32_to_cpup((void *) (DATA)))
   280 
   263 
   281 /**
   264 /** Read a 32-bit signed value from EtherCAT data.
   282  * Read a 32-bit signed value from EtherCAT data.
   265  * \param DATA EtherCAT data pointer
   283  * \param DATA EtherCAT data pointer
   266  * \return EtherCAT data value
   284  * \return EtherCAT data value
   267  */
   285  */
       
   286 
       
   287 #define EC_READ_S32(DATA) \
   268 #define EC_READ_S32(DATA) \
   288      ((int32_t) le32_to_cpup((void *) (DATA)))
   269      ((int32_t) le32_to_cpup((void *) (DATA)))
   289 
   270 
   290 /******************************************************************************
   271 /******************************************************************************
   291  *  Write macros
   272  * Write macros
   292  *****************************************************************************/
   273  *****************************************************************************/
   293 
   274 
   294 /**
   275 /** Write an 8-bit unsigned value to EtherCAT data.
   295  * Write an 8-bit unsigned value to EtherCAT data.
   276  * \param DATA EtherCAT data pointer
   296  * \param DATA EtherCAT data pointer
   277  * \param VAL new value
   297  * \param VAL new value
   278  */
   298  */
       
   299 
       
   300 #define EC_WRITE_U8(DATA, VAL) \
   279 #define EC_WRITE_U8(DATA, VAL) \
   301     do { \
   280     do { \
   302         *((uint8_t *)(DATA)) = ((uint8_t) (VAL)); \
   281         *((uint8_t *)(DATA)) = ((uint8_t) (VAL)); \
   303     } while (0)
   282     } while (0)
   304 
   283 
   305 /**
   284 /** Write an 8-bit signed value to EtherCAT data.
   306  * Write an 8-bit signed value to EtherCAT data.
   285  * \param DATA EtherCAT data pointer
   307  * \param DATA EtherCAT data pointer
   286  * \param VAL new value
   308  * \param VAL new value
   287  */
   309  */
       
   310 
       
   311 #define EC_WRITE_S8(DATA, VAL) EC_WRITE_U8(DATA, VAL)
   288 #define EC_WRITE_S8(DATA, VAL) EC_WRITE_U8(DATA, VAL)
   312 
   289 
   313 /**
   290 /** Write a 16-bit unsigned value to EtherCAT data.
   314  * Write a 16-bit unsigned value to EtherCAT data.
   291  * \param DATA EtherCAT data pointer
   315  * \param DATA EtherCAT data pointer
   292  * \param VAL new value
   316  * \param VAL new value
   293  */
   317  */
       
   318 
       
   319 #define EC_WRITE_U16(DATA, VAL) \
   294 #define EC_WRITE_U16(DATA, VAL) \
   320     do { \
   295     do { \
   321         *((uint16_t *) (DATA)) = (uint16_t) (VAL); \
   296         *((uint16_t *) (DATA)) = (uint16_t) (VAL); \
   322         cpu_to_le16s(DATA); \
   297         cpu_to_le16s(DATA); \
   323     } while (0)
   298     } while (0)
   324 
   299 
   325 /**
   300 /** Write a 16-bit signed value to EtherCAT data.
   326  * Write a 16-bit signed value to EtherCAT data.
   301  * \param DATA EtherCAT data pointer
   327  * \param DATA EtherCAT data pointer
   302  * \param VAL new value
   328  * \param VAL new value
   303  */
   329  */
       
   330 
       
   331 #define EC_WRITE_S16(DATA, VAL) EC_WRITE_U16(DATA, VAL)
   304 #define EC_WRITE_S16(DATA, VAL) EC_WRITE_U16(DATA, VAL)
   332 
   305 
   333 /**
   306 /** Write a 32-bit unsigned value to EtherCAT data.
   334  * Write a 32-bit unsigned value to EtherCAT data.
   307  * \param DATA EtherCAT data pointer
   335  * \param DATA EtherCAT data pointer
   308  * \param VAL new value
   336  * \param VAL new value
   309  */
   337  */
       
   338 
       
   339 #define EC_WRITE_U32(DATA, VAL) \
   310 #define EC_WRITE_U32(DATA, VAL) \
   340     do { \
   311     do { \
   341         *((uint32_t *) (DATA)) = (uint32_t) (VAL); \
   312         *((uint32_t *) (DATA)) = (uint32_t) (VAL); \
   342         cpu_to_le32s(DATA); \
   313         cpu_to_le32s(DATA); \
   343     } while (0)
   314     } while (0)
   344 
   315 
   345 /**
   316 /** Write a 32-bit signed value to EtherCAT data.
   346  * Write a 32-bit signed value to EtherCAT data.
   317  * \param DATA EtherCAT data pointer
   347  * \param DATA EtherCAT data pointer
   318  * \param VAL new value
   348  * \param VAL new value
   319  */
   349  */
       
   350 
       
   351 #define EC_WRITE_S32(DATA, VAL) EC_WRITE_U32(DATA, VAL)
   320 #define EC_WRITE_S32(DATA, VAL) EC_WRITE_U32(DATA, VAL)
   352 
   321 
   353 /*****************************************************************************/
   322 /*****************************************************************************/
   354 
   323 
   355 #endif
   324 #endif