Replaced timeval by 64-bit EtherCAT time.
authorFlorian Pose <fp@igh-essen.com>
Fri, 24 Apr 2009 10:24:53 +0000
changeset 1417 7c2d5d69134c
parent 1416 85d20ae6736e
child 1418 8b10ba4d3302
Replaced timeval by 64-bit EtherCAT time.
TODO
examples/dc_rtai/dc_rtai_sample.c
examples/dc_user/main.c
include/ecrt.h
lib/master.c
master/cdev.c
master/globals.h
master/ioctl.h
master/master.c
--- a/TODO	Fri Apr 24 09:51:11 2009 +0000
+++ b/TODO	Fri Apr 24 10:24:53 2009 +0000
@@ -12,8 +12,8 @@
 
 * Distributed clocks:
     - Delay calculation.
-    - User same application time offset when setting start times.
-    - Replace timeval by uint64 EtherCAT time.
+    - Use common application time offset when setting start times.
+    - Check 32/64 bit operations.
 * Fix arguments of reg_read.
 * Sign/Abs type for reg_ commands?
 * Number layout for reg_read.
--- a/examples/dc_rtai/dc_rtai_sample.c	Fri Apr 24 09:51:11 2009 +0000
+++ b/examples/dc_rtai/dc_rtai_sample.c	Fri Apr 24 10:24:53 2009 +0000
@@ -220,13 +220,16 @@
             tv.tv_usec -= 1000000;
             tv.tv_sec++;
         }
-        //printk(KERN_INFO PFX "tv=%u.%06u\n", (u32) tv.tv_sec, (u32) tv.tv_usec);
             
         if (sync_ref_counter) {
             sync_ref_counter--;
         } else {
             sync_ref_counter = 9;
-            ecrt_master_sync_reference_clock(master, &tv);
+#if 0
+            printk(KERN_INFO PFX "ref: %u %u %llu\n",
+                    (u32) tv.tv_sec, (u32) tv.tv_usec, EC_TIMEVAL2NANO(&tv));
+#endif
+            ecrt_master_sync_reference_clock(master, EC_TIMEVAL2NANO(&tv));
         }
 		ecrt_master_sync_slave_clocks(master);
         ecrt_domain_queue(domain1);
--- a/examples/dc_user/main.c	Fri Apr 24 09:51:11 2009 +0000
+++ b/examples/dc_user/main.c	Fri Apr 24 10:24:53 2009 +0000
@@ -169,7 +169,7 @@
         sync_ref_counter--;
     } else {
         sync_ref_counter = 9;
-        ecrt_master_sync_reference_clock(master, &app_time);
+        ecrt_master_sync_reference_clock(master, EC_TIMEVAL2NANO(&app_time));
     }
     ecrt_master_sync_slave_clocks(master);
 
--- a/include/ecrt.h	Fri Apr 24 09:51:11 2009 +0000
+++ b/include/ecrt.h	Fri Apr 24 10:24:53 2009 +0000
@@ -46,7 +46,8 @@
  *   ecrt_slave_config_dc_sync_cycle_times() and
  *   ecrt_slave_config_dc_sync_shift_times() to configure a slave for cyclic
  *   operation, and ecrt_master_sync_reference_clock() and
- *   ecrt_master_sync_slave_clocks() for drift compensation.
+ *   ecrt_master_sync_slave_clocks() for drift compensation. The
+ *   EC_TIMEVAL2NANO() macro can be used for epoch time conversion.
  * - Changed the meaning of the negative return values of
  *   ecrt_slave_config_reg_pdo_entry() and ecrt_slave_config_sdo*().
  * - Imlemented the Vendor-specific over EtherCAT mailbox protocol. See
@@ -114,6 +115,17 @@
  */
 #define EC_MAX_STRING_LENGTH 64
 
