Fixed jace in jiffies timeout calculation. stable-1.4
authorFlorian Pose <fp@igh-essen.com>
Fri, 17 Oct 2008 12:48:28 +0000
branchstable-1.4
changeset 1648 3e46fbb7eb1d
parent 1647 dd648d1786e9
child 1649 16641608a861
Fixed jace in jiffies timeout calculation.
TODO
master/master.c
master/master.h
master/module.c
--- a/TODO	Fri Oct 17 09:13:52 2008 +0000
+++ b/TODO	Fri Oct 17 12:48:28 2008 +0000
@@ -6,11 +6,6 @@
 
 -------------------------------------------------------------------------------
 
-Version 1.4.0:
-
-* Check for possible race condition in jiffy-based frame timeout calculation.
-* Update documentation.
-
 Future issues:
 
 * Distributed clocks.
@@ -34,7 +29,6 @@
     - Add a -n (numeric) switch.
 	- Check for unwanted options.
 * Segmented Sdo downloads.
-* Get original driver for r8169.
 
 Smaller issues:
 
--- a/master/master.c	Fri Oct 17 09:13:52 2008 +0000
+++ b/master/master.c	Fri Oct 17 12:48:28 2008 +0000
@@ -59,6 +59,22 @@
 
 /*****************************************************************************/
 
+#ifdef EC_HAVE_CYCLES
+
+/** Frame timeout in cycles.
+ */
+static cycles_t timeout_cycles;
+
+#else
+
+/** Frame timeout in jiffies.
+ */
+static unsigned long timeout_jiffies;
+    
+#endif
+
+/*****************************************************************************/
+
 void ec_master_clear_slave_configs(ec_master_t *);
 void ec_master_clear_domains(ec_master_t *);
 static int ec_master_idle_thread(void *);
@@ -69,6 +85,20 @@
 
 /*****************************************************************************/
 
+/** Static variables initializer.
+*/
+void ec_master_init_static(void)
+{
+#ifdef EC_HAVE_CYCLES
+    timeout_cycles = (cycles_t) EC_IO_TIMEOUT /* us */ * (cpu_khz / 1000);
+#else
+    // one jiffy may always elapse between time measurement
+    timeout_jiffies = max(EC_IO_TIMEOUT * HZ / 1000000, 1);
+#endif
+}
+
+/*****************************************************************************/
+
 /**
    Master constructor.
    \return 0 in case of success, else < 0
@@ -1346,33 +1376,21 @@
 void ecrt_master_receive(ec_master_t *master)
 {
     ec_datagram_t *datagram, *next;
-#ifdef EC_HAVE_CYCLES
-    cycles_t cycles_timeout;
-#else
-    unsigned long diff_ms, timeout_ms;
-#endif
     unsigned int frames_timed_out = 0;
 
     // receive datagrams
     ec_device_poll(&master->main_device);
 
-#ifdef EC_HAVE_CYCLES
-    cycles_timeout = (cycles_t) EC_IO_TIMEOUT /* us */ * (cpu_khz / 1000);
-#else
-    timeout_ms = max(EC_IO_TIMEOUT /* us */ / 1000, 2);
-#endif
-
     // dequeue all datagrams that timed out
     list_for_each_entry_safe(datagram, next, &master->datagram_queue, queue) {
         if (datagram->state != EC_DATAGRAM_SENT) continue;
 
 #ifdef EC_HAVE_CYCLES
         if (master->main_device.cycles_poll - datagram->cycles_sent
-            > cycles_timeout) {
+                > timeout_cycles) {
 #else
-        diff_ms = (master->main_device.jiffies_poll
-                - datagram->jiffies_sent) * 1000 / HZ;
-        if (diff_ms > timeout_ms) {
+        if (master->main_device.jiffies_poll - datagram->jiffies_sent
+                > timeout_jiffies) {
 #endif
             frames_timed_out = 1;
             list_del_init(&datagram->queue);
@@ -1386,7 +1404,8 @@
                 time_us = (unsigned int) (master->main_device.cycles_poll -
                         datagram->cycles_sent) * 1000 / cpu_khz;
 #else
-                time_us = (unsigned int) (diff_ms * 1000);
+                time_us = (unsigned int) ((master->main_device.jiffies_poll -
+                            datagram->jiffies_sent) * 1000000 / HZ);
 #endif
                 EC_DBG("TIMED OUT datagram %08x, index %02X waited %u us.\n",
                         (unsigned int) datagram, datagram->index, time_us);
--- a/master/master.h	Fri Oct 17 09:13:52 2008 +0000
+++ b/master/master.h	Fri Oct 17 12:48:28 2008 +0000
@@ -162,6 +162,9 @@
 
 /*****************************************************************************/
 
+// static funtions
+void ec_master_init_static(void);
+
 // master creation/deletion
 int ec_master_init(ec_master_t *, unsigned int, const uint8_t *,
         const uint8_t *, dev_t, struct class *);
--- a/master/module.c	Fri Oct 17 09:13:52 2008 +0000
+++ b/master/module.c	Fri Oct 17 12:48:28 2008 +0000
@@ -132,6 +132,9 @@
             goto out_class;
         }
     }
+
+    // initialize static master variables
+    ec_master_init_static();
     
     if (master_count) {
         if (!(masters = kmalloc(sizeof(ec_master_t) * master_count,