diff --git a/include/ecrt.h b/include/ecrt.h index 7be92216..d27dded2 100644 --- a/include/ecrt.h +++ b/include/ecrt.h @@ -75,7 +75,9 @@ * - Removed 'const' from argument of ecrt_sdo_request_state(), because the * userspace library has to modify object internals. * - Added 64-bit data access macros. - * - Added ecrt_slave_config_idn() method for storing SoE IDN configurations. + * - Added ecrt_slave_config_idn() method for storing SoE IDN configurations, + * and ecrt_master_read_idn() and ecrt_master_write_idn() to read/write IDNs + * ad-hoc via the user-space library. * * @{ */ @@ -661,6 +663,44 @@ int ecrt_master_sdo_upload( uint32_t *abort_code /**< Abort code of the SDO upload. */ ); +/** Executes an SoE write request. + * + * Starts writing an IDN and blocks until the request was processed, or an + * error occurred. + * + * \retval 0 Success. + * \retval -1 An error occured. + */ +int ecrt_master_write_idn( + ec_master_t *master, /**< EtherCAT master. */ + uint16_t slave_position, /**< Slave position. */ + uint16_t idn, /**< SoE IDN (see ecrt_slave_config_idn()). */ + uint8_t *data, /**< Pointer to data to write. */ + size_t data_size, /**< Size of data to write. */ + uint32_t *error_code /**< Pointer to variable, where an SoE error code + can be stored. */ + ); + +/** Executes an SoE read request. + * + * Starts reading an IDN and blocks until the request was processed, or an + * error occurred. + * + * \retval 0 Success. + * \retval -1 An error occured. + */ +int ecrt_master_read_idn( + ec_master_t *master, /**< EtherCAT master. */ + uint16_t slave_position, /**< Slave position. */ + uint16_t idn, /**< SoE IDN (see ecrt_slave_config_idn()). */ + uint8_t *target, /**< Pointer to memory where the read data can be + stored. */ + size_t target_size, /**< Size of the memory \a target points to. */ + size_t *result_size, /**< Actual size of the received data. */ + uint32_t *error_code /**< Pointer to variable, where an SoE error code + can be stored. */ + ); + #endif /* #ifndef __KERNEL__ */ /** Finishes the configuration phase and prepares for cyclic operation. diff --git a/lib/master.c b/lib/master.c index 0677ca0a..5b4f0580 100644 --- a/lib/master.c +++ b/lib/master.c @@ -304,6 +304,54 @@ int ecrt_master_sdo_upload(ec_master_t *master, uint16_t slave_position, /*****************************************************************************/ +int ecrt_master_write_idn(ec_master_t *master, uint16_t slave_position, + uint16_t idn, uint8_t *data, size_t data_size, uint32_t *error_code) +{ + ec_ioctl_slave_soe_write_t io; + + io.slave_position = slave_position; + io.idn = idn; + io.data_size = data_size; + io.data = data; + + if (ioctl(master->fd, EC_IOCTL_SLAVE_SOE_WRITE, &io) == -1) { + if (errno == EIO && error_code) { + *error_code = io.error_code; + } + fprintf(stderr, "Failed to write IDN: %s\n", strerror(errno)); + return -1; + } + + return 0; +} + +/*****************************************************************************/ + +int ecrt_master_read_idn(ec_master_t *master, uint16_t slave_position, + uint16_t idn, uint8_t *target, size_t target_size, + size_t *result_size, uint32_t *error_code) +{ + ec_ioctl_slave_soe_read_t io; + + io.slave_position = slave_position; + io.idn = idn; + io.mem_size = target_size; + io.data = target; + + if (ioctl(master->fd, EC_IOCTL_SLAVE_SOE_READ, &io) == -1) { + if (errno == EIO && error_code) { + *error_code = io.error_code; + } + fprintf(stderr, "Failed to read IDN: %s\n", strerror(errno)); + return -1; + } + + *result_size = io.data_size; + return 0; +} + +/*****************************************************************************/ + int ecrt_master_activate(ec_master_t *master) { if (ioctl(master->fd, EC_IOCTL_ACTIVATE,