Files
NeoPixelBus/src/internal/NeoPixelEsp8266.c
T
Unai Uribarri 68e48b7957 Add new method NeoEsp8266AsyncUart*KbpsMethod
Add two new methods NeoEsp8266AsyncUart800KbpsMethod &
NeoEsp8266AsyncUart400KbpsMethod, which uses UART1 interrupts to
output all pixels asynchronously.

Also, refactor old UART code to share as much code as possible.
2016-06-15 22:24:07 +02:00

131 lines
4.2 KiB
C

/*-------------------------------------------------------------------------
NeoPixel library helper functions for Esp8266.
Written by Michael C. Miller.
I invest time and resources providing this open source code,
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
-------------------------------------------------------------------------
This file is part of the Makuna/NeoPixelBus library.
NeoPixelBus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixelBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#ifdef ARDUINO_ARCH_ESP8266
#include <Arduino.h>
#include <eagle_soc.h>
inline uint32_t _getCycleCount()
{
uint32_t ccount;
__asm__ __volatile__("rsr %0,ccount":"=a" (ccount));
return ccount;
}
#define CYCLES_800_T0H (F_CPU / 2500000) // 0.4us
#define CYCLES_800_T1H (F_CPU / 1250000) // 0.8us
#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)
void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint8_t pin)
{
const uint32_t pinRegister = _BV(pin);
uint8_t mask;
uint8_t subpix;
uint32_t cyclesStart;
// trigger emediately
cyclesStart = _getCycleCount() - CYCLES_800;
do
{
subpix = *pixels++;
for (mask = 0x80; mask != 0; mask >>= 1)
{
// do the checks here while we are waiting on time to pass
uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_800_T1H : CYCLES_800_T0H;
uint32_t cyclesNext = cyclesStart;
uint32_t delta;
// after we have done as much work as needed for this next bit
// now wait for the HIGH
do
{
// cache and use this count so we don't incur another
// instruction before we turn the bit high
cyclesStart = _getCycleCount();
} while ((cyclesStart - cyclesNext) < CYCLES_800);
// set high
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister);
// wait for the LOW
do
{
cyclesNext = _getCycleCount();
} while ((cyclesNext - cyclesStart) < cyclesBit);
// set low
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister);
}
} while (pixels < end);
}
void ICACHE_RAM_ATTR bitbang_send_pixels_400(uint8_t* pixels, uint8_t* end, uint8_t pin)
{
const uint32_t pinRegister = _BV(pin);
uint8_t mask;
uint8_t subpix;
uint32_t cyclesStart;
// trigger emediately
cyclesStart = _getCycleCount() - CYCLES_400;
do
{
subpix = *pixels++;
for (mask = 0x80; mask; mask >>= 1)
{
uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_400_T1H : CYCLES_400_T0H;
uint32_t cyclesNext = cyclesStart;
// after we have done as much work as needed for this next bit
// now wait for the HIGH
do
{
// cache and use this count so we don't incur another
// instruction before we turn the bit high
cyclesStart = _getCycleCount();
} while ((cyclesStart - cyclesNext) < CYCLES_400);
// set high
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinRegister);
// wait for the LOW
do
{
cyclesNext = _getCycleCount();
} while ((cyclesNext - cyclesStart) < cyclesBit);
// set low
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinRegister);
}
} while (pixels < end);
}
#endif