+/** Timeval to nanoseconds conversion.
+ *
+ * This macro converts a unix epoch time to EtherCAT DC time.
+ *
+ * \see ecrt_master_sync_reference_clock()
+ *
+ * \param TV Pointer to struct timeval.
+ */
+#define EC_TIMEVAL2NANO(TV) \
+    (((TV)->tv_sec - 946684800ULL) * 1000000000ULL + (TV)->tv_usec * 1000ULL)
+
 /******************************************************************************
  * Data types 
  *****************************************************************************/
@@ -510,11 +522,13 @@
 
 /** Queues the DC reference clock drift compensation datagram for sending.
  *
- * The reference clock will by synchronized to the \a app_time.
+ * The reference clock will by synchronized to the \a app_time. The time is
+ * defined as nanoseconds from 2000-01-01 00:00. Converting an epoch time can
+ * be done with the EC_TIMEVAL2NANO() macro.
  */
 void ecrt_master_sync_reference_clock(
         ec_master_t *master, /**< EtherCAT master. */
-        const struct timeval *app_time /**< Application time. */
+        uint64_t app_time /**< Application time. */
         );
 
 /** Queues the DC clock drift compensation datagram for sending.
--- a/lib/master.c	Fri Apr 24 09:51:11 2009 +0000
+++ b/lib/master.c	Fri Apr 24 10:24:53 2009 +0000
@@ -190,13 +190,11 @@
 
 /*****************************************************************************/
 
-void ecrt_master_sync_reference_clock(ec_master_t *master,
-        const struct timeval *app_time)
+void ecrt_master_sync_reference_clock(ec_master_t *master, uint64_t 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;
+    data.app_time = app_time;
 
     if (ioctl(master->fd, EC_IOCTL_SYNC_REF, &data) == -1) {
         fprintf(stderr, "Failed to sync reference clock: %s\n",
--- a/master/cdev.c	Fri Apr 24 09:51:11 2009 +0000
+++ b/master/cdev.c	Fri Apr 24 10:24:53 2009 +0000
@@ -1647,7 +1647,7 @@
         return -EFAULT;
 
     spin_lock_bh(&master->internal_lock);
-    ecrt_master_sync_reference_clock(master, &data.app_time);
+    ecrt_master_sync_reference_clock(master, data.app_time);
     spin_unlock_bh(&master->internal_lock);
     return 0;
 }
--- a/master/globals.h	Fri Apr 24 09:51:11 2009 +0000
+++ b/master/globals.h	Fri Apr 24 10:24:53 2009 +0000
@@ -251,13 +251,6 @@
         .name = EC_STR(NAME), .owner = THIS_MODULE, .mode = S_IRUGO | S_IWUSR \
     }
 
-/** Timeval to nanoseconds conversion.
- *
- * \param TV Pointer to struct timeval.
- */
-#define EC_TIMEVAL2NANO(TV) \
-    (((TV)->tv_sec - 946684800ULL) * 1000000000ULL + (TV)->tv_usec * 1000ULL)
-
 /*****************************************************************************/
 
 extern char *ec_master_version_str;
--- a/master/ioctl.h	Fri Apr 24 09:51:11 2009 +0000
+++ b/master/ioctl.h	Fri Apr 24 10:24:53 2009 +0000
@@ -389,7 +389,7 @@
 
 typedef struct {
     // inputs
-    struct timeval app_time;
+    uint64_t app_time;
 } ec_ioctl_dc_t;
 
 /*****************************************************************************/
--- a/master/master.c	Fri Apr 24 09:51:11 2009 +0000
+++ b/master/master.c	Fri Apr 24 10:24:53 2009 +0000
@@ -1651,9 +1651,9 @@
 /*****************************************************************************/
 
 void ecrt_master_sync_reference_clock(ec_master_t *master,
-        const struct timeval *app_time)
-{
-    master->app_time = EC_TIMEVAL2NANO(app_time);
+        uint64_t app_time)
+{
+    master->app_time = app_time;
     EC_WRITE_U32(master->ref_sync_datagram.data, master->app_time);
     ec_master_queue_datagram(master, &master->ref_sync_datagram);
 }