82#include "platforms/esp/is_esp.h"
84#if defined(FL_IS_ESP8266)
92#include "platforms/esp/8266/fastled_esp8266_uart.h"
95#include <esp8266_peri.h>
96#include <user_interface.h>
97#include "platforms/esp/8266/i2s_ws2812_codec.h"
98#include "platforms/esp/8266/spi_hspi_clock.h"
99#include <core_esp8266_i2s.h>
106constexpr int kMaxLoopbackLeds = 10;
107constexpr int kMaxEdges = kMaxLoopbackLeds * 24 * 2 + 4;
110__attribute__((always_inline))
inline fl::u32 cycles() {
112 __asm__ __volatile__(
"rsr %0,ccount" :
"=a"(c));
116inline fl::u8 patternByte(
int i) {
117 static const fl::u8 kSeed[] = {0x00, 0xFF, 0xAA, 0x55, 0x0F, 0xF0};
118 if (i <
static_cast<int>(
sizeof(kSeed))) {
121 return static_cast<fl::u8>(i * 37 + 0x5A);
125 fl::u32 edges[kMaxEdges];
130inline EdgeCapture& edgeCapture() {
143 const fl::u8* encoded,
int nEncoded, fl::u32 mask, fl::u32 idleCycles,
145 bool level = (GPI & mask) != 0;
146 cap.firstLevel = level;
147 USF(fl::esp8266_uart::kUart) = encoded[0];
148 USF(fl::esp8266_uart::kUart) = encoded[1];
150 fl::u32 lastEdge = cycles();
151 while (cap.count < kMaxEdges) {
152 const bool now = (GPI & mask) != 0;
153 const fl::u32
t = cycles();
155 cap.edges[cap.count++] =
t;
158 if (!now && fed < nEncoded) {
159 USF(fl::esp8266_uart::kUart) = encoded[fed++];
161 }
else if (
t - lastEdge > idleCycles) {
167inline bool isUsableRxPin(
int pin) {
169 return pin == 0 || pin == 4 || pin == 5 || (pin >= 12 && pin <= 15);
172inline fl::json uartWs2812Loopback(
int nLeds,
int rxPin) {
173 if (!isUsableRxPin(rxPin)) {
175 r.
set(
"success",
false);
176 r.
set(
"error",
"rxPin must be one of 0,4,5,12,13,14,15");
179 if (nLeds < 1) nLeds = 1;
180 if (nLeds > kMaxLoopbackLeds) nLeds = kMaxLoopbackLeds;
181 const int nBytes = nLeds * 3;
183 fl::u8 encoded[kMaxLoopbackLeds * 12];
184 for (
int i = 0; i < nBytes; ++i) {
185 fl::esp8266_uart::encodeByte(patternByte(i), &encoded[i * 4]);
189 fl::esp8266_uart::begin(FASTLED_ESP8266_UART_BAUD);
192 const fl::u8 prevMhz = system_get_cpu_freq();
193 system_update_cpu_freq(160);
194 const fl::u32 cyclesPerUs = 160;
195 const fl::u32 idleCycles = 20 * cyclesPerUs;
197 EdgeCapture& cap = edgeCapture();
199 const fl::u32 mask = 1u << rxPin;
201 const fl::u32 savedPs = xt_rsil(15);
202 captureEdges(encoded, nLeds * 12, mask, idleCycles, cap);
204 system_update_cpu_freq(prevMhz);
207 fl::u32 t0hMin = 0xFFFFFFFF, t0hMax = 0, t1hMin = 0xFFFFFFFF, t1hMax = 0;
208 fl::u32 periodMin = 0xFFFFFFFF, periodMax = 0;
209 const fl::u32 splitCycles = (625 * cyclesPerUs) / 1000;
210 fl::u8 decoded[kMaxLoopbackLeds * 3] = {};
212 const int start = cap.firstLevel ? 1 : 0;
213 for (
int e = start; e + 1 < cap.count && bits < nBytes * 8; e += 2) {
214 const fl::u32 high = cap.edges[e + 1] - cap.edges[e];
215 const bool one = high > splitCycles;
217 decoded[bits / 8] |=
static_cast<fl::u8>(0x80 >> (bits % 8));
218 if (high < t1hMin) t1hMin = high;
219 if (high > t1hMax) t1hMax = high;
221 if (high < t0hMin) t0hMin = high;
222 if (high > t0hMax) t0hMax = high;
224 if (e + 2 < cap.count) {
225 const fl::u32 period = cap.edges[e + 2] - cap.edges[e];
226 if (period < periodMin) periodMin = period;
227 if (period > periodMax) periodMax = period;
234 for (
int i = 0; i < nBytes; ++i) {
235 if (decoded[i] != patternByte(i)) {
237 if (firstBad < 0) firstBad = i;
241 auto ns = [cyclesPerUs](fl::u32 c) ->
fl::i64 {
242 return c == 0xFFFFFFFF ? -1 :
static_cast<fl::i64>(c) * 1000 / cyclesPerUs;
245 r.
set(
"success", bits == nBytes * 8 && byteErrors == 0);
248 r.
set(
"edges",
static_cast<fl::i64>(cap.count));
250 r.
set(
"byte_errors",
static_cast<fl::i64>(byteErrors));
251 r.
set(
"first_bad_byte",
static_cast<fl::i64>(firstBad));
253 r.
set(
"expected",
static_cast<fl::i64>(patternByte(firstBad)));
254 r.
set(
"actual",
static_cast<fl::i64>(decoded[firstBad]));
256 r.
set(
"idle_low_before", !cap.firstLevel);
257 r.
set(
"t0h_ns_min", ns(t0hMin));
258 r.
set(
"t0h_ns_max", ns(t0hMax));
259 r.
set(
"t1h_ns_min", ns(t1hMin));
260 r.
set(
"t1h_ns_max", ns(t1hMax));
261 r.
set(
"period_ns_min", ns(periodMin));
262 r.
set(
"period_ns_max", ns(periodMax));
275inline ShowState& showState() {
279inline fl::json uartWs2812Show(
int nLeds) {
280 if (nLeds < 1) nLeds = 1;
281 if (nLeds > 64) nLeds = 64;
282 ShowState& st = showState();
288 st.controller.setLeds(fl::span<CRGB>(
leds, nLeds));
289 for (
int i = 0; i < nLeds; ++i) {
290 leds[i] =
CRGB(patternByte(i * 3), patternByte(i * 3 + 1), patternByte(i * 3 + 2));
292 const fl::u32 t0 =
micros();
293 st.controller.showLeds(255);
294 const fl::u32 elapsed =
micros() - t0;
296 r.
set(
"success",
true);
298 r.
set(
"show_us",
static_cast<fl::i64>(elapsed));
300 r.
set(
"expected_min_us",
static_cast<fl::i64>(nLeds * 30 + FASTLED_ESP8266_UART_RESET_US));
304constexpr int kClocklessPin = FL_PIN_CLOCKLESS_1;
310constexpr int kApaDataPin = FL_PIN_SPI_DATA_1;
311constexpr int kApaClockPin = FL_PIN_SPI_CLOCK_1;
312constexpr int kApaDataRealPin = fl::FastPin<FL_PIN_SPI_DATA_1>::kRealPin;
313constexpr int kApaClockRealPin = fl::FastPin<FL_PIN_SPI_CLOCK_1>::kRealPin;
315struct DriverShowState {
321inline DriverShowState& driverShowState() {
334 DriverShowState& st = driverShowState();
335 if (nLeds < 1) nLeds = 1;
336 if (nLeds > 64) nLeds = 64;
337 if (frames < 1) frames = 1;
338 if (frames > 200) frames = 200;
340 fl::u32 minUs = 0xFFFFFFFF, maxUs = 0;
343 int firstTruncated = -1;
344 for (
int f = 0; f < frames; ++f) {
345 for (
int i = 0; i < nLeds; ++i) {
346 st.leds[i] =
CHSV(
static_cast<fl::u8>(f * 7 + i * 13), 255, 255);
348 const fl::u32 t0 =
micros();
350 const fl::u32 us =
micros() - t0;
351 if (us < minUs) minUs = us;
352 if (us > maxUs) maxUs = us;
353 if (
static_cast<fl::i64>(us) < floorUs) {
354 if (firstTruncated < 0) firstTruncated = f;
358 if (minUsOut !=
nullptr) {
359 *minUsOut =
static_cast<fl::i64>(minUs);
362 r.
set(
"success", truncated == 0);
363 r.
set(
"truncated",
static_cast<fl::i64>(truncated));
364 r.
set(
"first_truncated_frame",
static_cast<fl::i64>(firstTruncated));
368 r.
set(
"wifi_opmode",
static_cast<fl::i64>(wifi_get_opmode()));
370 r.
set(
"frames",
static_cast<fl::i64>(frames));
373 r.
set(
"floor_us", floorUs);
377inline fl::json clocklessShow(
int nLeds,
int frames) {
378 DriverShowState& st = driverShowState();
382 const int n = nLeds < 1 ? 1 : (nLeds > 64 ? 64 : nLeds);
383 return timeFrames(st.clockless, n, frames,
static_cast<fl::i64>(n) * 30);
388inline fl::i64 apa102WireBytes(
int nLeds) {
392 static_cast<fl::size
>(nLeds)));
406constexpr fl::i64 kApa102FrameSlackUs = 400;
413inline fl::u32 hspiAppliedHz() {
414 const fl::u32 reg = SPI1CLK;
415 if (reg & fl::esp8266::kHspiEquSysClk) {
416 return fl::esp8266::kHspiSourceHz;
418 const fl::u32 pre = ((reg >> 18) & 0x1FFFUL) + 1;
419 const fl::u32 n = ((reg >> 12) & 0x3FUL) + 1;
420 return fl::esp8266::kHspiSourceHz / (pre * n);
423#if defined(FASTLED_ESP8266_RAW_PIN_ORDER)
426 fl::FastPin<4>::hi();
427 fl::FastPin<CLOCK>::hi();
428 fl::FastPin<CLOCK>::lo();
432 fl::u32 low, fl::u32 high) {
433 fl::FastPin<4>::fastset(port, low);
434 fl::FastPin<4>::fastset(port, high);
437template <
int MHZ,
int CLOCK>
438inline fl::json bitbangStreamAt(
int nBytes,
int frames,
int mode) {
440 const int n = nBytes < 1 ? 1 : (nBytes > 512 ? 512 : nBytes);
441 const int count = frames < 1 ? 1 : (frames > 100 ? 100 : frames);
442 for (
int i = 0; i < n; ++i) bytes[i] = static_cast<fl::u8>(i * 37 + 11);
443 fl::GenericSoftwareSPIOutput<4, CLOCK,
DATA_RATE_MHZ(MHZ)> spi;
445 fl::FastPin<CLOCK>::lo();
446 const auto port = fl::FastPin<4>::port();
447 const fl::u32 lowWord = fl::FastPin<4>::hival() & ~fl::FastPin<CLOCK>::mask();
448 const fl::u32 highWord = lowWord | fl::FastPin<CLOCK>::mask();
449 if (mode == 4 && port != fl::FastPin<CLOCK>::port()) {
451 error.
set(
"success",
false);
452 error.
set(
"error",
"two-write calibration requires a shared GPIO port");
455 fl::u32 best = 0xFFFFFFFF;
456 for (
int frame = 0; frame < count; ++frame) {
457 __sync_synchronize();
458 const fl::u32 start = cycles();
462 for (
int i = 0; i < n; ++i) {
463 bitbangFloorBit<CLOCK>(); bitbangFloorBit<CLOCK>();
464 bitbangFloorBit<CLOCK>(); bitbangFloorBit<CLOCK>();
465 bitbangFloorBit<CLOCK>(); bitbangFloorBit<CLOCK>();
466 bitbangFloorBit<CLOCK>(); bitbangFloorBit<CLOCK>();
468 }
else if (mode == 4) {
469 for (
int i = 0; i < n; ++i) {
470 bitbangFloorPair(port, lowWord, highWord); bitbangFloorPair(port, lowWord, highWord);
471 bitbangFloorPair(port, lowWord, highWord); bitbangFloorPair(port, lowWord, highWord);
472 bitbangFloorPair(port, lowWord, highWord); bitbangFloorPair(port, lowWord, highWord);
473 bitbangFloorPair(port, lowWord, highWord); bitbangFloorPair(port, lowWord, highWord);
475 fl::FastPin<CLOCK>::lo();
476 }
else if (mode == 3) {
479 for (
int i = 0; i < n; ++i) {
480 fl::FastPin<CLOCK>::hi(); fl::FastPin<CLOCK>::hi();
481 fl::FastPin<CLOCK>::hi(); fl::FastPin<CLOCK>::hi();
482 fl::FastPin<CLOCK>::hi(); fl::FastPin<CLOCK>::hi();
483 fl::FastPin<CLOCK>::hi(); fl::FastPin<CLOCK>::hi();
485 }
else if (mode == 1) {
486 for (
int i = 0; i < n; ++i) spi.writeByte(bytes[i]);
488 spi.writeBytes(bytes, n);
490 __sync_synchronize();
491 const fl::u32 elapsed = cycles() - start;
492 if (elapsed < best) best = elapsed;
493 fl::FastPin<CLOCK>::lo();
495 const fl::i64 cpuHz =
static_cast<fl::i64>(system_get_cpu_freq()) * 1000000;
497 r.
set(
"success",
true);
501 r.
set(
"clock_pin",
static_cast<fl::i64>(CLOCK));
503 r.
set(
"requested_mhz",
static_cast<fl::i64>(MHZ));
504 r.
set(
"cpu_hz", cpuHz);
505 r.
set(
"min_cycles",
static_cast<fl::i64>(best));
506 const fl::i64 rate = best ?
static_cast<fl::i64>(n) * 8 * cpuHz / best : 0;
507 if (mode == 3) r.
set(
"gpio_writes_per_second", rate);
508 else r.
set(
"bits_per_second", rate);
513inline fl::json bitbangStreamRate(
int nBytes,
int frames,
int mode,
int mhz) {
514 if (mhz == 12)
return bitbangStreamAt<12, CLOCK>(nBytes, frames, mode);
515 if (mhz == 40)
return bitbangStreamAt<40, CLOCK>(nBytes, frames, mode);
516 return bitbangStreamAt<6, CLOCK>(nBytes, frames, mode);
519inline fl::json bitbangStream(
int nBytes,
int frames,
int mode,
int mhz,
int cpuMhz,
int clockPin) {
520 if (clockPin != 14 && clockPin != 16) {
522 error.
set(
"success",
false);
523 error.
set(
"error",
"clock pin must be 14 or 16");
526 const fl::u8 previous = system_get_cpu_freq();
527 if (cpuMhz == 80 || cpuMhz == 160) {
528 system_update_cpu_freq(cpuMhz);
531 if (clockPin == 14) r = bitbangStreamRate<14>(nBytes, frames, mode, mhz);
532 else r = bitbangStreamRate<16>(nBytes, frames, mode, mhz);
533 system_update_cpu_freq(previous);
537inline fl::json bitbangStream(
int,
int,
int,
int,
int,
int) {
539 r.
set(
"success",
false);
540 r.
set(
"error",
"the bit-bang benchmark requires raw pin order");
545inline fl::json apa102RateReport(fl::json r,
fl::i64 minUs,
int nLeds,
int mhz,
547 const fl::i64 bytes = apa102WireBytes(nLeds);
548 const fl::i64 bits = bytes * 8;
549 const fl::i64 expected = hz ? (bits * 1000000 + hz - 1) /
static_cast<fl::i64>(hz) : -1;
550 r.
set(
"success", expected > 0 && minUs >= expected &&
551 minUs <= expected + kApa102FrameSlackUs);
553 r.
set(
"requested_hz",
static_cast<fl::i64>(mhz) * 1000000);
554 r.
set(
"achievable_hz",
static_cast<fl::i64>(hz));
555 r.
set(
"wire_bytes_est", bytes);
556 r.
set(
"expected_us", expected);
561 const fl::u32 reg = SPI1CLK;
562 r.
set(
"reg_equ",
static_cast<fl::i64>((reg >> 31) & 1UL));
563 r.
set(
"gpmux_equ",
static_cast<fl::i64>((GPMUX >> 9) & 1UL));
568inline fl::json apa102Show(
int nLeds,
int frames) {
569 DriverShowState& st = driverShowState();
577 const fl::u32 hz = hspiAppliedHz();
578 const int n = nLeds < 1 ? 1 : (nLeds > 64 ? 64 : nLeds);
582 fl::json r = timeFrames(st.apa102, n, frames, 0, &minUs);
583 return apa102RateReport(r, minUs, n, 6, hz);
586inline fl::json hspiPinError(
const char* what) {
588 e.
set(
"success",
false);
589 e.
set(
"error", what);
599#if defined(FASTLED_ESP8266_RAW_PIN_ORDER)
600constexpr int kBitbangDataPin = 4;
601constexpr int kBitbangClockPin = 16;
604inline fl::json apa102ShowBitbang(
int nLeds,
int frames) {
605#if !defined(FASTLED_ESP8266_RAW_PIN_ORDER)
608 return hspiPinError(
"the bit-bang baseline is only defined in raw pin order");
610 DriverShowState& st = driverShowState();
618 const int n = nLeds < 1 ? 1 : (nLeds > 64 ? 64 : nLeds);
619 fl::json r = timeFrames(c, n, frames, 0);
620 r.
set(
"data_pin",
static_cast<fl::i64>(kBitbangDataPin));
621 r.
set(
"clock_pin",
static_cast<fl::i64>(kBitbangClockPin));
622 r.
set(
"wire_bytes_est", apa102WireBytes(n));
627#if defined(FASTLED_ALL_PINS_HARDWARE_SPI)
628 r.
set(
"generic_bitbang",
false);
630 r.
set(
"generic_bitbang",
true);
646inline fl::json apa102ShowMhz(
int nLeds,
int frames,
int mhz) {
647 DriverShowState& st = driverShowState();
650 case 6: c = apa102At<6>(st.leds);
break;
651 case 10: c = apa102At<10>(st.leds);
break;
652 case 20: c = apa102At<20>(st.leds);
break;
653 case 40: c = apa102At<40>(st.leds);
break;
654 case 80: c = apa102At<80>(st.leds);
break;
657 e.
set(
"success",
false);
658 e.
set(
"error",
"mhz must be 6, 10, 20, 40 or 80");
662 const int n = nLeds < 1 ? 1 : (nLeds > 64 ? 64 : nLeds);
665 const fl::u32 hz = hspiAppliedHz();
667 fl::json r = timeFrames(c, n, frames, 0, &minUs);
668 r = apa102RateReport(r, minUs, n, mhz, hz);
669 r.
set(
"cpu_mhz",
static_cast<fl::i64>(system_get_cpu_freq()));
679constexpr int kHspiLoopBytes = 96;
681struct HspiLoopbackState {
682 volatile fl::u8 got[kHspiLoopBytes];
686 fl::u8 tx[kHspiLoopBytes];
689 volatile int falling;
694inline HspiLoopbackState& hspiLoopbackState() {
701__attribute__((always_inline))
inline HspiLoopbackState*& hspiLoopbackActive() {
702 static HspiLoopbackState* s =
nullptr;
706inline fl::u8 hspiPattern(
int i) {
return static_cast<fl::u8>(i * 37 + 11); }
711 HspiLoopbackState& s = *hspiLoopbackActive();
712 const fl::u32 gpi = GPI;
713 if (gpi & s.sckMask) {
715 if (s.bits < kHspiLoopBytes * 8) {
716 if (gpi & s.mosiMask) {
717 s.got[s.bits >> 3] |=
static_cast<fl::u8>(0x80 >> (s.bits & 7));
730inline fl::json apa102HspiLoopbackAt(
int mosiPin,
int sckPin) {
731 HspiLoopbackState& s = hspiLoopbackState();
732 hspiLoopbackActive() = &s;
733 s.mosiMask = 1u << mosiPin;
734 s.sckMask = 1u << sckPin;
738 for (
int i = 0; i < kHspiLoopBytes; ++i) {
740 s.tx[i] = hspiPattern(i);
743 fl::SPIOutput<kApaDataPin, kApaClockPin,
DATA_RATE_KHZ(KHZ)> spi;
749 const fl::u32 gpf13 = GPF13;
750 const fl::u32 gpf14 = GPF14;
751 const fl::u32 gpe = GPE;
755 const fl::u32 t0 =
micros();
756 attachInterrupt(digitalPinToInterrupt(sckPin), hspiSckSample, CHANGE);
757 spi.writeBytes(s.tx, kHspiLoopBytes);
758 const fl::u32 elapsed =
micros() - t0;
761 spi.endTransaction();
762 const int risingAfter = s.rising;
763 const int fallingAfter = s.falling;
764 const int bitsAfter = s.bits;
766 int badBytes = 0, firstBad = -1;
768 for (
int i = 0; i < kHspiLoopBytes; ++i) {
769 if (s.got[i] != hspiPattern(i)) {
774 firstBadGot = s.got[i];
785 pinMode(kApaClockRealPin, OUTPUT);
786 for (
int i = 0; i < 3; ++i) {
792 const int probeEdges = s.rising - risingAfter;
793 detachInterrupt(digitalPinToInterrupt(sckPin));
797 r.
set(
"success", bitsAfter == kHspiLoopBytes * 8 && badBytes == 0);
798 r.
set(
"bytes",
static_cast<fl::i64>(kHspiLoopBytes));
799 r.
set(
"bits",
static_cast<fl::i64>(bitsAfter));
800 r.
set(
"sck_rising_edges",
static_cast<fl::i64>(risingAfter));
801 r.
set(
"sck_falling_edges",
static_cast<fl::i64>(fallingAfter));
808 r.
set(
"probe_edges",
static_cast<fl::i64>(probeEdges));
809 r.
set(
"isr_live_after_transfer", probeEdges == 3);
810 r.
set(
"bad_bytes",
static_cast<fl::i64>(badBytes));
811 r.
set(
"first_bad",
static_cast<fl::i64>(firstBad));
813 r.
set(
"first_bad_got",
static_cast<fl::i64>(firstBadGot));
814 r.
set(
"first_bad_want",
static_cast<fl::i64>(hspiPattern(firstBad)));
817 r.
set(
"elapsed_us",
static_cast<fl::i64>(elapsed));
820 r.
set(
"wifi_opmode",
static_cast<fl::i64>(wifi_get_opmode()));
824#if defined(FASTLED_ESP8266_RAW_PIN_ORDER)
826inline fl::json bitbangLoopbackAt(
int pattern,
int sensePin,
int dataSense,
827 int nBytes,
int writerMode) {
828 if (sensePin != 0 && sensePin != 5 && sensePin != 12 && sensePin != 15) {
829 return hspiPinError(
"sense pin must be GPIO0,5,12 or15; connect the selected clock pin to it");
831 const int n = nBytes < 0 ? 0 : (nBytes > kHspiLoopBytes ? kHspiLoopBytes : nBytes);
832 if (writerMode < 0 || writerMode > 4 || ((writerMode == 2 || writerMode == 3) && n % 3 != 0) || (writerMode == 3 && n > 90) ||
833 (writerMode == 1 && pattern < 0)) {
834 return hspiPinError(
"pixel counts need whole RGB pixels; start-bit mode allows 30 pixels; repeat mode needs a constant pattern");
836 fl::GenericSoftwareSPIOutput<4, CLOCK,
DATA_RATE_KHZ(50)> spi;
840 fl::FastPin<CLOCK>::hi();
842 const bool high = (GPI & (1UL << sensePin)) != 0;
843 fl::FastPin<CLOCK>::lo();
845 const bool low = (GPI & (1UL << sensePin)) == 0;
846 if (!high || !low)
return hspiPinError(
"selected clock jumper does not follow high/low");
849 const bool probeUnrelated = sensePin != 12 && sensePin != 13;
850 if (probeUnrelated) {
856 HspiLoopbackState& s = hspiLoopbackState();
857 hspiLoopbackActive() = &s;
858 s.mosiMask = 1UL << dataSense;
859 s.sckMask = 1UL << sensePin;
860 s.bits = s.rising = s.falling = 0;
861 for (
int i = 0; i < kHspiLoopBytes; ++i) {
863 s.tx[i] = pattern < 0 ? hspiPattern(i) : static_cast<fl::u8>(pattern);
866 for (
int i = 0; i < n / 3; ++i) {
867 leds[i] =
CRGB(s.tx[i * 3], s.tx[i * 3 + 1], s.tx[i * 3 + 2]);
869 attachInterrupt(digitalPinToInterrupt(sensePin), hspiSckSample, RISING);
870 const fl::u32 start =
micros();
871 if (writerMode == 1) {
872 spi.writeBytesValueRaw(
static_cast<fl::u8>(pattern), n);
873 }
else if (writerMode == 4) {
874 for (
int i = 0; i < n; ++i) spi.writeByte(s.tx[i]);
875 }
else if (writerMode >= 2) {
877 if (writerMode == 3) spi.template writePixels<FLAG_START_BIT, DATA_NOP, RGB>(pixels);
878 else spi.template writePixels<0, DATA_NOP, RGB>(pixels);
880 spi.writeBytes(s.tx, n);
882 spi.endTransaction();
883 const fl::u32 elapsed =
micros() - start;
884 detachInterrupt(digitalPinToInterrupt(sensePin));
885 const bool clockLow = (GPI & (1UL << sensePin)) == 0;
888 const int expectedBits = writerMode == 3 ? (n / 3) * 25 : n * 8;
890 for (
int bit = 0; bit < expectedBits; ++bit) {
891 bool expected =
true;
892 if (writerMode != 3 || bit % 25 != 0) {
893 const int payloadBit = writerMode == 3 ? (bit / 25) * 24 + bit % 25 - 1 : bit;
894 expected = (s.tx[payloadBit / 8] & (0x80 >> (payloadBit % 8))) != 0;
896 const bool actual = (s.got[bit / 8] & (0x80 >> (bit % 8))) != 0;
897 if (actual != expected) ++badBits;
900 r.
set(
"success", s.bits == expectedBits && s.rising == expectedBits && badBits == 0 && clockLow && unrelatedHigh);
903 r.
set(
"rising_edges",
static_cast<fl::i64>(s.rising));
904 r.
set(
"bad_bits",
static_cast<fl::i64>(badBits));
905 r.
set(
"clock_low", clockLow);
906 r.
set(
"unrelated_output_final_high", unrelatedHigh);
907 r.
set(
"unrelated_output_tested", probeUnrelated);
908 r.
set(
"writer_mode",
static_cast<fl::i64>(writerMode));
909 r.
set(
"elapsed_us",
static_cast<fl::i64>(elapsed));
910 r.
set(
"pattern",
static_cast<fl::i64>(pattern));
911 r.
set(
"clock_pin",
static_cast<fl::i64>(CLOCK));
912 r.
set(
"data_sense_pin",
static_cast<fl::i64>(dataSense));
913 r.
set(
"clock_sense_pin",
static_cast<fl::i64>(sensePin));
914 r.
set(
"requested_khz",
static_cast<fl::i64>(50));
917inline fl::json bitbangLoopback(
int pattern,
int sensePin,
int clockPin,
int nBytes,
int writerMode) {
918 if (clockPin != 14 && clockPin != 16)
return hspiPinError(
"clock pin must be 14 or 16");
919 if (clockPin == 14)
return bitbangLoopbackAt<14>(pattern, sensePin, 2, nBytes, writerMode);
920 return bitbangLoopbackAt<16>(pattern, sensePin, 4, nBytes, writerMode);
923inline fl::json bitbangLoopback(
int,
int,
int,
int,
int) {
924 return hspiPinError(
"the bit-bang loopback requires raw pin order");
935inline bool hspiSensePinOk(
int pin,
int other) {
936 const bool usable = pin == 0 || pin == 5 || pin == 12 || pin == 15;
937 return usable && pin != other;
948inline fl::json apa102HspiJumperCheck(
int sensePin,
int txPin) {
949 if (!hspiSensePinOk(sensePin, -1)) {
950 return hspiPinError(
"sensePin must be one of 0, 5, 12, 15");
952 if (txPin != kApaDataRealPin && txPin != kApaClockRealPin) {
953 return hspiPinError(
"txPin must be 13 (MOSI/D7) or 14 (SCK/D5)");
955 const int sckPin = sensePin;
956 const int drivePin = txPin;
957 HspiLoopbackState& s = hspiLoopbackState();
958 hspiLoopbackActive() = &s;
960 s.sckMask = 1u << sckPin;
969 attachInterrupt(digitalPinToInterrupt(sckPin), hspiSckSample, CHANGE);
970 bool seenHigh =
false, seenLow =
false;
971 for (
int i = 0; i < 10; ++i) {
974 if (GPI & s.sckMask) {
979 if ((GPI & s.sckMask) == 0) {
983 detachInterrupt(digitalPinToInterrupt(sckPin));
988 const bool levelFollows = seenHigh && seenLow;
989 const bool isrCounted = s.rising == 10 && s.falling == 10;
993 r.
set(
"success", levelFollows && isrCounted);
994 r.
set(
"level_follows_tx", levelFollows);
995 r.
set(
"isr_counted_edges", isrCounted);
996 r.
set(
"rising",
static_cast<fl::i64>(s.rising));
997 r.
set(
"falling",
static_cast<fl::i64>(s.falling));
998 r.
set(
"sense_pin",
static_cast<fl::i64>(sensePin));
1003inline fl::json apa102HspiLoopback(
int khz,
int mosiPin,
int sckPin) {
1004 if (!hspiSensePinOk(mosiPin, sckPin) || !hspiSensePinOk(sckPin, mosiPin)) {
1005 return hspiPinError(
1006 "sense pins must be two different pins from 0, 5, 12, 15");
1012 case 25:
return apa102HspiLoopbackAt<25>(mosiPin, sckPin);
1013 case 50:
return apa102HspiLoopbackAt<50>(mosiPin, sckPin);
1016 e.
set(
"success",
false);
1017 e.
set(
"error",
"khz must be 25 or 50");
1030inline fl::json apa102HspiWatch(
int sckPin,
int nBytes,
int nField) {
1031 if (!hspiSensePinOk(sckPin, -1)) {
1032 return hspiPinError(
"sckPin must be one of 0, 5, 12, 15");
1034 const int n = nBytes < 1 ? 1 : (nBytes > 64 ? 64 : nBytes);
1035 fl::SPIOutput<kApaDataPin, kApaClockPin,
DATA_RATE_KHZ(25)> spi;
1044 const fl::u32 clkBefore = SPI1CLK;
1046 const fl::u32 pre1 = ((clkBefore >> 18) & 0x1FFFUL) + 1;
1047 const fl::u32 n1 = ((clkBefore >> 12) & 0x3FUL) + 1;
1048 const fl::u32 divider = pre1 * n1;
1049 const fl::u32 nNew = (
static_cast<fl::u32
>(nField) & 0x3FUL) + 1;
1050 const fl::u32 preNew = divider / nNew == 0 ? 1 : divider / nNew;
1052 SPI1CLK = ((nNew / 2) & 0x3FUL) | ((nNew - 1) << 12) | ((preNew - 1) << 18);
1055 const fl::u32 mask = 1u << sckPin;
1058 const fl::u32 bits =
static_cast<fl::u32
>(n) * 8 - 1;
1059 const fl::u32 lenMask = (
static_cast<fl::u32
>(SPIMMOSI) << SPILMOSI) |
1060 (
static_cast<fl::u32
>(SPIMMISO) << SPILMISO);
1061 while (SPI1CMD & SPIBUSY) {}
1062 SPI1U1 = (SPI1U1 & ~lenMask) | (bits << SPILMOSI) | (bits << SPILMISO);
1063 volatile fl::u32* fifo = &SPI1W0;
1064 for (
int i = 0; i < (n + 3) / 4; ++i) {
1065 const int b = i * 4;
1066 fl::u32 word =
static_cast<fl::u32
>(hspiPattern(b));
1067 if (b + 1 < n) word |=
static_cast<fl::u32
>(hspiPattern(b + 1)) << 8;
1068 if (b + 2 < n) word |=
static_cast<fl::u32
>(hspiPattern(b + 2)) << 16;
1069 if (b + 3 < n) word |=
static_cast<fl::u32
>(hspiPattern(b + 3)) << 24;
1072 int transitions = 0, highSamples = 0, samples = 0;
1073 fl::u32 last = GPI & mask;
1074 const fl::u32 t0 =
micros();
1079 __sync_synchronize();
1081 while (SPI1CMD & SPIBUSY) {
1082 const fl::u32 now = GPI & mask;
1092 const fl::u32 elapsed =
micros() - t0;
1096 r.
set(
"success", transitions > 0);
1100 r.
set(
"sck_pin",
static_cast<fl::i64>(sckPin));
1101 r.
set(
"transitions",
static_cast<fl::i64>(transitions));
1102 r.
set(
"high_samples",
static_cast<fl::i64>(highSamples));
1103 r.
set(
"samples",
static_cast<fl::i64>(samples));
1104 r.
set(
"elapsed_us",
static_cast<fl::i64>(elapsed));
1108 const fl::u32 clkRan = SPI1CLK;
1109 r.
set(
"clk_cnt_l",
static_cast<fl::i64>(clkRan & 0x3FUL));
1110 r.
set(
"clk_cnt_h",
static_cast<fl::i64>((clkRan >> 6) & 0x3FUL));
1111 r.
set(
"clk_cnt_n",
static_cast<fl::i64>((clkRan >> 12) & 0x3FUL));
1112 r.
set(
"clk_pre",
static_cast<fl::i64>((clkRan >> 18) & 0x1FFFUL));
1113 r.
set(
"clk_before_pre",
static_cast<fl::i64>((clkBefore >> 18) & 0x1FFFUL));
1114 r.
set(
"n_field",
static_cast<fl::i64>(nField));
1119inline fl::json wifiOff() {
1120 wifi_set_opmode_current(NULL_MODE);
1122 r.
set(
"success", wifi_get_opmode() == NULL_MODE);
1123 r.
set(
"wifi_opmode",
static_cast<fl::i64>(wifi_get_opmode()));
1130inline fl::json uartWs2812InternalLoopback(fl::u32 baud) {
1131 namespace u = fl::esp8266_uart;
1132 u::begin(baud ? baud : FASTLED_ESP8266_UART_BAUD);
1136 USC0(u::kUart) &= ~(1u << UCTXI);
1137 USC0(u::kUart) |= (1u << UCLBE);
1138 USC0(u::kUart) |= (1u << UCRXRST);
1139 USC0(u::kUart) &= ~(1u << UCRXRST);
1140 const fl::u8 pixel[3] = {0xA5, 0x0F, 0xC3};
1142 for (
int i = 0; i < 3; ++i) {
1143 u::encodeByte(pixel[i], &sent[i * 4]);
1145 u::write(sent,
sizeof(sent));
1148 const fl::u32 got = (USS(u::kUart) >> USRXC) & 0xFF;
1149 fl::u32 matched = 0;
1151 for (fl::u32 i = 0; i < got && i <
sizeof(sent); ++i) {
1152 const fl::u8 c =
static_cast<fl::u8>(USF(u::kUart) & 0x3F);
1154 if (c == (sent[i] & 0x3F)) {
1158 USC0(u::kUart) &= ~(1u << UCLBE);
1159 USC0(u::kUart) |= (1u << UCTXI);
1161 r.
set(
"sent",
static_cast<fl::i64>(
sizeof(sent)));
1162 r.
set(
"received",
static_cast<fl::i64>(got));
1163 r.
set(
"matched",
static_cast<fl::i64>(matched));
1165 r.
set(
"success", got ==
sizeof(sent) && matched ==
sizeof(sent));
1172inline fl::json jumperCheck(
int rxPin) {
1174 if (!isUsableRxPin(rxPin)) {
1175 r.
set(
"success",
false);
1176 r.
set(
"error",
"rxPin must be one of 0,4,5,12,13,14,15");
1181 const fl::u32 mask = 1u << rxPin;
1184 const bool seenHigh = (GPI & mask) != 0;
1187 const bool seenLow = (GPI & mask) == 0;
1188 r.
set(
"rx_pin",
static_cast<fl::i64>(rxPin));
1189 r.
set(
"rx_high_when_tx_high", seenHigh);
1190 r.
set(
"rx_low_when_tx_low", seenLow);
1191 r.
set(
"success", seenHigh && seenLow);
1201inline int rpcArgInt(
const fl::json&
args, fl::size index,
int fallback) {
1202 if (
args.is_array() && index <
args.size() &&
args[index].is_number()) {
1205 return static_cast<int>(value.
value());
1217 remote.bind(
"resetCause", [](
const fl::json& ) -> fl::json {
1221 r.
set(
"crash_count",
static_cast<fl::i64>(wdt.consecutiveCrashCount()));
1223 r.
set(
"cont_stack_free",
static_cast<fl::i64>(cont_get_free_stack(g_pcont)));
1226 remote.bind(
"watchdogStall", [](
const fl::json&
args) -> fl::json {
1227 const int ms = rpcArgInt(
args, 0, -1);
1230 e.
set(
"error",
"ms must be >= 0");
1234 while (
fl::millis() - start <
static_cast<fl::u32
>(ms)) {
1246 remote.bind(
"uartWs2812Loopback", [](
const fl::json&
args) -> fl::json {
1247 const int nLeds = rpcArgInt(
args, 0, -1);
1248 const int rxPin = rpcArgInt(
args, 1, -1);
1251 if (nLeds < 0 || nLeds > 80000) {
1253 e.
set(
"error",
"kbaud must be in [0, 80000]");
1256 return uartWs2812InternalLoopback(
static_cast<fl::u32
>(nLeds) * 1000u);
1260 return jumperCheck(rxPin);
1262 return uartWs2812Loopback(nLeds, rxPin);
1264 remote.bind(
"uartWs2812Show", [](
const fl::json&
args) -> fl::json {
1265 return uartWs2812Show(rpcArgInt(
args, 0, 0));
1270 remote.bind(
"wifiOff", [](
const fl::json& ) -> fl::json {
return wifiOff(); });
1271 remote.bind(
"clocklessShow", [](
const fl::json&
args) -> fl::json {
1272 return clocklessShow(rpcArgInt(
args, 0, 0), rpcArgInt(
args, 1, 1));
1274 remote.bind(
"apa102Show", [](
const fl::json&
args) -> fl::json {
1275 return apa102Show(rpcArgInt(
args, 0, 0), rpcArgInt(
args, 1, 1));
1282 remote.bind(
"apa102ShowMhz", [](
const fl::json&
args) -> fl::json {
1283 return apa102ShowMhz(rpcArgInt(
args, 0, 0), 20, rpcArgInt(
args, 1, 6));
1285 remote.bind(
"apa102HspiLoopback", [](
const fl::json&
args) -> fl::json {
1286 return apa102HspiLoopback(rpcArgInt(
args, 0, 0), rpcArgInt(
args, 1, -1),
1287 rpcArgInt(
args, 2, -1));
1289 remote.bind(
"apa102HspiJumperCheck", [](
const fl::json&
args) -> fl::json {
1290 return apa102HspiJumperCheck(rpcArgInt(
args, 0, -1), rpcArgInt(
args, 1, -1));
1292 remote.bind(
"apa102HspiWatch", [](
const fl::json&
args) -> fl::json {
1293 return apa102HspiWatch(rpcArgInt(
args, 0, -1), rpcArgInt(
args, 1, 1),
1294 rpcArgInt(
args, 2, -1));
1301 remote.bind(
"bitbangLoopback", [](
const fl::json&
args) -> fl::json {
1302 return bitbangLoopback(rpcArgInt(
args, 0, -1), rpcArgInt(
args, 1, 5), rpcArgInt(
args, 2, 16),
1303 rpcArgInt(
args, 3, 96), rpcArgInt(
args, 4, 0));
1305 remote.bind(
"bitbangStream", [](
const fl::json&
args) -> fl::json {
1306 return bitbangStream(rpcArgInt(
args, 0, 512), rpcArgInt(
args, 1, 20),
1307 rpcArgInt(
args, 2, 0), rpcArgInt(
args, 3, 6),
1308 rpcArgInt(
args, 4, 0), rpcArgInt(
args, 5, 16));
1310 remote.bind(
"apa102ShowBitbang", [](
const fl::json&
args) -> fl::json {
1311 return apa102ShowBitbang(rpcArgInt(
args, 0, 0), rpcArgInt(
args, 1, 1));
1319 remote.bind(
"i2sLoopback", [](
int nBytes) -> fl::json {
1321 if (nBytes < 1 || nBytes > 300) {
1322 r.
set(
"error",
"nBytes must be in [1, 300]");
1325 const int n = nBytes;
1327 for (
int i = 0; i < n; ++i) {
1328 tx[i] =
static_cast<fl::u8>(i * 53 + 0x1B);
1331 static fl::u16 samples[600];
1332 for (
int i = 0; i < n; ++i) {
1333 fl::esp8266::encodeByte(tx[i], &samples[i * 2]);
1335 const int nSamples = n * 2;
1338 (void)i2s_rxtxdrive_begin(
true,
true,
true,
false);
1339 i2s_set_rate(fl::esp8266::kI2sSlotHz);
1341 for (
int i = 0; i < nSamples; ++i) {
1342 (void)i2s_write_sample_nb(samples[i]);
1344 static fl::u16 got[600];
1347 while (cap < nSamples &&
fl::millis() < deadline) {
1348 if (i2s_rx_available() > 0) {
1349 fl::i16 l = 0, rr = 0;
1350 if (i2s_read_sample(&l, &rr,
false)) {
1353 fl::u16 s =
static_cast<fl::u16
>(l);
1355 s =
static_cast<fl::u16
>(rr);
1362 for (
int i = 0; i < n; ++i) {
1363 if (i * 2 + 1 >= cap) { firstBad = i;
break; }
1364 if (fl::esp8266::decodeByte(got[i * 2], got[i * 2 + 1]) != tx[i]) {
1369 r.
set(
"success", firstBad < 0);
1371 r.
set(
"captured_samples",
static_cast<fl::i64>(cap));
1372 r.
set(
"first_bad",
static_cast<fl::i64>(firstBad));
1373 r.
set(
"s0",
static_cast<fl::i64>(samples[0]));
1376 r.
set(
"dec0",
static_cast<fl::i64>(fl::esp8266::decodeByte(got[0], got[1])));
1387 remote.bind(
"spiLoopback", [](
int nBytes) -> fl::json {
1389 if (nBytes < 1 || nBytes > 64) {
1390 r.
set(
"error",
"nBytes must be in [1, 64]");
1393 const int n = nBytes;
1401 while (SPI1CMD & SPIBUSY) {
1405 GPMUX &= ~(1UL << 9);
1406 SPI1CLK = fl::esp8266::hspiClockFor(1000000).reg_value;
1407 SPI1U = SPIUMOSI | SPIUDUPLEX | SPIUSSE;
1408 SPI1U1 = (7UL << SPILMOSI) | (7UL << SPILMISO);
1412 for (
int i = 0; i < n; ++i) {
1414 while (SPI1CMD & SPIBUSY) {
1418 while (SPI1CMD & SPIBUSY) {
1420 lastGot =
static_cast<fl::u8>(SPI1W0 & 0xFF);
1421 if (lastGot != b && firstBad < 0) {
1425 r.
set(
"success", firstBad < 0);
1427 r.
set(
"first_bad",
static_cast<fl::i64>(firstBad));
1428 r.
set(
"last_got",
static_cast<fl::i64>(lastGot));
1433inline void bindEsp8266Benches(fl::Remote&
remote) {
1434 bindEsp8266WatchdogRpcs(
remote);
1435 bindEsp8266UartRpcs(
remote);
1436 bindEsp8266DriverRpcs(
remote);
1437 bindEsp8266HspiRpcs(
remote);
1438 bindEsp8266HspiBenchRpcs(
remote);
1439 bindEsp8266I2sRpcs(
remote);
1440 bindEsp8266SpiRpcs(
remote);
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
@ APA102
APA102 LED chipset.
CLEDController * controller
fl::unique_ptr< fl::Remote > remote
static constexpr size_t calculateBytes(size_t num_leds) FL_NO_EXCEPT
Calculate total byte count for APA102 protocol Used for quad-SPI buffer pre-allocation.
::CLEDController & addLeds(::CLEDController *pLed, CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0) FL_NO_EXCEPT
Add a CLEDController instance to the world.
CLEDController & setLeds(CRGB *data, int nLeds) FL_NO_EXCEPT
Set the default array of LEDs to be used by this controller.
VIRTUAL_IF_NOT_AVR void showLeds(fl::u8 brightness) FL_NO_EXCEPT
virtual void init() FL_NO_EXCEPT=0
Initialize the LED controller.
bool has_value() const FL_NO_EXCEPT
static T & instance() FL_NO_EXCEPT
static Watchdog & instance() FL_NO_EXCEPT
void set(const fl::string &key, const json &value) FL_NO_EXCEPT
void push_back(const json &value) FL_NO_EXCEPT
static json object() FL_NO_EXCEPT
static json array() FL_NO_EXCEPT
fl::CLEDController CLEDController
#define DISABLE_DITHER
Disable dithering.
FastLED's Elegant JSON Library: fl::json
void delay(u32 ms, bool run_async=true) FL_NO_EXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
u32 _retry_cnt
Frame retry counter for ESP8266 clockless drivers (FASTLED_DEBUG_COUNT_FRAME_RETRIES).
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
@ High
Logic high (3.3V / 5V, platform-dependent)
u32 _frame_cnt
Frame counter for ESP8266 clockless drivers (FASTLED_DEBUG_COUNT_FRAME_RETRIES).
fl::string_view resetCauseName(ResetCause c) FL_NO_EXCEPT
Return a static-lifetime name for c — zero-cost, no allocation.
void digitalWrite(int pin, PinValue val) FL_NO_EXCEPT
Write digital value to pin.
PinValue digitalRead(int pin) FL_NO_EXCEPT
Read digital value from pin.
@ Output
Digital output (push-pull)
@ Input
Digital input (high impedance)
void pinMode(int pin, PinMode mode) FL_NO_EXCEPT
Set pin mode (input, output, pull-up, pull-down)
void delayMicroseconds(u32 us) FL_NO_EXCEPT
Delay for a given number of microseconds.
#define FASTLED_FORCE_INLINE
static ColorAdjustment noAdjustment() FL_NO_EXCEPT
the per-channel scale values premixed with brightness.
Unified cross-platform Watchdog Timer API for FastLED.