master/slave.c
changeset 199 04ecf40fc2e9
parent 197 b9a6e2c22745
child 238 b4960499098f
equal deleted inserted replaced
198:f7dff1ed01ba 199:04ecf40fc2e9
     1 /******************************************************************************
     1 /******************************************************************************
     2  *
       
     3  *  s l a v e . c
       
     4  *
       
     5  *  EtherCAT slave methods.
       
     6  *
     2  *
     7  *  $Id$
     3  *  $Id$
     8  *
     4  *
     9  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
     5  *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
    10  *
     6  *
    23  *  along with the IgH EtherCAT Master; if not, write to the Free Software
    19  *  along with the IgH EtherCAT Master; if not, write to the Free Software
    24  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    25  *
    21  *
    26  *****************************************************************************/
    22  *****************************************************************************/
    27 
    23 
       
    24 /**
       
    25    \file
       
    26    EtherCAT slave methods.
       
    27 */
       
    28 
       
    29 /*****************************************************************************/
       
    30 
    28 #include <linux/module.h>
    31 #include <linux/module.h>
    29 #include <linux/delay.h>
    32 #include <linux/delay.h>
    30 
    33 
    31 #include "globals.h"
    34 #include "globals.h"
    32 #include "slave.h"
    35 #include "slave.h"
    42 int ec_slave_fetch_pdo(ec_slave_t *, const uint8_t *, size_t, ec_pdo_type_t);
    45 int ec_slave_fetch_pdo(ec_slave_t *, const uint8_t *, size_t, ec_pdo_type_t);
    43 int ec_slave_locate_string(ec_slave_t *, unsigned int, char **);
    46 int ec_slave_locate_string(ec_slave_t *, unsigned int, char **);
    44 ssize_t ec_show_slave_attribute(struct kobject *, struct attribute *, char *);
    47 ssize_t ec_show_slave_attribute(struct kobject *, struct attribute *, char *);
    45 
    48 
    46 /*****************************************************************************/
    49 /*****************************************************************************/
       
    50 
       
    51 /** \cond */
    47 
    52 
    48 EC_SYSFS_READ_ATTR(ring_position);
    53 EC_SYSFS_READ_ATTR(ring_position);
    49 EC_SYSFS_READ_ATTR(coupler_address);
    54 EC_SYSFS_READ_ATTR(coupler_address);
    50 EC_SYSFS_READ_ATTR(vendor_name);
    55 EC_SYSFS_READ_ATTR(vendor_name);
    51 EC_SYSFS_READ_ATTR(product_name);
    56 EC_SYSFS_READ_ATTR(product_name);
    71     .release = ec_slave_clear,
    76     .release = ec_slave_clear,
    72     .sysfs_ops = &sysfs_ops,
    77     .sysfs_ops = &sysfs_ops,
    73     .default_attrs = def_attrs
    78     .default_attrs = def_attrs
    74 };
    79 };
    75 
    80 
       
    81 /** \endcond */
       
    82 
    76 /*****************************************************************************/
    83 /*****************************************************************************/
    77 
    84 
    78 const ec_code_msg_t al_status_messages[];
    85 const ec_code_msg_t al_status_messages[];
    79 
    86 
    80 /*****************************************************************************/
    87 /*****************************************************************************/
    81 
    88 
    82 /**
    89 /**
    83    Slave constructor.
    90    Slave constructor.
    84    \return 0 in case of success, else < 0
    91    \return 0 in case of success, else < 0
    85    \ingroup Slave
       
    86 */
    92 */
    87 
    93 
    88 int ec_slave_init(ec_slave_t *slave, /**< EtherCAT slave */
    94 int ec_slave_init(ec_slave_t *slave, /**< EtherCAT slave */
    89                   ec_master_t *master, /**< EtherCAT master */
    95                   ec_master_t *master, /**< EtherCAT master */
    90                   uint16_t ring_position, /**< ring position */
    96                   uint16_t ring_position, /**< ring position */
   151 
   157 
   152 /*****************************************************************************/
   158 /*****************************************************************************/
   153 
   159 
   154 /**
   160 /**
   155    Slave destructor.
   161    Slave destructor.
   156    \ingroup Slave
       
   157 */
   162 */
   158 
   163 
   159 void ec_slave_clear(struct kobject *kobj /**< kobject of the slave */)
   164 void ec_slave_clear(struct kobject *kobj /**< kobject of the slave */)
   160 {
   165 {
   161     ec_slave_t *slave;
   166     ec_slave_t *slave;
   218 /*****************************************************************************/
   223 /*****************************************************************************/
   219 
   224 
   220 /**
   225 /**
   221    Reads all necessary information from a slave.
   226    Reads all necessary information from a slave.
   222    \return 0 in case of success, else < 0
   227    \return 0 in case of success, else < 0
   223    \ingroup Slave
       
   224 */
   228 */
   225 
   229 
   226 int ec_slave_fetch(ec_slave_t *slave /**< EtherCAT slave */)
   230 int ec_slave_fetch(ec_slave_t *slave /**< EtherCAT slave */)
   227 {
   231 {
   228     ec_command_t *command;
   232     ec_command_t *command;
   296 /*****************************************************************************/
   300 /*****************************************************************************/
   297 
   301 
   298 /**
   302 /**
   299    Reads 16 bit from the slave information interface (SII).
   303    Reads 16 bit from the slave information interface (SII).
   300    \return 0 in case of success, else < 0
   304    \return 0 in case of success, else < 0
   301    \ingroup Slave
       
   302 */
   305 */
   303 
   306 
   304 int ec_slave_sii_read16(ec_slave_t *slave,
   307 int ec_slave_sii_read16(ec_slave_t *slave,
   305                         /**< EtherCAT slave */
   308                         /**< EtherCAT slave */
   306                         uint16_t offset,
   309                         uint16_t offset,
   357 /*****************************************************************************/
   360 /*****************************************************************************/
   358 
   361 
   359 /**
   362 /**
   360    Reads 32 bit from the slave information interface (SII).
   363    Reads 32 bit from the slave information interface (SII).
   361    \return 0 in case of success, else < 0
   364    \return 0 in case of success, else < 0
   362    \ingroup Slave
       
   363 */
   365 */
   364 
   366 
   365 int ec_slave_sii_read32(ec_slave_t *slave,
   367 int ec_slave_sii_read32(ec_slave_t *slave,
   366                         /**< EtherCAT slave */
   368                         /**< EtherCAT slave */
   367                         uint16_t offset,
   369                         uint16_t offset,
   418 /*****************************************************************************/
   420 /*****************************************************************************/
   419 
   421 
   420 /**
   422 /**
   421    Writes 16 bit of data to the slave information interface (SII).
   423    Writes 16 bit of data to the slave information interface (SII).
   422    \return 0 in case of success, else < 0
   424    \return 0 in case of success, else < 0
   423    \ingroup Slave
       
   424 */
   425 */
   425 
   426 
   426 int ec_slave_sii_write16(ec_slave_t *slave,
   427 int ec_slave_sii_write16(ec_slave_t *slave,
   427                          /**< EtherCAT slave */
   428                          /**< EtherCAT slave */
   428                          uint16_t offset,
   429                          uint16_t offset,
   489 /*****************************************************************************/
   490 /*****************************************************************************/
   490 
   491 
   491 /**
   492 /**
   492    Fetches data from slave's EEPROM.
   493    Fetches data from slave's EEPROM.
   493    \return 0 in case of success, else < 0
   494    \return 0 in case of success, else < 0
   494    \ingroup Slave
       
   495 */
   495 */
   496 
   496 
   497 int ec_slave_fetch_categories(ec_slave_t *slave /**< EtherCAT slave */)
   497 int ec_slave_fetch_categories(ec_slave_t *slave /**< EtherCAT slave */)
   498 {
   498 {
   499     uint16_t word_offset, cat_type, word_count;
   499     uint16_t word_offset, cat_type, word_count;
   582 /*****************************************************************************/
   582 /*****************************************************************************/
   583 
   583 
   584 /**
   584 /**
   585    Fetches data from a STRING category.
   585    Fetches data from a STRING category.
   586    \return 0 in case of success, else < 0
   586    \return 0 in case of success, else < 0
   587    \ingroup Slave
       
   588 */
   587 */
   589 
   588 
   590 int ec_slave_fetch_strings(ec_slave_t *slave, /**< EtherCAT slave */
   589 int ec_slave_fetch_strings(ec_slave_t *slave, /**< EtherCAT slave */
   591                            const uint8_t *data /**< category data */
   590                            const uint8_t *data /**< category data */
   592                            )
   591                            )
   621 /*****************************************************************************/
   620 /*****************************************************************************/
   622 
   621 
   623 /**
   622 /**
   624    Fetches data from a GENERAL category.
   623    Fetches data from a GENERAL category.
   625    \return 0 in case of success, else < 0
   624    \return 0 in case of success, else < 0
   626    \ingroup Slave
       
   627 */
   625 */
   628 
   626 
   629 int ec_slave_fetch_general(ec_slave_t *slave, /**< EtherCAT slave */
   627 int ec_slave_fetch_general(ec_slave_t *slave, /**< EtherCAT slave */
   630                            const uint8_t *data /**< category data */
   628                            const uint8_t *data /**< category data */
   631                            )
   629                            )
   649 /*****************************************************************************/
   647 /*****************************************************************************/
   650 
   648 
   651 /**
   649 /**
   652    Fetches data from a SYNC MANAGER category.
   650    Fetches data from a SYNC MANAGER category.
   653    \return 0 in case of success, else < 0
   651    \return 0 in case of success, else < 0
   654    \ingroup Slave
       
   655 */
   652 */
   656 
   653 
   657 int ec_slave_fetch_sync(ec_slave_t *slave, /**< EtherCAT slave */
   654 int ec_slave_fetch_sync(ec_slave_t *slave, /**< EtherCAT slave */
   658                         const uint8_t *data, /**< category data */
   655                         const uint8_t *data, /**< category data */
   659                         size_t word_count /**< number of words */
   656                         size_t word_count /**< number of words */
   686 /*****************************************************************************/
   683 /*****************************************************************************/
   687 
   684 
   688 /**
   685 /**
   689    Fetches data from a [RT]XPDO category.
   686    Fetches data from a [RT]XPDO category.
   690    \return 0 in case of success, else < 0
   687    \return 0 in case of success, else < 0
   691    \ingroup Slave
       
   692 */
   688 */
   693 
   689 
   694 int ec_slave_fetch_pdo(ec_slave_t *slave, /**< EtherCAT slave */
   690 int ec_slave_fetch_pdo(ec_slave_t *slave, /**< EtherCAT slave */
   695                        const uint8_t *data, /**< category data */
   691                        const uint8_t *data, /**< category data */
   696                        size_t word_count, /**< number of words */
   692                        size_t word_count, /**< number of words */
   748 /*****************************************************************************/
   744 /*****************************************************************************/
   749 
   745 
   750 /**
   746 /**
   751    Searches the string list for an index and allocates a new string.
   747    Searches the string list for an index and allocates a new string.
   752    \return 0 in case of success, else < 0
   748    \return 0 in case of success, else < 0
   753    \ingroup Slave
       
   754    \todo documentation
   749    \todo documentation
   755 */
   750 */
   756 
   751 
   757 int ec_slave_locate_string(ec_slave_t *slave, /**< EtherCAT slave */
   752 int ec_slave_locate_string(ec_slave_t *slave, /**< EtherCAT slave */
   758                            unsigned int index, /**< string index */
   753                            unsigned int index, /**< string index */
   798 
   793 
   799 /*****************************************************************************/
   794 /*****************************************************************************/
   800 
   795 
   801 /**
   796 /**
   802    Acknowledges an error after a state transition.
   797    Acknowledges an error after a state transition.
   803    \ingroup Slave
       
   804 */
   798 */
   805 
   799 
   806 void ec_slave_state_ack(ec_slave_t *slave, /**< EtherCAT slave */
   800 void ec_slave_state_ack(ec_slave_t *slave, /**< EtherCAT slave */
   807                         uint8_t state /**< previous state */
   801                         uint8_t state /**< previous state */
   808                         )
   802                         )
   855 
   849 
   856 /**
   850 /**
   857    Reads the AL status code of a slave and displays it.
   851    Reads the AL status code of a slave and displays it.
   858    If the AL status code is not supported, or if no error occurred (both
   852    If the AL status code is not supported, or if no error occurred (both
   859    resulting in code = 0), nothing is displayed.
   853    resulting in code = 0), nothing is displayed.
   860    \ingroup Slave
       
   861 */
   854 */
   862 
   855 
   863 void ec_slave_read_al_status_code(ec_slave_t *slave /**< EtherCAT slave */)
   856 void ec_slave_read_al_status_code(ec_slave_t *slave /**< EtherCAT slave */)
   864 {
   857 {
   865     ec_command_t *command;
   858     ec_command_t *command;
   891 /*****************************************************************************/
   884 /*****************************************************************************/
   892 
   885 
   893 /**
   886 /**
   894    Does a state transition.
   887    Does a state transition.
   895    \return 0 in case of success, else < 0
   888    \return 0 in case of success, else < 0
   896    \ingroup Slave
       
   897 */
   889 */
   898 
   890 
   899 int ec_slave_state_change(ec_slave_t *slave, /**< EtherCAT slave */
   891 int ec_slave_state_change(ec_slave_t *slave, /**< EtherCAT slave */
   900                           uint8_t state /**< new state */
   892                           uint8_t state /**< new state */
   901                           )
   893                           )
   961    of the corresponding sync manager is covered. Seperate FMMUs arce configured
   953    of the corresponding sync manager is covered. Seperate FMMUs arce configured
   962    for each domain.
   954    for each domain.
   963    If the FMMU configuration is already prepared, the function returns with
   955    If the FMMU configuration is already prepared, the function returns with
   964    success.
   956    success.
   965    \return 0 in case of success, else < 0
   957    \return 0 in case of success, else < 0
   966    \ingroup Slave
       
   967 */
   958 */
   968 
   959 
   969 int ec_slave_prepare_fmmu(ec_slave_t *slave, /**< EtherCAT slave */
   960 int ec_slave_prepare_fmmu(ec_slave_t *slave, /**< EtherCAT slave */
   970                           const ec_domain_t *domain, /**< domain */
   961                           const ec_domain_t *domain, /**< domain */
   971                           const ec_sync_t *sync  /**< sync manager */
   962                           const ec_sync_t *sync  /**< sync manager */
  1000    Outputs all information about a certain slave.
   991    Outputs all information about a certain slave.
  1001    Verbosity:
   992    Verbosity:
  1002    - 0: Only slave types and addresses
   993    - 0: Only slave types and addresses
  1003    - 1: with EEPROM information
   994    - 1: with EEPROM information
  1004    - >1: with SDO dictionaries
   995    - >1: with SDO dictionaries
  1005    \ingroup Slave
       
  1006 */
   996 */
  1007 
   997 
  1008 void ec_slave_print(const ec_slave_t *slave, /**< EtherCAT slave */
   998 void ec_slave_print(const ec_slave_t *slave, /**< EtherCAT slave */
  1009                     unsigned int verbosity /**< verbosity level */
   999                     unsigned int verbosity /**< verbosity level */
  1010                     )
  1000                     )
  1162 /*****************************************************************************/
  1152 /*****************************************************************************/
  1163 
  1153 
  1164 /**
  1154 /**
  1165    Outputs the values of the CRC faoult counters and resets them.
  1155    Outputs the values of the CRC faoult counters and resets them.
  1166    \return 0 in case of success, else < 0
  1156    \return 0 in case of success, else < 0
  1167    \ingroup Slave
       
  1168 */
  1157 */
  1169 
  1158 
  1170 int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT slave */)
  1159 int ec_slave_check_crc(ec_slave_t *slave /**< EtherCAT slave */)
  1171 {
  1160 {
  1172     ec_command_t *command;
  1161     ec_command_t *command;
  1216 }
  1205 }
  1217 
  1206 
  1218 /*****************************************************************************/
  1207 /*****************************************************************************/
  1219 
  1208 
  1220 /**
  1209 /**
  1221    Writes the "configured station alias" to the slave's EEPROM.
       
  1222    \return 0 in case of success, else < 0
       
  1223    \ingroup Slave
       
  1224 */
       
  1225 
       
  1226 int ecrt_slave_write_alias(ec_slave_t *slave, /**< EtherCAT slave */
       
  1227                            uint16_t alias /**< new alias */
       
  1228                            )
       
  1229 {
       
  1230     return ec_slave_sii_write16(slave, 0x0004, alias);
       
  1231 }
       
  1232 
       
  1233 /*****************************************************************************/
       
  1234 
       
  1235 /**
       
  1236    Formats attribute data for SysFS read access.
  1210    Formats attribute data for SysFS read access.
  1237    \return number of bytes to read
  1211    \return number of bytes to read
  1238    \ingroup Slave
  1212    \ingroup RealTimeInterface
  1239 */
  1213 */
  1240 
  1214 
  1241 ssize_t ec_show_slave_attribute(struct kobject *kobj, /**< slave's kobject */
  1215 ssize_t ec_show_slave_attribute(struct kobject *kobj, /**< slave's kobject */
  1242                                 struct attribute *attr, /**< attribute */
  1216                                 struct attribute *attr, /**< attribute */
  1243                                 char *buffer /**< memory to store data */
  1217                                 char *buffer /**< memory to store data */
  1275 
  1249 
  1276     return 0;
  1250     return 0;
  1277 }
  1251 }
  1278 
  1252 
  1279 /*****************************************************************************/
  1253 /*****************************************************************************/
       
  1254 
       
  1255 /**
       
  1256    Application layer status messages.
       
  1257 */
  1280 
  1258 
  1281 const ec_code_msg_t al_status_messages[] = {
  1259 const ec_code_msg_t al_status_messages[] = {
  1282     {0x0001, "Unspecified error"},
  1260     {0x0001, "Unspecified error"},
  1283     {0x0011, "Invalud requested state change"},
  1261     {0x0011, "Invalud requested state change"},
  1284     {0x0012, "Unknown requested state"},
  1262     {0x0012, "Unknown requested state"},
  1296     {0x0022, "Slave needs PREOP"},
  1274     {0x0022, "Slave needs PREOP"},
  1297     {0x0023, "Slave needs SAVEOP"},
  1275     {0x0023, "Slave needs SAVEOP"},
  1298     {}
  1276     {}
  1299 };
  1277 };
  1300 
  1278 
  1301 /*****************************************************************************/
  1279 /******************************************************************************
       
  1280  *  Realtime interface
       
  1281  *****************************************************************************/
       
  1282 
       
  1283 /**
       
  1284    Writes the "configured station alias" to the slave's EEPROM.
       
  1285    \return 0 in case of success, else < 0
       
  1286    \ingroup RealtimeInterface
       
  1287 */
       
  1288 
       
  1289 int ecrt_slave_write_alias(ec_slave_t *slave, /**< EtherCAT slave */
       
  1290                            uint16_t alias /**< new alias */
       
  1291                            )
       
  1292 {
       
  1293     return ec_slave_sii_write16(slave, 0x0004, alias);
       
  1294 }
       
  1295 
       
  1296 /*****************************************************************************/
       
  1297 
       
  1298 /**< \cond */
  1302 
  1299 
  1303 EXPORT_SYMBOL(ecrt_slave_write_alias);
  1300 EXPORT_SYMBOL(ecrt_slave_write_alias);
  1304 
  1301 
  1305 /*****************************************************************************/
  1302 /**< \endcond */
       
  1303 
       
  1304 /*****************************************************************************/