From 70816eeb52fd09c6a6fbcfd3ef5a4f1d6f0d624e Mon Sep 17 00:00:00 2001 From: Makuna Date: Fri, 1 May 2015 12:12:53 -0700 Subject: [PATCH 1/2] Reasonable with Glitches --- NeoPixelBus.cpp | 77 ++++++++++++++++++++++++++++++++++--------------- NeoPixelBus.h | 11 +------ RgbColor.h | 11 +------ 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/NeoPixelBus.cpp b/NeoPixelBus.cpp index cd67645..6ab3c5e 100644 --- a/NeoPixelBus.cpp +++ b/NeoPixelBus.cpp @@ -76,16 +76,57 @@ void NeoPixelBus::Begin(void) #if defined(ESP8266) #pragma optimize( "", off ) -void send_ws_0_800(uint8_t gpio) { +void ICACHE_FLASH_ATTR send_ws_0_800(uint32_t pinRegister) { uint8_t i; - i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); + + i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); + i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); } -void send_ws_1_800(uint8_t gpio) { +void ICACHE_FLASH_ATTR send_ws_1_800(uint32_t pinRegister) { uint8_t i; - i = 9; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 2; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); + + i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); + i = 1; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); +} + +//void ICACHE_FLASH_ATTR send_subpix_800(uint8_t subpix, uint8_t pin) +//{ +// uint8_t mask; +// for (mask = 0x80; mask; mask >>= 1) +// { +// if (subpix & mask) +// { +// send_ws_1_800(pin); +// } +// else +// { +// send_ws_0_800(pin); +// } +// } +//} + +void ICACHE_FLASH_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin) +{ + uint8_t mask; + uint8_t subpix; + uint32_t pinRegister = _BV(pin); + + while (pixels < end) + { + subpix = *pixels++; + for (mask = 0x80; mask; mask >>= 1) + { + if (subpix & mask) + { + send_ws_1_800(pinRegister); + } + else + { + send_ws_0_800(pinRegister); + } + } + } } void send_ws_0_400(uint8_t gpio) { @@ -747,21 +788,11 @@ void NeoPixelBus::Show(void) { #endif // 800 KHz bitstream - while (p < end) - { - uint8_t subpix = *p++; - for (uint8_t mask = 0x80; mask; mask >>= 1) - { - if (subpix & mask) - { - send_ws_1_800(_pin); - } - else - { - send_ws_0_800(_pin); - } - } - } + send_pixels_800(p, end, _pin); + //while (p < end) + //{ + // send_subpix_800(*p++, _pin); + //} #ifdef INCLUDE_NEO_KHZ400_SUPPORT } else @@ -769,8 +800,8 @@ void NeoPixelBus::Show(void) // 400 kHz bitstream while (p < end) { - uint8_t subpix = *p++; - for (uint8_t mask = 0x80; mask; mask >>= 1) + subpix = *p++; + for (mask = 0x80; mask; mask >>= 1) { if (subpix & mask) { diff --git a/NeoPixelBus.h b/NeoPixelBus.h index 8a75af5..cf61d13 100644 --- a/NeoPixelBus.h +++ b/NeoPixelBus.h @@ -15,17 +15,9 @@ You should have received a copy of the GNU Lesser General Public License along with NeoPixel. If not, see . --------------------------------------------------------------------*/ +#pragma once -#ifndef NEOPIXELBUS_H -#define NEOPIXELBUS_H - -#if (ARDUINO >= 100) #include -#else -#include -#include -#endif - #include "RgbColor.h" // '_flagsPixels' flags for LED _pixels (third parameter to constructor): @@ -125,4 +117,3 @@ private: }; -#endif // NEOPIXELBUS_H diff --git a/RgbColor.h b/RgbColor.h index 48aa81c..fc3ca1f 100644 --- a/RgbColor.h +++ b/RgbColor.h @@ -13,16 +13,9 @@ You should have received a copy of the GNU Lesser General Public License along with NeoPixel. If not, see . --------------------------------------------------------------------*/ +#pragma once -#ifndef RGBCOLOR_H -#define RGBCOLOR_H - -#if (ARDUINO >= 100) #include -#else -#include -#include -#endif // ------------------------------------------------------------------------ // RgbColor represents a color object that is represented by Red, Green, Blue @@ -95,5 +88,3 @@ struct RgbColor uint8_t B; }; - -#endif // RGBCOLOR_H \ No newline at end of file From b1e6be2ba5b7ab5158ba890717e2d722d3edacd1 Mon Sep 17 00:00:00 2001 From: Makuna Date: Sat, 2 May 2015 13:41:22 -0700 Subject: [PATCH 2/2] Use CCOUNT to time pulses --- NeoPixelBus.cpp | 155 +++++++++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 68 deletions(-) diff --git a/NeoPixelBus.cpp b/NeoPixelBus.cpp index 6ab3c5e..e31fc59 100644 --- a/NeoPixelBus.cpp +++ b/NeoPixelBus.cpp @@ -74,78 +74,114 @@ void NeoPixelBus::Begin(void) } #if defined(ESP8266) -#pragma optimize( "", off ) -void ICACHE_FLASH_ATTR send_ws_0_800(uint32_t pinRegister) { - uint8_t i; +#define CYCLES_800_T0H (F_CPU / 2500000 - 2) // 0.4us +#define CYCLES_800_T1H (F_CPU / 1250000 - 2) // 0.8us +#define CYCLES_800 (F_CPU / 800000 - 2) // 1.25us per bit +#define CYCLES_400_T0H (F_CPU / 2000000 - 2) +#define CYCLES_400_T1H (F_CPU / 833333 - 2) +#define CYCLES_400 (F_CPU / 400000 - 2) - i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); - i = 6; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); -} +#define RSR_CCOUNT(r) __asm__ __volatile__("rsr %0,234":"=a" (r)) -void ICACHE_FLASH_ATTR send_ws_1_800(uint32_t pinRegister) { - uint8_t i; - - i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); - i = 1; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); -} - -//void ICACHE_FLASH_ATTR send_subpix_800(uint8_t subpix, uint8_t pin) -//{ -// uint8_t mask; -// for (mask = 0x80; mask; mask >>= 1) -// { -// if (subpix & mask) -// { -// send_ws_1_800(pin); -// } -// else -// { -// send_ws_0_800(pin); -// } -// } -//} - -void ICACHE_FLASH_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin) +static inline ICACHE_FLASH_ATTR uint32_t get_ccount(void) { - uint8_t mask; - uint8_t subpix; - uint32_t pinRegister = _BV(pin); + uint32_t ccount; + RSR_CCOUNT(ccount); + return ccount; +} + +static inline void ICACHE_FLASH_ATTR send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin) +{ + const uint32_t pinRegister = _BV(pin); + uint32_t mask; // 32 bit work is optimized for the chip + uint32_t subpix; // 32 bit work is optimized for the chip + uint32_t cyclesStart; + + cyclesStart = get_ccount() + CYCLES_800; while (pixels < end) { subpix = *pixels++; for (mask = 0x80; mask; mask >>= 1) { - if (subpix & mask) + uint32_t nextBit = (subpix & mask); + uint32_t cyclesNext = cyclesStart; + + // after we have done as much work as needed for this next bit + // now wait for the HIGH + do { - send_ws_1_800(pinRegister); + cyclesStart = get_ccount(); + } + while ((cyclesStart - cyclesNext) < CYCLES_800); + + + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); + + // wait for the LOW + if (nextBit) + { + while ((get_ccount() - cyclesStart) < CYCLES_800_T1H); } else { - send_ws_0_800(pinRegister); + while ((get_ccount() - cyclesStart) < CYCLES_800_T0H); } + + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); } } + while ((get_ccount() - cyclesStart) < CYCLES_800); } -void send_ws_0_400(uint8_t gpio) { - uint8_t i; - i = 8; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 12; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); +static inline void ICACHE_FLASH_ATTR send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin) +{ + const uint32_t pinRegister = _BV(pin); + uint32_t mask; // 32 bit work is optimized for the chip + uint32_t subpix; // 32 bit work is optimized for the chip + uint32_t cyclesStart; + + cyclesStart = get_ccount() + CYCLES_400; + while (pixels < end) + { + subpix = *pixels++; + for (mask = 0x80; mask; mask >>= 1) + { + uint32_t nextBit = (subpix & mask); + uint32_t cyclesNext = cyclesStart; + + // after we have done as much work as needed for this next bit + // now wait for the HIGH + do + { + cyclesStart = get_ccount(); + } while ((cyclesStart - cyclesNext) < CYCLES_400); + + + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); + + // wait for the LOW + if (nextBit) + { + while ((get_ccount() - cyclesStart) < CYCLES_400_T1H); + } + else + { + while ((get_ccount() - cyclesStart) < CYCLES_400_T0H); + } + + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); + } + } + while ((get_ccount() - cyclesStart) < CYCLES_400); } -void send_ws_1_400(uint8_t gpio) { - uint8_t i; - i = 18; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); -} -#pragma optimize( "", on ) #endif #if defined(ESP8266) -#pragma optimize( "", off ) +//#pragma optimize( "", off ) #endif void NeoPixelBus::Show(void) { @@ -789,30 +825,13 @@ void NeoPixelBus::Show(void) #endif // 800 KHz bitstream send_pixels_800(p, end, _pin); - //while (p < end) - //{ - // send_subpix_800(*p++, _pin); - //} + #ifdef INCLUDE_NEO_KHZ400_SUPPORT } else { // 400 kHz bitstream - while (p < end) - { - subpix = *p++; - for (mask = 0x80; mask; mask >>= 1) - { - if (subpix & mask) - { - send_ws_1_400(_pin); - } - else - { - send_ws_0_400(_pin); - } - } - } + } #endif @@ -823,7 +842,7 @@ void NeoPixelBus::Show(void) #if defined(__MK20DX128__) || defined(__MK20DX256__) // Teensy 3.0 & 3.1 #define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us #define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us -#define CYCLES_800 (F_CPU / 800000) // 12.5us per bit +#define CYCLES_800 (F_CPU / 800000) // 1.25us per bit #define CYCLES_400_T0H (F_CPU / 2000000) #define CYCLES_400_T1H (F_CPU / 833333) #define CYCLES_400 (F_CPU / 400000) @@ -973,7 +992,7 @@ void NeoPixelBus::Show(void) _endTime = micros(); // Save EOD time for latch on next call } #if defined(ESP8266) -#pragma optimize( "", on ) +//#pragma optimize( "", on ) #endif // Set the output pin number