Userspace library implementation of DC functions.
authorFlorian Pose <fp@igh-essen.com>
Thu, 23 Apr 2009 14:45:07 +0000
changeset 1413 44c2b7c0ae1a
parent 1412 8f7bbbd3b7ac
child 1414 0037a63d3cc5
Userspace library implementation of DC functions.
TODO
lib/master.c
lib/slave_config.c
master/cdev.c
master/ioctl.h
--- a/TODO	Thu Apr 23 12:25:09 2009 +0000
+++ b/TODO	Thu Apr 23 14:45:07 2009 +0000
@@ -12,8 +12,8 @@
 
 * Distributed clocks:
     - Delay calculation.
-    - Synchronize the reference clock to the master clock.
 	- Output reference clock and application time in 'ethercat master'.
+    - User same application time offset when setting start times.
 * Read alias from register 0x0012 instead of SII.
 * Finish library implementation.
 * Re-work EoE code.
--- a/lib/master.c	Thu Apr 23 12:25:09 2009 +0000
+++ b/lib/master.c	Thu Apr 23 14:45:07 2009 +0000
@@ -189,3 +189,28 @@
 }
 
 /*****************************************************************************/
+
+void ecrt_master_sync_reference_clock(ec_master_t *master,
+        const struct timeval *app_time)
+{
+    ec_ioctl_dc_t data;
+
+    data.app_time.tv_sec = app_time->tv_sec;
+    data.app_time.tv_usec = app_time->tv_usec;
+
+    if (ioctl(master->fd, EC_IOCTL_SYNC_REF, &data) == -1) {
+        fprintf(stderr, "Failed to sync reference clock: %s\n",
+                strerror(errno));
+    }
+}
+
+/*****************************************************************************/
+
+void ecrt_master_sync_slave_clocks(ec_master_t *master)
+{
+    if (ioctl(master->fd, EC_IOCTL_SYNC_SLAVES, NULL) == -1) {
+        fprintf(stderr, "Failed to sync slave clocks: %s\n", strerror(errno));
+    }
+}
+
+/*****************************************************************************/
--- a/lib/slave_config.c	Thu Apr 23 12:25:09 2009 +0000
+++ b/lib/slave_config.c	Thu Apr 23 14:45:07 2009 +0000
@@ -244,6 +244,53 @@
 
 /*****************************************************************************/
 
