From 6fbfd3f17e67dcb8164486b6c06985a3a3cc0732 Mon Sep 17 00:00:00 2001 From: "danielgarcia@gmail.com" Date: Mon, 18 Feb 2013 10:59:19 +0000 Subject: Some timing adjustments for ws2811 - account for the fact that teensy 3 at 96Mhz appears to really be 48Mhz --- FastSPI_LED2.h | 4 ++-- clockless.h | 25 +++++++++++++++---------- examples/Fast2Dev/Fast2Dev.ino | 8 ++++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/FastSPI_LED2.h b/FastSPI_LED2.h index 015dc6b8..f9e6781d 100644 --- a/FastSPI_LED2.h +++ b/FastSPI_LED2.h @@ -95,8 +95,8 @@ class TM1809Controller800Mhz : public ClocklessController -class WS2811Controller800Mhz : public ClocklessController {}; -#if NO_TIME(350, 350, 550) +class WS2811Controller800Mhz : public ClocklessController {}; +#if NO_TIME(320, 320, 550) #pragma message "No enough clock cycles available for the UCS103" #endif diff --git a/clockless.h b/clockless.h index 53a7760c..8f665044 100644 --- a/clockless.h +++ b/clockless.h @@ -5,7 +5,12 @@ #include // for cli/se definitions // Macro to convert from nano-seconds to clocks -#define NS(_NS) (_NS / (1000 / (F_CPU / 1000000L))) +// #define NS(_NS) (_NS / (1000 / (F_CPU / 1000000L))) +#if F_CPU < 96000000 +#define NS(_NS) ( (_NS * (F_CPU / 1000000L))) / 1000 +#else +#define NS(_NS) ( (_NS * (F_CPU / 2000000L))) / 1000 +#endif // Macro for making sure there's enough time available #define NO_TIME(A, B, C) (NS(A) < 3 || NS(B) < 2 || NS(C) < 6) @@ -79,30 +84,30 @@ public: #if defined(__MK20DX128__) register uint32_t b = *data++; - while(data != end) { + while(data <= end) { for(register uint32_t i = 7; i > 0; i--) { Pin::fastset(port, hi); - delaycycles<1 + T1 - 4>(); // 4 cycles - 2 store, 1 test, 1 if + delaycycles(); // 4 cycles - 1 store, 1 test, 1 if if(b & 0x80) { Pin::fastset(port, hi); } else { Pin::fastset(port, lo); } b <<= 1; - delaycycles<1 + T2 - 5>(); // 5 cycles, 2 store, 2 store/skip, 1 shift + delaycycles(); // 3 cycles, 1 store, 1 store/skip, 1 shift Pin::fastset(port, lo); - delaycycles<1 + T3 - 5>(); // 5 cycles, 2 store, 1 sub, 2 branch backwards + delaycycles(); // 3 cycles, 1 store, 1 sub, 1 branch backwards } // extra delay because branch is faster falling through delaycycles<1>(); // 8th bit, interleave loading rest of data Pin::fastset(port, hi); - delaycycles<1 + T1 - 4>(); + delaycycles(); if(b & 0x80) { Pin::fastset(port, hi); } else { Pin::fastset(port, lo); } - delaycycles<1 + T2 - 4>(); // 4 cycles, 2 store, store/skip + delaycycles(); // 4 cycles, 2 store, store/skip Pin::fastset(port, lo); b = *data++; - delaycycles<1 + T3 - 8>(); // 2 store, 2 load, 1 cmp, 2 branch backwards, 1 movim - } + delaycycles(); // 1 store, 2 load, 1 cmp, 1 branch backwards, 1 movim + }; #else - while(data != end) { + while(data <= end) { register uint8_t b = *data++; bitSetFast<7>(port, hi, lo, b); bitSetFast<6>(port, hi, lo, b); diff --git a/examples/Fast2Dev/Fast2Dev.ino b/examples/Fast2Dev/Fast2Dev.ino index 7038858e..7a976c08 100644 --- a/examples/Fast2Dev/Fast2Dev.ino +++ b/examples/Fast2Dev/Fast2Dev.ino @@ -31,7 +31,7 @@ // #include #define NUM_LEDS 86 -struct CRGB { byte r; byte g; byte b; }; +struct CRGB { byte g; byte r; byte b; }; struct CRGB leds[NUM_LEDS]; @@ -67,8 +67,8 @@ struct CRGB leds[NUM_LEDS]; // TM1809Controller800Mhz<6> LED; // UCS1903Controller400Mhz<7> LED; -// WS2811Controller800Mhz<12> LED; -WS2811Controller800Mhz<5> LED; +WS2811Controller800Mhz<12> LED; +// WS2811Controller800Mhz<5> LED; // TM1803Controller400Mhz<5> LED; // struct aLED { void init() { FastSPI_LED.setLeds(NUM_LEDS, (unsigned char*)leds); FastSPI_LED.setPin(8); FastSPI_LED.setChipset(CFastSPI_LED::SPI_LPD8806); FastSPI_LED.init(); FastSPI_LED.start();}; void showRGB(byte *, int) { FastSPI_LED.show();} } LED; @@ -118,7 +118,7 @@ void setup() { void loop() { #if 0 - memset(leds, 0, NUM_LEDS * sizeof(struct CRGB)); + memset(leds, 255, NUM_LEDS * sizeof(struct CRGB)); LED.showRGB((byte*)leds, NUM_LEDS); delay(20); #else -- cgit v1.2.3