diff --git a/NeoPixelBus.cpp b/NeoPixelBus.cpp index ffc777f..1882b56 100644 --- a/NeoPixelBus.cpp +++ b/NeoPixelBus.cpp @@ -75,35 +75,113 @@ void NeoPixelBus::Begin(void) #if defined(ESP8266) -void ICACHE_FLASH_ATTR send_ws_0_800(uint8_t gpio) { - uint8_t i; - i = 4; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << gpio); - i = 7; while (i--) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << gpio); + +#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) + +#define RSR_CCOUNT(r) __asm__ __volatile__("rsr %0,234":"=a" (r)) + +static inline ICACHE_FLASH_ATTR uint32_t get_ccount(void) +{ + uint32_t ccount; + RSR_CCOUNT(ccount); + return ccount; } -void ICACHE_FLASH_ATTR send_ws_1_800(uint8_t gpio) { - 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); + +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) + { + 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_800); + + + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister); + + // wait for the LOW + if (nextBit) + { + while ((get_ccount() - cyclesStart) < CYCLES_800_T1H); + } + else + { + while ((get_ccount() - cyclesStart) < CYCLES_800_T0H); + } + + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister); + } + } + while ((get_ccount() - cyclesStart) < CYCLES_800); } -void ICACHE_FLASH_ATTR 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; -void ICACHE_FLASH_ATTR 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); + 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); } #endif #if defined(ESP8266) - void ICACHE_FLASH_ATTR NeoPixelBus::Show(void) #else @@ -749,41 +827,14 @@ 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); + #ifdef INCLUDE_NEO_KHZ400_SUPPORT } else { // 400 kHz bitstream - while (p < end) - { - uint8_t subpix = *p++; - for (uint8_t mask = 0x80; mask; mask >>= 1) - { - if (subpix & mask) - { - send_ws_1_400(_pin); - } - else - { - send_ws_0_400(_pin); - } - } - } + send_pixels_400(p, end, _pin); } #endif @@ -794,7 +845,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) @@ -943,9 +994,7 @@ void NeoPixelBus::Show(void) ResetDirty(); _endTime = micros(); // Save EOD time for latch on next call } -#if defined(ESP8266) -#pragma optimize( "", on ) -#endif + // Set the output pin number void NeoPixelBus::setPin(uint8_t p) 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