+void ecrt_slave_config_dc_assign_activate(ec_slave_config_t *sc,
+        uint16_t assign_activate)
+{
+    ec_ioctl_sc_dc_t data;
+
+    data.config_index = sc->index;
+    data.assign_activate = assign_activate;
+
+    if (ioctl(sc->master->fd, EC_IOCTL_SC_DC_ASSIGN, &data) == -1) {
+        fprintf(stderr, "Failed to set assign_activate word.\n");
+    }
+}
+
+/*****************************************************************************/
+
+void ecrt_slave_config_dc_sync_cycle_times(ec_slave_config_t *sc,
+        uint32_t sync0_cycle_time, uint32_t sync1_cycle_time)
+{
+    ec_ioctl_sc_dc_t data;
+
+    data.config_index = sc->index;
+	data.cycle[0] = sync0_cycle_time;
+	data.cycle[1] = sync1_cycle_time;
+
+    if (ioctl(sc->master->fd, EC_IOCTL_SC_DC_CYCLE, &data) == -1) {
+        fprintf(stderr, "Failed to set assign_activate word.\n");
+    }
+}
+
+/*****************************************************************************/
+
+void ecrt_slave_config_dc_sync_shift_times(ec_slave_config_t *sc,
+        uint32_t sync0_shift_time, uint32_t sync1_shift_time)
+{
+    ec_ioctl_sc_dc_t data;
+
+    data.config_index = sc->index;
+	data.shift[0] = sync0_shift_time;
+	data.shift[1] = sync1_shift_time;
+
+    if (ioctl(sc->master->fd, EC_IOCTL_SC_DC_SHIFT, &data) == -1) {
+        fprintf(stderr, "Failed to set assign_activate word.\n");
+    }
+}
+
+/*****************************************************************************/
+
 int ecrt_slave_config_sdo(ec_slave_config_t *sc, uint16_t index,
         uint8_t subindex, const uint8_t *sdo_data, size_t size)
 {
--- a/master/cdev.c	Thu Apr 23 12:25:09 2009 +0000
+++ b/master/cdev.c	Thu Apr 23 14:45:07 2009 +0000
@@ -1621,6 +1621,49 @@
 
 /*****************************************************************************/
 
+/** Sync the reference clock.
+ */
+int ec_cdev_ioctl_sync_ref(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+    ec_ioctl_dc_t data;
+    
+	if (unlikely(!priv->requested))
+		return -EPERM;
+
+    if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
+        return -EFAULT;
+
+    spin_lock_bh(&master->internal_lock);
+    ecrt_master_sync_reference_clock(master, &data.app_time);
+    spin_unlock_bh(&master->internal_lock);
+    return 0;
+}
+
+/*****************************************************************************/
+
+/** Sync the slave clocks.
+ */
+int ec_cdev_ioctl_sync_slaves(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+	if (unlikely(!priv->requested))
+		return -EPERM;
+
+    spin_lock_bh(&master->internal_lock);
+    ecrt_master_sync_slave_clocks(master);
+    spin_unlock_bh(&master->internal_lock);
+    return 0;
+}
+
+/*****************************************************************************/
+
 /** Set the direction of a sync manager.
  */
 int ec_cdev_ioctl_sc_sync(
@@ -1848,6 +1891,108 @@
 
 /*****************************************************************************/
 
+/** Sets the DC AssignActivate word.
+ */
+int ec_cdev_ioctl_sc_dc_assign(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+    ec_ioctl_sc_dc_t data;
+    ec_slave_config_t *sc;
+
+	if (unlikely(!priv->requested))
+        return -EPERM;
+
+    if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
+        return -EFAULT;
+
+    if (down_interruptible(&master->master_sem))
+        return -EINTR;
+
+    if (!(sc = ec_master_get_config(master, data.config_index))) {
+        up(&master->master_sem);
+        return -ENOENT;
+    }
+
+    ecrt_slave_config_dc_assign_activate(sc, data.assign_activate);
+
+    up(&master->master_sem);
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/** Sets the DC cycle times.
+ */
+int ec_cdev_ioctl_sc_dc_cycle(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+    ec_ioctl_sc_dc_t data;
+    ec_slave_config_t *sc;
+
+	if (unlikely(!priv->requested))
+        return -EPERM;
+
+    if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
+        return -EFAULT;
+
+    if (down_interruptible(&master->master_sem))
+        return -EINTR;
+
+    if (!(sc = ec_master_get_config(master, data.config_index))) {
+        up(&master->master_sem);
+        return -ENOENT;
+    }
+
+    ecrt_slave_config_dc_sync_cycle_times(sc, data.cycle[0], data.cycle[1]);
+
+    up(&master->master_sem);
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/** Sets the DC shift times.
+ */
+int ec_cdev_ioctl_sc_dc_shift(
+        ec_master_t *master, /**< EtherCAT master. */
+        unsigned long arg, /**< ioctl() argument. */
+        ec_cdev_priv_t *priv /**< Private data structure of file handle. */
+        )
+{
+    ec_ioctl_sc_dc_t data;
+    ec_slave_config_t *sc;
+
+	if (unlikely(!priv->requested))
+        return -EPERM;
+
+    if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
+        return -EFAULT;
+
+    if (down_interruptible(&master->master_sem))
+        return -EINTR;
+
+    if (!(sc = ec_master_get_config(master, data.config_index))) {
+        up(&master->master_sem);
+        return -ENOENT;
+    }
+
+    ecrt_slave_config_dc_sync_shift_times(sc, data.shift[0], data.shift[1]);
+
+    up(&master->master_sem);
+
+    return 0;
+}
+
+/*****************************************************************************/
+
 /** Configures an SDO.
  */
 int ec_cdev_ioctl_sc_sdo(
@@ -3035,6 +3180,14 @@
 			return ec_cdev_ioctl_receive(master, arg, priv);
         case EC_IOCTL_MASTER_STATE:
 			return ec_cdev_ioctl_master_state(master, arg, priv);
+        case EC_IOCTL_SYNC_REF:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_sync_ref(master, arg, priv);
+        case EC_IOCTL_SYNC_SLAVES:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_sync_slaves(master, arg, priv);
         case EC_IOCTL_SC_SYNC:
             if (!(filp->f_mode & FMODE_WRITE))
 				return -EPERM;
@@ -3059,6 +3212,18 @@
             if (!(filp->f_mode & FMODE_WRITE))
 				return -EPERM;
 			return ec_cdev_ioctl_sc_reg_pdo_entry(master, arg, priv);
+        case EC_IOCTL_SC_DC_ASSIGN:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_sc_dc_assign(master, arg, priv);
+        case EC_IOCTL_SC_DC_CYCLE:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_sc_dc_cycle(master, arg, priv);
+        case EC_IOCTL_SC_DC_SHIFT:
+            if (!(filp->f_mode & FMODE_WRITE))
+				return -EPERM;
+			return ec_cdev_ioctl_sc_dc_shift(master, arg, priv);
         case EC_IOCTL_SC_SDO:
             if (!(filp->f_mode & FMODE_WRITE))
 				return -EPERM;
--- a/master/ioctl.h	Thu Apr 23 12:25:09 2009 +0000
+++ b/master/ioctl.h	Thu Apr 23 14:45:07 2009 +0000
@@ -86,32 +86,37 @@
 #define EC_IOCTL_SEND                   EC_IO(0x1c)
 #define EC_IOCTL_RECEIVE                EC_IO(0x1d)
 #define EC_IOCTL_MASTER_STATE          EC_IOR(0x1e, ec_master_state_t)
-#define EC_IOCTL_SC_SYNC               EC_IOW(0x1f, ec_ioctl_config_t)
-#define EC_IOCTL_SC_ADD_PDO            EC_IOW(0x20, ec_ioctl_config_pdo_t)
-#define EC_IOCTL_SC_CLEAR_PDOS         EC_IOW(0x21, ec_ioctl_config_pdo_t)
-#define EC_IOCTL_SC_ADD_ENTRY          EC_IOW(0x22, ec_ioctl_add_pdo_entry_t)
-#define EC_IOCTL_SC_CLEAR_ENTRIES      EC_IOW(0x23, ec_ioctl_config_pdo_t)
-#define EC_IOCTL_SC_REG_PDO_ENTRY     EC_IOWR(0x24, ec_ioctl_reg_pdo_entry_t)
-#define EC_IOCTL_SC_SDO                EC_IOW(0x25, ec_ioctl_sc_sdo_t)
-#define EC_IOCTL_SC_SDO_REQUEST       EC_IOWR(0x26, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_SC_VOE               EC_IOWR(0x27, ec_ioctl_voe_t)
-#define EC_IOCTL_SC_STATE             EC_IOWR(0x28, ec_ioctl_sc_state_t)
-#define EC_IOCTL_DOMAIN_OFFSET          EC_IO(0x29)
-#define EC_IOCTL_DOMAIN_PROCESS         EC_IO(0x2a)
-#define EC_IOCTL_DOMAIN_QUEUE           EC_IO(0x2b)
-#define EC_IOCTL_DOMAIN_STATE         EC_IOWR(0x2c, ec_ioctl_domain_state_t)
-#define EC_IOCTL_SDO_REQUEST_TIMEOUT  EC_IOWR(0x2d, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_SDO_REQUEST_STATE    EC_IOWR(0x2e, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_SDO_REQUEST_READ     EC_IOWR(0x2f, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_SDO_REQUEST_WRITE    EC_IOWR(0x30, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_SDO_REQUEST_DATA     EC_IOWR(0x31, ec_ioctl_sdo_request_t)
-#define EC_IOCTL_VOE_SEND_HEADER       EC_IOW(0x32, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_REC_HEADER       EC_IOWR(0x33, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_READ              EC_IOW(0x34, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_READ_NOSYNC       EC_IOW(0x35, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_WRITE            EC_IOWR(0x36, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_EXEC             EC_IOWR(0x37, ec_ioctl_voe_t)
-#define EC_IOCTL_VOE_DATA             EC_IOWR(0x38, ec_ioctl_voe_t)
+#define EC_IOCTL_SYNC_REF              EC_IOW(0x1f, ec_ioctl_dc_t)
+#define EC_IOCTL_SYNC_SLAVES           EC_IOW(0x20, ec_ioctl_dc_t)
+#define EC_IOCTL_SC_SYNC               EC_IOW(0x21, ec_ioctl_config_t)
+#define EC_IOCTL_SC_ADD_PDO            EC_IOW(0x22, ec_ioctl_config_pdo_t)
+#define EC_IOCTL_SC_CLEAR_PDOS         EC_IOW(0x23, ec_ioctl_config_pdo_t)
+#define EC_IOCTL_SC_ADD_ENTRY          EC_IOW(0x24, ec_ioctl_add_pdo_entry_t)
+#define EC_IOCTL_SC_CLEAR_ENTRIES      EC_IOW(0x25, ec_ioctl_config_pdo_t)
+#define EC_IOCTL_SC_REG_PDO_ENTRY     EC_IOWR(0x26, ec_ioctl_reg_pdo_entry_t)
+#define EC_IOCTL_SC_DC_ASSIGN          EC_IOW(0x27, ec_ioctl_sc_dc_t)
+#define EC_IOCTL_SC_DC_CYCLE           EC_IOW(0x28, ec_ioctl_sc_dc_t)
+#define EC_IOCTL_SC_DC_SHIFT           EC_IOW(0x29, ec_ioctl_sc_dc_t)
+#define EC_IOCTL_SC_SDO                EC_IOW(0x2a, ec_ioctl_sc_sdo_t)
+#define EC_IOCTL_SC_SDO_REQUEST       EC_IOWR(0x2b, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_SC_VOE               EC_IOWR(0x2c, ec_ioctl_voe_t)
+#define EC_IOCTL_SC_STATE             EC_IOWR(0x2d, ec_ioctl_sc_state_t)
+#define EC_IOCTL_DOMAIN_OFFSET          EC_IO(0x2e)
+#define EC_IOCTL_DOMAIN_PROCESS         EC_IO(0x2f)
+#define EC_IOCTL_DOMAIN_QUEUE           EC_IO(0x30)
+#define EC_IOCTL_DOMAIN_STATE         EC_IOWR(0x31, ec_ioctl_domain_state_t)
+#define EC_IOCTL_SDO_REQUEST_TIMEOUT  EC_IOWR(0x32, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_SDO_REQUEST_STATE    EC_IOWR(0x33, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_SDO_REQUEST_READ     EC_IOWR(0x34, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_SDO_REQUEST_WRITE    EC_IOWR(0x35, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_SDO_REQUEST_DATA     EC_IOWR(0x36, ec_ioctl_sdo_request_t)
+#define EC_IOCTL_VOE_SEND_HEADER       EC_IOW(0x37, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_REC_HEADER       EC_IOWR(0x38, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_READ              EC_IOW(0x39, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_READ_NOSYNC       EC_IOW(0x3a, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_WRITE            EC_IOWR(0x3b, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_EXEC             EC_IOWR(0x3c, ec_ioctl_voe_t)
+#define EC_IOCTL_VOE_DATA             EC_IOWR(0x3d, ec_ioctl_voe_t)
 
 /*****************************************************************************/
 
@@ -382,6 +387,13 @@
 
 typedef struct {
     // inputs
+    struct timeval app_time;
+} ec_ioctl_dc_t;
+
+/*****************************************************************************/
+
+typedef struct {
+    // inputs
     uint32_t config_index;
     uint8_t sync_index;
     uint16_t pdo_pos;
@@ -451,6 +463,16 @@
 typedef struct {
     // inputs
     uint32_t config_index;
+    uint16_t assign_activate;
+    uint32_t cycle[2];
+    uint32_t shift[2];
+} ec_ioctl_sc_dc_t;
+
+/*****************************************************************************/
+
+typedef struct {
+    // inputs
+    uint32_t config_index;
     uint16_t index;
     uint8_t subindex;
     const uint8_t *data;