2016-02-11 13:19:11 +00:00
|
|
|
// Original version from https://github.com/adafruit/Adafruit_NeoPixel
|
|
|
|
// Modifications by dpgeorge to support auto-CPU-frequency detection
|
|
|
|
|
|
|
|
// This is a mash-up of the Due show() code + insights from Michael Miller's
|
|
|
|
// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus
|
|
|
|
// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution.
|
|
|
|
|
2016-10-08 22:56:46 +01:00
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#if MICROPY_ESP8266_NEOPIXEL
|
|
|
|
|
2016-02-11 13:19:11 +00:00
|
|
|
#include "c_types.h"
|
|
|
|
#include "eagle_soc.h"
|
|
|
|
#include "user_interface.h"
|
|
|
|
#include "espneopixel.h"
|
2016-08-07 14:03:00 +01:00
|
|
|
#include "esp_mphal.h"
|
2016-02-11 13:19:11 +00:00
|
|
|
|
|
|
|
#define NEO_KHZ400 (1)
|
|
|
|
|
|
|
|
void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
|
|
|
|
|
|
|
|
uint8_t *p, *end, pix, mask;
|
|
|
|
uint32_t t, time0, time1, period, c, startTime, pinMask;
|
|
|
|
|
|
|
|
pinMask = 1 << pin;
|
|
|
|
p = pixels;
|
|
|
|
end = p + numBytes;
|
|
|
|
pix = *p++;
|
|
|
|
mask = 0x80;
|
|
|
|
startTime = 0;
|
|
|
|
|
|
|
|
uint32_t fcpu = system_get_cpu_freq() * 1000000;
|
|
|
|
|
|
|
|
#ifdef NEO_KHZ400
|
|
|
|
if (is800KHz) {
|
|
|
|
#endif
|
2016-10-18 21:43:43 +01:00
|
|
|
time0 = fcpu / 2857143; // 0.35us
|
2016-02-11 13:19:11 +00:00
|
|
|
time1 = fcpu / 1250000; // 0.8us
|
|
|
|
period = fcpu / 800000; // 1.25us per bit
|
|
|
|
#ifdef NEO_KHZ400
|
|
|
|
} else { // 400 KHz bitstream
|
|
|
|
time0 = fcpu / 2000000; // 0.5uS
|
|
|
|
time1 = fcpu / 833333; // 1.2us
|
|
|
|
period = fcpu / 400000; // 2.5us per bit
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-30 10:11:56 +01:00
|
|
|
uint32_t irq_state = mp_hal_quiet_timing_enter();
|
2016-02-11 13:19:11 +00:00
|
|
|
for (t = time0;; t = time0) {
|
|
|
|
if (pix & mask) {
|
|
|
|
t = time1; // Bit high duration
|
2020-02-27 04:36:53 +00:00
|
|
|
}
|
2016-08-07 14:03:00 +01:00
|
|
|
while (((c = mp_hal_ticks_cpu()) - startTime) < period) {
|
|
|
|
; // Wait for bit start
|
2020-02-27 04:36:53 +00:00
|
|
|
}
|
2016-02-11 13:19:11 +00:00
|
|
|
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, pinMask); // Set high
|
|
|
|
startTime = c; // Save start time
|
2016-08-07 14:03:00 +01:00
|
|
|
while (((c = mp_hal_ticks_cpu()) - startTime) < t) {
|
|
|
|
; // Wait high duration
|
2020-02-27 04:36:53 +00:00
|
|
|
}
|
2016-02-11 13:19:11 +00:00
|
|
|
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, pinMask); // Set low
|
|
|
|
if (!(mask >>= 1)) { // Next bit/byte
|
|
|
|
if (p >= end) {
|
|
|
|
break;
|
2020-02-27 04:36:53 +00:00
|
|
|
}
|
2016-02-11 13:19:11 +00:00
|
|
|
pix = *p++;
|
|
|
|
mask = 0x80;
|
2020-02-27 04:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-07 14:03:00 +01:00
|
|
|
while ((mp_hal_ticks_cpu() - startTime) < period) {
|
|
|
|
; // Wait for last bit
|
2016-02-11 13:19:11 +00:00
|
|
|
}
|
2016-06-30 10:11:56 +01:00
|
|
|
mp_hal_quiet_timing_exit(irq_state);
|
2016-02-11 13:19:11 +00:00
|
|
|
}
|
2016-10-08 22:56:46 +01:00
|
|
|
|
|
|
|
#endif // MICROPY_ESP8266_NEOPIXEL
|