debug

Fork of PololuLedStripx by Michael Koster

Revision:
14:672baf3cf941
Parent:
13:9c72841ec45e
Child:
15:d69eebdee025
diff -r 9c72841ec45e -r 672baf3cf941 PololuLedStrip.cpp
--- a/PololuLedStrip.cpp	Fri Mar 01 04:46:25 2013 +0000
+++ b/PololuLedStrip.cpp	Fri Mar 01 05:00:49 2013 +0000
@@ -2,36 +2,46 @@
 
 bool PololuLedStrip::interruptFriendly = false;
 
+// The three timed delays, in units of half-instructions.
 uint8_t led_strip_write_delays[3];
 
-static const uint8_t delay_fudges[] = { 23, 28, 23 };
-
 void PololuLedStrip::calculateDelays()
 {
     // Get the clock frequency in MHz.
     int f_mhz = SystemCoreClock / 1000000;
-    
-    // Arrange for a 700 nanosecond delay between the rise time and the fall time for a 0 bit.
-    led_strip_write_delays[0] = 700*f_mhz/1000;
-    
-    // Arrange for a 600 nanosecond delay between the fall time for a 0 bit and the fall time for a 1 bit.
-    // This means the pulses representing a 1 will be 700+600 = 1300 nanoseconds.
-    led_strip_write_delays[1] = 600*f_mhz/1000;
+
+    if (f_mhz <= 48)
+    {
+        // The delays below result in 800/1590 ns pulses and a 2500 ns period on the mbed NXP LPC11U24.        
+        led_strip_write_delays[0] = 0;
+        led_strip_write_delays[1] = 0;
+        led_strip_write_delays[2] = 5 << 1;
+    }
+    else
+    {
+        // Try to generally compute what the delays should be for any frequency clock.
+        
+        // The fudge factors below were experimentally chosen so that we would have
+        // 700/1300 ns pulses and a ~ 2500 ns period on the mbed NXP LPC1768 (96 MHz Cortex-M3).
+        // If you ever change these numbers, it is important to check the the subtractions below
+        // will not overflow in the worst case, which is f_mhz = 48.
+        static const uint8_t delay_fudges[] = { 23, 28, 23 };
+        
+        led_strip_write_delays[0] = 700*f_mhz/1000;
+        led_strip_write_delays[1] = 600*f_mhz/1000;
+        led_strip_write_delays[2] = 1200*f_mhz/1000;
     
-    // Arrange for a 1200 nanosecond delay between the fall time for a 1 bit and rise time of the next bit.
-    // This means the period of the signal will be 2500 nanoseconds.
-    led_strip_write_delays[2] = 1200*f_mhz/1000;
-    
-    for(int i = 0; i < 3; i++)
-    {
-        if (led_strip_write_delays[i] < delay_fudges[i])
+        for(int i = 0; i < 3; i++)
         {
-            led_strip_write_delays[i] = 0;
-        }
-        else
-        {
-            led_strip_write_delays[i] -= delay_fudges[i];
-            led_strip_write_delays[i] <<= 1;
+            if (led_strip_write_delays[i] < delay_fudges[i])
+            {
+                led_strip_write_delays[i] = 0;
+            }
+            else
+            {
+                led_strip_write_delays[i] -= delay_fudges[i];
+                led_strip_write_delays[i] <<= 1;
+            }
         }
     }
 }